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.workplace.tools.content;
029
030import org.opencms.util.CmsHtmlParser;
031import org.opencms.util.CmsHtmlTagRemoveFactory;
032import org.opencms.util.CmsStringUtil;
033
034import java.util.Iterator;
035
036import org.htmlparser.NodeFactory;
037import org.htmlparser.Parser;
038import org.htmlparser.Tag;
039import org.htmlparser.lexer.Lexer;
040import org.htmlparser.lexer.Page;
041import org.htmlparser.util.ParserException;
042
043/**
044 *
045 * Html parser / visitor combination that visits a document and replaces Tag names by using the
046 * replacement configuration of a {@link org.opencms.workplace.tools.content.CmsTagReplaceSettings}
047 * instance.
048 * <p>
049 *
050 * Instances are reusable.
051 * <p>
052 *
053 * @since 6.1.7
054 *
055 */
056public final class CmsTagReplaceParser extends CmsHtmlParser {
057
058    /** A tag factory that is able to make tags invisible to visitors. */
059    private final NodeFactory m_nodeFactory;
060
061    /**
062     * Boolean flag that is set to true if during last call to {@link #process(String, String)}
063     * content was changed.
064     */
065    private boolean m_changedContent;
066    /**
067     * The settings to use for replacing tags.
068     */
069    private final CmsTagReplaceSettings m_settings;
070
071    /**
072     * Default constructor that turns echo on and uses the settings for replacing tags.
073     * <p>
074     *
075     * @param settings the settings to use for tag replacement.
076     */
077    public CmsTagReplaceParser(CmsTagReplaceSettings settings) {
078
079        // echo on
080        super(true);
081        m_settings = settings;
082        CmsHtmlTagRemoveFactory nodeFactory = new CmsHtmlTagRemoveFactory();
083        // add the removals of the settings to the tag factory:
084        Iterator itDeleteTags = m_settings.getDeleteTags().iterator();
085        while (itDeleteTags.hasNext()) {
086            nodeFactory.addTagRemoval((Tag)itDeleteTags.next());
087        }
088        m_nodeFactory = nodeFactory;
089
090    }
091
092    /**
093     * Overridden to also return the attributes of the Tag.
094     * <p>
095     *
096     * @see org.opencms.util.CmsHtmlParser#getTagHtml(org.htmlparser.Tag)
097     */
098    @Override
099    public String getTagHtml(Tag tag) {
100
101        if (CmsStringUtil.isEmpty(tag.getTagName())) {
102            return "";
103        }
104        StringBuffer result = new StringBuffer(32);
105        result.append('<');
106        // Tag name is the first "Attribute"...
107        Iterator itAttributes = tag.getAttributesEx().iterator();
108        while (itAttributes.hasNext()) {
109            result.append(itAttributes.next().toString());
110            // avoid trailing whitespaces like <H1 >
111            // in 2nd run htmlparser 1.5 would turn the whitespace into an Attribute with null name
112            if (itAttributes.hasNext()) {
113                result.append(' ');
114            }
115        }
116        result.append('>');
117        return result.toString();
118    }
119
120    /**
121     * Extracts the text from the given html content, assuming the given html encoding.
122     * <p>
123     * Additionally tags are replaced / removed according to the configuration of this instance.
124     * <p>
125     *
126     * <h3>Please note:</h3>
127     * There are static process methods in the superclass that will not do the replacements /
128     * removals. Don't mix them up with this method.
129     * <p>
130     *
131     * @param html the content to extract the plain text from.
132     *
133     * @param encoding the encoding to use.
134     *
135     * @return the text extracted from the given html content.
136     *
137     * @throws ParserException if something goes wrong.
138     */
139    @Override
140    public String process(String html, String encoding) throws ParserException {
141
142        // clear from potential previous run:
143        m_result = new StringBuffer();
144        m_changedContent = false;
145
146        // initialize a parser with the given charset
147        Parser parser = new Parser();
148        parser.setNodeFactory(m_nodeFactory);
149        Lexer lexer = new Lexer();
150        Page page = new Page(html, encoding);
151        lexer.setPage(page);
152        parser.setLexer(lexer);
153        // process the page using the given visitor
154        parser.visitAllNodesWith(this);
155        // return the result
156        return getResult();
157    }
158
159    /**
160     * @see org.opencms.util.CmsHtmlParser#visitEndTag(org.htmlparser.Tag)
161     */
162    @Override
163    public void visitEndTag(Tag tag) {
164
165        boolean change = m_settings.replace(tag);
166        if (change) {
167            m_changedContent = true;
168        }
169        super.visitEndTag(tag);
170    }
171
172    /**
173     * @see org.opencms.util.CmsHtmlParser#visitTag(org.htmlparser.Tag)
174     */
175    @Override
176    public void visitTag(Tag tag) {
177
178        boolean change = m_settings.replace(tag);
179        if (change) {
180            m_changedContent = true;
181        }
182        super.visitTag(tag);
183    }
184
185    /**
186     * Returns the changedContent.
187     * <p>
188     *
189     * @return the changedContent
190     */
191    public boolean isChangedContent() {
192
193        return m_changedContent;
194    }
195
196}