001/*
002 * (C) Copyright 2002-2013 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 */
018
019package org.nuxeo.ecm.platform.importer.xml.parser;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.InputStream;
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Map;
027
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.runtime.model.ComponentInstance;
030import org.nuxeo.runtime.model.DefaultComponent;
031
032/**
033 * Main Nuxeo Runtime component managing extension points and exposing {@link XMLImporterService}
034 *
035 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
036 */
037public class XMLImporterComponent extends DefaultComponent implements XMLImporterService {
038
039    protected List<DocConfigDescriptor> docConfigs = new ArrayList<DocConfigDescriptor>();
040
041    protected List<AttributeConfigDescriptor> attributeConfigs = new ArrayList<AttributeConfigDescriptor>();
042
043    @Override
044    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
045        if ("documentMapping".equals(extensionPoint)) {
046            docConfigs.add((DocConfigDescriptor) contribution);
047        } else if ("attributeMapping".equals(extensionPoint)) {
048            attributeConfigs.add((AttributeConfigDescriptor) contribution);
049        }
050    }
051
052    protected ParserConfigRegistry getRegistry() {
053        return new ParserConfigRegistry() {
054
055            @Override
056            public List<DocConfigDescriptor> getDocCreationConfigs() {
057                return docConfigs;
058            }
059
060            @Override
061            public List<AttributeConfigDescriptor> getAttributConfigs() {
062                return attributeConfigs;
063            }
064        };
065    }
066
067    @Override
068    public List<DocumentModel> importDocuments(DocumentModel root, File xmlFile) throws IOException {
069        return importDocuments(root, xmlFile, null);
070    }
071    
072    @Override
073    public List<DocumentModel> importDocuments(DocumentModel root, File xmlFile, boolean deferSave) throws IOException {
074        return importDocuments(root, xmlFile, null, deferSave);
075    }
076    
077    @Override
078    public List<DocumentModel> importDocuments(DocumentModel root, InputStream xmlStream) throws IOException {
079        return importDocuments(root, xmlStream, null);
080    }
081
082    @Override
083    public List<DocumentModel> importDocuments(DocumentModel root, InputStream xmlStream,
084            Map<String, Object> mvelContext) throws IOException {
085        XMLImporterServiceImpl importer = new XMLImporterServiceImpl(root, getRegistry(), mvelContext, false);
086        return importer.parse(xmlStream);
087    }
088
089        @Override
090        public List<DocumentModel> importDocuments(DocumentModel root, File source, Map<String, Object> mvelContext)
091                        throws IOException {
092        XMLImporterServiceImpl importer = new XMLImporterServiceImpl(root, getRegistry(), mvelContext, false);
093        return importer.parse(source);
094        }
095        
096        @Override
097        public List<DocumentModel> importDocuments(DocumentModel root, File source, Map<String, Object> mvelContext,
098                        boolean deferSave)
099                        throws IOException {
100        XMLImporterServiceImpl importer = new XMLImporterServiceImpl(root, getRegistry(), mvelContext, deferSave);
101        return importer.parse(source);
102        }
103}