Skip to content

Commit a961e3b

Browse files
committed
use our own XMLLocatorImpl
1 parent f09aef4 commit a961e3b

File tree

7 files changed

+126
-99
lines changed

7 files changed

+126
-99
lines changed

src/main/java/org/htmlunit/cyberneko/HTMLScanner.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,8 @@ public void pushInputSource(final XMLInputSource inputSource) {
523523
final String publicId = inputSource.getPublicId();
524524
final String baseSystemId = inputSource.getBaseSystemId();
525525
final String literalSystemId = inputSource.getSystemId();
526-
final String expandedSystemId = expandSystemId(literalSystemId, baseSystemId);
527-
fCurrentEntity = new CurrentEntity(reader, encoding, publicId, baseSystemId, literalSystemId, expandedSystemId);
526+
final String systemId = systemId(literalSystemId, baseSystemId);
527+
fCurrentEntity = new CurrentEntity(reader, encoding, publicId, baseSystemId, literalSystemId, systemId);
528528
}
529529

530530
private Reader getReader(final XMLInputSource inputSource) {
@@ -560,8 +560,8 @@ public void evaluateInputSource(final XMLInputSource inputSource) {
560560
final String publicId = inputSource.getPublicId();
561561
final String baseSystemId = inputSource.getBaseSystemId();
562562
final String literalSystemId = inputSource.getSystemId();
563-
final String expandedSystemId = expandSystemId(literalSystemId, baseSystemId);
564-
fCurrentEntity = new CurrentEntity(reader, encoding, publicId, baseSystemId, literalSystemId, expandedSystemId);
563+
final String systemId = systemId(literalSystemId, baseSystemId);
564+
fCurrentEntity = new CurrentEntity(reader, encoding, publicId, baseSystemId, literalSystemId, systemId);
565565
setScanner(fContentScanner);
566566
setScannerState(STATE_CONTENT);
567567
try {
@@ -631,7 +631,7 @@ public String getLiteralSystemId() {
631631
/** Returns the expanded system identifier. */
632632
@Override
633633
public String getSystemId() {
634-
return fCurrentEntity != null ? fCurrentEntity.expandedSystemId : null;
634+
return fCurrentEntity != null ? fCurrentEntity.systemId : null;
635635
}
636636

637637
/** Returns the current line number. */
@@ -823,14 +823,14 @@ public void setInputSource(final XMLInputSource source) throws IOException {
823823
final String publicId = source.getPublicId();
824824
final String baseSystemId = source.getBaseSystemId();
825825
final String literalSystemId = source.getSystemId();
826-
final String expandedSystemId = expandSystemId(literalSystemId, baseSystemId);
826+
final String systemId = systemId(literalSystemId, baseSystemId);
827827

828828
// open stream
829829
Reader reader = source.getCharacterStream();
830830
if (reader == null) {
831831
InputStream inputStream = source.getByteStream();
832832
if (inputStream == null) {
833-
final URL url = new URL(expandedSystemId);
833+
final URL url = new URL(systemId);
834834
inputStream = url.openStream();
835835
}
836836
fByteStream = new PlaybackInputStream(inputStream);
@@ -868,7 +868,7 @@ public void setInputSource(final XMLInputSource source) throws IOException {
868868
reader = new BufferedReader(new InputStreamReader(fByteStream, fJavaEncoding));
869869
}
870870
}
871-
fCurrentEntity = new CurrentEntity(reader, fIANAEncoding, publicId, baseSystemId, literalSystemId, expandedSystemId);
871+
fCurrentEntity = new CurrentEntity(reader, fIANAEncoding, publicId, baseSystemId, literalSystemId, systemId);
872872

873873
// set scanner and state
874874
if (fFragmentSpecialScannerTag_ != null) {
@@ -954,7 +954,7 @@ protected static String getValue(final XMLAttributes attrs, final String aname)
954954
*
955955
*/
956956
@SuppressWarnings("unused")
957-
public static String expandSystemId(final String systemId, final String baseSystemId) {
957+
public static String systemId(final String systemId, final String baseSystemId) {
958958

959959
// check for bad parameters id
960960
if (systemId == null || systemId.length() == 0) {
@@ -1748,7 +1748,7 @@ private static final class CurrentEntity {
17481748
public final String literalSystemId;
17491749

17501750
/** Expanded system identifier. */
1751-
final String expandedSystemId;
1751+
final String systemId;
17521752

17531753
/** XML version. */
17541754
public final String version = "1.0";
@@ -1777,13 +1777,13 @@ private static final class CurrentEntity {
17771777

17781778
// Constructs an entity from the specified stream.
17791779
CurrentEntity(final Reader stream, final String encoding, final String publicId,
1780-
final String baseSystemId, final String literalSystemId, final String expandedSystemId) {
1780+
final String baseSystemId, final String literalSystemId, final String systemId) {
17811781
stream_ = stream;
17821782
encoding_ = encoding;
17831783
this.publicId = publicId;
17841784
this.baseSystemId = baseSystemId;
17851785
this.literalSystemId = literalSystemId;
1786-
this.expandedSystemId = expandedSystemId;
1786+
this.systemId = systemId;
17871787
}
17881788

17891789
char getCurrentChar() {

src/main/java/org/htmlunit/cyberneko/xerces/parsers/AbstractDOMParser.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ public void parse(final String systemId) throws SAXException, IOException {
716716
// a SAXParseException
717717
final LocatorImpl locatorImpl = new LocatorImpl();
718718
locatorImpl.setPublicId(e.getPublicId());
719-
locatorImpl.setSystemId(e.getExpandedSystemId());
719+
locatorImpl.setSystemId(e.getSystemId());
720720
locatorImpl.setLineNumber(e.getLineNumber());
721721
locatorImpl.setColumnNumber(e.getColumnNumber());
722722
throw (ex == null) ? new SAXParseException(e.getMessage(), locatorImpl)
@@ -775,7 +775,7 @@ public void parse(final InputSource inputSource) throws SAXException, IOExceptio
775775
// a SAXParseException
776776
final LocatorImpl locatorImpl = new LocatorImpl();
777777
locatorImpl.setPublicId(e.getPublicId());
778-
locatorImpl.setSystemId(e.getExpandedSystemId());
778+
locatorImpl.setSystemId(e.getSystemId());
779779
locatorImpl.setLineNumber(e.getLineNumber());
780780
locatorImpl.setColumnNumber(e.getColumnNumber());
781781
throw (ex == null) ? new SAXParseException(e.getMessage(), locatorImpl)

src/main/java/org/htmlunit/cyberneko/xerces/parsers/AbstractSAXParser.java

+11-23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import org.htmlunit.cyberneko.xerces.util.ErrorHandlerWrapper;
2121
import org.htmlunit.cyberneko.xerces.util.SAXMessageFormatter;
22+
import org.htmlunit.cyberneko.xerces.util.XMLLocatorImpl;
2223
import org.htmlunit.cyberneko.xerces.xni.Augmentations;
2324
import org.htmlunit.cyberneko.xerces.xni.NamespaceContext;
2425
import org.htmlunit.cyberneko.xerces.xni.QName;
@@ -42,7 +43,6 @@
4243
import org.xml.sax.SAXParseException;
4344
import org.xml.sax.XMLReader;
4445
import org.xml.sax.ext.LexicalHandler;
45-
import org.xml.sax.ext.Locator2Impl;
4646

4747
/**
4848
* This is the base class of all SAX parsers. It implements both the SAX1 and
@@ -441,17 +441,11 @@ public void parse(final String systemId) throws SAXException, IOException {
441441
if (ex == null || ex instanceof CharConversionException) {
442442
// must be a parser exception; mine it for locator info
443443
// and throw a SAXParseException
444-
final Locator2Impl locatorImpl = new Locator2Impl();
445-
// since XMLParseExceptions know nothing about encoding,
446-
// we cannot return anything meaningful in this context.
447-
// We *could* consult the LocatorProxy, but the
448-
// application can do this itself if it wishes to possibly
449-
// be mislead.
450-
locatorImpl.setXMLVersion(fVersion);
451-
locatorImpl.setPublicId(e.getPublicId());
452-
locatorImpl.setSystemId(e.getExpandedSystemId());
453-
locatorImpl.setLineNumber(e.getLineNumber());
454-
locatorImpl.setColumnNumber(e.getColumnNumber());
444+
final XMLLocatorImpl locatorImpl = new XMLLocatorImpl(
445+
e.getPublicId(),
446+
e.getSystemId(),
447+
e.getLineNumber(),
448+
e.getColumnNumber());
455449
throw (ex == null) ? new SAXParseException(e.getMessage(), locatorImpl)
456450
: new SAXParseException(e.getMessage(), locatorImpl, ex);
457451
}
@@ -500,17 +494,11 @@ public void parse(final InputSource inputSource) throws SAXException, IOExceptio
500494
if (ex == null || ex instanceof CharConversionException) {
501495
// must be a parser exception; mine it for locator info
502496
// and throw a SAXParseException
503-
final Locator2Impl locatorImpl = new Locator2Impl();
504-
// since XMLParseExceptions know nothing about encoding,
505-
// we cannot return anything meaningful in this context.
506-
// We *could* consult the LocatorProxy, but the
507-
// application can do this itself if it wishes to possibly
508-
// be mislead.
509-
locatorImpl.setXMLVersion(fVersion);
510-
locatorImpl.setPublicId(e.getPublicId());
511-
locatorImpl.setSystemId(e.getExpandedSystemId());
512-
locatorImpl.setLineNumber(e.getLineNumber());
513-
locatorImpl.setColumnNumber(e.getColumnNumber());
497+
final XMLLocatorImpl locatorImpl = new XMLLocatorImpl(
498+
e.getPublicId(),
499+
e.getSystemId(),
500+
e.getLineNumber(),
501+
e.getColumnNumber());
514502
throw (ex == null) ? new SAXParseException(e.getMessage(), locatorImpl)
515503
: new SAXParseException(e.getMessage(), locatorImpl, ex);
516504
}

src/main/java/org/htmlunit/cyberneko/xerces/util/DefaultErrorHandler.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private void printError(final String type, final XMLParseException ex) {
6868
fOut_.print("[");
6969
fOut_.print(type);
7070
fOut_.print("] ");
71-
String systemId = ex.getExpandedSystemId();
71+
String systemId = ex.getSystemId();
7272
if (systemId != null) {
7373
final int index = systemId.lastIndexOf('/');
7474
if (index != -1) {

src/main/java/org/htmlunit/cyberneko/xerces/util/ErrorHandlerWrapper.java

+7-52
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
*/
1515
package org.htmlunit.cyberneko.xerces.util;
1616

17-
import org.htmlunit.cyberneko.xerces.xni.XMLLocator;
1817
import org.htmlunit.cyberneko.xerces.xni.XNIException;
1918
import org.htmlunit.cyberneko.xerces.xni.parser.XMLErrorHandler;
2019
import org.htmlunit.cyberneko.xerces.xni.parser.XMLParseException;
@@ -159,62 +158,18 @@ public void fatalError(final String domain, final String key, final XMLParseExce
159158

160159
// Creates a SAXParseException from an XMLParseException.
161160
protected static SAXParseException createSAXParseException(final XMLParseException exception) {
162-
return new SAXParseException(exception.getMessage(), exception.getPublicId(), exception.getExpandedSystemId(),
161+
return new SAXParseException(exception.getMessage(), exception.getPublicId(), exception.getSystemId(),
163162
exception.getLineNumber(), exception.getColumnNumber(), exception.getException());
164163
}
165164

166165
// Creates an XMLParseException from a SAXParseException. */
167166
protected static XMLParseException createXMLParseException(final SAXParseException exception) {
168-
final String fPublicId = exception.getPublicId();
169-
final String fSystemId = exception.getSystemId();
170-
final int fLineNumber = exception.getLineNumber();
171-
final int fColumnNumber = exception.getColumnNumber();
172-
final XMLLocator location = new XMLLocator() {
173-
@Override
174-
public String getPublicId() {
175-
return fPublicId;
176-
}
177-
178-
@Override
179-
public String getSystemId() {
180-
return fSystemId;
181-
}
182-
183-
@Override
184-
public String getBaseSystemId() {
185-
return null;
186-
}
187-
188-
@Override
189-
public String getLiteralSystemId() {
190-
return null;
191-
}
192-
193-
@Override
194-
public int getColumnNumber() {
195-
return fColumnNumber;
196-
}
197-
198-
@Override
199-
public int getLineNumber() {
200-
return fLineNumber;
201-
}
202-
203-
@Override
204-
public int getCharacterOffset() {
205-
return -1;
206-
}
207-
208-
@Override
209-
public String getEncoding() {
210-
return null;
211-
}
212-
213-
@Override
214-
public String getXMLVersion() {
215-
return null;
216-
}
217-
};
167+
final XMLLocatorImpl location = new XMLLocatorImpl(
168+
exception.getPublicId(),
169+
exception.getSystemId(),
170+
exception.getLineNumber(),
171+
exception.getColumnNumber()
172+
);
218173
return new XMLParseException(location, exception.getMessage(), exception);
219174
}
220175

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2017-2024 Ronald Brill
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* https://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
package org.htmlunit.cyberneko.xerces.util;
16+
17+
import org.htmlunit.cyberneko.xerces.xni.XMLLocator;
18+
19+
/**
20+
* The XMLLocatorImpl class is an implementation of the XMLLocator
21+
* interface.
22+
*
23+
* @author Ronald Brill
24+
*/
25+
public class XMLLocatorImpl implements XMLLocator {
26+
27+
private final String publicId_;
28+
private final String systemId_;
29+
private final int lineNumber_;
30+
private final int columnNumber_;
31+
32+
public XMLLocatorImpl(final String publicId, final String systemId,
33+
final int lineNumber, final int columnNumber) {
34+
publicId_ = publicId;
35+
systemId_ = systemId;
36+
lineNumber_ = lineNumber;
37+
columnNumber_ = columnNumber;
38+
}
39+
40+
@Override
41+
public String getXMLVersion() {
42+
return null;
43+
}
44+
45+
@Override
46+
public String getEncoding() {
47+
return null;
48+
}
49+
50+
@Override
51+
public String getPublicId() {
52+
return publicId_;
53+
}
54+
55+
@Override
56+
public String getSystemId() {
57+
return systemId_;
58+
}
59+
60+
@Override
61+
public int getLineNumber() {
62+
return lineNumber_;
63+
}
64+
65+
@Override
66+
public int getColumnNumber() {
67+
return columnNumber_;
68+
}
69+
70+
@Override
71+
public String getLiteralSystemId() {
72+
return null;
73+
}
74+
75+
@Override
76+
public String getBaseSystemId() {
77+
return null;
78+
}
79+
80+
@Override
81+
public int getCharacterOffset() {
82+
return -1;
83+
}
84+
}

src/main/java/org/htmlunit/cyberneko/xerces/xni/parser/XMLParseException.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ public class XMLParseException extends XNIException {
2929
/** Public identifier. */
3030
private String publicId_;
3131

32+
/** System identifier. */
33+
private String systemId_;
34+
3235
/** literal System identifier. */
3336
private String literalSystemId_;
3437

35-
/** expanded System identifier. */
36-
private String expandedSystemId_;
37-
3838
/** Base system identifier. */
3939
private String baseSystemId_;
4040

@@ -53,7 +53,7 @@ public XMLParseException(final XMLLocator locator, final String message) {
5353
if (locator != null) {
5454
publicId_ = locator.getPublicId();
5555
literalSystemId_ = locator.getLiteralSystemId();
56-
expandedSystemId_ = locator.getSystemId();
56+
systemId_ = locator.getSystemId();
5757
baseSystemId_ = locator.getBaseSystemId();
5858
lineNumber_ = locator.getLineNumber();
5959
columnNumber_ = locator.getColumnNumber();
@@ -67,7 +67,7 @@ public XMLParseException(final XMLLocator locator, final String message, final E
6767
if (locator != null) {
6868
publicId_ = locator.getPublicId();
6969
literalSystemId_ = locator.getLiteralSystemId();
70-
expandedSystemId_ = locator.getSystemId();
70+
systemId_ = locator.getSystemId();
7171
baseSystemId_ = locator.getBaseSystemId();
7272
lineNumber_ = locator.getLineNumber();
7373
columnNumber_ = locator.getColumnNumber();
@@ -81,8 +81,8 @@ public String getPublicId() {
8181
}
8282

8383
/** @return the expanded system identifier. */
84-
public String getExpandedSystemId() {
85-
return expandedSystemId_;
84+
public String getSystemId() {
85+
return systemId_;
8686
}
8787

8888
/** @return the literal system identifier. */
@@ -121,8 +121,8 @@ public String toString() {
121121
str.append(literalSystemId_);
122122
}
123123
str.append(':');
124-
if (expandedSystemId_ != null) {
125-
str.append(expandedSystemId_);
124+
if (systemId_ != null) {
125+
str.append(systemId_);
126126
}
127127
str.append(':');
128128
if (baseSystemId_ != null) {

0 commit comments

Comments
 (0)