diff --git a/release-notes/VERSION b/release-notes/VERSION index e6eeec5e..bdf30160 100644 --- a/release-notes/VERSION +++ b/release-notes/VERSION @@ -4,6 +4,12 @@ Project: woodstox === Releases === ------------------------------------------------------------------------ +6.7.1 (not yet released) + +#213: SAX: `Locator#getSystemId` and `Locator#getPublicId` are not + available during `startDocument` event + (fix contributed by Philipp N) + 6.7.0 (21-Jun-2024) #204: Non-conformant `XMLEventFactory.setLocation(null)` diff --git a/src/main/java/com/ctc/wstx/sax/WstxSAXParser.java b/src/main/java/com/ctc/wstx/sax/WstxSAXParser.java index 858a582c..f2888a4a 100644 --- a/src/main/java/com/ctc/wstx/sax/WstxSAXParser.java +++ b/src/main/java/com/ctc/wstx/sax/WstxSAXParser.java @@ -579,14 +579,8 @@ 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: - */ + // Note: since we are reusing the same config instance, need to + // make sure state is not carried forward. Thus: cfg.resetState(); try { @@ -620,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 (IOException io) { throwSaxException(io); diff --git a/src/test/java/wstxtest/sax/TestBasicSax.java b/src/test/java/wstxtest/sax/TestBasicSax.java index e8cc389a..59484cd8 100644 --- a/src/test/java/wstxtest/sax/TestBasicSax.java +++ b/src/test/java/wstxtest/sax/TestBasicSax.java @@ -23,6 +23,7 @@ public class TestBasicSax +"" +"" +"rock'n "; + final static String DOCUMENT_SYSTEMID = "document.xml"; public void testSimpleNs() throws Exception @@ -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); @@ -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) {