Skip to content

Ensure every Locator is also a Locator2 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/nu/validator/htmlparser/impl/LocatorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,38 @@
package nu.validator.htmlparser.impl;

import org.xml.sax.Locator;
import org.xml.sax.ext.Locator2;

public class LocatorImpl implements Locator {
public class LocatorImpl implements Locator, Locator2 {

private final String systemId;

private final String publicId;

private final String encoding;

private final int column;

private final int line;

/**
* Create a new Locator with default values
*/
public LocatorImpl() {
this.systemId = this.publicId = this.encoding = null;
this.column = this.line = 0;
}

public LocatorImpl(Locator locator) {
this.systemId = locator.getSystemId();
this.publicId = locator.getPublicId();
this.column = locator.getColumnNumber();
this.line = locator.getLineNumber();
if (locator instanceof Locator2) {
this.encoding = ((Locator2)locator).getEncoding();
} else {
this.encoding = null;
}
}

public final int getColumnNumber() {
Expand All @@ -57,4 +73,12 @@ public final String getPublicId() {
public final String getSystemId() {
return systemId;
}

public final String getXMLVersion() {
return "1.0";
}

public final String getEncoding() {
return encoding;
}
}
21 changes: 20 additions & 1 deletion src/nu/validator/htmlparser/impl/Tokenizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.ext.Locator2;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

Expand Down Expand Up @@ -66,7 +67,7 @@
* @version $Id$
* @author hsivonen
*/
public class Tokenizer implements Locator {
public class Tokenizer implements Locator, Locator2 {

private static final int DATA_AND_RCDATA_MASK = ~1;

Expand Down Expand Up @@ -874,6 +875,24 @@ public String getSystemId() {
return systemId;
}

/**
* @see org.xml.sax.ext.Locator2#getXMLVersion()
*/
public String getXMLVersion() {
return "1.0";
}

/**
* @see org.xml.sax.ext.Locator2#getXMLVersion()
*/
public String getEncoding() {
try {
return encodingDeclarationHandler == null ? null : encodingDeclarationHandler.getCharacterEncoding();
} catch (SAXException e) {
return null;
}
}

// end Locator impl

// end public API
Expand Down
2 changes: 1 addition & 1 deletion src/nu/validator/htmlparser/io/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ public void setXmlnsPolicy(XmlViolationPolicy xmlnsPolicy) {
}

public String getCharacterEncoding() throws SAXException {
return characterEncoding.getCanonName();
return characterEncoding == null ? null : characterEncoding.getCanonName();
}

public Locator getDocumentLocator() {
Expand Down
17 changes: 16 additions & 1 deletion src/nu/validator/htmlparser/io/HtmlInputStreamReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.ext.Locator2;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

Expand All @@ -59,7 +60,7 @@
* @author hsivonen
*/
public final class HtmlInputStreamReader extends Reader implements
ByteReadable, Locator {
ByteReadable, Locator, Locator2 {

private static final int SNIFFING_LIMIT = 1024;

Expand Down Expand Up @@ -453,6 +454,20 @@ public String getSystemId() {
return null;
}

public String getXMLVersion() {
if (tokenizer != null) {
return tokenizer.getXMLVersion();
}
return null;
}

public String getEncoding() {
if (tokenizer != null) {
return tokenizer.getEncoding();
}
return null;
}

/**
* @param string
* @throws SAXException
Expand Down
17 changes: 16 additions & 1 deletion src/nu/validator/htmlparser/io/MetaSniffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@

import org.xml.sax.ErrorHandler;
import org.xml.sax.Locator;
import org.xml.sax.ext.Locator2;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class MetaSniffer extends MetaScanner implements Locator {
public class MetaSniffer extends MetaScanner implements Locator, Locator2 {

private Encoding characterEncoding = null;

Expand Down Expand Up @@ -142,6 +143,20 @@ public String getSystemId() {
}
return null;
}

public String getXMLVersion() {
if (locator != null) {
return ((Locator2)locator).getXMLVersion();
}
return null;
}

public String getEncoding() {
if (locator != null) {
return ((Locator2)locator).getEncoding();
}
return null;
}

protected boolean tryCharset(String encoding) throws SAXException {
encoding = Encoding.toAsciiLowerCase(encoding);
Expand Down
2 changes: 1 addition & 1 deletion src/nu/validator/saxtree/DocumentFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

package nu.validator.saxtree;

import org.xml.sax.helpers.LocatorImpl;
import nu.validator.htmlparser.impl.LocatorImpl;

/**
* A document fragment.
Expand Down
31 changes: 30 additions & 1 deletion src/nu/validator/saxtree/LocatorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
package nu.validator.saxtree;

import org.xml.sax.Locator;
import org.xml.sax.ext.Locator2;

/**
* A locator implementation.
* @version $Id$
* @author hsivonen
*/
public final class LocatorImpl implements Locator {
public final class LocatorImpl implements Locator, Locator2 {

/**
* The system id.
Expand All @@ -42,6 +43,11 @@ public final class LocatorImpl implements Locator {
*/
private final String publicId;

/**
* The encoding.
*/
private final String encoding;

/**
* The column.
*/
Expand All @@ -62,11 +68,17 @@ public LocatorImpl(Locator locator) {
this.publicId = null;
this.column = -1;
this.line = -1;
this.encoding = null;
} else {
this.systemId = locator.getSystemId();
this.publicId = locator.getPublicId();
this.column = locator.getColumnNumber();
this.line = locator.getLineNumber();
if (locator instanceof Locator2) {
this.encoding = ((Locator2)locator).getEncoding();
} else {
this.encoding = null;
}
}
}

Expand Down Expand Up @@ -101,4 +113,21 @@ public String getPublicId() {
public String getSystemId() {
return systemId;
}

/**
*
* @see org.xml.sax.ext.Locator2#getXMLVersion()
*/
public String getXMLVersion() {
return "1.0";
}

/**
*
* @see org.xml.sax.ext.Locator2#getEncoding()
*/
public String getEncoding() {
return encoding;
}

}
28 changes: 27 additions & 1 deletion src/nu/validator/saxtree/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.ext.Locator2;
import org.xml.sax.SAXException;

/**
* The common node superclass.
* @version $Id$
* @author hsivonen
*/
public abstract class Node implements Locator {
public abstract class Node implements Locator, Locator2 {

/**
* The system id.
Expand All @@ -45,6 +46,11 @@ public abstract class Node implements Locator {
* The public id.
*/
private final String publicId;

/**
* The encoding.
*/
private final String encoding;

/**
* The column.
Expand Down Expand Up @@ -75,13 +81,19 @@ public abstract class Node implements Locator {
if (locator == null) {
this.systemId = null;
this.publicId = null;
this.encoding = null;
this.column = -1;
this.line = -1;
} else {
this.systemId = locator.getSystemId();
this.publicId = locator.getPublicId();
this.column = locator.getColumnNumber();
this.line = locator.getLineNumber();
if (locator instanceof Locator2) {
this.encoding = ((Locator2)locator).getEncoding();
} else {
this.encoding = null;
}
}
}

Expand Down Expand Up @@ -117,6 +129,20 @@ public String getSystemId() {
return systemId;
}

/**
* @see org.xml.sax.ext.Locator2#getXMLVersion()
*/
public String getXMLVersion() {
return "1.0";
}

/**
* @see org.xml.sax.ext.Locator2#getEncoding
*/
public String getEncoding() {
return encoding;
}

/**
* Visit the node.
*
Expand Down
26 changes: 24 additions & 2 deletions src/nu/validator/saxtree/TreeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.ext.Locator2;
import org.xml.sax.SAXException;
import org.xml.sax.ext.LexicalHandler;

Expand All @@ -34,7 +35,7 @@
* @version $Id$
* @author hsivonen
*/
public final class TreeParser implements Locator {
public final class TreeParser implements Locator, Locator2 {

/**
* The content handler.
Expand Down Expand Up @@ -283,7 +284,6 @@ public String getPublicId() {
if (locatorDelegate == null) {
return null;
} else {

return locatorDelegate.getPublicId();
}
}
Expand All @@ -298,4 +298,26 @@ public String getSystemId() {
return locatorDelegate.getSystemId();
}
}

/**
* @see org.xml.sax.Locator#getSystemId()
*/
public String getXMLVersion() {
if (!(locatorDelegate instanceof Locator2)) {
return null;
} else {
return ((Locator2)locatorDelegate).getXMLVersion();
}
}

/**
* @see org.xml.sax.Locator#getSystemId()
*/
public String getEncoding() {
if (!(locatorDelegate instanceof Locator2)) {
return null;
} else {
return ((Locator2)locatorDelegate).getEncoding();
}
}
}