javax.swing.text.html.parser
Class Parser

java.lang.Object
  extended byjavax.swing.text.html.parser.Parser
All Implemented Interfaces:
DTDConstants
Direct Known Subclasses:
DocumentParser

public class Parser
extends Object
implements DTDConstants

A simple DTD-driven HTML parser. The parser reads an HTML file from an InputStream and calls various methods (which should be overridden in a subclass) when tags and data are encountered.

Unfortunately there are many badly implemented HTML parsers out there, and as a result there are many badly formatted HTML files. This parser attempts to parse most HTML files. This means that the implementation sometimes deviates from the SGML specification in favor of HTML.

The parser treats \r and \r\n as \n. Newlines after starttags and before end tags are ignored just as specified in the SGML/HTML specification.

The html spec does not specify how spaces are to be coalesced very well. Specifically, the following scenarios are not discussed (note that a space should be used here, but I am using &nbsp to force the space to be displayed):

'<b>blah <i> <strike> foo' which can be treated as: '<b>blah <i><strike>foo'

as well as: '<p><a href="xx"> <em>Using</em></a></p>' which appears to be treated as: '<p><a href="xx"><em>Using</em></a></p>'

If strict is false, when a tag that breaks flow, (TagElement.breaksFlows) or trailing whitespace is encountered, all whitespace will be ignored until a non whitespace character is encountered. This appears to give behavior closer to the popular browsers.

Author:
Arthur van Hoff, Sunita Mani
See Also:
DTD, TagElement, SimpleAttributeSet

Field Summary
private  SimpleAttributeSet attributes
           
private  char[] buf
           
private  int ch
           
private  int crCount
          Number of \r's encountered.
private  int crlfCount
          Number of \r\n's encountered.
private  int currentBlockStartPos
          The start position of the current block.
private  int currentPosition
           
protected  DTD dtd
           
private  boolean ignoreSpace
          The html spec does not specify how spaces are coalesced very well.
private  Reader in
           
private  TagElement last
           
private  int lastBlockStartPos
          Start position of the last block.
private  TagElement lastFormSent
           
private  int len
           
private  int lfCount
          Number of \n's encountered.
private  int ln
           
private  int pos
           
private  Element recent
           
private  boolean seenBody
           
private  boolean seenHead
           
private  boolean seenHtml
           
private  boolean skipTag
           
private  boolean space
           
private  TagStack stack
           
private  char[] str
           
protected  boolean strict
          This flag determines whether or not the Parser will be strict in enforcing SGML compatibility.
private  int strpos
           
private  char[] text
           
private  int textpos
           
 
Fields inherited from interface javax.swing.text.html.parser.DTDConstants
ANY, CDATA, CONREF, CURRENT, DEFAULT, EMPTY, ENDTAG, ENTITIES, ENTITY, FIXED, GENERAL, ID, IDREF, IDREFS, IMPLIED, MD, MODEL, MS, NAME, NAMES, NMTOKEN, NMTOKENS, NOTATION, NUMBER, NUMBERS, NUTOKEN, NUTOKENS, PARAMETER, PI, PUBLIC, RCDATA, REQUIRED, SDATA, STARTTAG, SYSTEM
 
Constructor Summary
Parser(DTD dtd)
           
 
Method Summary
(package private)  void addString(int c)
          Add a char to the string buffer.
protected  void endTag(boolean omitted)
          Handle an end tag.
protected  void error(String err)
           
protected  void error(String err, String arg1)
           
protected  void error(String err, String arg1, String arg2)
           
protected  void error(String err, String arg1, String arg2, String arg3)
          Invoke the error handler.
(package private)  void errorContext()
          Error context.
protected  void flushAttributes()
           
protected  SimpleAttributeSet getAttributes()
           
(package private)  int getBlockStartPosition()
          Returns the start position of the current block.
(package private)  char[] getChars(int pos)
           
(package private)  char[] getChars(int pos, int endPos)
           
protected  int getCurrentLine()
           
protected  int getCurrentPos()
           
(package private)  String getEndOfLineString()
          Returns the end of line string.
(package private)  String getString(int pos)
          Get the string that's been accumulated.
protected  void handleComment(char[] text)
          Called when an HTML comment is encountered.
protected  void handleEmptyTag(TagElement tag)
          Called when an empty tag is encountered.
protected  void handleEndTag(TagElement tag)
          Called when an end tag is encountered.
protected  void handleEOFInComment()
           
protected  void handleError(int ln, String msg)
          An error has occurred.
protected  void handleStartTag(TagElement tag)
          Called when a start tag is encountered.
