javax.swing.text.html
Class CSSParser

java.lang.Object
  extended byjavax.swing.text.html.CSSParser

class CSSParser
extends Object

A CSS parser. This works by way of a delegate that implements the CSSParserCallback interface. The delegate is notified of the following events:

This will parse much more than CSS 1, and loosely implements the recommendation for Forward-compatible parsing in section 7.1 of the CSS spec found at: http://www.w3.org/TR/REC-CSS1. If an error results in parsing, a RuntimeException will be thrown.

This will preserve case. If the callback wishes to treat certain poritions case insensitively (such as selectors), it should use toLowerCase, or something similar.

Author:
Scott Violet

Nested Class Summary
(package private) static interface CSSParser.CSSParserCallback
           
 
Field Summary
private static int BRACE_CLOSE
           
private static int BRACE_OPEN
           
private static int BRACKET_CLOSE
           
private static int BRACKET_OPEN
           
private  CSSParser.CSSParserCallback callback
          Notified of state.
private static char[] charMapping
           
private  boolean didPushChar
          Set to true if one character has been read ahead.
private  boolean encounteredRuleSet
          Set to true when the first non @ rule is encountered.
private static int END
           
private static int IDENTIFIER
           
private static int PAREN_CLOSE
           
private static int PAREN_OPEN
           
private  int pushedChar
          The read ahead character.
private  Reader reader
          Holds the incoming CSS rules.
private  boolean readWS
          Set to true if any whitespace is read.
private  int stackCount
          Number of valid blocks.
private  char[] tokenBuffer
          nextToken() inserts the string here.
private  int tokenBufferLength
          Current number of chars in tokenBufferLength.
private  StringBuffer unitBuffer
          Temporary place to hold identifiers.
private  int[] unitStack
          Used to indicate blocks.
 
Constructor Summary
(package private) CSSParser()
           
 
Method Summary
private  void append(char character)
           
private  void endBlock(int endToken)
          Called when an end block is encountered )]}
private  boolean getIdentifier(char stopChar)
          Gets an identifier, returning true if the length of the string is greater than 0, stopping when stopChar, whitespace, or one of {}()[] is hit.
private  boolean getNextStatement()
          Gets the next statement, returning false if the end is reached.
private  boolean inBlock()
           
private  int nextToken(char idChar)
          Fetches the next token.
(package private)  void parse(Reader reader, CSSParser.CSSParserCallback callback, boolean inRule)
           
private  void parseAtRule()
          Parses an @ rule, stopping at a matching brace pair, or ;.
private  int parseDeclaration()
          Parses a single declaration, which is an identifier a : and another identifier.
private  void parseDeclarationBlock()
          Parses a declaration block.
private  int parseIdentifiers(char extraChar, boolean wantsBlocks)
          Parses identifiers until extraChar is encountered, returning the ending token, which will be IDENTIFIER if extraChar is found.
private  void parseRuleSet()
          Parses the next rule set, which is a selector followed by a declaration block.
private  boolean parseSelectors()
          Parses a set of selectors, returning false if the end of the stream is reached.
private  void parseTillClosed(int openToken)
          Parses till a matching block close is encountered.
private  void pushChar(int tempChar)
          Supports one character look ahead, this will throw if called twice in a row.
private  int readChar()
          Reads a character from the stream.
private  void readComment()
          Parses a comment block.
private  void readTill(char stopChar)
          Reads till a stopChar is encountered, escaping characters as necessary.
private  int readWS()
          Skips any white space, returning the character after the white space.
