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.util;
029
030import org.opencms.staticexport.CmsLinkProcessor;
031
032import java.io.ByteArrayInputStream;
033import java.io.InputStream;
034import java.io.UnsupportedEncodingException;
035
036import org.htmlparser.Parser;
037import org.htmlparser.beans.StringBean;
038import org.htmlparser.lexer.Lexer;
039import org.htmlparser.lexer.Page;
040import org.htmlparser.util.ParserException;
041
042/**
043 * Extracts plain text from HTML.<p>
044 *
045 * @since 6.0.0
046 */
047public final class CmsHtmlExtractor {
048
049    /**
050     * Hides the public constructor.<p>
051     */
052    private CmsHtmlExtractor() {
053
054        // hides the public constructor
055    }
056
057    /**
058     * Extract the text from a HTML page.<p>
059     *
060     * @param in the html content input stream
061     * @param encoding the encoding of the content
062     *
063     * @return the extracted text from the page
064     * @throws ParserException if the parsing of the HTML failed
065     * @throws UnsupportedEncodingException if the given encoding is not supported
066     */
067    public static String extractText(InputStream in, String encoding)
068    throws ParserException, UnsupportedEncodingException {
069
070        Parser parser = new Parser();
071        Lexer lexer = new Lexer();
072        Page page = new Page(in, encoding);
073        lexer.setPage(page);
074        parser.setLexer(lexer);
075
076        StringBean stringBean = new StringBean();
077        parser.visitAllNodesWith(stringBean);
078
079        String result = stringBean.getStrings();
080        return result == null ? "" : result;
081    }
082
083    /**
084     * Extract the text from a HTML page.<p>
085     *
086     * @param content the html content
087     * @param encoding the encoding of the content
088     *
089     * @return the extracted text from the page
090     * @throws ParserException if the parsing of the HTML failed
091     * @throws UnsupportedEncodingException if the given encoding is not supported
092     */
093    public static String extractText(String content, String encoding)
094    throws ParserException, UnsupportedEncodingException {
095
096        if (CmsStringUtil.isEmpty(content)) {
097            // if there is no HTML, then we don't need to extract anything
098            return content;
099        }
100
101        // we must make sure that the content passed to the parser always is
102        // a "valid" HTML page, i.e. is surrounded by <html><body>...</body></html>
103        // otherwise you will get strange results for some specific HTML constructs
104        StringBuffer newContent = new StringBuffer(content.length() + 32);
105
106        newContent.append(CmsLinkProcessor.HTML_START);
107        newContent.append(content);
108        newContent.append(CmsLinkProcessor.HTML_END);
109
110        // make sure the Lexer uses the right encoding
111        InputStream in = new ByteArrayInputStream(newContent.toString().getBytes(encoding));
112
113        // use the stream based version to process the results
114        return extractText(in, encoding);
115    }
116}