com.google.streamhtmlparser.impl
Class GenericParser

java.lang.Object
  extended by com.google.streamhtmlparser.impl.GenericParser
All Implemented Interfaces:
Parser
Direct Known Subclasses:
HtmlParserImpl, JavascriptParserImpl

public class GenericParser
extends Object
implements Parser

An implementation of the Parser interface that is common to both HtmlParser and JavascriptParser.

Provides methods for parsing input and ensuring that all in-state, entering-a-state and exiting-a-state callbacks are invoked as appropriate.

This class started as abstract but it was found better for testing to make it instantiatable so that the parsing logic can be tested with dummy state transitions.


Field Summary
protected  int columnNumber
           
protected  com.google.streamhtmlparser.impl.InternalState currentState
           
protected  com.google.streamhtmlparser.impl.InternalState initialState
           
protected  Map<com.google.streamhtmlparser.impl.InternalState,ExternalState> intToExtStateTable
           
protected  int lineNumber
           
protected  com.google.streamhtmlparser.impl.ParserStateTable parserStateTable
           
 
Fields inherited from interface com.google.streamhtmlparser.Parser
STATE_ERROR
 
Constructor Summary
protected GenericParser(GenericParser aGenericParser)
          Constructs a generic parser that is an exact copy of the one given.
protected GenericParser(com.google.streamhtmlparser.impl.ParserStateTable parserStateTable, Map<com.google.streamhtmlparser.impl.InternalState,ExternalState> intToExtStateTable, com.google.streamhtmlparser.impl.InternalState initialState)
           
 
Method Summary
 int getColumnNumber()
          Returns the current column number.
 int getLineNumber()
          Returns the current line number.
 ExternalState getState()
          Return the current state of the parser.
protected  com.google.streamhtmlparser.impl.InternalState handleEnterState(com.google.streamhtmlparser.impl.InternalState currentState, com.google.streamhtmlparser.impl.InternalState expectedNextState, char input)
          Invoked when the parser enters a new state.
protected  com.google.streamhtmlparser.impl.InternalState handleExitState(com.google.streamhtmlparser.impl.InternalState currentState, com.google.streamhtmlparser.impl.InternalState expectedNextState, char input)
          Invoked when the parser exits a state.
protected  com.google.streamhtmlparser.impl.InternalState handleInState(com.google.streamhtmlparser.impl.InternalState currentState, char input)
          Invoked for each character read when no state change occured.
 void parse(char input)
          Main loop for parsing of input.
 void parse(String input)
          Tell the parser to process the provided String.
protected  void record(char input)
          Perform some processing on the given character.
 void reset()
          Reset the parser back to its initial default state.
 void setColumnNumber(int columnNumber)
          Sets the current column number which is returned during error messages.
 void setLineNumber(int lineNumber)
          Sets the current line number which is returned during error messages.
protected  void setNextState(com.google.streamhtmlparser.impl.InternalState nextState)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

parserStateTable

protected final com.google.streamhtmlparser.impl.ParserStateTable parserStateTable

intToExtStateTable

protected final Map<com.google.streamhtmlparser.impl.InternalState,ExternalState> intToExtStateTable

initialState

protected final com.google.streamhtmlparser.impl.InternalState initialState

currentState

protected com.google.streamhtmlparser.impl.InternalState currentState

lineNumber

protected int lineNumber

columnNumber

protected int columnNumber
Constructor Detail

GenericParser

protected GenericParser(com.google.streamhtmlparser.impl.ParserStateTable parserStateTable,
                        Map<com.google.streamhtmlparser.impl.InternalState,ExternalState> intToExtStateTable,
                        com.google.streamhtmlparser.impl.InternalState initialState)

GenericParser

protected GenericParser(GenericParser aGenericParser)
Constructs a generic parser that is an exact copy of the one given. Note that here too, data structures that do not change are shallow-copied (parser state table and state mappings).

