001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */package org.nuxeo.ecm.platform.importer.xml.parser;
021
022import java.io.IOException;
023import java.util.List;
024
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.NuxeoException;
028import org.nuxeo.ecm.platform.importer.base.GenericMultiThreadedImporter;
029import org.nuxeo.ecm.platform.importer.factories.ImporterDocumentModelFactory;
030import org.nuxeo.ecm.platform.importer.source.FileSourceNode;
031import org.nuxeo.ecm.platform.importer.source.SourceNode;
032import org.nuxeo.ecm.platform.scanimporter.processor.ScanedFileFactory;
033import org.nuxeo.ecm.platform.scanimporter.processor.ScannedFileImporter;
034import org.nuxeo.ecm.platform.scanimporter.service.ImporterConfig;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * Replace default XML Parser used into the Scan Importer service by the advanced one implemented into
039 * nuxeo-importer-xml-parser
040 *
041 * @author Benjamin JALON
042 */
043public class AdvancedScannedFileFactory extends ScanedFileFactory implements ImporterDocumentModelFactory {
044
045    public AdvancedScannedFileFactory(ImporterConfig config) {
046        super(config);
047    }
048
049    protected static String targetContainerType = null;
050
051    protected ImporterConfig config;
052
053    @Override
054    public DocumentModel createLeafNode(CoreSession session, DocumentModel parent, SourceNode node) throws IOException {
055
056        XMLImporterService importer = Framework.getService(XMLImporterService.class);
057        if (!(node instanceof FileSourceNode)) {
058            throw new NuxeoException("Waiting a FileSourceNode object not: " + node.getClass().getName());
059        }
060        FileSourceNode fileNode = (FileSourceNode) node;
061        List<DocumentModel> docCreated = importer.importDocuments(parent, fileNode.getFile());
062
063        if (docCreated == null || docCreated.size() < 1) {
064            return null;
065        }
066
067        ScannedFileImporter.addProcessedDescriptor(fileNode.getSourcePath());
068
069        GenericMultiThreadedImporter.addCreatedDoc("_XMLimporter", docCreated.size() - 1);
070        return docCreated.get(0);
071    }
072}