001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.search.extractors;
029
030import org.opencms.xml.CmsXmlGenericWrapper;
031
032import java.io.InputStream;
033import java.util.Iterator;
034import java.util.List;
035import java.util.zip.ZipEntry;
036import java.util.zip.ZipInputStream;
037
038import org.dom4j.Document;
039import org.dom4j.Node;
040import org.dom4j.io.SAXReader;
041
042/**
043 * Extracts the text from OpenOffice documents (.ods, .odf).<p>
044 *
045 * @since 7.0.4
046 */
047public final class CmsExtractorOpenOffice extends A_CmsTextExtractor {
048
049    /** Static member instance of the extractor. */
050    private static final CmsExtractorOpenOffice INSTANCE = new CmsExtractorOpenOffice();
051
052    /**
053     * Hide the public constructor.<p>
054     */
055    private CmsExtractorOpenOffice() {
056
057        // noop
058    }
059
060    /**
061     * Returns an instance of this text extractor.<p>
062     *
063     * @return an instance of this text extractor
064     */
065    public static I_CmsTextExtractor getExtractor() {
066
067        return INSTANCE;
068    }
069
070    /**
071     * @see org.opencms.search.extractors.A_CmsTextExtractor#extractText(java.io.InputStream, java.lang.String)
072     */
073    @Override
074    public I_CmsExtractionResult extractText(InputStream in, String encoding) throws Exception {
075
076        try (ZipInputStream zin = new ZipInputStream(in)) {
077            ZipEntry ze;
078            boolean FOUND_CONTENT = false;
079            String result = "";
080            while (!FOUND_CONTENT) {
081                ze = zin.getNextEntry();
082                FOUND_CONTENT = ze.getName().equalsIgnoreCase("content.xml");
083                if (FOUND_CONTENT) {
084                    result = readContent(zin);
085                }
086            }
087            result = removeControlChars(result);
088            return new CmsExtractionResult(result);
089        }
090    }
091
092    /**
093     * Internal routine that parses the specific content.xml part of
094     * an odf document.<p>
095     *
096     * @param in the input stream spooled to the start of the content.xml part
097     *
098     * @return the extracted content
099     *
100     * @throws Exception if sth goes wrong
101     */
102    private String readContent(java.io.InputStream in) throws Exception {
103
104        StringBuffer resultBuffer = new StringBuffer();
105        SAXReader reader = new SAXReader();
106        Document doc = reader.read(in);
107        List<Node> textlist = CmsXmlGenericWrapper.selectNodes(doc, "//text:p[@*] | //text:span[@*]");
108        Iterator<Node> li = textlist.iterator();
109        while (li.hasNext()) {
110            Node textNode = li.next();
111            String text = textNode.getText();
112            if (text.length() > 1) {
113                text = " " + text + " ";
114                resultBuffer.append(text);
115            }
116        }
117        return resultBuffer.toString();
118    }
119
120}