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
018 *     Antoine Taillefer
019 */
020
021package org.nuxeo.ecm.core.convert.plugins.text.extractors;
022
023import java.io.IOException;
024import java.io.Serializable;
025import java.util.Map;
026import java.util.zip.ZipInputStream;
027
028import javax.xml.parsers.ParserConfigurationException;
029import javax.xml.parsers.SAXParser;
030import javax.xml.parsers.SAXParserFactory;
031
032import org.nuxeo.ecm.core.api.Blobs;
033import org.nuxeo.ecm.core.api.blobholder.BlobHolder;
034import org.nuxeo.ecm.core.convert.api.ConversionException;
035import org.nuxeo.ecm.core.convert.cache.SimpleCachableBlobHolder;
036import org.nuxeo.ecm.core.convert.extension.Converter;
037import org.nuxeo.ecm.core.convert.extension.ConverterDescriptor;
038import org.xml.sax.SAXException;
039import org.xml.sax.XMLReader;
040
041/**
042 * XML zip to text converter: parses the XML zip entries to read their content.
043 */
044public abstract class XmlZip2TextConverter implements Converter {
045
046    @Override
047    public BlobHolder convert(BlobHolder blobHolder, Map<String, Serializable> parameters) throws ConversionException {
048
049        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
050        parserFactory.setValidating(false);
051
052        try {
053            SAXParser parser = parserFactory.newSAXParser();
054            XMLReader reader = parser.getXMLReader();
055            reader.setFeature("http://xml.org/sax/features/validation", false);
056            reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
057            reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
058
059            StringBuilder sb = new StringBuilder();
060            UnclosableZipInputStream zis = new UnclosableZipInputStream(blobHolder.getBlob().getStream());
061            try {
062                readXmlZipContent(zis, reader, sb);
063            } finally {
064                zis.close(); // appease code analyzers
065                zis.doClose(); // real close
066            }
067            return new SimpleCachableBlobHolder(Blobs.createBlob(sb.toString()));
068        } catch (IOException | ParserConfigurationException | SAXException e) {
069            throw new ConversionException("Error during OpenXml2Text conversion", blobHolder, e);
070        }
071    }
072
073    @Override
074    public void init(ConverterDescriptor descriptor) {
075    }
076
077    protected abstract void readXmlZipContent(ZipInputStream zis, XMLReader reader, StringBuilder sb)
078            throws IOException, SAXException;
079}