Parameters:
aGenericParser - the GenericParser to copy
Method Detail

parse

public void parse(String input)
           throws ParseException
Tell the parser to process the provided String. This is just a convenience method that wraps over Parser.parse(char).

Specified by:
parse in interface Parser
Parameters:
input - the String to parse
Throws:
ParseException - if an unrecoverable error occurred during parsing

parse

public void parse(char input)
           throws ParseException
Main loop for parsing of input.

Absent any callbacks defined, this function simply determines the next state to switch to based on the ParserStateTable which is derived from a state-machine configuration file in the original C++ parser.

However some states have specific callbacks defined which when receiving specific characters may decide to overwrite the next state to go to. Hence the next state is a function both of the main state table in ParserStateTable as well as specific run-time information from the callback functions.

Also note that the callbacks are called in a proper sequence, first the exit-state one then the enter-state one and finally the in-state one. Changing the order may result in a functional change.

Specified by:
parse in interface Parser
Parameters:
input - the input character to parse (process)
Throws:
ParseException - if an unrecoverable error occurred during parsing

getState

public ExternalState getState()
Return the current state of the parser.

Specified by:
getState in interface Parser
Returns:
current state of the parser

reset

public void reset()
Reset the parser back to its initial default state.

Specified by:
reset in interface Parser

setLineNumber

public void setLineNumber(int lineNumber)
Sets the current line number which is returned during error messages.

Specified by:
setLineNumber in interface Parser
Parameters:
lineNumber - the line number to set in the parser

getLineNumber

public int getLineNumber()
Returns the current line number.

Specified by:
getLineNumber in interface Parser

setColumnNumber

public void setColumnNumber(int columnNumber)
Sets the current column number which is returned during error messages.

Specified by:
setColumnNumber in interface Parser
Parameters:
columnNumber - the column number to set in the parser

getColumnNumber

public int getColumnNumber()
Returns the current column number.

Specified by:
getColumnNumber in interface Parser

setNextState

protected void setNextState(com.google.streamhtmlparser.impl.InternalState nextState)
                     throws ParseException
Throws:
ParseException

handleEnterState

protected com.google.streamhtmlparser.impl.InternalState handleEnterState(com.google.streamhtmlparser.impl.InternalState currentState,
                                                                          com.google.streamhtmlparser.impl.InternalState expectedNextState,
                                                                          char input)
                                                                   throws ParseException
Invoked when the parser enters a new state.

Parameters:
currentState - the current state of the parser
expectedNextState - the next state according to the state table definition
input - the last character parsed
Returns:
the state to change to, could be the same as the expectedNextState provided
Throws:
ParseException - if an unrecoverable error occurred during parsing

handleExitState

protected com.google.streamhtmlparser.impl.InternalState handleExitState(com.google.streamhtmlparser.impl.InternalState currentState,
                                                                         com.google.streamhtmlparser.impl.InternalState expectedNextState,
                                                                         char input)
                                                                  throws ParseException
Invoked when the parser exits a state.

Parameters:
currentState - the current state of the parser
expectedNextState - the next state according to the state table definition
input - the last character parsed
Returns:
the state to change to, could be the same as the expectedNextState provided
Throws:
ParseException - if an unrecoverable error occurred during parsing

handleInState

protected com.google.streamhtmlparser.impl.InternalState handleInState(com.google.streamhtmlparser.impl.InternalState currentState,
                                                                       char input)
                                                                throws ParseException
Invoked for each character read when no state change occured.

Parameters:
currentState - the current state of the parser
input - the last character parsed
Returns:
the state to change to, could be the same as the expectedNextState provided
Throws:
ParseException - if an unrecoverable error occurred during parsing

record

protected void record(char input)
Perform some processing on the given character. Derived classes may override this method in order to perform additional logic on every processed character beyond the logic defined in state transitions.

Parameters:
input - the input character to operate on


Copyright © 2010-2012 Google. All Rights Reserved.