Source code: com/meterware/httpunit/parsing/HTMLParserFactory.java
1 package com.meterware.httpunit.parsing;
2 /********************************************************************************************************************
3 * $Id: HTMLParserFactory.java,v 1.3 2002/12/26 04:59:35 russgold Exp $
4 *
5 * Copyright (c) 2002, Russell Gold
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
10 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
16 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
18 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 * DEALINGS IN THE SOFTWARE.
20 *
21 *******************************************************************************************************************/
22 import java.util.Vector;
23
24
25 /**
26 * Factory for creating HTML parsers. Parser customization properties can be specified but do not necessarily work
27 * for every parser type.
28 *
29 * @since 1.5.2
30 * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
31 * @author <a href="mailto:bw@xmlizer.biz">Bernhard Wagner</a>
32 **/
33 abstract public class HTMLParserFactory {
34
35 private static Vector _listeners = new Vector();
36 private static HTMLParser _jtidyParser;
37 private static HTMLParser _nekoParser;
38
39 private static HTMLParser _htmlParser;
40 private static boolean _preserveTagCase;
41 private static boolean _returnHTMLDocument;
42 private static boolean _parserWarningsEnabled;
43
44
45 /**
46 * Resets all settings to their default values. This includes the parser selection.
47 */
48 public static void reset() {
49 _preserveTagCase = false;
50 _returnHTMLDocument = true;
51 _parserWarningsEnabled = false;
52 _htmlParser = null;
53 }
54
55
56 /**
57 * Selects the JTidy parser, if present.
58 */
59 public static void useJTidyParser() {
60 if (_jtidyParser == null) throw new RuntimeException( "JTidy parser not available" );
61 _htmlParser = _jtidyParser;
62 }
63
64
65 /**
66 * Selects the NekoHTML parser, if present.
67 */
68 public static void useNekoHTMLParser() {
69 if (_nekoParser == null) throw new RuntimeException( "NekoHTML parser not available" );
70 _htmlParser = _nekoParser;
71 }
72
73
74 /**
75 * Specifies the parser to use.
76 */
77 public static void setHTMLParser( HTMLParser htmlParser ) {
78 _htmlParser = htmlParser;
79 }
80
81
82 /**
83 * Returns the current selected parser.
84 */
85 public static HTMLParser getHTMLParser() {
86 if (_htmlParser == null) {
87 if (_nekoParser != null) {
88 _htmlParser = _nekoParser;
89 } else if (_jtidyParser != null) {
90 _htmlParser = _jtidyParser;
91 } else {
92 throw new RuntimeException( "No HTML parser found. Make sure that either nekoHTML.jar or Tidy.jar is in the in classpath" );
93 }
94 }
95 return _htmlParser;
96 }
97
98
99 /**
100 * Returns true if the current parser will preserve the case of HTML tags and attributes.
101 */
102 public static boolean isPreserveTagCase() {
103 return _preserveTagCase && getHTMLParser().supportsPreserveTagCase();
104 }
105
106
107 /**
108 * Specifies whether the parser should preserve the case of HTML tags and attributes. Not every parser can
109 * support this capability. Note that enabling this will disable support for the HTMLDocument class.
110 * @see #setReturnHTMLDocument
111 */
112 public static void setPreserveTagCase( boolean preserveTagCase ) {
113 _preserveTagCase = preserveTagCase;
114 if (preserveTagCase) _returnHTMLDocument = false;
115 }
116
117
118 /**
119 * Returns true if the current parser will return an HTMLDocument object rather than a Document object.
120 */
121 public static boolean isReturnHTMLDocument() {
122 return _returnHTMLDocument && getHTMLParser().supportsReturnHTMLDocument();
123 }
124
125
126 /**
127 * Specifies whether the parser should return an HTMLDocument object rather than a Document object.
128 * Not every parser can support this capability. Note that enabling this will disable preservation of tag case.
129 * @see #setPreserveTagCase
130 */
131 public static void setReturnHTMLDocument( boolean returnHTMLDocument ) {
132 _returnHTMLDocument = returnHTMLDocument;
133 if (returnHTMLDocument) _preserveTagCase = false;
134 }
135
136
137 /**
138 * Returns true if parser warnings are enabled.
139 **/
140 public static boolean isParserWarningsEnabled() {
141 return _parserWarningsEnabled && getHTMLParser().supportsParserWarnings();
142 }
143
144
145 /**
146 * If true, tells the parser to display warning messages. The default is false (warnings are not shown).
147 **/
148 public static void setParserWarningsEnabled( boolean enabled ) {
149 _parserWarningsEnabled = enabled;
150 }
151
152
153 /**
154 * Remove an HTML Parser listener.
155 **/
156 public static void removeHTMLParserListener( HTMLParserListener el ) {
157 _listeners.removeElement( el );
158 }
159
160
161 /**
162 * Add an HTML Parser listener.
163 **/
164 public static void addHTMLParserListener( HTMLParserListener el ) {
165 _listeners.addElement( el );
166 }
167
168
169 //------------------------------------- package protected members ------------------------------------------------------
170
171
172 /**
173 * Get the list of Html Error Listeners
174 **/
175 static Vector getHTMLParserListeners() {
176 return _listeners;
177 }
178
179
180 private static HTMLParser loadParserIfSupported( final String testClassName, final String parserClassName ) {
181 try {
182 Class.forName( testClassName );
183 return (HTMLParser) Class.forName( parserClassName ).newInstance();
184 } catch (InstantiationException e) {
185 } catch (IllegalAccessException e) {
186 } catch (ClassNotFoundException e) {
187 }
188 return null;
189 }
190
191
192 static {
193 _jtidyParser = loadParserIfSupported( "org.w3c.tidy.Parser", "com.meterware.httpunit.parsing.JTidyHTMLParser" );
194 _nekoParser = loadParserIfSupported( "org.cyberneko.html.HTMLConfiguration", "com.meterware.httpunit.parsing.NekoHTMLParser" );
195 reset();
196 }
197
198
199 }