Skip to content

Commit 2f61c94

Browse files
faceless2hsivonen
authored andcommitted
Ensure every Locator is also a Locator2 (#10)
* Replace org.xml.sax.helpers.LocatorImpl with nu.validator.htmlparser.impl.LocatorImpl * instanceof before cast to Locator2
1 parent 2f6f559 commit 2f61c94

File tree

9 files changed

+160
-10
lines changed

9 files changed

+160
-10
lines changed

src/nu/validator/htmlparser/impl/LocatorImpl.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,38 @@
2424
package nu.validator.htmlparser.impl;
2525

2626
import org.xml.sax.Locator;
27+
import org.xml.sax.ext.Locator2;
2728

28-
public class LocatorImpl implements Locator {
29+
public class LocatorImpl implements Locator, Locator2 {
2930

3031
private final String systemId;
3132

3233
private final String publicId;
3334

35+
private final String encoding;
36+
3437
private final int column;
3538

3639
private final int line;
3740

41+
/**
42+
* Create a new Locator with default values
43+
*/
44+
public LocatorImpl() {
45+
this.systemId = this.publicId = this.encoding = null;
46+
this.column = this.line = 0;
47+
}
48+
3849
public LocatorImpl(Locator locator) {
3950
this.systemId = locator.getSystemId();
4051
this.publicId = locator.getPublicId();
4152
this.column = locator.getColumnNumber();
4253
this.line = locator.getLineNumber();
54+
if (locator instanceof Locator2) {
55+
this.encoding = ((Locator2)locator).getEncoding();
56+
} else {
57+
this.encoding = null;
58+
}
4359
}
4460

4561
public final int getColumnNumber() {
@@ -57,4 +73,12 @@ public final String getPublicId() {
5773
public final String getSystemId() {
5874
return systemId;
5975
}
76+
77+
public final String getXMLVersion() {
78+
return "1.0";
79+
}
80+
81+
public final String getEncoding() {
82+
return encoding;
83+
}
6084
}

src/nu/validator/htmlparser/impl/Tokenizer.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import org.xml.sax.ErrorHandler;
3939
import org.xml.sax.Locator;
40+
import org.xml.sax.ext.Locator2;
4041
import org.xml.sax.SAXException;
4142
import org.xml.sax.SAXParseException;
4243

@@ -66,7 +67,7 @@
6667
* @version $Id$
6768
* @author hsivonen
6869
*/
69-
public class Tokenizer implements Locator {
70+
public class Tokenizer implements Locator, Locator2 {
7071

7172
private static final int DATA_AND_RCDATA_MASK = ~1;
7273

@@ -864,6 +865,24 @@ public String getSystemId() {
864865
return systemId;
865866
}
866867

868+
/**
869+
* @see org.xml.sax.ext.Locator2#getXMLVersion()
870+
*/
871+
public String getXMLVersion() {
872+
return "1.0";
873+
}
874+
875+
/**
876+
* @see org.xml.sax.ext.Locator2#getXMLVersion()
877+
*/
878+
public String getEncoding() {
879+
try {
880+
return encodingDeclarationHandler == null ? null : encodingDeclarationHandler.getCharacterEncoding();
881+
} catch (SAXException e) {
882+
return null;
883+
}
884+
}
885+
867886
// end Locator impl
868887

869888
// end public API

src/nu/validator/htmlparser/io/Driver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ public void setXmlnsPolicy(XmlViolationPolicy xmlnsPolicy) {
579579
}
580580

581581
public String getCharacterEncoding() throws SAXException {
582-
return characterEncoding.getCanonName();
582+
return characterEncoding == null ? null : characterEncoding.getCanonName();
583583
}
584584

585585
public Locator getDocumentLocator() {

src/nu/validator/htmlparser/io/HtmlInputStreamReader.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
import org.xml.sax.ErrorHandler;
4444
import org.xml.sax.Locator;
45+
import org.xml.sax.ext.Locator2;
4546
import org.xml.sax.SAXException;
4647
import org.xml.sax.SAXParseException;
4748

@@ -58,7 +59,7 @@
5859
* @author hsivonen
5960
*/
6061
public final class HtmlInputStreamReader extends Reader implements
61-
ByteReadable, Locator {
62+
ByteReadable, Locator, Locator2 {
6263

6364
private static final int SNIFFING_LIMIT = 1024;
6465

@@ -452,6 +453,20 @@ public String getSystemId() {
452453
return null;
453454
}
454455

456+
public String getXMLVersion() {
457+
if (tokenizer != null) {
458+
return tokenizer.getXMLVersion();
459+
}
460+
return null;
461+
}
462+
463+
public String getEncoding() {
464+
if (tokenizer != null) {
465+
return tokenizer.getEncoding();
466+
}
467+
return null;
468+
}
469+
455470
/**
456471
* @param string
457472
* @throws SAXException

src/nu/validator/htmlparser/io/MetaSniffer.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030

3131
import org.xml.sax.ErrorHandler;
3232
import org.xml.sax.Locator;
33+
import org.xml.sax.ext.Locator2;
3334
import org.xml.sax.SAXException;
3435
import org.xml.sax.SAXParseException;
3536

36-
public class MetaSniffer extends MetaScanner implements Locator {
37+
public class MetaSniffer extends MetaScanner implements Locator, Locator2 {
3738

3839
private Encoding characterEncoding = null;
3940

@@ -142,6 +143,20 @@ public String getSystemId() {
142143
}
143144
return null;
144145
}
146+
147+
public String getXMLVersion() {
148+
if (locator != null) {
149+
return ((Locator2)locator).getXMLVersion();
150+
}
151+
return null;
152+
}
153+
154+
public String getEncoding() {
155+
if (locator != null) {
156+
return ((Locator2)locator).getEncoding();
157+
}
158+
return null;
159+
}
145160

146161
protected boolean tryCharset(String encoding) throws SAXException {
147162
encoding = Encoding.toAsciiLowerCase(encoding);

src/nu/validator/saxtree/DocumentFragment.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
package nu.validator.saxtree;
2525

26-
import org.xml.sax.helpers.LocatorImpl;
26+
import nu.validator.htmlparser.impl.LocatorImpl;
2727

2828
/**
2929
* A document fragment.

src/nu/validator/saxtree/LocatorImpl.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@
2424
package nu.validator.saxtree;
2525

2626
import org.xml.sax.Locator;
27+
import org.xml.sax.ext.Locator2;
2728

2829
/**
2930
* A locator implementation.
3031
* @version $Id$
3132
* @author hsivonen
3233
*/
33-
public final class LocatorImpl implements Locator {
34+
public final class LocatorImpl implements Locator, Locator2 {
3435

3536
/**
3637
* The system id.
@@ -42,6 +43,11 @@ public final class LocatorImpl implements Locator {
4243
*/
4344
private final String publicId;
4445

46+
/**
47+
* The encoding.
48+
*/
49+
private final String encoding;
50+
4551
/**
4652
* The column.
4753
*/
@@ -62,11 +68,17 @@ public LocatorImpl(Locator locator) {
6268
this.publicId = null;
6369
this.column = -1;
6470
this.line = -1;
71+
this.encoding = null;
6572
} else {
6673
this.systemId = locator.getSystemId();
6774
this.publicId = locator.getPublicId();
6875
this.column = locator.getColumnNumber();
6976
this.line = locator.getLineNumber();
77+
if (locator instanceof Locator2) {
78+
this.encoding = ((Locator2)locator).getEncoding();
79+
} else {
80+
this.encoding = null;
81+
}
7082
}
7183
}
7284

@@ -101,4 +113,21 @@ public String getPublicId() {
101113
public String getSystemId() {
102114
return systemId;
103115
}
116+
117+
/**
118+
*
119+
* @see org.xml.sax.ext.Locator2#getXMLVersion()
120+
*/
121+
public String getXMLVersion() {
122+
return "1.0";
123+
}
124+
125+
/**
126+
*
127+
* @see org.xml.sax.ext.Locator2#getEncoding()
128+
*/
129+
public String getEncoding() {
130+
return encoding;
131+
}
132+
104133
}

src/nu/validator/saxtree/Node.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@
2727

2828
import org.xml.sax.Attributes;
2929
import org.xml.sax.Locator;
30+
import org.xml.sax.ext.Locator2;
3031
import org.xml.sax.SAXException;
3132

3233
/**
3334
* The common node superclass.
3435
* @version $Id$
3536
* @author hsivonen
3637
*/
37-
public abstract class Node implements Locator {
38+
public abstract class Node implements Locator, Locator2 {
3839

3940
/**
4041
* The system id.
@@ -45,6 +46,11 @@ public abstract class Node implements Locator {
4546
* The public id.
4647
*/
4748
private final String publicId;
49+
50+
/**
51+
* The encoding.
52+
*/
53+
private final String encoding;
4854

4955
/**
5056
* The column.
@@ -75,13 +81,19 @@ public abstract class Node implements Locator {
7581
if (locator == null) {
7682
this.systemId = null;
7783
this.publicId = null;
84+
this.encoding = null;
7885
this.column = -1;
7986
this.line = -1;
8087
} else {
8188
this.systemId = locator.getSystemId();
8289
this.publicId = locator.getPublicId();
8390
this.column = locator.getColumnNumber();
8491
this.line = locator.getLineNumber();
92+
if (locator instanceof Locator2) {
93+
this.encoding = ((Locator2)locator).getEncoding();
94+
} else {
95+
this.encoding = null;
96+
}
8597
}
8698
}
8799

@@ -117,6 +129,20 @@ public String getSystemId() {
117129
return systemId;
118130
}
119131

132+
/**
133+
* @see org.xml.sax.ext.Locator2#getXMLVersion()
134+
*/
135+
public String getXMLVersion() {
136+
return "1.0";
137+
}
138+
139+
/**
140+
* @see org.xml.sax.ext.Locator2#getEncoding
141+
*/
142+
public String getEncoding() {
143+
return encoding;
144+
}
145+
120146
/**
121147
* Visit the node.
122148
*

src/nu/validator/saxtree/TreeParser.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.xml.sax.Attributes;
2727
import org.xml.sax.ContentHandler;
2828
import org.xml.sax.Locator;
29+
import org.xml.sax.ext.Locator2;
2930
import org.xml.sax.SAXException;
3031
import org.xml.sax.ext.LexicalHandler;
3132

@@ -34,7 +35,7 @@
3435
* @version $Id$
3536
* @author hsivonen
3637
*/
37-
public final class TreeParser implements Locator {
38+
public final class TreeParser implements Locator, Locator2 {
3839

3940
/**
4041
* The content handler.
@@ -283,7 +284,6 @@ public String getPublicId() {
283284
if (locatorDelegate == null) {
284285
return null;
285286
} else {
286-
287287
return locatorDelegate.getPublicId();
288288
}
289289
}
@@ -298,4 +298,26 @@ public String getSystemId() {
298298
return locatorDelegate.getSystemId();
299299
}
300300
}
301+
302+
/**
303+
* @see org.xml.sax.Locator#getSystemId()
304+
*/
305+
public String getXMLVersion() {
306+
if (!(locatorDelegate instanceof Locator2)) {
307+
return null;
308+
} else {
309+
return ((Locator2)locatorDelegate).getXMLVersion();
310+
}
311+
}
312+
313+
/**
314+
* @see org.xml.sax.Locator#getSystemId()
315+
*/
316+
public String getEncoding() {
317+
if (!(locatorDelegate instanceof Locator2)) {
318+
return null;
319+
} else {
320+
return ((Locator2)locatorDelegate).getEncoding();
321+
}
322+
}
301323
}

0 commit comments

Comments
 (0)