001: package com.meterware.httpunit.parsing;
002:
003: /********************************************************************************************************************
004: * $Id: HTMLParserFactory.java,v 1.3 2002/12/26 04:59:35 russgold Exp $
005: *
006: * Copyright (c) 2002, Russell Gold
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
009: * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
010: * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
011: * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included in all copies or substantial portions
014: * of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
017: * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
018: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
019: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
020: * DEALINGS IN THE SOFTWARE.
021: *
022: *******************************************************************************************************************/
023: import java.util.Vector;
024:
025: /**
026: * Factory for creating HTML parsers. Parser customization properties can be specified but do not necessarily work
027: * for every parser type.
028: *
029: * @since 1.5.2
030: * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
031: * @author <a href="mailto:bw@xmlizer.biz">Bernhard Wagner</a>
032: **/
033: abstract public class HTMLParserFactory {
034:
035: private static Vector _listeners = new Vector();
036: private static HTMLParser _jtidyParser;
037: private static HTMLParser _nekoParser;
038:
039: private static HTMLParser _htmlParser;
040: private static boolean _preserveTagCase;
041: private static boolean _returnHTMLDocument;
042: private static boolean _parserWarningsEnabled;
043:
044: /**
045: * Resets all settings to their default values. This includes the parser selection.
046: */
047: public static void reset() {
048: _preserveTagCase = false;
049: _returnHTMLDocument = true;
050: _parserWarningsEnabled = false;
051: _htmlParser = null;
052: }
053:
054: /**
055: * Selects the JTidy parser, if present.
056: */
057: public static void useJTidyParser() {
058: if (_jtidyParser == null)
059: throw new RuntimeException("JTidy parser not available");
060: _htmlParser = _jtidyParser;
061: }
062:
063: /**
064: * Selects the NekoHTML parser, if present.
065: */
066: public static void useNekoHTMLParser() {
067: if (_nekoParser == null)
068: throw new RuntimeException("NekoHTML parser not available");
069: _htmlParser = _nekoParser;
070: }
071:
072: /**
073: * Specifies the parser to use.
074: */
075: public static void setHTMLParser(HTMLParser htmlParser) {
076: _htmlParser = htmlParser;
077: }
078:
079: /**
080: * Returns the current selected parser.
081: */
082: public static HTMLParser getHTMLParser() {
083: if (_htmlParser == null) {
084: if (_nekoParser != null) {
085: _htmlParser = _nekoParser;
086: } else if (_jtidyParser != null) {
087: _htmlParser = _jtidyParser;
088: } else {
089: throw new RuntimeException(
090: "No HTML parser found. Make sure that either nekoHTML.jar or Tidy.jar is in the in classpath");
091: }
092: }
093: return _htmlParser;
094: }
095:
096: /**
097: * Returns true if the current parser will preserve the case of HTML tags and attributes.
098: */
099: public static boolean isPreserveTagCase() {
100: return _preserveTagCase
101: && getHTMLParser().supportsPreserveTagCase();
102: }
103:
104: /**
105: * Specifies whether the parser should preserve the case of HTML tags and attributes. Not every parser can
106: * support this capability. Note that enabling this will disable support for the HTMLDocument class.
107: * @see #setReturnHTMLDocument
108: */
109: public static void setPreserveTagCase(boolean preserveTagCase) {
110: _preserveTagCase = preserveTagCase;
111: if (preserveTagCase)
112: _returnHTMLDocument = false;
113: }
114:
115: /**
116: * Returns true if the current parser will return an HTMLDocument object rather than a Document object.
117: */
118: public static boolean isReturnHTMLDocument() {
119: return _returnHTMLDocument
120: && getHTMLParser().supportsReturnHTMLDocument();
121: }
122:
123: /**
124: * Specifies whether the parser should return an HTMLDocument object rather than a Document object.
125: * Not every parser can support this capability. Note that enabling this will disable preservation of tag case.
126: * @see #setPreserveTagCase
127: */
128: public static void setReturnHTMLDocument(boolean returnHTMLDocument) {
129: _returnHTMLDocument = returnHTMLDocument;
130: if (returnHTMLDocument)
131: _preserveTagCase = false;
132: }
133:
134: /**
135: * Returns true if parser warnings are enabled.
136: **/
137: public static boolean isParserWarningsEnabled() {
138: return _parserWarningsEnabled
139: && getHTMLParser().supportsParserWarnings();
140: }
141:
142: /**
143: * If true, tells the parser to display warning messages. The default is false (warnings are not shown).
144: **/
145: public static void setParserWarningsEnabled(boolean enabled) {
146: _parserWarningsEnabled = enabled;
147: }
148:
149: /**
150: * Remove an HTML Parser listener.
151: **/
152: public static void removeHTMLParserListener(HTMLParserListener el) {
153: _listeners.removeElement(el);
154: }
155:
156: /**
157: * Add an HTML Parser listener.
158: **/
159: public static void addHTMLParserListener(HTMLParserListener el) {
160: _listeners.addElement(el);
161: }
162:
163: //------------------------------------- package protected members ------------------------------------------------------
164:
165: /**
166: * Get the list of Html Error Listeners
167: **/
168: static Vector getHTMLParserListeners() {
169: return _listeners;
170: }
171:
172: private static HTMLParser loadParserIfSupported(
173: final String testClassName, final String parserClassName) {
174: try {
175: Class.forName(testClassName);
176: return (HTMLParser) Class.forName(parserClassName)
177: .newInstance();
178: } catch (InstantiationException e) {
179: } catch (IllegalAccessException e) {
180: } catch (ClassNotFoundException e) {
181: }
182: return null;
183: }
184:
185: static {
186: _jtidyParser = loadParserIfSupported("org.w3c.tidy.Parser",
187: "com.meterware.httpunit.parsing.JTidyHTMLParser");
188: _nekoParser = loadParserIfSupported(
189: "org.cyberneko.html.HTMLConfiguration",
190: "com.meterware.httpunit.parsing.NekoHTMLParser");
191: reset();
192: }
193:
194: }
|