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

hazendaz / httpunit / 656

06 Dec 2025 09:11PM UTC coverage: 80.452% (+0.02%) from 80.435%
656

push

github

hazendaz
[maven-release-plugin] prepare for next development iteration

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10137 relevant lines covered (81.34%)

0.81 hits per line

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

83.08
/src/main/java/com/meterware/httpunit/parsing/ScriptFilter.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 com.meterware.httpunit.HttpUnitOptions;
23

24
import java.io.IOException;
25
import java.io.StringReader;
26

27
import net.sourceforge.htmlunit.cyberneko.HTMLConfiguration;
28
import net.sourceforge.htmlunit.cyberneko.filters.DefaultFilter;
29

30
import org.apache.xerces.xni.Augmentations;
31
import org.apache.xerces.xni.QName;
32
import org.apache.xerces.xni.XMLAttributes;
33
import org.apache.xerces.xni.XMLLocator;
34
import org.apache.xerces.xni.XMLString;
35
import org.apache.xerces.xni.XNIException;
36
import org.apache.xerces.xni.parser.XMLInputSource;
37

38
/**
39
 * A filter to interpret JavaScript script blocks, based on the sample Scripts program provided by NekoHTML.
40
 **/
41
class ScriptFilter extends DefaultFilter {
42

43
    /** The NekoHTML configuration. */
44
    private HTMLConfiguration _configuration;
45

46
    /** A string buffer to collect the script. */
47
    private StringBuilder _activeScriptBlock;
48

49
    /** The name of the current script language. **/
50
    private String _scriptLanguage;
51

52
    /** The system identifier of the source document. */
53
    private String _systemID = "";
1✔
54

55
    /** The number of the current script. */
56
    private int _scriptIndex;
57

58
    /** The parser in which this filter is running. **/
59
    private ScriptHandler _scriptHandler;
60

61
    /**
62
     * Constructs a script object with the specified configuration.
63
     *
64
     * @param config
65
     *            the config
66
     */
67
    ScriptFilter(HTMLConfiguration config) {
1✔
68
        _configuration = config;
1✔
69
    }
1✔
70

71
    /**
72
     * Sets the script handler.
73
     *
74
     * @param scriptHandler
75
     *            the new script handler
76
     */
77
    public void setScriptHandler(ScriptHandler scriptHandler) {
78
        _scriptHandler = scriptHandler;
1✔
79
    }
1✔
80

81
    /**
82
     * Start document.
83
     *
84
     * @param locator
85
     *            the locator
86
     * @param encoding
87
     *            the encoding
88
     * @param augs
89
     *            the augs
90
     *
91
     * @throws XNIException
92
     *             the XNI exception
93
     */
94
    public void startDocument(XMLLocator locator, String encoding, Augmentations augs) throws XNIException {
95
        _activeScriptBlock = null;
×
96
        _systemID = locator == null ? "" : locator.getLiteralSystemId() + "_";
×
97
        _scriptIndex = 0;
×
98
        super.startDocument(locator, encoding, null, augs);
×
99
    }
×
100

101
    /**
102
     * Invoked for a start element. If the element is a <script>, overrides the normal behavior to begin collecting the
103
     * script text.
104
     */
105
    @Override
106
    public void startElement(QName element, XMLAttributes attrs, Augmentations augs) throws XNIException {
107
        if (!isSupportedScript(element, attrs)) {
1✔
108
            super.startElement(element, attrs, augs);
1✔
109
        } else {
110
            _activeScriptBlock = new StringBuilder();
1✔
111
            _scriptLanguage = getScriptLanguage(attrs);
1✔
112
            String srcAttribute = attrs.getValue("src");
1✔
113
            if (srcAttribute != null) {
1✔
114
                _activeScriptBlock.append(_scriptHandler.getIncludedScript(srcAttribute));
1✔
115
            }
116
        }
117
    }
1✔
118

119
    /**
120
     * Checks if is supported script.
121
     *
122
     * @param element
123
     *            the element
124
     * @param attrs
125
     *            the attrs
126
     *
127
     * @return true, if is supported script
128
     */
129
    private boolean isSupportedScript(QName element, XMLAttributes attrs) {
130
        if (!element.rawname.equalsIgnoreCase("script") || attrs == null) {
1!
131
            return false;
1✔
132
        }
133
        String value = getScriptLanguage(attrs);
1✔
134
        return HttpUnitOptions.isScriptingEnabled() && _scriptHandler.supportsScriptLanguage(value);
1✔
135
    }
136

137
    /**
138
     * Gets the script language.
139
     *
140
     * @param attrs
141
     *            the attrs
142
     *
143
     * @return the script language
144
     */
145
    private String getScriptLanguage(XMLAttributes attrs) {
146
        return attrs == null ? null : attrs.getValue("language");
1!
147
    }
148

149
    @Override
150
    public void emptyElement(QName element, XMLAttributes attrs, Augmentations augs) throws XNIException {
151
        if (!isSupportedScript(element, attrs)) {
1!
152
            super.emptyElement(element, attrs, augs);
1✔
153
        }
154
    }
1✔
155

156
    @Override
157
    public void characters(XMLString text, Augmentations augs) throws XNIException {
158
        if (_activeScriptBlock != null) {
1✔
159
            _activeScriptBlock.append(text.ch, text.offset, text.length);
1✔
160
        } else {
161
            super.characters(text, augs);
1✔
162
        }
163
    }
1✔
164

165
    @Override
166
    public void endElement(QName element, Augmentations augs) throws XNIException {
167
        if (_activeScriptBlock == null) {
1✔
168
            super.endElement(element, augs);
1✔
169
        } else {
170
            try {
171
                final String scriptText = _activeScriptBlock.toString();
1✔
172
                String replacementText = getTranslatedScript(_scriptLanguage, scriptText);
1✔
173
                _configuration.pushInputSource(newInputSource(replacementText));
1✔
174
            } catch (IOException e) { // ignore
×
175
            } finally {
176
                _activeScriptBlock = null;
1✔
177
            }
178
        }
179
    }
1✔
180

181
    /**
182
     * New input source.
183
     *
184
     * @param replacementText
185
     *            the replacement text
186
     *
187
     * @return the XML input source
188
     */
189
    private XMLInputSource newInputSource(String replacementText) {
190
        StringBuilder systemID = new StringBuilder(_systemID);
1✔
191
        systemID.append("script").append(++_scriptIndex);
1✔
192

193
        return new XMLInputSource(null, systemID.toString(), null, new StringReader(replacementText), "UTF-8");
1✔
194
    }
195

196
    /**
197
     * Gets the translated script.
198
     *
199
     * @param language
200
     *            the language
201
     * @param scriptText
202
     *            the script text
203
     *
204
     * @return the translated script
205
     *
206
     * @throws IOException
207
     *             Signals that an I/O exception has occurred.
208
     */
209
    protected String getTranslatedScript(final String language, final String scriptText) throws IOException {
210
        return _scriptHandler.runScript(language, scriptText);
1✔
211
    }
212

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