protected  void handleText(char[] text)
          Called when PCDATA is encountered.
(package private)  void handleText(TagElement tag)
          Output text.
protected  void handleTitle(char[] text)
          Called when an HTML title tag is encountered.
(package private)  boolean ignoreElement(Element elem)
           
(package private)  boolean legalElementContext(Element elem)
          Create a legal content for an element.
(package private)  void legalTagContext(TagElement tag)
          Create a legal context for a tag.
protected  TagElement makeTag(Element elem)
           
protected  TagElement makeTag(Element elem, boolean fictional)
          Makes a TagElement.
protected  void markFirstTime(Element elem)
          Marks the first time a tag has been seen in a document
 void parse(Reader in)
          Parse an HTML stream, given a DTD.
(package private)  void parseAttributeSpecificationList(Element elem)
          Parse attribute specification List. [31] 327:17
(package private)  String parseAttributeValue(boolean lower)
          Parse attribute value. [33] 331:1
(package private)  void parseComment()
          Parse a comment. [92] 391:7
(package private)  void parseContent()
          Parse Content. [24] 320:1
 String parseDTDMarkup()
          Parses th Document Declaration Type markup declaration.
private  char[] parseEntityReference()
          Parse an entity reference. [59] 350:17
(package private)  boolean parseIdentifier(boolean lower)
          Parse identifier.
(package private)  void parseInvalidTag()
          Parse an invalid tag.
(package private)  void parseLiteral(boolean replace)
          Parse literal content. [46] 343:1 and [47] 344:1
protected  boolean parseMarkupDeclarations(StringBuffer strBuff)
          Parse markup declarations.
(package private)  void parseTag()
          Parse a start or end tag.
private  int readCh()
           
(package private)  void resetStrBuffer()
           
(package private)  void skipSpace()
          Skip space
protected  void startTag(TagElement tag)
          Handle a start tag.
(package private)  int strIndexOf(char target)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

text

private char[] text

textpos

private int textpos

last

private TagElement last

space

private boolean space

str

private char[] str

strpos

private int strpos

dtd

protected DTD dtd

ch

private int ch

ln

private int ln

in

private Reader in

recent

private Element recent

stack

private TagStack stack

skipTag

private boolean skipTag

lastFormSent

private TagElement lastFormSent

attributes

private SimpleAttributeSet attributes

seenHtml

private boolean seenHtml

seenHead

private boolean seenHead

seenBody

private boolean seenBody

ignoreSpace

private boolean ignoreSpace
The html spec does not specify how spaces are coalesced very well. If strict == false, ignoreSpace is used to try and mimic the behavior of the popular browsers.

The problematic scenarios are: '<b>blah <i> <strike> foo' which can be treated as: '<b>blah <i><strike>foo' as well as: '<p><a href="xx"> <em>Using</em></a></p>' which appears to be treated as: '<p><a href="xx"><em>Using</em></a></p>'

When a tag that breaks flow, or trailing whitespace is encountered ignoreSpace is set to true. From then on, all whitespace will be ignored. ignoreSpace will be set back to false the first time a non whitespace character is encountered. This appears to give behavior closer to the popular browsers.


strict

protected boolean strict
This flag determines whether or not the Parser will be strict in enforcing SGML compatibility. If false, it will be lenient with certain common classes of erroneous HTML constructs. Strict or not, in either case an error will be recorded.


crlfCount

private int crlfCount
Number of \r\n's encountered.


crCount

private int crCount
Number of \r's encountered. A \r\n will not increment this.


lfCount

private int lfCount
Number of \n's encountered. A \r\n will not increment this.


currentBlockStartPos

private int currentBlockStartPos
The start position of the current block. Block is overloaded here, it really means the current start position for the current comment, tag, text. Use getBlockStartPosition to access this.


lastBlockStartPos

private int lastBlockStartPos
Start position of the last block.


buf

private char[] buf

pos

private int pos

len

private int len

currentPosition

private int currentPosition
Constructor Detail

Parser

public Parser(DTD dtd)
Method Detail

getCurrentLine

protected int getCurrentLine()
Returns:
the line number of the line currently being parsed

getBlockStartPosition

int getBlockStartPosition()
Returns the start position of the current block. Block is overloaded here, it really means the current start position for the current comment tag, text, block.... This is provided for subclassers that wish to know the start of the current block when called with one of the handleXXX methods.


makeTag

protected TagElement makeTag(Element elem,
                             boolean fictional)
Makes a TagElement.


makeTag

protected TagElement makeTag(Element elem)

getAttributes

protected SimpleAttributeSet getAttributes()

flushAttributes

