com.sibvisions.util.type
Class StringParser

java.lang.Object
  extended by com.sibvisions.util.type.StringParser

public class StringParser
extends Object

The StringParser is a very simple text parser, that just implements splitting a single line into fields.


Constructor Summary
StringParser(char pSeparator)
          Creates a new instance of StringParser with supplied separator.
StringParser(char pSeparator, char pQuote)
          Creates a new instance of StringParser with supplied separator and quote char.
StringParser(char pSeparator, char pQuote, char pEscape)
          Creates a new instance of StringParser with supplied separator, quote and escape character.
 
Method Summary
protected  boolean isAllWhiteSpace(CharSequence pText)
          Gets whether the given text only contains whitespaces or is empty.
 boolean isIgnoreLeadingWhiteSpace()
          Gets whether leading whitespaces should be ignored.
 boolean isMustQuote()
          Gets whether the strict quote mode is enabled.
protected  boolean isNextCharacterEscapable(String pText, boolean pInQuotes, int pIndex)
          Gets whether the next character is an escape.
 boolean isPending()
          Gets whether parsing has open fields from last call(s).
 boolean isRemoveQuotes()
          Gets whether leading and trailing quotes will be removed from the fields.
 String[] parse(String pText)
          Parse a line that contains one filed.
 String[] parse(String pText, boolean pMulti)
          Parse a line with multi field support.
 void setIgnoreLeadingWhiteSpace(boolean pIgnore)
          Sets whether leading whitespaces should be ignored.
 void setMustQuote(boolean pMustQuote)
          Sets whether a field must be in quotes to be a valid field.
 void setRemoveQuotes(boolean pIgnoreQuotes)
          Sets whether leading and trailing quotes should be removed from the fields.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringParser

public StringParser(char pSeparator)
Creates a new instance of StringParser with supplied separator. The default quote character & and escape character \ will be used.

Parameters:
pSeparator - the delimiter to use for separating entries

StringParser

public StringParser(char pSeparator,
                    char pQuote)
Creates a new instance of StringParser with supplied separator and quote char. The default escape character \ will be used.

Parameters:
pSeparator - the delimiter to use for separating entries
pQuote - the character to use for quoted elements

StringParser

public StringParser(char pSeparator,
                    char pQuote,
                    char pEscape)
Creates a new instance of StringParser with supplied separator, quote and escape character.

Parameters:
pSeparator - the delimiter to use for separating entries
pQuote - the character to use for quoted elements
pEscape - the character to use for escaping a separator or quote
Method Detail

parse

public String[] parse(String pText)
               throws IOException
Parse a line that contains one filed.

Parameters:
pText - the text to parse
Returns:
the parsed fields
Throws:
IOException - if parsing fails

parse

public String[] parse(String pText,
                      boolean pMulti)
               throws IOException
Parse a line with multi field support.

Parameters:
pText - the text to parse
pMulti - whether the text contains more than one field
Returns:
the parsed fields
Throws:
IOException - if parsing fails

isPending

public boolean isPending()
Gets whether parsing has open fields from last call(s).

Returns:
true if something was left over from last call(s)

isNextCharacterEscapable

protected boolean isNextCharacterEscapable(String pText,
                                           boolean pInQuotes,
                                           int pIndex)
Gets whether the next character is an escape.

Parameters:
pText - the current text
pInQuotes - true if the current context is quoted
pIndex - current index in text
Returns:
true if the next character is a quote

isAllWhiteSpace

protected boolean isAllWhiteSpace(CharSequence pText)
Gets whether the given text only contains whitespaces or is empty.

Parameters:
pText - a sequence of characters to examine
Returns:
true if every character in the sequence is whitespace

setRemoveQuotes

public void setRemoveQuotes(boolean pIgnoreQuotes)
Sets whether leading and trailing quotes should be removed from the fields.

Parameters:
pIgnoreQuotes - true to remove quotes from found fields, false if found should contain leading and trailing quotes

isRemoveQuotes

public boolean isRemoveQuotes()
Gets whether leading and trailing quotes will be removed from the fields.

Returns:
true if leading and trailing quotes will be removed, false otherwise

setMustQuote

public void setMustQuote(boolean pMustQuote)
Sets whether a field must be in quotes to be a valid field.

Parameters:
pMustQuote - true to enable strict quote mode, false bo allow mixed fields

isMustQuote

public boolean isMustQuote()
Gets whether the strict quote mode is enabled.

Returns:
true if strict quote mode is enabled, false otherwise
See Also:
setMustQuote(boolean)

setIgnoreLeadingWhiteSpace

public void setIgnoreLeadingWhiteSpace(boolean pIgnore)
Sets whether leading whitespaces should be ignored.

Parameters:
pIgnore - true to ignore leading whitespaces, false otherwise

isIgnoreLeadingWhiteSpace

public boolean isIgnoreLeadingWhiteSpace()
Gets whether leading whitespaces should be ignored.

Returns:
true if leading whitespaces will be ignored, false otherwise


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.