org.unitils.dbmaintainer.script.impl
Class DefaultScriptParser

java.lang.Object
  extended by org.unitils.dbmaintainer.script.impl.DefaultScriptParser
All Implemented Interfaces:
ScriptParser
Direct Known Subclasses:
OracleScriptParser

public class DefaultScriptParser
extends Object
implements ScriptParser

A class for parsing statements out of sql scripts.

All statements should be separated with a semicolon (;). The last statement will be added even if it does not end with a semicolon. The semicolons will not be included in the returned statements.

This parser also takes quoted literals, double quoted text and in-line (--comment) and block (/ * comment * /) into account when parsing the statements.

Author:
Tim Ducheyne, Filip Neven, Stefan Bangels

Field Summary
protected  int currentChar
          The current parsed character
protected  ParsingState currentParsingState
          The current state.
protected  ParsingState initialParsingState
          The starting state.
static String PROPKEY_BACKSLASH_ESCAPING_ENABLED
          Property indicating if the characters can be escaped by using backslashes.
protected  Reader scriptReader
          The reader for the script content stream.
 
Constructor Summary
DefaultScriptParser()
           
 
Method Summary
protected  InBlockCommentParsingState createInBlockCommentParsingState()
          Factory method for the in-block comment (/ * comment * /) parsing state.
protected  InDoubleQuotesParsingState createInDoubleQuotesParsingState()
          Factory method for the double quotes ("text") literal parsing state.
protected  ParsingState createInitialParsingState(boolean backSlashEscapingEnabled)
          Builds the initial parsing state.
protected  InLineCommentParsingState createInLineCommentParsingState()
          Factory method for the in-line comment (-- comment) parsing state.
protected  InSingleQuotesParsingState createInSingleQuotesParsingState()
          Factory method for the single quotes ('text') parsing state.
protected  NormalParsingState createNormalParsingState()
          Factory method for the normal parsing state.
protected  StatementBuilder createStatementBuilder()
          Factory method for the statement builder.
 String getNextStatement()
          Parses the next statement out of the given script stream.
protected  String getNextStatementImpl()
          Actual implementation of getNextStatement.
 void init(Properties configuration, Reader scriptReader)
          Initializes the parser with the given configuration settings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPKEY_BACKSLASH_ESCAPING_ENABLED

public static final String PROPKEY_BACKSLASH_ESCAPING_ENABLED
Property indicating if the characters can be escaped by using backslashes. For example '\'' instead of the standard SQL way ''''.

See Also:
Constant Field Values

initialParsingState

protected ParsingState initialParsingState
The starting state.


currentParsingState

protected ParsingState currentParsingState
The current state.


currentChar

protected int currentChar
The current parsed character


scriptReader

protected Reader scriptReader
The reader for the script content stream.

Constructor Detail

DefaultScriptParser

public DefaultScriptParser()
Method Detail

init

public void init(Properties configuration,
                 Reader scriptReader)
Initializes the parser with the given configuration settings.

Specified by:
init in interface ScriptParser
Parameters:
configuration - The config, not null
scriptReader - the script stream, not null

getNextStatement

public String getNextStatement()
Parses the next statement out of the given script stream.

Specified by:
getNextStatement in interface ScriptParser
Returns:
the statements, null if no more statements

getNextStatementImpl

protected String getNextStatementImpl()
                               throws IOException
Actual implementation of getNextStatement.

Returns:
the statements, null if no more statements
Throws:
IOException

createInitialParsingState

protected ParsingState createInitialParsingState(boolean backSlashEscapingEnabled)
Builds the initial parsing state. This will create a normal, in-line-comment, in-block-comment, in-double-quotes and in-single-quotes state and link them together.

Parameters:
backSlashEscapingEnabled - True if a backslash can be used for escaping characters
Returns:
The initial parsing state, not null

createStatementBuilder

protected StatementBuilder createStatementBuilder()
Factory method for the statement builder.

Returns:
The statement builder, not null

createNormalParsingState

protected NormalParsingState createNormalParsingState()
Factory method for the normal parsing state.

Returns:
The normal state, not null

createInLineCommentParsingState

protected InLineCommentParsingState createInLineCommentParsingState()
Factory method for the in-line comment (-- comment) parsing state.

Returns:
The normal state, not null

createInBlockCommentParsingState

protected InBlockCommentParsingState createInBlockCommentParsingState()
Factory method for the in-block comment (/ * comment * /) parsing state.

Returns:
The normal state, not null

createInSingleQuotesParsingState

protected InSingleQuotesParsingState createInSingleQuotesParsingState()
Factory method for the single quotes ('text') parsing state.

Returns:
The normal state, not null

createInDoubleQuotesParsingState

protected InDoubleQuotesParsingState createInDoubleQuotesParsingState()
Factory method for the double quotes ("text") literal parsing state.

Returns:
The normal state, not null


Copyright © 2011. All Rights Reserved.