001/*
002 * (C) Copyright 2006-2013 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 */
020
021package org.nuxeo.ecm.platform.importer.xml.parser;
022
023import java.io.File;
024import java.io.IOException;
025import java.io.InputStream;
026import java.util.ArrayList;
027import java.util.List;
028import java.util.Map;
029
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.runtime.model.ComponentInstance;
032import org.nuxeo.runtime.model.DefaultComponent;
033
034/**
035 * Main Nuxeo Runtime component managing extension points and exposing {@link XMLImporterService}
036 *
037 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
038 */
039public class XMLImporterComponent extends DefaultComponent implements XMLImporterService {
040
041    protected List<DocConfigDescriptor> docConfigs = new ArrayList<>();
042
043    protected List<AttributeConfigDescriptor> attributeConfigs = new ArrayList<>();
044
045    @Override
046    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
047        if ("documentMapping".equals(extensionPoint)) {
048            docConfigs.add((DocConfigDescriptor) contribution);
049        } else if ("attributeMapping".equals(extensionPoint)) {
050            attributeConfigs.add((AttributeConfigDescriptor) contribution);
051        }
052    }
053
054    protected ParserConfigRegistry getConfigRegistry() {
055        return new ParserConfigRegistry() {
056
057            @Override
058            public List<DocConfigDescriptor> getDocCreationConfigs() {
059                return docConfigs;
060            }
061
062            @Override
063            public List<AttributeConfigDescriptor> getAttributConfigs() {
064                return attributeConfigs;
065            }
066        };
067    }
068
069    @Override
070    public List<DocumentModel> importDocuments(DocumentModel root, File xmlFile) throws IOException {
071        return importDocuments(root, xmlFile, null);
072    }
073
074    @Override
075    public List<DocumentModel> importDocuments(DocumentModel root, File xmlFile, boolean deferSave) throws IOException {
076        return importDocuments(root, xmlFile, null, deferSave);
077    }
078
079    @Override
080    public List<DocumentModel> importDocuments(DocumentModel root, InputStream xmlStream) throws IOException {
081        return importDocuments(root, xmlStream, null);
082    }
083
084    @Override
085    public List<DocumentModel> importDocuments(DocumentModel root, InputStream xmlStream,
086            Map<String, Object> mvelContext) throws IOException {
087        XMLImporterServiceImpl importer = new XMLImporterServiceImpl(root, getConfigRegistry(), mvelContext, false);
088        return importer.parse(xmlStream);
089    }
090
091    @Override
092    public List<DocumentModel> importDocuments(DocumentModel root, File source, Map<String, Object> mvelContext)
093            throws IOException {
094        XMLImporterServiceImpl importer = new XMLImporterServiceImpl(root, getConfigRegistry(), mvelContext, false);
095        return importer.parse(source);
096    }
097
098    @Override
099    public List<DocumentModel> importDocuments(DocumentModel root, File source, Map<String, Object> mvelContext,
100            boolean deferSave)
101            throws IOException {
102        XMLImporterServiceImpl importer = new XMLImporterServiceImpl(root, getConfigRegistry(), mvelContext, deferSave);
103        return importer.parse(source);
104    }
105}