001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.jsp.parse;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.util.I_CmsHtmlNodeVisitor;
033
034import java.util.List;
035
036import org.htmlparser.util.ParserException;
037
038/**
039 * Base class for all classes that are specified for the <cms:parse parserClass="name"
040 * param="config" /&gt; tag in the parserClass Attribute.<p>
041 *
042 * Entry point for the tag implementation ({@link org.opencms.jsp.CmsJspTagParse}). The tag will
043 * provide a valid {@link org.opencms.file.CmsObject} and it's configuration parameter String to
044 * subclasses of this instances. Implementations just choose the type of
045 * {@link org.opencms.util.I_CmsHtmlNodeVisitor} they will use for visiting the content to be
046 * parsed.<p>
047 *
048 * To implement a custom class that may be used with the <nobr>&lt;cms:parse parserClass="name"
049 * param="config" /&gt;</nobr> tag the only thing that has to be done is to implement the method
050 * {@link #createVisitorInstance()} and return the desired {@link org.opencms.util.I_CmsHtmlNodeVisitor}
051 * implementation.<p>
052 *
053 * @since 6.1.7
054 */
055public abstract class A_CmsConfiguredHtmlParser {
056
057    /** The internal cms object for accessing core functionality. */
058    private CmsObject m_cmsObject;
059
060    /** The attribute value of the attribute param of the &lt;cms:parse&gt; tag. */
061    private String m_param;
062
063    /** The internal visitor implementation that will do the parsing. */
064    private I_CmsHtmlNodeVisitor m_visitor;
065
066    /**
067     * Default constructor that initializes the internal visitor by using the abstract template
068     * method {@link #createVisitorInstance()}.<p>
069     */
070    protected A_CmsConfiguredHtmlParser() {
071
072        // nop
073    }
074
075    /**
076     * Returns the result of subsequent parsing to the &lt;cms:parse&lt; tag implementation.<p>
077     *
078     * @param encoding the encoding to use for parsing
079     * @param html the html content to parse
080     * @param noAutoCloseTags a list of upper case tag names for which parsing / visiting should not correct missing closing tags.
081     *
082     * @return the result of subsequent parsing to the &lt;cms:parse&lt; tag implementation
083     *
084     * @throws ParserException if something goes wrong at parsing
085     * @throws CmsException if something goes wrong at accessing OpenCms core functionality
086    */
087    public String doParse(String html, String encoding, List<String> noAutoCloseTags)
088    throws ParserException, CmsException {
089
090        m_visitor = createVisitorInstance();
091        m_visitor.setNoAutoCloseTags(noAutoCloseTags);
092        String result = "";
093        m_visitor.process(html, encoding);
094        result = m_visitor.getResult();
095        return result;
096    }
097
098    /**
099     * Sets the internal cms object for accessing core functionality.<p>
100     *
101     * This will be invokde by the &tl;cms:parse&gt; tag implementation.<p>
102     *
103     * @param cmsObject the internal cms object for accessing core functionality to set
104     */
105    public void setCmsObject(CmsObject cmsObject) {
106
107        m_cmsObject = cmsObject;
108    }
109
110    /**
111     * The attribute value of the attribute param of the &lt;cms:parse&gt; tag.<p>
112     *
113     * Will be set by the &lt;cms:parse&gt; implementation.<p>
114     *
115     * @param param the param to set
116     */
117    public void setParam(String param) {
118
119        m_param = param;
120    }
121
122    /**
123     * Subclasses have to create their desired instance for parsing the html here.<p>
124     *
125     * You have access to {@link #getCmsObject()} and {@link #getParam()} already here and may pass those to
126     * the visitor to return.<p>
127     *
128     * @return the instance to be used for parsing the html
129     *
130     * @throws CmsException if sth. goes wrong
131     */
132    protected abstract I_CmsHtmlNodeVisitor createVisitorInstance() throws CmsException;
133
134    /**
135     * Returns the internal cms object for accessing core functionality.<p>
136     *
137     * This value will be initialized by the &lt;cms:parse&gt; tag.<p>
138     *
139     * @return the internal cms object for accessing core functionality
140     */
141    protected CmsObject getCmsObject() {
142
143        return m_cmsObject;
144    }
145
146    /**
147     * Returns the param.<p>
148     *
149     * @return the param
150     */
151    protected String getParam() {
152
153        return m_param;
154    }
155
156    /**
157     * Returns the visitor.<p>
158     *
159     * @return the visitor
160     */
161    protected I_CmsHtmlNodeVisitor getVisitor() {
162
163        return m_visitor;
164    }
165}