• 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

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
    /** Constructs a script object with the specified configuration. */
62
    ScriptFilter(HTMLConfiguration config) {
1✔
63
        _configuration = config;
1✔
64
    }
1✔
65

66
    public void setScriptHandler(ScriptHandler scriptHandler) {
67
        _scriptHandler = scriptHandler;
1✔
68
    }
1✔
69

70
    public void startDocument(XMLLocator locator, String encoding, Augmentations augs) throws XNIException {
71
        _activeScriptBlock = null;
×
72
        _systemID = locator == null ? "" : locator.getLiteralSystemId() + "_";
×
73
        _scriptIndex = 0;
×
74
        super.startDocument(locator, encoding, null, augs);
×
75
    }
×
76

77
    /**
78
     * Invoked for a start element. If the element is a <script>, overrides the normal behavior to begin collecting the
79
     * script text.
80
     */
81
    @Override
82
    public void startElement(QName element, XMLAttributes attrs, Augmentations augs) throws XNIException {
83
        if (!isSupportedScript(element, attrs)) {
1✔
84
            super.startElement(element, attrs, augs);
1✔
85
        } else {
86
            _activeScriptBlock = new StringBuilder();
1✔
87
            _scriptLanguage = getScriptLanguage(attrs);
1✔
88
            String srcAttribute = attrs.getValue("src");
1✔
89
            if (srcAttribute != null) {
1✔
90
                _activeScriptBlock.append(_scriptHandler.getIncludedScript(srcAttribute));
1✔
91
            }
92
        }
93
    }
1✔
94

95
    private boolean isSupportedScript(QName element, XMLAttributes attrs) {
96
        if (!element.rawname.equalsIgnoreCase("script") || attrs == null) {
1!
97
            return false;
1✔
98
        }
99
        String value = getScriptLanguage(attrs);
1✔
100
        return HttpUnitOptions.isScriptingEnabled() && _scriptHandler.supportsScriptLanguage(value);
1✔
101
    }
102

103
    private String getScriptLanguage(XMLAttributes attrs) {
104
        return attrs == null ? null : attrs.getValue("language");
1!
105
    }
106

107
    @Override
108
    public void emptyElement(QName element, XMLAttributes attrs, Augmentations augs) throws XNIException {
109
        if (!isSupportedScript(element, attrs)) {
1!
110
            super.emptyElement(element, attrs, augs);
1✔
111
        }
112
    }
1✔
113

114
    @Override
115
    public void characters(XMLString text, Augmentations augs) throws XNIException {
116
        if (_activeScriptBlock != null) {
1✔
117
            _activeScriptBlock.append(text.ch, text.offset, text.length);
1✔
118
        } else {
119
            super.characters(text, augs);
1✔
120
        }
121
    }
1✔
122

123
    @Override
124
    public void endElement(QName element, Augmentations augs) throws XNIException {
125
        if (_activeScriptBlock == null) {
1✔
126
            super.endElement(element, augs);
1✔
127
        } else {
128
            try {
129
                final String scriptText = _activeScriptBlock.toString();
1✔
130
                String replacementText = getTranslatedScript(_scriptLanguage, scriptText);
1✔
131
                _configuration.pushInputSource(newInputSource(replacementText));
1✔
132
            } catch (IOException e) { // ignore
×
133
            } finally {
134
                _activeScriptBlock = null;
1✔
135
            }
136
        }
137
    }
1✔
138

139
    private XMLInputSource newInputSource(String replacementText) {
140
        StringBuilder systemID = new StringBuilder(_systemID);
1✔
141
        systemID.append("script").append(++_scriptIndex);
1✔
142

143
        return new XMLInputSource(null, systemID.toString(), null, new StringReader(replacementText), "UTF-8");
1✔
144
    }
145

146
    protected String getTranslatedScript(final String language, final String scriptText) throws IOException {
147
        return _scriptHandler.runScript(language, scriptText);
1✔
148
    }
149

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