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

hazendaz / httpunit / #155

20 Aug 2024 11:42PM UTC coverage: 80.622% (+0.004%) from 80.618%
#155

push

github

hazendaz
[ci] format the code

3231 of 4119 branches covered (78.44%)

Branch coverage included in aggregate %.

68 of 80 new or added lines in 21 files covered. (85.0%)

4 existing lines in 4 files now uncovered.

8285 of 10165 relevant lines covered (81.51%)

0.82 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-2024 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
 * @since 1.5.2
31
 *
32
 * @author <a href="mailto:russgold@httpunit.org">Russell Gold</a>
33
 * @author <a href="mailto:bw@xmlizer.biz">Bernhard Wagner</a>
34
 **/
35
public abstract class HTMLParserFactory {
×
36

37
    private static List<HTMLParserListener> _listeners = new ArrayList<>();
1✔
38
    private static HTMLParser _jtidyParser;
39
    private static HTMLParser _nekoParser;
40

41
    private static HTMLParser _htmlParser;
42
    private static boolean _preserveTagCase;
43
    private static boolean _returnHTMLDocument;
44
    private static boolean _parserWarningsEnabled;
45
    // for parsers that support forcing Case
46
    private static boolean _forceUpper;
47
    private static boolean _forceLower;
48

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

62
    /**
63
     * Selects the JTidy parser, if present.
64
     */
65
    public static void useJTidyParser() {
66
        if (_jtidyParser == null) {
×
67
            throw new RuntimeException("JTidy parser not available");
×
68
        }
69
        _htmlParser = _jtidyParser;
×
70
    }
×
71

72
    /**
73
     * Selects the NekoHTML parser, if present.
74
     */
75
    public static void useNekoHTMLParser() {
76
        if (_nekoParser == null) {
×
77
            throw new RuntimeException("NekoHTML parser not available");
×
78
        }
79
        _htmlParser = _nekoParser;
×
80
    }
×
81

82
    /**
83
     * Specifies the parser to use.
84
     */
85
    public static void setHTMLParser(HTMLParser htmlParser) {
86
        _htmlParser = htmlParser;
1✔
87
    }
1✔
88

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

106
    /**
107
     * Returns true if the current parser will preserve the case of HTML tags and attributes.
108
     */
109
    public static boolean isPreserveTagCase() {
110
        return _preserveTagCase && getHTMLParser().supportsPreserveTagCase();
1!
111
    }
112

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

130
    /**
131
     * Returns true if the current parser will return an HTMLDocument object rather than a Document object.
132
     */
133
    public static boolean isReturnHTMLDocument() {
134
        return _returnHTMLDocument && !isPreserveTagCase() && getHTMLParser().supportsReturnHTMLDocument();
1!
135
    }
136

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

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

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

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

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

217
    /**
218
     * Returns true if parser warnings are enabled.
219
     **/
220
    public static boolean isParserWarningsEnabled() {
221
        return _parserWarningsEnabled && getHTMLParser().supportsParserWarnings();
1!
222
    }
223

224
    /**
225
     * If true, tells the parser to display warning messages. The default is false (warnings are not shown).
226
     **/
227
    public static void setParserWarningsEnabled(boolean enabled) {
228
        _parserWarningsEnabled = enabled;
1✔
229
    }
1✔
230

231
    /**
232
     * Remove an HTML Parser listener.
233
     **/
234
    public static void removeHTMLParserListener(HTMLParserListener el) {
235
        _listeners.remove(el);
1✔
236
    }
1✔
237

238
    /**
239
     * Add an HTML Parser listener.
240
     **/
241
    public static void addHTMLParserListener(HTMLParserListener el) {
242
        _listeners.add(el);
1✔
243
    }
1✔
244

245
    // ------------------------------------- package protected members
246
    // ------------------------------------------------------
247

248
    /**
249
     * Get the list of Html Error Listeners
250
     **/
251
    static List<HTMLParserListener> getHTMLParserListeners() {
252
        return _listeners;
1✔
253
    }
254

255
    private static HTMLParser loadParserIfSupported(final String testClassName, final String parserClassName) {
256
        try {
257
            Class.forName(testClassName);
1✔
258
            return (HTMLParser) Class.forName(parserClassName).getDeclaredConstructor().newInstance();
1✔
NEW
259
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
×
260
                | NoSuchMethodException | SecurityException | ClassNotFoundException e) {
UNCOV
261
            e.printStackTrace();
×
262
        }
263
        return null;
×
264
    }
265

266
    static {
267
        _jtidyParser = loadParserIfSupported("org.w3c.tidy.Parser", "com.meterware.httpunit.parsing.JTidyHTMLParser");
1✔
268
        _nekoParser = loadParserIfSupported("net.sourceforge.htmlunit.cyberneko.HTMLConfiguration",
1✔
269
                "com.meterware.httpunit.parsing.NekoHTMLParser");
270
        reset();
1✔
271
    }
1✔
272

273
}
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