• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / httpunit / 636

05 Dec 2025 03:27AM UTC coverage: 80.509%. Remained the same
636

push

github

hazendaz
Cleanup more old since tags

you guessed it, at this point going to jautodoc the rest so the warnings on builds go away ;)

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8249 of 10132 relevant lines covered (81.42%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

43.12
/src/main/java/com/meterware/httpunit/parsing/HTMLParserFactory.java
1
/*
2
 * MIT License
3
 *
4
 * Copyright 2011-2025 Russell Gold
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
9
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
 * of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
15
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
 * DEALINGS IN THE SOFTWARE.
19
 */
20
package com.meterware.httpunit.parsing;
21

22
import java.lang.reflect.InvocationTargetException;
23
import java.util.ArrayList;
24
import java.util.List;
25

26
/**
27
 * Factory for creating HTML parsers. Parser customization properties can be specified but do not necessarily work for
28
 * every parser type.
29
 **/
30
public abstract class HTMLParserFactory {
×
31

32
    private static List<HTMLParserListener> _listeners = new ArrayList<>();
1✔
33
    private static HTMLParser _jtidyParser;
34
    private static HTMLParser _nekoParser;
35

36
    private static HTMLParser _htmlParser;
37
    private static boolean _preserveTagCase;
38
    private static boolean _returnHTMLDocument;
39
    private static boolean _parserWarningsEnabled;
40
    // for parsers that support forcing Case
41
    private static boolean _forceUpper;
42
    private static boolean _forceLower;
43

44
    /**
45
     * Resets all settings to their default values. This includes the parser selection.
46
     */
47
    public static void reset() {
48
        _preserveTagCase = false;
1✔
49
        _returnHTMLDocument = true;
1✔
50
        _parserWarningsEnabled = false;
1✔
51
        _htmlParser = null;
1✔
52
        _forceUpper = false;
1✔
53
        _forceLower = false;
1✔
54
        _listeners.clear();
1✔
55
    }
1✔
56

57
    /**
58
     * Selects the JTidy parser, if present.
59
     */
60
    public static void useJTidyParser() {
61
        if (_jtidyParser == null) {
×
62
            throw new RuntimeException("JTidy parser not available");
×
63
        }
64
        _htmlParser = _jtidyParser;
×
65
    }
×
66

67
    /**
68
     * Selects the NekoHTML parser, if present.
69
     */
70
    public static void useNekoHTMLParser() {
71
        if (_nekoParser == null) {
×
72
            throw new RuntimeException("NekoHTML parser not available");
×
73
        }
74
        _htmlParser = _nekoParser;
×
75
    }
×
76

77
    /**
78
     * Specifies the parser to use.
79
     */
80
    public static void setHTMLParser(HTMLParser htmlParser) {
81
        _htmlParser = htmlParser;
1✔
82
    }
1✔
83

84
    /**
85
     * Returns the current selected parser.
86
     */
87
    public static HTMLParser getHTMLParser() {
88
        if (_htmlParser == null) {
1✔
89
            if (_nekoParser != null) {
1!
90
                _htmlParser = _nekoParser;
1✔
91
            } else if (_jtidyParser != null) {
×
92
                _htmlParser = _jtidyParser;
×
93
            } else {
94
                throw new RuntimeException(
×
95
                        "No HTML parser found. Make sure that either nekoHTML.jar or Tidy.jar is in the in classpath");
96
            }
97
        }
98
        return _htmlParser;
1✔
99
    }
100

101
    /**
102
     * Returns true if the current parser will preserve the case of HTML tags and attributes.
103
     */
104
    public static boolean isPreserveTagCase() {
105
        return _preserveTagCase && getHTMLParser().supportsPreserveTagCase();
1!
106
    }
107

108
    /**
109
     * Specifies whether the parser should preserve the case of HTML tags and attributes. Not every parser can support
110
     * this capability. Note that enabling this will disable support for the HTMLDocument class. override any previous
111
     * behaviour configured by calling {@link #setForceUpperCase(boolean)} or {@link #setForceLowerCase(boolean)}
112
     *
113
     * @see #setReturnHTMLDocument
114
     * @see #setForceUpperCase(boolean)
115
     * @see #setForceLowerCase(boolean)
116
     */
117
    public static void setPreserveTagCase(boolean preserveTagCase) {
118
        _preserveTagCase = preserveTagCase;
1✔
119
        if (preserveTagCase) {
1!
120
            _forceLower = false;
×
121
            _forceUpper = false;
×
122
        }
123
    }
1✔
124

125
    /**
126
     * Returns true if the current parser will return an HTMLDocument object rather than a Document object.
127
     */
128
    public static boolean isReturnHTMLDocument() {
129
        return _returnHTMLDocument && !isPreserveTagCase() && getHTMLParser().supportsReturnHTMLDocument();
1!
130
    }
131

132
    /**
133
     * Specifies whether the parser should return an HTMLDocument object rather than a Document object. Not every parser
134
     * can support this capability. Note that enabling this will disable preservation of tag case. and/or the forcing of
135
     * the tag case to upper or lower case.
136
     *
137
     * @see #setPreserveTagCase
138
     * @see #setForceUpperCase(boolean)
139
     * @see #setForceLowerCase(boolean)
140
     */
141
    public static void setReturnHTMLDocument(boolean returnHTMLDocument) {
142
        _returnHTMLDocument = returnHTMLDocument;
×
143
        if (returnHTMLDocument) {
×
144
            _preserveTagCase = false;
×
145
            _forceLower = false;
×
146
            _forceUpper = false;
×
147
        }
148
    }
×
149

150
    /**
151
     * Specifies whether the parser should force the case of HTML tags and attributes to be upper case. Not every parser
152
     * can support this capability. Note that enabling this will disable support for the HTMLDocument class and override
153
     * any previous behaviour configured by enabling {@link #setPreserveTagCase(boolean)} or
154
     * {@link #setForceLowerCase(boolean)}
155
     *
156
     * @see #setReturnHTMLDocument
157
     * @see #setPreserveTagCase(boolean)
158
     * @see #setForceLowerCase(boolean)
159
     *
160
     * @param forceUpper
161
     *            boolean indicating whether to enable this functionality
162
     */
163
    public static void setForceUpperCase(boolean forceUpper) {
164
        _forceUpper = forceUpper;
×
165
        if (_forceUpper) {
×
166
            _forceLower = false;
×
167
            _preserveTagCase = false;
×
168
            // _returnHTMLDocument = false;
169
        }
170
    }
×
171

172
    /**
173
     * Return true if the current parser will support forcing the tags and attributes to upper case
174
     *
175
     * @return boolean flag
176
     */
177
    public static boolean getForceUpperCase() {
178
        return _forceUpper && getHTMLParser().supportsPreserveTagCase();
1!
179
    }
180

181
    /**
182
     * Specifies whether the parser should force the case of HTML tags and attributes to lower case. Not every parser
183
     * can support this capability. Note that enabling this will disable support for the HTMLDocument class and override
184
     * any previous behaviour configured by enabling {@link #setPreserveTagCase(boolean)} or
185
     * {@link #setForceUpperCase(boolean)}
186
     *
187
     * @see #setReturnHTMLDocument
188
     * @see #setPreserveTagCase(boolean)
189
     * @see #setForceUpperCase(boolean)
190
     *
191
     * @param forceLower
192
     *            boolean indicating whether to enable this functionality
193
     */
194
    public static void setForceLowerCase(boolean forceLower) {
195
        _forceLower = forceLower;
×
196
        if (_forceLower) {
×
197
            _forceUpper = false;
×
198
            _preserveTagCase = false;
×
199
            // _returnHTMLDocument = false;
200
        }
201
    }
×
202

203
    /**
204
     * Return true if the current parser will support forcing the tags and attributes to lower case
205
     *
206
     * @return boolean flag
207
     */
208
    public static boolean getForceLowerCase() {
209
        return _forceLower && getHTMLParser().supportsPreserveTagCase();
1!
210
    }
211

212
    /**
213
     * Returns true if parser warnings are enabled.
214
     **/
215
    public static boolean isParserWarningsEnabled() {
216
        return _parserWarningsEnabled && getHTMLParser().supportsParserWarnings();
1!
217
    }
218

219
    /**
220
     * If true, tells the parser to display warning messages. The default is false (warnings are not shown).
221
     **/
222
    public static void setParserWarningsEnabled(boolean enabled) {
223
        _parserWarningsEnabled = enabled;
1✔
224
    }
1✔
225

226
    /**
227
     * Remove an HTML Parser listener.
228
     **/
229
    public static void removeHTMLParserListener(HTMLParserListener el) {
230
        _listeners.remove(el);
1✔
231
    }
1✔
232

233
    /**
234
     * Add an HTML Parser listener.
235
     **/
236
    public static void addHTMLParserListener(HTMLParserListener el) {
237
        _listeners.add(el);
1✔
238
    }
1✔
239

240
    // ------------------------------------- package protected members
241
    // ------------------------------------------------------
242

243
    /**
244
     * Get the list of Html Error Listeners
245
     **/
246
    static List<HTMLParserListener> getHTMLParserListeners() {
247
        return _listeners;
1✔
248
    }
249

250
    private static HTMLParser loadParserIfSupported(final String testClassName, final String parserClassName) {
251
        try {
252
            Class.forName(testClassName);
1✔
253
            return (HTMLParser) Class.forName(parserClassName).getDeclaredConstructor().newInstance();
1✔
254
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
×
255
                | NoSuchMethodException | SecurityException | ClassNotFoundException e) {
256
            e.printStackTrace();
×
257
        }
258
        return null;
×
259
    }
260

261
    static {
262
        _jtidyParser = loadParserIfSupported("org.w3c.tidy.Parser", "com.meterware.httpunit.parsing.JTidyHTMLParser");
1✔
263
        _nekoParser = loadParserIfSupported("net.sourceforge.htmlunit.cyberneko.HTMLConfiguration",
1✔
264
                "com.meterware.httpunit.parsing.NekoHTMLParser");
265
        reset();
1✔
266
    }
1✔
267

268
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc