@@ -983,6 +983,7 @@ final class Handler extends DefaultHandler2 {
983983 int eventOrdinal ;
984984 Locator locator ;
985985 boolean keepCurrentPositionForNextCall ;
986+ boolean notStandaloneDtd ;
986987 final Map <String , ExternalEntityInfo > externalEntities = new HashMap <>();
987988 final Set <String > resolvedExternalEntities = new HashSet <>();
988989
@@ -1021,16 +1022,18 @@ public void startDocument() {
10211022
10221023 @ Override
10231024 public void startDTD (String name , String publicId , String systemId ) {
1024- if (xmlDeclInfo != null && xmlDeclInfo .standalone == 0 ) {
1025- call (parser .getNotStandaloneHandler ());
1025+ notStandaloneDtd = (xmlDeclInfo != null && xmlDeclInfo .standalone == 0 ) ||
1026+ ((systemId != null || publicId != null ) && (xmlDeclInfo == null || xmlDeclInfo .standalone != 1 ));
1027+ if (notStandaloneDtd ) {
1028+ callInt (parser .getNotStandaloneHandler ());
10261029 }
10271030 call (parser .getStartDoctypeDeclHandler (), toTs (name ), toTs (systemId ), toOptionalTs (publicId ), doctypeHasInternalSubset ? 1 : 0 );
10281031 }
10291032
10301033 @ Override
10311034 public void endDTD () {
1032- if (xmlDeclInfo != null && xmlDeclInfo . standalone == 0 ) {
1033- call (parser .getNotStandaloneHandler ());
1035+ if (notStandaloneDtd ) {
1036+ callInt (parser .getNotStandaloneHandler ());
10341037 }
10351038 call (parser .getEndDoctypeDeclHandler ());
10361039 }
@@ -1200,10 +1203,10 @@ private String attributeName(Attributes attrs, int i) {
12001203 return qName == null || qName .isEmpty () ? localName : qName ;
12011204 }
12021205
1203- private void call (Object handler , Object ... args ) {
1206+ private Object call (Object handler , Object ... args ) {
12041207 boolean shouldDeliver = eventOrdinal ++ >= parser .getDeliveredEventCount ();
12051208 if (!shouldDeliver ) {
1206- return ;
1209+ return PNone . NO_VALUE ;
12071210 }
12081211 if (!keepCurrentPositionForNextCall && locator != null ) {
12091212 line = Math .max (1 , locator .getLineNumber ());
@@ -1215,7 +1218,15 @@ private void call(Object handler, Object... args) {
12151218 }
12161219 keepCurrentPositionForNextCall = false ;
12171220 if (handler != PNone .NONE ) {
1218- CallNode .executeUncached (handler , args );
1221+ return CallNode .executeUncached (handler , args );
1222+ }
1223+ return PNone .NO_VALUE ;
1224+ }
1225+
1226+ private void callInt (Object handler , Object ... args ) {
1227+ Object result = call (handler , args );
1228+ if (result != PNone .NO_VALUE ) {
1229+ PyLongAsLongNode .executeUncached (result );
12191230 }
12201231 }
12211232
@@ -1510,20 +1521,27 @@ private static boolean detectDoctypeHasInternalSubset(byte[] data) {
15101521
15111522 @ TruffleBoundary
15121523 private static XmlDeclInfo detectXmlDecl (byte [] data ) {
1513- if (data .length == 0 || data [ 0 ] != '<' ) {
1524+ if (data .length == 0 ) {
15141525 return null ;
15151526 }
1516- int end = -1 ;
1517- for (int i = 1 ; i < data .length ; i ++) {
1518- if (data [i - 1 ] == '?' && data [i ] == '>' ) {
1519- end = i + 1 ;
1520- break ;
1521- }
1527+ String text ;
1528+ if ((data .length >= 2 && data [0 ] == (byte ) 0xfe && data [1 ] == (byte ) 0xff ) ||
1529+ (data .length >= 4 && data [0 ] == 0 && data [1 ] == '<' && data [2 ] == 0 && data [3 ] == '?' )) {
1530+ text = new String (data , java .nio .charset .StandardCharsets .UTF_16BE );
1531+ } else if ((data .length >= 2 && data [0 ] == (byte ) 0xff && data [1 ] == (byte ) 0xfe ) ||
1532+ (data .length >= 4 && data [0 ] == '<' && data [1 ] == 0 && data [2 ] == '?' && data [3 ] == 0 )) {
1533+ text = new String (data , java .nio .charset .StandardCharsets .UTF_16LE );
1534+ } else {
1535+ text = new String (data , java .nio .charset .StandardCharsets .ISO_8859_1 );
1536+ }
1537+ if (!text .isEmpty () && text .charAt (0 ) == '\ufeff' ) {
1538+ text = text .substring (1 );
15221539 }
1523- if (end == -1 ) {
1540+ int end = text .indexOf ("?>" );
1541+ if (end < 0 ) {
15241542 return null ;
15251543 }
1526- String decl = new String ( data , 0 , end , java . nio . charset . StandardCharsets . ISO_8859_1 );
1544+ String decl = text . substring ( 0 , end + 2 );
15271545 if (!decl .startsWith ("<?xml" )) {
15281546 return null ;
15291547 }
@@ -1583,6 +1601,9 @@ private static String formatErrorMessage(SAXParseException e) {
15831601 return "undefined entity &" + entity + ";: line " + e .getLineNumber () + ", column " + Math .max (0 , e .getColumnNumber () - 1 );
15841602 }
15851603 }
1604+ if (message .contains ("Content is not allowed in trailing section" )) {
1605+ return "junk after document element: line " + e .getLineNumber () + ", column " + Math .max (0 , e .getColumnNumber () - 1 );
1606+ }
15861607 return message ;
15871608 }
15881609
0 commit comments