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.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     * Subclasses have to create their desired instance for parsing the html here.<p>
077     *
078     * You have access to {@link #getCmsObject()} and {@link #getParam()} already here and may pass those to
079     * the visitor to return.<p>
080     *
081     * @return the instance to be used for parsing the html
082     *
083     * @throws CmsException if sth. goes wrong
084     */
085    protected abstract I_CmsHtmlNodeVisitor createVisitorInstance() throws CmsException;
086
087    /**
088     * Returns the result of subsequent parsing to the &lt;cms:parse&lt; tag implementation.<p>
089     *
090     * @param encoding the encoding to use for parsing
091     * @param html the html content to parse
092     * @param noAutoCloseTags a list of upper case tag names for which parsing / visiting should not correct missing closing tags.
093     *
094     * @return the result of subsequent parsing to the &lt;cms:parse&lt; tag implementation
095     *
096     * @throws ParserException if something goes wrong at parsing
097     * @throws CmsException if something goes wrong at accessing OpenCms core functionality
098    */
099    public String doParse(String html, String encoding, List<String> noAutoCloseTags)
100    throws ParserException, CmsException {
101
102        m_visitor = createVisitorInstance();
103        m_visitor.setNoAutoCloseTags(noAutoCloseTags);
104        String result = "";
105        m_visitor.process(html, encoding);
106        result = m_visitor.getResult();
107        return result;
108    }
109
110    /**
111     * Returns the internal cms object for accessing core functionality.<p>
112     *
113     * This value will be initialized by the &lt;cms:parse&gt; tag.<p>
114     *
115     * @return the internal cms object for accessing core functionality
116     */
117    protected CmsObject getCmsObject() {
118
119        return m_cmsObject;
120    }
121
122    /**
123     * Returns the param.<p>
124     *
125     * @return the param
126     */
127    protected String getParam() {
128
129        return m_param;
130    }
131
132    /**
133     * Returns the visitor.<p>
134     *
135     * @return the visitor
136     */
137    protected I_CmsHtmlNodeVisitor getVisitor() {
138
139        return m_visitor;
140    }
141
142    /**
143     * Sets the internal cms object for accessing core functionality.<p>
144     *
145     * This will be invokde by the &tl;cms:parse&gt; tag implementation.<p>
146     *
147     * @param cmsObject the internal cms object for accessing core functionality to set
148     */
149    public void setCmsObject(CmsObject cmsObject) {
150
151        m_cmsObject = cmsObject;
152    }
153
154    /**
155     * The attribute value of the attribute param of the &lt;cms:parse&gt; tag.<p>
156     *
157     * Will be set by the &lt;cms:parse&gt; implementation.<p>
158     *
159     * @param param the param to set
160     */
161    public void setParam(String param) {
162
163        m_param = param;
164    }
165}