private  void startBlock(int startToken)
          Called when a block start is encountered ({[.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IDENTIFIER

private static final int IDENTIFIER
See Also:
Constant Field Values

BRACKET_OPEN

private static final int BRACKET_OPEN
See Also:
Constant Field Values

BRACKET_CLOSE

private static final int BRACKET_CLOSE
See Also:
Constant Field Values

BRACE_OPEN

private static final int BRACE_OPEN
See Also:
Constant Field Values

BRACE_CLOSE

private static final int BRACE_CLOSE
See Also:
Constant Field Values

PAREN_OPEN

private static final int PAREN_OPEN
See Also:
Constant Field Values

PAREN_CLOSE

private static final int PAREN_CLOSE
See Also:
Constant Field Values

END

private static final int END
See Also:
Constant Field Values

charMapping

private static final char[] charMapping

didPushChar

private boolean didPushChar
Set to true if one character has been read ahead.


pushedChar

private int pushedChar
The read ahead character.


unitBuffer

private StringBuffer unitBuffer
Temporary place to hold identifiers.


unitStack

private int[] unitStack
Used to indicate blocks.


stackCount

private int stackCount
Number of valid blocks.


reader

private Reader reader
Holds the incoming CSS rules.


encounteredRuleSet

private boolean encounteredRuleSet
Set to true when the first non @ rule is encountered.


callback

private CSSParser.CSSParserCallback callback
Notified of state.


tokenBuffer

private char[] tokenBuffer
nextToken() inserts the string here.


tokenBufferLength

private int tokenBufferLength
Current number of chars in tokenBufferLength.


readWS

private boolean readWS
Set to true if any whitespace is read.

Constructor Detail

CSSParser

CSSParser()
Method Detail

parse

void parse(Reader reader,
           CSSParser.CSSParserCallback callback,
           boolean inRule)
     throws IOException
Throws:
IOException

getNextStatement

private boolean getNextStatement()
                          throws IOException
Gets the next statement, returning false if the end is reached. A statement is either an @rule, or a ruleset.

Throws:
IOException

parseAtRule

private void parseAtRule()
                  throws IOException
Parses an @ rule, stopping at a matching brace pair, or ;.

Throws:
IOException

parseRuleSet

private void parseRuleSet()
                   throws IOException
Parses the next rule set, which is a selector followed by a declaration block.

Throws:
IOException

parseSelectors

private boolean parseSelectors()
                        throws IOException
Parses a set of selectors, returning false if the end of the stream is reached.

Throws:
IOException

parseDeclarationBlock

private void parseDeclarationBlock()
                            throws IOException
Parses a declaration block. Which a number of declarations followed by a })].

Throws:
IOException

parseDeclaration

private int parseDeclaration()
                      throws IOException
Parses a single declaration, which is an identifier a : and another identifier. This returns the last token seen.

Throws:
IOException

parseIdentifiers

private int parseIdentifiers(char extraChar,
                             boolean wantsBlocks)
                      throws IOException
Parses identifiers until extraChar is encountered, returning the ending token, which will be IDENTIFIER if extraChar is found.

Throws:
IOException

parseTillClosed

private void parseTillClosed(int openToken)
                      throws IOException
Parses till a matching block close is encountered. This is only appropriate to be called at the top level (no nesting).

Throws:
IOException

nextToken

private int nextToken(char idChar)
               throws IOException
Fetches the next token.

Throws:
IOException

getIdentifier

private boolean getIdentifier(char stopChar)
                       throws IOException
Gets an identifier, returning true if the length of the string is greater than 0, stopping when stopChar, whitespace, or one of {}()[] is hit.

Throws:
IOException

readTill

private void readTill(char stopChar)
               throws IOException
Reads till a stopChar is encountered, escaping characters as necessary.

Throws:
IOException

append

private void append(char character)

readComment

private void readComment()
                  throws IOException
Parses a comment block.

Throws:
IOException

startBlock

private void startBlock(int startToken)
Called when a block start is encountered ({[.


endBlock

private void endBlock(int endToken)
Called when an end block is encountered )]}


inBlock

private boolean inBlock()
Returns:
true if currently in a block.

readWS

private int readWS()
            throws IOException
Skips any white space, returning the character after the white space.

Throws:
IOException

readChar

private int readChar()
              throws IOException
Reads a character from the stream.

Throws:
IOException

pushChar

private void pushChar(int tempChar)
Supports one character look ahead, this will throw if called twice in a row.