Skip to content

Commit

Permalink
use our own XMLLocatorImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Oct 4, 2024
1 parent f09aef4 commit a961e3b
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 99 deletions.
24 changes: 12 additions & 12 deletions src/main/java/org/htmlunit/cyberneko/HTMLScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,8 @@ public void pushInputSource(final XMLInputSource inputSource) {
final String publicId = inputSource.getPublicId();
final String baseSystemId = inputSource.getBaseSystemId();
final String literalSystemId = inputSource.getSystemId();
final String expandedSystemId = expandSystemId(literalSystemId, baseSystemId);
fCurrentEntity = new CurrentEntity(reader, encoding, publicId, baseSystemId, literalSystemId, expandedSystemId);
final String systemId = systemId(literalSystemId, baseSystemId);
fCurrentEntity = new CurrentEntity(reader, encoding, publicId, baseSystemId, literalSystemId, systemId);
}

private Reader getReader(final XMLInputSource inputSource) {
Expand Down Expand Up @@ -560,8 +560,8 @@ public void evaluateInputSource(final XMLInputSource inputSource) {
final String publicId = inputSource.getPublicId();
final String baseSystemId = inputSource.getBaseSystemId();
final String literalSystemId = inputSource.getSystemId();
final String expandedSystemId = expandSystemId(literalSystemId, baseSystemId);
fCurrentEntity = new CurrentEntity(reader, encoding, publicId, baseSystemId, literalSystemId, expandedSystemId);
final String systemId = systemId(literalSystemId, baseSystemId);
fCurrentEntity = new CurrentEntity(reader, encoding, publicId, baseSystemId, literalSystemId, systemId);
setScanner(fContentScanner);
setScannerState(STATE_CONTENT);
try {
Expand Down Expand Up @@ -631,7 +631,7 @@ public String getLiteralSystemId() {
/** Returns the expanded system identifier. */
@Override
public String getSystemId() {
return fCurrentEntity != null ? fCurrentEntity.expandedSystemId : null;
return fCurrentEntity != null ? fCurrentEntity.systemId : null;
}

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

// open stream
Reader reader = source.getCharacterStream();
if (reader == null) {
InputStream inputStream = source.getByteStream();
if (inputStream == null) {
final URL url = new URL(expandedSystemId);
final URL url = new URL(systemId);
inputStream = url.openStream();
}
fByteStream = new PlaybackInputStream(inputStream);
Expand Down Expand Up @@ -868,7 +868,7 @@ public void setInputSource(final XMLInputSource source) throws IOException {
reader = new BufferedReader(new InputStreamReader(fByteStream, fJavaEncoding));
}
}
fCurrentEntity = new CurrentEntity(reader, fIANAEncoding, publicId, baseSystemId, literalSystemId, expandedSystemId);
fCurrentEntity = new CurrentEntity(reader, fIANAEncoding, publicId, baseSystemId, literalSystemId, systemId);

// set scanner and state
if (fFragmentSpecialScannerTag_ != null) {
Expand Down Expand Up @@ -954,7 +954,7 @@ protected static String getValue(final XMLAttributes attrs, final String aname)
*
*/
@SuppressWarnings("unused")
public static String expandSystemId(final String systemId, final String baseSystemId) {
public static String systemId(final String systemId, final String baseSystemId) {

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

/** Expanded system identifier. */
final String expandedSystemId;
final String systemId;

/** XML version. */
public final String version = "1.0";
Expand Down Expand Up @@ -1777,13 +1777,13 @@ private static final class CurrentEntity {

// Constructs an entity from the specified stream.
CurrentEntity(final Reader stream, final String encoding, final String publicId,
final String baseSystemId, final String literalSystemId, final String expandedSystemId) {
final String baseSystemId, final String literalSystemId, final String systemId) {
stream_ = stream;
encoding_ = encoding;
this.publicId = publicId;
this.baseSystemId = baseSystemId;
this.literalSystemId = literalSystemId;
this.expandedSystemId = expandedSystemId;
this.systemId = systemId;
}

char getCurrentChar() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ public void parse(final String systemId) throws SAXException, IOException {
// a SAXParseException
final LocatorImpl locatorImpl = new LocatorImpl();
locatorImpl.setPublicId(e.getPublicId());
locatorImpl.setSystemId(e.getExpandedSystemId());
locatorImpl.setSystemId(e.getSystemId());
locatorImpl.setLineNumber(e.getLineNumber());
locatorImpl.setColumnNumber(e.getColumnNumber());
throw (ex == null) ? new SAXParseException(e.getMessage(), locatorImpl)
Expand Down Expand Up @@ -775,7 +775,7 @@ public void parse(final InputSource inputSource) throws SAXException, IOExceptio
// a SAXParseException
final LocatorImpl locatorImpl = new LocatorImpl();
locatorImpl.setPublicId(e.getPublicId());
locatorImpl.setSystemId(e.getExpandedSystemId());
locatorImpl.setSystemId(e.getSystemId());
locatorImpl.setLineNumber(e.getLineNumber());
locatorImpl.setColumnNumber(e.getColumnNumber());
throw (ex == null) ? new SAXParseException(e.getMessage(), locatorImpl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.htmlunit.cyberneko.xerces.util.ErrorHandlerWrapper;
import org.htmlunit.cyberneko.xerces.util.SAXMessageFormatter;
import org.htmlunit.cyberneko.xerces.util.XMLLocatorImpl;
import org.htmlunit.cyberneko.xerces.xni.Augmentations;
import org.htmlunit.cyberneko.xerces.xni.NamespaceContext;
import org.htmlunit.cyberneko.xerces.xni.QName;
Expand All @@ -42,7 +43,6 @@
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.ext.Locator2Impl;

/**
* This is the base class of all SAX parsers. It implements both the SAX1 and
Expand Down Expand Up @@ -441,17 +441,11 @@ public void parse(final String systemId) throws SAXException, IOException {
if (ex == null || ex instanceof CharConversionException) {
// must be a parser exception; mine it for locator info
// and throw a SAXParseException
final Locator2Impl locatorImpl = new Locator2Impl();
// since XMLParseExceptions know nothing about encoding,
// we cannot return anything meaningful in this context.
// We *could* consult the LocatorProxy, but the
// application can do this itself if it wishes to possibly
// be mislead.
locatorImpl.setXMLVersion(fVersion);
locatorImpl.setPublicId(e.getPublicId());
locatorImpl.setSystemId(e.getExpandedSystemId());
locatorImpl.setLineNumber(e.getLineNumber());
locatorImpl.setColumnNumber(e.getColumnNumber());
final XMLLocatorImpl locatorImpl = new XMLLocatorImpl(
e.getPublicId(),
e.getSystemId(),
e.getLineNumber(),
e.getColumnNumber());
throw (ex == null) ? new SAXParseException(e.getMessage(), locatorImpl)
: new SAXParseException(e.getMessage(), locatorImpl, ex);
}
Expand Down Expand Up @@ -500,17 +494,11 @@ public void parse(final InputSource inputSource) throws SAXException, IOExceptio
if (ex == null || ex instanceof CharConversionException) {
// must be a parser exception; mine it for locator info
// and throw a SAXParseException
final Locator2Impl locatorImpl = new Locator2Impl();
// since XMLParseExceptions know nothing about encoding,
// we cannot return anything meaningful in this context.
// We *could* consult the LocatorProxy, but the
// application can do this itself if it wishes to possibly
// be mislead.
locatorImpl.setXMLVersion(fVersion);
locatorImpl.setPublicId(e.getPublicId());
locatorImpl.setSystemId(e.getExpandedSystemId());
locatorImpl.setLineNumber(e.getLineNumber());
locatorImpl.setColumnNumber(e.getColumnNumber());
final XMLLocatorImpl locatorImpl = new XMLLocatorImpl(
e.getPublicId(),
e.getSystemId(),
e.getLineNumber(),
e.getColumnNumber());
throw (ex == null) ? new SAXParseException(e.getMessage(), locatorImpl)
: new SAXParseException(e.getMessage(), locatorImpl, ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private void printError(final String type, final XMLParseException ex) {
fOut_.print("[");
fOut_.print(type);
fOut_.print("] ");
String systemId = ex.getExpandedSystemId();
String systemId = ex.getSystemId();
if (systemId != null) {
final int index = systemId.lastIndexOf('/');
if (index != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
*/
package org.htmlunit.cyberneko.xerces.util;

import org.htmlunit.cyberneko.xerces.xni.XMLLocator;
import org.htmlunit.cyberneko.xerces.xni.XNIException;
import org.htmlunit.cyberneko.xerces.xni.parser.XMLErrorHandler;
import org.htmlunit.cyberneko.xerces.xni.parser.XMLParseException;
Expand Down Expand Up @@ -159,62 +158,18 @@ public void fatalError(final String domain, final String key, final XMLParseExce

// Creates a SAXParseException from an XMLParseException.
protected static SAXParseException createSAXParseException(final XMLParseException exception) {
return new SAXParseException(exception.getMessage(), exception.getPublicId(), exception.getExpandedSystemId(),
return new SAXParseException(exception.getMessage(), exception.getPublicId(), exception.getSystemId(),
exception.getLineNumber(), exception.getColumnNumber(), exception.getException());
}

// Creates an XMLParseException from a SAXParseException. */
protected static XMLParseException createXMLParseException(final SAXParseException exception) {
final String fPublicId = exception.getPublicId();
final String fSystemId = exception.getSystemId();
final int fLineNumber = exception.getLineNumber();
final int fColumnNumber = exception.getColumnNumber();
final XMLLocator location = new XMLLocator() {
@Override
public String getPublicId() {
return fPublicId;
}

@Override
public String getSystemId() {
return fSystemId;
}

@Override
public String getBaseSystemId() {
return null;
}

@Override
public String getLiteralSystemId() {
return null;
}

@Override
public int getColumnNumber() {
return fColumnNumber;
}

@Override
public int getLineNumber() {
return fLineNumber;
}

@Override
public int getCharacterOffset() {
return -1;
}

@Override
public String getEncoding() {
return null;
}

@Override
public String getXMLVersion() {
return null;
}
};
final XMLLocatorImpl location = new XMLLocatorImpl(
exception.getPublicId(),
exception.getSystemId(),
exception.getLineNumber(),
exception.getColumnNumber()
);
return new XMLParseException(location, exception.getMessage(), exception);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2017-2024 Ronald Brill
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.cyberneko.xerces.util;

import org.htmlunit.cyberneko.xerces.xni.XMLLocator;

/**
* The XMLLocatorImpl class is an implementation of the XMLLocator
* interface.
*
* @author Ronald Brill
*/
public class XMLLocatorImpl implements XMLLocator {

private final String publicId_;
private final String systemId_;
private final int lineNumber_;
private final int columnNumber_;

public XMLLocatorImpl(final String publicId, final String systemId,
final int lineNumber, final int columnNumber) {
publicId_ = publicId;
systemId_ = systemId;
lineNumber_ = lineNumber;
columnNumber_ = columnNumber;
}

@Override
public String getXMLVersion() {
return null;
}

@Override
public String getEncoding() {
return null;
}

@Override
public String getPublicId() {
return publicId_;
}

@Override
public String getSystemId() {
return systemId_;
}

@Override
public int getLineNumber() {
return lineNumber_;
}

@Override
public int getColumnNumber() {
return columnNumber_;
}

@Override
public String getLiteralSystemId() {
return null;
}

@Override
public String getBaseSystemId() {
return null;
}

@Override
public int getCharacterOffset() {
return -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public class XMLParseException extends XNIException {
/** Public identifier. */
private String publicId_;

/** System identifier. */
private String systemId_;

/** literal System identifier. */
private String literalSystemId_;

/** expanded System identifier. */
private String expandedSystemId_;

/** Base system identifier. */
private String baseSystemId_;

Expand All @@ -53,7 +53,7 @@ public XMLParseException(final XMLLocator locator, final String message) {
if (locator != null) {
publicId_ = locator.getPublicId();
literalSystemId_ = locator.getLiteralSystemId();
expandedSystemId_ = locator.getSystemId();
systemId_ = locator.getSystemId();
baseSystemId_ = locator.getBaseSystemId();
lineNumber_ = locator.getLineNumber();
columnNumber_ = locator.getColumnNumber();
Expand All @@ -67,7 +67,7 @@ public XMLParseException(final XMLLocator locator, final String message, final E
if (locator != null) {
publicId_ = locator.getPublicId();
literalSystemId_ = locator.getLiteralSystemId();
expandedSystemId_ = locator.getSystemId();
systemId_ = locator.getSystemId();
baseSystemId_ = locator.getBaseSystemId();
lineNumber_ = locator.getLineNumber();
columnNumber_ = locator.getColumnNumber();
Expand All @@ -81,8 +81,8 @@ public String getPublicId() {
}

/** @return the expanded system identifier. */
public String getExpandedSystemId() {
return expandedSystemId_;
public String getSystemId() {
return systemId_;
}

/** @return the literal system identifier. */
Expand Down Expand Up @@ -121,8 +121,8 @@ public String toString() {
str.append(literalSystemId_);
}
str.append(':');
if (expandedSystemId_ != null) {
str.append(expandedSystemId_);
if (systemId_ != null) {
str.append(systemId_);
}
str.append(':');
if (baseSystemId_ != null) {
Expand Down

0 comments on commit a961e3b

Please sign in to comment.