com.plumtree.uiinfrastructure.utils
Class JavascriptParser

java.lang.Object
  extended by com.plumtree.uiinfrastructure.utils.JavascriptParser
Direct Known Subclasses:
JSONParser

public class JavascriptParser
extends java.lang.Object

Parses Javascript String into Java object representation. The Javascript needs start as either an JS Object or JS array. Use JSONParser class to parse JSON strings The unit test for this class lives in portaluiinfrastructure project

Author:
rozhang

Field Summary
protected static char CLOSE_BRACKET
           
protected static char CLOSE_CURLY_BRACKET
           
protected static char COLON
           
protected static char COMMA
           
protected static char ESCAPE
           
protected  XPArrayList m_rootList
           
protected  XPHashtable m_rootObj
           
protected static char OPEN_BRACKET
           
protected static char OPEN_CURLY_BRACKET
           
protected static char QUOTE
           
protected static char SINGLE_QUOTE
           
 
Constructor Summary
JavascriptParser()
           
 
Method Summary
protected  java.lang.String GetEscapedTokenAndEndQuote(XPStringTokenizer tokz, boolean doubleQuote)
          This gets an escaped quoted sequence, starting immediately after the opening quote.
protected  java.lang.String GetNextToken(XPStringTokenizer tok)
           
 XPArrayList GetRootArray()
           
 XPHashtable GetRootObject()
           
protected  java.lang.String GetToken(XPStringTokenizer _tokenizer)
           
protected  boolean IsJSComment(java.lang.String strToken, XPStringTokenizer tokz)
          Check for Javascript comments and advance the tokenizer until end of comment
protected  boolean IsNewArray(java.lang.String token)
           
protected  boolean IsNewJSObject(java.lang.String token)
           
 boolean IsRootArray()
          ACCESSORS
 boolean IsRootObject()
           
protected  boolean IsWhiteSpace(java.lang.String _strToken)
          Checks if a token is a single piece of whitespace.
protected  XPArrayList ParseJSArray(XPStringTokenizer tokz)
          Returns a javascript array in an XPArraList
protected  XPHashtable ParseJSObject(XPStringTokenizer tokz)
          Parse Javascript object and create hashtable representation Expects the object begin curly bracket ("{") to have been already processed in the string tokenizer
protected  java.lang.String ParseJSOMemberLabel(XPStringTokenizer tokz)
          Gets the name of a javascript object member Expects the next token of the string tokenizer to be the double quote around the label.
protected  java.lang.Object ParseJSOMemberValue(java.lang.String memberName, XPStringTokenizer tokz)
          Parses JS Object member value Expects next value in tokenizer to be the first character of the value.
protected  java.lang.Object ParseJSSimpleValue(java.lang.String initialToken, XPStringTokenizer tokz)
           
 void ParseJSString(java.lang.String injs)
          Parses Javascript string and stores a Java representation
protected  void SkipToken(XPStringTokenizer _tokenizer, char _token)
          Skip the next token if it matches the expected token.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OPEN_CURLY_BRACKET

protected static final char OPEN_CURLY_BRACKET
See Also:
Constant Field Values

CLOSE_CURLY_BRACKET

protected static final char CLOSE_CURLY_BRACKET
See Also:
Constant Field Values

OPEN_BRACKET

protected static final char OPEN_BRACKET
See Also:
Constant Field Values

CLOSE_BRACKET

protected static final char CLOSE_BRACKET
See Also:
Constant Field Values

QUOTE

protected static final char QUOTE
See Also:
Constant Field Values

SINGLE_QUOTE

protected static final char SINGLE_QUOTE
See Also:
Constant Field Values

COLON

protected static final char COLON
See Also:
Constant Field Values

COMMA

protected static final char COMMA
See Also:
Constant Field Values

ESCAPE

protected static final char ESCAPE
See Also:
Constant Field Values

m_rootObj

protected XPHashtable m_rootObj

m_rootList

protected XPArrayList m_rootList
Constructor Detail

JavascriptParser

public JavascriptParser()
Method Detail

ParseJSString

public void ParseJSString(java.lang.String injs)
Parses Javascript string and stores a Java representation

Parameters:
injs - Javascript String

ParseJSOMemberValue

protected java.lang.Object ParseJSOMemberValue(java.lang.String memberName,
                                               XPStringTokenizer tokz)
Parses JS Object member value Expects next value in tokenizer to be the first character of the value. Ex: quote for strings, bracket for array, or number

Parameters:
memberName -
tokz - String tokenizer of the JS string. Next value in the tokenizer is the first character of the value
Returns:
Java object representation of the value. XPHashtable -> JS Object, XPArrayList -> JS Array, String -> JS string, Integer -> JS number, Boolean -> JS boolean

ParseJSObject

protected XPHashtable ParseJSObject(XPStringTokenizer tokz)
Parse Javascript object and create hashtable representation Expects the object begin curly bracket ("{") to have been already processed in the string tokenizer

Parameters:
tok -
Returns:

ParseJSOMemberLabel

protected java.lang.String ParseJSOMemberLabel(XPStringTokenizer tokz)
Gets the name of a javascript object member Expects the next token of the string tokenizer to be the double quote around the label. This method will also consume the ending double quote and following colon.

Parameters:
tokz -
Returns:

ParseJSArray

protected XPArrayList ParseJSArray(XPStringTokenizer tokz)
Returns a javascript array in an XPArraList

Parameters:
tokz -
Returns:

ParseJSSimpleValue

protected java.lang.Object ParseJSSimpleValue(java.lang.String initialToken,
                                              XPStringTokenizer tokz)
Parameters:
tokz -
Returns:

GetToken

protected java.lang.String GetToken(XPStringTokenizer _tokenizer)
Parameters:
_tokenizer - the JSON tokenizer
Returns:
the next token without whitespace.

SkipToken

protected void SkipToken(XPStringTokenizer _tokenizer,
                         char _token)
Skip the next token if it matches the expected token.

Parameters:
_tokenizer - the JSON tokenizer
_token - the expected token
Throws:
XPFormatException - if the next token doesn't match the expected token.

GetNextToken

protected java.lang.String GetNextToken(XPStringTokenizer tok)

IsWhiteSpace

protected boolean IsWhiteSpace(java.lang.String _strToken)
Checks if a token is a single piece of whitespace.

Parameters:
_strToken -
Returns:
boolean true means that the token is whitespace.

IsJSComment

protected boolean IsJSComment(java.lang.String strToken,
                              XPStringTokenizer tokz)
Check for Javascript comments and advance the tokenizer until end of comment

Parameters:
strToken -
tokz -
Returns:

GetEscapedTokenAndEndQuote

protected java.lang.String GetEscapedTokenAndEndQuote(XPStringTokenizer tokz,
                                                      boolean doubleQuote)
This gets an escaped quoted sequence, starting immediately after the opening quote. It reads the end quote, but does not return it.

Parameters:
tokz -
doubleQuote - true if string value uses double quotes or false for single quote
Returns:
the escaped string token. The closing quote has been read.
Throws:
XPFormatException

IsNewArray

protected boolean IsNewArray(java.lang.String token)
Parameters:
token -
Returns:

IsNewJSObject

protected boolean IsNewJSObject(java.lang.String token)
Parameters:
token - Single token from String tokenizer
Returns:

IsRootArray

public boolean IsRootArray()
ACCESSORS


IsRootObject

public boolean IsRootObject()

GetRootArray

public XPArrayList GetRootArray()

GetRootObject

public XPHashtable GetRootObject()



Copyright © 2002,2003,2004,2005 Plumtree Software, Inc., All Rights Reserved.