@@ -936,13 +936,20 @@ public boolean scanDocument(final boolean complete) throws XNIException, IOExcep
936936 return true ;
937937 }
938938
939- /** Sets the document handler. */
939+ /**
940+ * {@inheritDoc}
941+ */
940942 @ Override
941943 public void setDocumentHandler (final XMLDocumentHandler handler ) {
944+ if (handler == null ) {
945+ throw new NullPointerException ("HTMLScanner always requires a non null document handler" );
946+ }
942947 fDocumentHandler = handler ;
943948 }
944949
945- /** Returns the document handler. */
950+ /**
951+ * {@inheritDoc}
952+ */
946953 @ Override
947954 public XMLDocumentHandler getDocumentHandler () {
948955 return fDocumentHandler ;
@@ -1198,13 +1205,11 @@ else if (skip("SYSTEM")) {
11981205 }
11991206 }
12001207
1201- if (fDocumentHandler != null ) {
1202- if (fOverrideDoctype_ ) {
1203- pubid = fDoctypePubid ;
1204- sysid = fDoctypeSysid ;
1205- }
1206- fDocumentHandler .doctypeDecl (root , pubid , sysid , locationAugs (fCurrentEntity ));
1208+ if (fOverrideDoctype_ ) {
1209+ pubid = fDoctypePubid ;
1210+ sysid = fDoctypeSysid ;
12071211 }
1212+ fDocumentHandler .doctypeDecl (root , pubid , sysid , locationAugs (fCurrentEntity ));
12081213 }
12091214
12101215 // Scans a quoted literal.
@@ -1542,7 +1547,7 @@ protected int scanEntityRef(final XMLString str, final XMLString plainValue, fin
15421547 }
15431548
15441549 private int returnEntityRefString (final XMLString str , final boolean content ) {
1545- if (content && fDocumentHandler != null && fElementCount >= fElementDepth ) {
1550+ if (content && fElementCount >= fElementDepth ) {
15461551 fDocumentHandler .characters (str , locationAugs (fCurrentEntity ));
15471552 }
15481553 return -1 ;
@@ -2084,7 +2089,7 @@ else if (c == '&') {
20842089 if (fReportErrors_ ) {
20852090 fErrorReporter .reportError ("HTML1003" , null );
20862091 }
2087- if (fDocumentHandler != null && fElementCount >= fElementDepth ) {
2092+ if (fElementCount >= fElementDepth ) {
20882093 fStringBuffer .clearAndAppend ('<' );
20892094 fDocumentHandler .characters (fStringBuffer , null );
20902095 }
@@ -2182,7 +2187,7 @@ else if (ename != null) {
21822187 break ;
21832188 }
21842189 case STATE_START_DOCUMENT : {
2185- if (fDocumentHandler != null && fElementCount >= fElementDepth ) {
2190+ if (fElementCount >= fElementDepth ) {
21862191 if (DEBUG_CALLBACKS ) {
21872192 System .out .println ("startDocument()" );
21882193 }
@@ -2191,7 +2196,7 @@ else if (ename != null) {
21912196 new NamespaceSupport (),
21922197 locationAugs (fCurrentEntity ));
21932198 }
2194- if (fInsertDoctype_ && fDocumentHandler != null ) {
2199+ if (fInsertDoctype_ ) {
21952200 String root = htmlConfiguration_ .getHtmlElements ().getElement (HTMLElements .HTML ).name ;
21962201 root = modifyName (root , fNamesElems );
21972202 final String pubid = fDoctypePubid ;
@@ -2202,7 +2207,7 @@ else if (ename != null) {
22022207 break ;
22032208 }
22042209 case STATE_END_DOCUMENT : {
2205- if (fDocumentHandler != null && fElementCount >= fElementDepth && complete ) {
2210+ if (fElementCount >= fElementDepth && complete ) {
22062211 if (DEBUG_CALLBACKS ) {
22072212 System .out .println ("endDocument()" );
22082213 }
@@ -2277,7 +2282,7 @@ private void scanUntilEndTag(final String tagName) throws IOException {
22772282 }
22782283 }
22792284 }
2280- if (fScanUntilEndTag .length () > 0 && fDocumentHandler != null ) {
2285+ if (fScanUntilEndTag .length () > 0 ) {
22812286 fDocumentHandler .characters (fScanUntilEndTag , locationAugs (fCurrentEntity ));
22822287 }
22832288 }
@@ -2307,7 +2312,7 @@ protected void scanCharacters() throws IOException {
23072312 break ;
23082313 }
23092314 }
2310- if (fCurrentEntity .offset_ > offset && fDocumentHandler != null && fElementCount >= fElementDepth ) {
2315+ if (fCurrentEntity .offset_ > offset && fElementCount >= fElementDepth ) {
23112316 if (DEBUG_CALLBACKS ) {
23122317 final XMLString xmlString = new XMLString (fCurrentEntity .buffer_ , offset ,
23132318 fCurrentEntity .offset_ - offset );
@@ -2339,7 +2344,7 @@ protected void scanCDATA() throws IOException {
23392344 }
23402345 fStringBuffer .clear ();
23412346 if (fCDATASections_ ) {
2342- if (fDocumentHandler != null && fElementCount >= fElementDepth ) {
2347+ if (fElementCount >= fElementDepth ) {
23432348 if (DEBUG_CALLBACKS ) {
23442349 System .out .println ("startCDATA()" );
23452350 }
@@ -2351,7 +2356,7 @@ protected void scanCDATA() throws IOException {
23512356 }
23522357 final boolean eof = scanCDataContent (fStringBuffer );
23532358
2354- if (fDocumentHandler != null && fElementCount >= fElementDepth ) {
2359+ if (fElementCount >= fElementDepth ) {
23552360 if (fCDATASections_ ) {
23562361 if (DEBUG_CALLBACKS ) {
23572362 System .out .println ("characters(" + fStringBuffer + ")" );
@@ -2422,7 +2427,7 @@ else if (c != '>') {
24222427 break ;
24232428 }
24242429 }
2425- if (fDocumentHandler != null && fElementCount >= fElementDepth ) {
2430+ if (fElementCount >= fElementDepth ) {
24262431 if (DEBUG_CALLBACKS ) {
24272432 System .out .println ("comment(" + fScanComment + ")" );
24282433 }
@@ -2664,10 +2669,8 @@ else if (c == '\r' || c == '\n') {
26642669 }
26652670 else if (c == '>' ) {
26662671 // invalid procession instruction, handle as comment
2667- if (fDocumentHandler != null ) {
2668- fStringBuffer .append (target );
2669- fDocumentHandler .comment (fStringBuffer , locationAugs (fCurrentEntity ));
2670- }
2672+ fStringBuffer .append (target );
2673+ fDocumentHandler .comment (fStringBuffer , locationAugs (fCurrentEntity ));
26712674 return ;
26722675 }
26732676 else {
@@ -2678,9 +2681,7 @@ else if (c == '>') {
26782681 }
26792682 }
26802683 }
2681- if (fDocumentHandler != null ) {
2682- fDocumentHandler .processingInstruction (target , fStringBuffer , locationAugs (fCurrentEntity ));
2683- }
2684+ fDocumentHandler .processingInstruction (target , fStringBuffer , locationAugs (fCurrentEntity ));
26842685 }
26852686
26862687 // scan xml/text declaration
@@ -2704,21 +2705,20 @@ else if (c == '>') {
27042705 aindex ++;
27052706 }
27062707 }
2707- if (fDocumentHandler != null ) {
2708- final String version = attributes_ .getValue ("version" );
2709- final String encoding = attributes_ .getValue ("encoding" );
2710- final String standalone = attributes_ .getValue ("standalone" );
27112708
2712- // if the encoding is successfully changed, the stream will be processed again
2713- // with the right encoding and we will come here again but without need to change
2714- // the encoding
2715- final boolean xmlDeclNow = fIgnoreSpecifiedCharset_ || !changeEncoding (encoding );
2716- if (xmlDeclNow ) {
2717- fBeginLineNumber = beginLineNumber ;
2718- fBeginColumnNumber = beginColumnNumber ;
2719- fBeginCharacterOffset = beginCharacterOffset ;
2720- fDocumentHandler .xmlDecl (version , encoding , standalone , locationAugs (fCurrentEntity ));
2721- }
2709+ final String version = attributes_ .getValue ("version" );
2710+ final String encoding = attributes_ .getValue ("encoding" );
2711+ final String standalone = attributes_ .getValue ("standalone" );
2712+
2713+ // if the encoding is successfully changed, the stream will be processed again
2714+ // with the right encoding and we will come here again but without need to change
2715+ // the encoding
2716+ final boolean xmlDeclNow = fIgnoreSpecifiedCharset_ || !changeEncoding (encoding );
2717+ if (xmlDeclNow ) {
2718+ fBeginLineNumber = beginLineNumber ;
2719+ fBeginColumnNumber = beginColumnNumber ;
2720+ fBeginCharacterOffset = beginCharacterOffset ;
2721+ fDocumentHandler .xmlDecl (version , encoding , standalone , locationAugs (fCurrentEntity ));
27222722 }
27232723 }
27242724
@@ -2742,7 +2742,7 @@ protected String scanStartElement(final boolean[] empty) throws IOException {
27422742 if (fReportErrors_ ) {
27432743 fErrorReporter .reportError ("HTML1009" , null );
27442744 }
2745- if (fDocumentHandler != null && fElementCount >= fElementDepth ) {
2745+ if (fElementCount >= fElementDepth ) {
27462746 fStringBuffer .clearAndAppend ('<' );
27472747 fDocumentHandler .characters (fStringBuffer , null );
27482748 }
@@ -2809,7 +2809,7 @@ else if (fByteStream != null && "BODY".equalsIgnoreCase(ename)) {
28092809 }
28102810 }
28112811
2812- if (fDocumentHandler != null && fElementCount >= fElementDepth ) {
2812+ if (fElementCount >= fElementDepth ) {
28132813 qName_ .setValues (null , ename , ename , null );
28142814 if (DEBUG_CALLBACKS ) {
28152815 System .out .println ("startElement(" + qName_ + ',' + attributes_ + ")" );
@@ -3218,7 +3218,7 @@ protected void scanEndElement() throws IOException {
32183218 skipMarkup (false );
32193219 if (ename != null ) {
32203220 ename = modifyName (ename , fNamesElems );
3221- if (fDocumentHandler != null && fElementCount >= fElementDepth ) {
3221+ if (fElementCount >= fElementDepth ) {
32223222 qName_ .setValues (null , ename , ename , null );
32233223 if (DEBUG_CALLBACKS ) {
32243224 System .out .println ("endElement(" + qName_ + ")" );
@@ -3310,7 +3310,7 @@ public boolean scan(final boolean complete) throws IOException {
33103310 if (ename .equalsIgnoreCase (fElementName )) {
33113311 if (fCurrentEntity .read () == '>' ) {
33123312 ename = modifyName (ename , fNamesElems );
3313- if (fDocumentHandler != null && fElementCount >= fElementDepth ) {
3313+ if (fElementCount >= fElementDepth ) {
33143314 fQName_ .setValues (null , ename , ename , null );
33153315 if (DEBUG_CALLBACKS ) {
33163316 System .out .println ("endElement(" + fQName_ + ")" );
@@ -3401,7 +3401,7 @@ else if (c == '\r' || c == '\n') {
34013401 }
34023402 }
34033403
3404- if (buffer .length () > 0 && fDocumentHandler != null && fElementCount >= fElementDepth ) {
3404+ if (buffer .length () > 0 && fElementCount >= fElementDepth ) {
34053405 if (DEBUG_CALLBACKS ) {
34063406 System .out .println ("characters(" + buffer + ")" );
34073407 }
@@ -3446,7 +3446,7 @@ protected void scanCharacters(final XMLString buffer, final boolean complete) th
34463446 }
34473447 }
34483448
3449- if (buffer .length () > 0 && fDocumentHandler != null && fElementCount >= fElementDepth ) {
3449+ if (buffer .length () > 0 && fElementCount >= fElementDepth ) {
34503450 if (DEBUG_CALLBACKS ) {
34513451 System .out .println ("characters(" + buffer + ")" );
34523452 }
@@ -3621,7 +3621,7 @@ else if (c == '<') {
36213621 fScanScriptContent .trimToContent ("<![CDATA[" , "]]>" );
36223622 }
36233623
3624- if (fScanScriptContent .length () > 0 && fDocumentHandler != null && fElementCount >= fElementDepth ) {
3624+ if (fScanScriptContent .length () > 0 && fElementCount >= fElementDepth ) {
36253625 if (DEBUG_CALLBACKS ) {
36263626 System .out .println ("characters(" + fScanScriptContent + ")" );
36273627 }
0 commit comments