001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/parse/A_CmsConfiguredHtmlParser.java,v $
003: * Date : $Date: 2008-02-27 12:05:50 $
004: * Version: $Revision: 1.7 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.jsp.parse;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.main.CmsException;
036: import org.opencms.util.I_CmsHtmlNodeVisitor;
037:
038: import java.util.List;
039:
040: import org.htmlparser.util.ParserException;
041:
042: /**
043: * Base class for all classes that are specified for the <cms:parse parserClass="name"
044: * param="config" /> tag in the parserClass Attribute.<p>
045: *
046: * Entry point for the tag implementation ({@link org.opencms.jsp.CmsJspTagParse}). The tag will
047: * provide a valid {@link org.opencms.file.CmsObject} and it's configuration parameter String to
048: * subclasses of this instances. Implementations just choose the type of
049: * {@link org.opencms.util.I_CmsHtmlNodeVisitor} they will use for visiting the content to be
050: * parsed.<p>
051: *
052: * To implement a custom class that may be used with the <nobr><cms:parse parserClass="name"
053: * param="config" /></nobr> tag the only thing that has to be done is to implement the method
054: * {@link #createVisitorInstance()} and return the desired {@link org.opencms.util.I_CmsHtmlNodeVisitor}
055: * implementation.<p>
056: *
057: * @author Achim Westermann
058: *
059: * @version $Revision: 1.7 $
060: *
061: * @since 6.1.7
062: */
063: public abstract class A_CmsConfiguredHtmlParser {
064:
065: /** The internal cms object for accessing core functionality. */
066: private CmsObject m_cmsObject;
067:
068: /** The attribute value of the attribute param of the <cms:parse> tag. */
069: private String m_param;
070:
071: /** The internal visitor implementation that will do the parsing. */
072: private I_CmsHtmlNodeVisitor m_visitor;
073:
074: /**
075: * Default constructor that initializes the internal visitor by using the abstract template
076: * method {@link #createVisitorInstance()}.<p>
077: */
078: protected A_CmsConfiguredHtmlParser() {
079:
080: // nop
081: }
082:
083: /**
084: * Subclasses have to create their desired instance for parsing the html here.<p>
085: *
086: * You have access to {@link #getCmsObject()} and {@link #getParam()} already here and may pass those to
087: * the visitor to return.<p>
088: *
089: * @return the instance to be used for parsing the html
090: *
091: * @throws CmsException if sth. goes wrong
092: */
093: protected abstract I_CmsHtmlNodeVisitor createVisitorInstance()
094: throws CmsException;
095:
096: /**
097: * Returns the result of subsequent parsing to the <cms:parse< tag implementation.<p>
098: *
099: * @param encoding the encoding to use for parsing
100: * @param html the html content to parse
101: * @param noAutoCloseTags a list of upper case tag names for which parsing / visiting should not correct missing closing tags.
102: *
103: * @return the result of subsequent parsing to the <cms:parse< tag implementation
104: *
105: * @throws ParserException if sth. goes wrong at parsing
106: * @throws CmsException if sth. goes wrong at accessing OpenCms core functionality
107: */
108: public String doParse(String html, String encoding,
109: List noAutoCloseTags) throws ParserException, CmsException {
110:
111: m_visitor = createVisitorInstance();
112: m_visitor.setNoAutoCloseTags(noAutoCloseTags);
113: String result = "";
114: m_visitor.process(html, encoding);
115: result = m_visitor.getResult();
116: // fool Checkstyle - we need to grant this exception for more complex subclasses that e.g.
117: // have to be configured from VFS:
118: if (false) {
119: throw new CmsException(null);
120: }
121: return result;
122: }
123:
124: /**
125: * Returns the internal cms object for accessing core functionality.<p>
126: *
127: * This value will be initialized by the <cms:parse> tag.<p>
128: *
129: * @return the internal cms object for accessing core functionality
130: */
131: protected CmsObject getCmsObject() {
132:
133: return m_cmsObject;
134: }
135:
136: /**
137: * Returns the param.<p>
138: *
139: * @return the param
140: */
141: protected String getParam() {
142:
143: return m_param;
144: }
145:
146: /**
147: * Returns the visitor.<p>
148: *
149: * @return the visitor
150: */
151: protected I_CmsHtmlNodeVisitor getVisitor() {
152:
153: return m_visitor;
154: }
155:
156: /**
157: * Sets the internal cms object for accessing core functionality.<p>
158: *
159: * This will be invokde by the &tl;cms:parse> tag implementation.<p>
160: *
161: * @param cmsObject the internal cms object for accessing core functionality to set
162: */
163: public void setCmsObject(CmsObject cmsObject) {
164:
165: m_cmsObject = cmsObject;
166: }
167:
168: /**
169: * The attribute value of the attribute param of the <cms:parse> tag.<p>
170: *
171: * Will be set by the <cms:parse> implementation.<p>
172: *
173: * @param param the param to set
174: */
175: public void setParam(String param) {
176:
177: m_param = param;
178: }
179: }
|