jdk/src/share/classes/javax/swing/text/html/parser/Parser.java
changeset 24567 a0ebe5fd56ff
parent 24528 21c5bb3d76cc
parent 24494 67129b9360d2
child 25147 fd9451d440ff
equal deleted inserted replaced
24566:a43e10e7f3a6 24567:a0ebe5fd56ff
   214      * Returns the start position of the current block. Block is
   214      * Returns the start position of the current block. Block is
   215      * overloaded here, it really means the current start position for
   215      * overloaded here, it really means the current start position for
   216      * the current comment tag, text, block.... This is provided for
   216      * the current comment tag, text, block.... This is provided for
   217      * subclassers that wish to know the start of the current block when
   217      * subclassers that wish to know the start of the current block when
   218      * called with one of the handleXXX methods.
   218      * called with one of the handleXXX methods.
       
   219      *
       
   220      * @return the start position of the current block
   219      */
   221      */
   220     int getBlockStartPosition() {
   222     int getBlockStartPosition() {
   221         return Math.max(0, lastBlockStartPos - 1);
   223         return Math.max(0, lastBlockStartPos - 1);
   222     }
   224     }
   223 
   225 
   224     /**
   226     /**
   225      * Makes a TagElement.
   227      * Makes a TagElement.
       
   228      *
       
   229      * @param elem       the element storing the tag definition
       
   230      * @param fictional  the value of the flag "{@code fictional}" to be set for the tag
       
   231      *
       
   232      * @return the created {@code TagElement}
   226      */
   233      */
   227     protected TagElement makeTag(Element elem, boolean fictional) {
   234     protected TagElement makeTag(Element elem, boolean fictional) {
   228         return new TagElement(elem, fictional);
   235         return new TagElement(elem, fictional);
   229     }
   236     }
   230 
   237 
       
   238     /**
       
   239      * Makes a TagElement.
       
   240      *
       
   241      * @param elem  the element storing the tag definition
       
   242      *
       
   243      * @return the created {@code TagElement}
       
   244      */
   231     protected TagElement makeTag(Element elem) {
   245     protected TagElement makeTag(Element elem) {
   232         return makeTag(elem, false);
   246         return makeTag(elem, false);
   233     }
   247     }
   234 
   248 
       
   249     /**
       
   250      * Returns attributes for the current tag.
       
   251      *
       
   252      * @return {@code SimpleAttributeSet} containing the attributes
       
   253      */
   235     protected SimpleAttributeSet getAttributes() {
   254     protected SimpleAttributeSet getAttributes() {
   236         return attributes;
   255         return attributes;
   237     }
   256     }
   238 
   257 
       
   258     /**
       
   259      * Removes the current attributes.
       
   260      */
   239     protected void flushAttributes() {
   261     protected void flushAttributes() {
   240         attributes.removeAttributes(attributes);
   262         attributes.removeAttributes(attributes);
   241     }
   263     }
   242 
   264 
   243     /**
   265     /**
   244      * Called when PCDATA is encountered.
   266      * Called when PCDATA is encountered.
       
   267      *
       
   268      * @param text  the section text
   245      */
   269      */
   246     protected void handleText(char text[]) {
   270     protected void handleText(char text[]) {
   247     }
   271     }
   248 
   272 
   249     /**
   273     /**
   250      * Called when an HTML title tag is encountered.
   274      * Called when an HTML title tag is encountered.
       
   275      *
       
   276      * @param text  the title text
   251      */
   277      */
   252     protected void handleTitle(char text[]) {
   278     protected void handleTitle(char text[]) {
   253         // default behavior is to call handleText. Subclasses
   279         // default behavior is to call handleText. Subclasses
   254         // can override if necessary.
   280         // can override if necessary.
   255         handleText(text);
   281         handleText(text);
   256     }
   282     }
   257 
   283 
   258     /**
   284     /**
   259      * Called when an HTML comment is encountered.
   285      * Called when an HTML comment is encountered.
       
   286      *
       
   287      * @param text  the comment being handled
   260      */
   288      */
   261     protected void handleComment(char text[]) {
   289     protected void handleComment(char text[]) {
   262     }
   290     }
   263 
   291 
       
   292     /**
       
   293      * Called when the content terminates without closing the HTML comment.
       
   294      */
   264     protected void handleEOFInComment() {
   295     protected void handleEOFInComment() {
   265         // We've reached EOF.  Our recovery strategy is to
   296         // We've reached EOF.  Our recovery strategy is to
   266         // see if we have more than one line in the comment;
   297         // see if we have more than one line in the comment;
   267         // if so, we pretend that the comment was an unterminated
   298         // if so, we pretend that the comment was an unterminated
   268         // single line comment, and reparse the lines after the
   299         // single line comment, and reparse the lines after the
   286         }
   317         }
   287     }
   318     }
   288 
   319 
   289     /**
   320     /**
   290      * Called when an empty tag is encountered.
   321      * Called when an empty tag is encountered.
       
   322      *
       
   323      * @param tag  the tag being handled
       
   324      * @throws ChangedCharSetException if the document charset was changed
   291      */
   325      */
   292     protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException {
   326     protected void handleEmptyTag(TagElement tag) throws ChangedCharSetException {
   293     }
   327     }
   294 
   328 
   295     /**
   329     /**
   296      * Called when a start tag is encountered.
   330      * Called when a start tag is encountered.
       
   331      *
       
   332      * @param tag  the tag being handled
   297      */
   333      */
   298     protected void handleStartTag(TagElement tag) {
   334     protected void handleStartTag(TagElement tag) {
   299     }
   335     }
   300 
   336 
   301     /**
   337     /**
   302      * Called when an end tag is encountered.
   338      * Called when an end tag is encountered.
       
   339      *
       
   340      * @param tag  the tag being handled
   303      */
   341      */
   304     protected void handleEndTag(TagElement tag) {
   342     protected void handleEndTag(TagElement tag) {
   305     }
   343     }
   306 
   344 
   307     /**
   345     /**
   308      * An error has occurred.
   346      * An error has occurred.
       
   347      *
       
   348      * @param ln   the number of line containing the error
       
   349      * @param msg  the error message
   309      */
   350      */
   310     protected void handleError(int ln, String msg) {
   351     protected void handleError(int ln, String msg) {
   311         /*
   352         /*
   312         Thread.dumpStack();
   353         Thread.dumpStack();
   313         System.out.println("**** " + stack);
   354         System.out.println("**** " + stack);
   366         last = tag;
   407         last = tag;
   367         space = false;
   408         space = false;
   368     }
   409     }
   369 
   410 
   370     /**
   411     /**
   371      * Invoke the error handler.
   412      * Invokes the error handler.
       
   413      *
       
   414      * @param err   the error type
       
   415      * @param arg1  the 1st error message argument
       
   416      * @param arg2  the 2nd error message argument
       
   417      * @param arg3  the 3rd error message argument
   372      */
   418      */
   373     protected void error(String err, String arg1, String arg2,
   419     protected void error(String err, String arg1, String arg2,
   374         String arg3) {
   420         String arg3) {
   375         handleError(ln, err + " " + arg1 + " " + arg2 + " " + arg3);
   421         handleError(ln, err + " " + arg1 + " " + arg2 + " " + arg3);
   376     }
   422     }
   388 
   434 
   389     /**
   435     /**
   390      * Handle a start tag. The new tag is pushed
   436      * Handle a start tag. The new tag is pushed
   391      * onto the tag stack. The attribute list is
   437      * onto the tag stack. The attribute list is
   392      * checked for required attributes.
   438      * checked for required attributes.
       
   439      *
       
   440      * @param tag  the tag
       
   441      * @throws ChangedCharSetException if the document charset was changed
   393      */
   442      */
   394     protected void startTag(TagElement tag) throws ChangedCharSetException {
   443     protected void startTag(TagElement tag) throws ChangedCharSetException {
   395         Element elem = tag.getElement();
   444         Element elem = tag.getElement();
   396 
   445 
   397         // If the tag is an empty tag and texpos != 0
   446         // If the tag is an empty tag and texpos != 0
   439     }
   488     }
   440 
   489 
   441     /**
   490     /**
   442      * Handle an end tag. The end tag is popped
   491      * Handle an end tag. The end tag is popped
   443      * from the tag stack.
   492      * from the tag stack.
       
   493      *
       
   494      * @param omitted  {@code true} if the tag is no actually present in the
       
   495      *                 document, but is supposed by the parser
   444      */
   496      */
   445     protected void endTag(boolean omitted) {
   497     protected void endTag(boolean omitted) {
   446         handleText(stack.tag);
   498         handleText(stack.tag);
   447 
   499 
   448         if (omitted && !stack.elem.omitEnd()) {
   500         if (omitted && !stack.elem.omitEnd()) {
   496     }
   548     }
   497 
   549 
   498 
   550 
   499     /**
   551     /**
   500      * Marks the first time a tag has been seen in a document
   552      * Marks the first time a tag has been seen in a document
       
   553      *
       
   554      * @param elem  the element represented by the tag
   501      */
   555      */
   502 
   556 
   503     protected void markFirstTime(Element elem) {
   557     protected void markFirstTime(Element elem) {
   504         String elemName = elem.getName();
   558         String elemName = elem.getName();
   505         if (elemName.equals("html")) {
   559         if (elemName.equals("html")) {
  1480             }
  1534             }
  1481         }
  1535         }
  1482     }
  1536     }
  1483 
  1537 
  1484     /**
  1538     /**
  1485      * Parses th Document Declaration Type markup declaration.
  1539      * Parses the Document Type Declaration markup declaration.
  1486      * Currently ignores it.
  1540      * Currently ignores it.
       
  1541      *
       
  1542      * @return the string representation of the markup declaration
       
  1543      * @throws IOException if an I/O error occurs
  1487      */
  1544      */
  1488     public String parseDTDMarkup() throws IOException {
  1545     public String parseDTDMarkup() throws IOException {
  1489 
  1546 
  1490         StringBuilder strBuff = new StringBuilder();
  1547         StringBuilder strBuff = new StringBuilder();
  1491         ch = readCh();
  1548         ch = readCh();
  1525 
  1582 
  1526     /**
  1583     /**
  1527      * Parse markup declarations.
  1584      * Parse markup declarations.
  1528      * Currently only handles the Document Type Declaration markup.
  1585      * Currently only handles the Document Type Declaration markup.
  1529      * Returns true if it is a markup declaration false otherwise.
  1586      * Returns true if it is a markup declaration false otherwise.
       
  1587      *
       
  1588      * @param strBuff  the markup declaration
       
  1589      * @return {@code true} if this is a valid markup declaration;
       
  1590      *         otherwise {@code false}
       
  1591      * @throws IOException if an I/O error occurs
  1530      */
  1592      */
  1531     protected boolean parseMarkupDeclarations(StringBuffer strBuff) throws IOException {
  1593     protected boolean parseMarkupDeclarations(StringBuffer strBuff) throws IOException {
  1532 
  1594 
  1533         /* Currently handles only the DOCTYPE */
  1595         /* Currently handles only the DOCTYPE */
  1534         if ((strBuff.length() == "DOCTYPE".length()) &&
  1596         if ((strBuff.length() == "DOCTYPE".length()) &&
  2244         }
  2306         }
  2245     }
  2307     }
  2246 
  2308 
  2247     /**
  2309     /**
  2248      * Parse an HTML stream, given a DTD.
  2310      * Parse an HTML stream, given a DTD.
       
  2311      *
       
  2312      * @param in  the reader to read the source from
       
  2313      * @throws IOException if an I/O error occurs
  2249      */
  2314      */
  2250     public synchronized void parse(Reader in) throws IOException {
  2315     public synchronized void parse(Reader in) throws IOException {
  2251         this.in = in;
  2316         this.in = in;
  2252 
  2317 
  2253         this.ln = 1;
  2318         this.ln = 1;