001/*
002 * (C) Copyright 2002-2007 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 */
018package org.nuxeo.ecm.core.convert.plugins.text.extractors;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.Serializable;
023import java.util.Map;
024
025import javax.xml.parsers.ParserConfigurationException;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.core.api.Blobs;
030import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
031import org.nuxeo.ecm.core.convert.api.ConversionException;
032import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
033import org.nuxeo.ecm.core.convert.extension.Converter;
034import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
035import org.xml.sax.SAXException;
036
037public class XML2TextConverter implements Converter {
038
039    private static final Log log = LogFactory.getLog(XML2TextConverter.class);
040
041    @Override
042    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
043
044        InputStream stream = null;
045        try {
046            stream = blobHolder.getBlob().getStream();
047            Xml2TextHandler xml2text = new Xml2TextHandler();
048            String text = xml2text.parse(stream);
049
050            return new SimpleCachableBlobHolder(Blobs.createBlob(text));
051        } catch (IOException | SAXException | ParserConfigurationException e) {
052            throw new ConversionException("Error during XML2Text conversion", e);
053        } finally {
054            if (stream != null) {
055                try {
056                    stream.close();
057                } catch (IOException e) {
058                    log.error("Error while closing Blob stream", e);
059                }
060            }
061        }
062    }
063
064    @Override
065    public void init(ConverterDescriptor descriptor) {
066    }
067
068}