protected void flushAttributes()

handleText

protected void handleText(char[] text)
Called when PCDATA is encountered.


handleTitle

protected void handleTitle(char[] text)
Called when an HTML title tag is encountered.


handleComment

protected void handleComment(char[] text)
Called when an HTML comment is encountered.


handleEOFInComment

protected void handleEOFInComment()

handleEmptyTag

protected void handleEmptyTag(TagElement tag)
                       throws ChangedCharSetException
Called when an empty tag is encountered.

Throws:
ChangedCharSetException

handleStartTag

protected void handleStartTag(TagElement tag)
Called when a start tag is encountered.


handleEndTag

protected void handleEndTag(TagElement tag)
Called when an end tag is encountered.


handleError

protected void handleError(int ln,
                           String msg)
An error has occurred.


handleText

void handleText(TagElement tag)
Output text.


error

protected void error(String err,
                     String arg1,
                     String arg2,
                     String arg3)
Invoke the error handler.


error

protected void error(String err,
                     String arg1,
                     String arg2)

error

protected void error(String err,
                     String arg1)

error

protected void error(String err)

startTag

protected void startTag(TagElement tag)
                 throws ChangedCharSetException
Handle a start tag. The new tag is pushed onto the tag stack. The attribute list is checked for required attributes.

Throws:
ChangedCharSetException

endTag

protected void endTag(boolean omitted)
Handle an end tag. The end tag is popped from the tag stack.


ignoreElement

boolean ignoreElement(Element elem)

markFirstTime

protected void markFirstTime(Element elem)
Marks the first time a tag has been seen in a document


legalElementContext

boolean legalElementContext(Element elem)
                      throws ChangedCharSetException
Create a legal content for an element.

Throws:
ChangedCharSetException

legalTagContext

void legalTagContext(TagElement tag)
               throws ChangedCharSetException
Create a legal context for a tag.

Throws:
ChangedCharSetException

errorContext

void errorContext()
            throws ChangedCharSetException
Error context. Something went wrong, make sure we are in the document's body context

Throws:
ChangedCharSetException

addString

void addString(int c)
Add a char to the string buffer.


getString

String getString(int pos)
Get the string that's been accumulated.


getChars

char[] getChars(int pos)

getChars

char[] getChars(int pos,
                int endPos)

resetStrBuffer

void resetStrBuffer()

strIndexOf

int strIndexOf(char target)

skipSpace

void skipSpace()
         throws IOException
Skip space. [5] 297:5

Throws:
IOException

parseIdentifier

boolean parseIdentifier(boolean lower)
                  throws IOException
Parse identifier. Uppercase characters are folded to lowercase when lower is true. Returns falsed if no identifier is found. [55] 346:17

Throws:
IOException

parseEntityReference

private char[] parseEntityReference()
                             throws IOException
Parse an entity reference. [59] 350:17

Throws:
IOException

parseComment

void parseComment()
            throws IOException
Parse a comment. [92] 391:7

Throws:
IOException

parseLiteral

void parseLiteral(boolean replace)
            throws IOException
Parse literal content. [46] 343:1 and [47] 344:1

Throws:
IOException

parseAttributeValue

String parseAttributeValue(boolean lower)
                     throws IOException
Parse attribute value. [33] 331:1

Throws:
IOException

parseAttributeSpecificationList

void parseAttributeSpecificationList(Element elem)
                               throws IOException
Parse attribute specification List. [31] 327:17

Throws:
IOException

parseDTDMarkup

public String parseDTDMarkup()
                      throws IOException
Parses th Document Declaration Type markup declaration. Currently ignores it.

Throws:
IOException

parseMarkupDeclarations

protected boolean parseMarkupDeclarations(StringBuffer strBuff)
                                   throws IOException
Parse markup declarations. Currently only handles the Document Type Declaration markup. Returns true if it is a markup declaration false otherwise.

Throws:
IOException

parseInvalidTag

void parseInvalidTag()
               throws IOException
Parse an invalid tag.

Throws:
IOException

parseTag

void parseTag()
        throws IOException
Parse a start or end tag.

Throws:
IOException

parseContent

void parseContent()
            throws IOException
Parse Content. [24] 320:1

Throws:
IOException

getEndOfLineString

String getEndOfLineString()
Returns the end of line string. This will return the end of line string that has been encountered the most, one of \r, \n or \r\n.


parse

public void parse(Reader in)
           throws IOException
Parse an HTML stream, given a DTD.

Throws:
IOException

readCh

private final int readCh()
                  throws IOException
Throws:
IOException

getCurrentPos

protected int getCurrentPos()