Skip to content

Commit

Permalink
Fix Locator not being fully initialized when ContentHandler.setLocato…
Browse files Browse the repository at this point in the history
…r is being called (#214)
  • Loading branch information
philippn authored Nov 12, 2024
1 parent 704a455 commit 78062bd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/main/java/com/ctc/wstx/sax/WstxSAXParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,6 @@ public void parse(InputSource input) throws SAXException
}
}

if (mContentHandler != null) {
mContentHandler.setDocumentLocator(this);
mContentHandler.startDocument();
}

// Note: since we are reusing the same config instance, need to
// make sure state is not carried forward. Thus:
cfg.resetState();
Expand Down Expand Up @@ -619,6 +614,12 @@ public void parse(InputSource input) throws SAXException
mStandalone = mScanner.standaloneSet();
mAttrCollector = mScanner.getAttributeCollector();
mElemStack = mScanner.getInputElementStack();

if (mContentHandler != null) {
mContentHandler.setDocumentLocator(this);
mContentHandler.startDocument();
}

fireEvents();
} catch (Exception e) {
throwSaxException(e);
Expand Down
17 changes: 15 additions & 2 deletions src/test/java/wstxtest/sax/TestBasicSax.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class TestBasicSax
+"<!DOCTYPE root []>"
+"<root><!-- comment -->"
+"<leaf attr='a&amp;b!'>rock&apos;n <![CDATA[roll]]></leaf><?proc instr?></root> ";
final static String DOCUMENT_SYSTEMID = "document.xml";

public void testSimpleNs()
throws Exception
Expand Down Expand Up @@ -89,8 +90,10 @@ public void doTestSimple(boolean ns, boolean useReader)
} else {
src = new InputSource(new ByteArrayInputStream(XML.getBytes("UTF-8")));
}
src.setSystemId(DOCUMENT_SYSTEMID);

sp.parse(src, h);
assertEquals(DOCUMENT_SYSTEMID, h._systemId);
assertEquals(2, h._elems);
assertEquals(1, h._attrs);
assertEquals(11, h._charCount);
Expand All @@ -111,9 +114,19 @@ public String getText() {
final static class MyHandler
extends DefaultHandler
{
public int _elems, _attrs;
public int _elems, _attrs, _charCount;
public String _systemId;
private Locator locator;

public int _charCount;
@Override
public void setDocumentLocator(Locator locator) {
this.locator = locator;
}

@Override
public void startDocument() throws SAXException {
_systemId = locator.getSystemId();
}

@Override
public void characters(char[] ch, int start, int length) {
Expand Down

0 comments on commit 78062bd

Please sign in to comment.