Main Page | Packages | Class Hierarchy | Class List | File List | Class Members

xmlexpr.ParseExpToXML Class Reference

Collaboration diagram for xmlexpr.ParseExpToXML:

Collaboration graph
[legend]
List of all members.

Detailed Description

ParseExpToXML.

The syntax for assignments and expressions in the input string is:

        statement: assignment | addExp

        assignment: ident "=" addExp

        addExp: term | term addOp addExp

        term: unaryExpr | unaryExpr mulOp term

        unaryExpr: factor | minus factor

        factor: ident | number | "(" addExp ")"

Author:
Ian Kaplan, iank@bearcave.com, Jul 5, 2004


Public Member Functions

String parse (String exp, String topTag, String prefix, String nameSpace, String schemaLoc) throws ExpParseException

Private Member Functions

Element createElement (String xmlTag)
 Create an DOM Element object.
String documentToString (Document doc)
Node factor () throws ExpParseException
 factor: ident | number | "(" expression ")"
Exceptions:
ExpParseException 

Node unaryExpr () throws ExpParseException
 unaryExpr: factor | minus factor
Node term () throws ExpParseException
 term: unaryExpr | unaryExpr addOp term
Exceptions:
ExpParseException 

Node addExp () throws ExpParseException
 addExp: term | term addOp addExp
Exceptions:
ExpParseException 

Node statement () throws ExpParseException
 statement: assign | expression assign: ident "=" expression;
Exceptions:
ExpParseException 


Private Attributes

Document mDoc = null
Scanner mScan = null
String mNameSpace = null
String mPrefix = null


Member Function Documentation

Node xmlexpr.ParseExpToXML.addExp  )  throws ExpParseException [private]
 

addExp: term | term addOp addExp

Exceptions:
ExpParseException 

00209 { 00210 Node exp = null; 00211 Node t = term(); 00212 Token op = mScan.getToken(); 00213 if (op.getType() == TokenType.MINUS || op.getType() == TokenType.PLUS) { 00214 Node rhs = addExp(); 00215 Node addOp = createElement( op.getType().getString() ); 00216 addOp.appendChild( t ); 00217 addOp.appendChild( rhs ); 00218 exp = addOp; 00219 } 00220 else { 00221 exp = t; 00222 if (op.getType() != TokenType.EOL) { 00223 mScan.pushToken( op ); 00224 } 00225 } 00226 return exp; 00227 } // addExp

Element xmlexpr.ParseExpToXML.createElement String  xmlTag  )  [private]
 

Create an DOM Element object.

If mNameSpace is set then create the Element with a name space, otherwise, create an unqualified Element.

00063 { 00064 Element elem = null; 00065 if (mNameSpace == null) { 00066 elem = mDoc.createElement( xmlTag ); 00067 } 00068 else { 00069 if (mPrefix != null) { 00070 xmlTag = mPrefix + ":" + xmlTag; 00071 } 00072 elem = mDoc.createElementNS( mNameSpace, xmlTag ); 00073 } 00074 return elem; 00075 } // createElement

String xmlexpr.ParseExpToXML.documentToString Document  doc  )  [private]
 

00084 { 00085 StringWriter writer = new StringWriter(); 00086 00087 OutputFormat out = new OutputFormat(); 00088 out.setOmitXMLDeclaration(true); 00089 out.setIndenting( true ); 00090 out.setIndent( 4 ); 00091 out.setLineSeparator(System.getProperty("line.separator")); 00092 out.setLineWidth(Integer.MAX_VALUE); 00093 00094 XMLSerializer serializer = new XMLSerializer(writer, out); 00095 try { 00096 Element rootElement = doc.getDocumentElement(); 00097 serializer.serialize(rootElement); 00098 } 00099 catch (IOException e) { 00100 System.out.println("ParseExpToXML::documentToString: IOException = " + e ); 00101 } 00102 return writer.toString(); 00103 } // documentToString

Node xmlexpr.ParseExpToXML.factor  )  throws ExpParseException [private]
 

factor: ident | number | "(" expression ")"

Exceptions:
ExpParseException 

00111 { 00112 final String msg = "identifier, number or ( exp ) expected"; 00113 Node fact = null; 00114 Token f = mScan.getToken(); 00115 String errMsg = null; 00116 if (f != null) { 00117 if (f.getType() == TokenType.IDENT || f.getType() == TokenType.INT) { 00118 if (f.getType() == TokenType.INT) { 00119 fact = createElement( TokenType.INT.getString() ); 00120 } 00121 else { 00122 fact = createElement( TokenType.IDENT.getString() ); 00123 } 00124 Text txt = mDoc.createTextNode( f.getString() ); 00125 fact.appendChild( txt ); 00126 } 00127 else if (f.getType() == TokenType.LPAREN) { 00128 fact = addExp(); 00129 f = mScan.getToken(); 00130 if (f == null || f.getType() != TokenType.RPAREN) { 00131 errMsg = "\")\" expected"; 00132 } 00133 else { 00134 Node top = createElement( TokenType.PAREN.getString() ); 00135 top.appendChild( fact ); 00136 fact = top; 00137 } 00138 } 00139 else { 00140 errMsg = msg; 00141 } 00142 } 00143 else { 00144 errMsg = msg; 00145 } 00146 00147 if (errMsg != null) { 00148 throw new ExpParseException( errMsg ); 00149 } 00150 return fact; 00151 } // factor

String xmlexpr.ParseExpToXML.parse String  exp,
String  topTag,
String  prefix,
String  nameSpace,
String  schemaLoc
throws ExpParseException
 

00277 { 00278 mDoc = DocumentFactory.newDocument( topTag, 00279 prefix, 00280 nameSpace, 00281 schemaLoc ); 00282 mPrefix = prefix; 00283 mNameSpace = nameSpace; 00284 mScan = new Scanner( exp ); 00285 Node root = (Node)mDoc.getDocumentElement(); 00286 Node child = statement(); 00287 if (child != null) { 00288 root.appendChild( child ); 00289 } 00290 String xml = documentToString( mDoc ); 00291 return xml; 00292 } // parse

Node xmlexpr.ParseExpToXML.statement  )  throws ExpParseException [private]
 

statement: assign | expression assign: ident "=" expression;

Exceptions:
ExpParseException 

00237 { 00238 String errMsg = null; 00239 Node stmt = null; 00240 Node lhs = addExp(); 00241 Token t = mScan.getToken(); 00242 if (mScan.isEOL()) { 00243 stmt = lhs; 00244 } 00245 else if (t.getType() == TokenType.EQUAL) { 00246 if (lhs.getLocalName().equals(TokenType.IDENT.getString())) { 00247 Node rhs = addExp(); 00248 Node equals = (Node)createElement( t.getType().getString() ); 00249 equals.appendChild( lhs ); 00250 equals.appendChild( rhs ); 00251 stmt = equals; 00252 t = mScan.getToken(); 00253 if (t.getType() != TokenType.EOL) { 00254 errMsg = "Syntax error: badly formed expression"; 00255 } 00256 } 00257 else { 00258 errMsg = "LHS identifier expected"; 00259 } 00260 } 00261 else { 00262 errMsg = "\"=\" expected"; 00263 } 00264 if (errMsg != null) { 00265 throw new ExpParseException( errMsg ); 00266 } 00267 return stmt; 00268 } // statement

Node xmlexpr.ParseExpToXML.term  )  throws ExpParseException [private]
 

term: unaryExpr | unaryExpr addOp term

Exceptions:
ExpParseException 

00181 { 00182 Node exp = null; 00183 Node t = unaryExpr(); // either unaryExp or the LHS of the sub-expression 00184 Token op = mScan.getToken(); 00185 if (op.getType() == TokenType.TIMES || 00186 op.getType() == TokenType.DIV || 00187 op.getType() == TokenType.MOD) { 00188 Node rhs = term(); 00189 Node mulOp = createElement( op.getType().getString() ); 00190 mulOp.appendChild( t ); 00191 mulOp.appendChild( rhs ); 00192 exp = mulOp; 00193 } 00194 else { 00195 exp = t; 00196 if (op.getType() != TokenType.EOL) { 00197 mScan.pushToken( op ); 00198 } 00199 } 00200 return exp; 00201 } // term

Node xmlexpr.ParseExpToXML.unaryExpr  )  throws ExpParseException [private]
 

unaryExpr: factor | minus factor

00158 { 00159 boolean unaryMinus = false; 00160 Token t = mScan.getToken(); 00161 if (t.getType() == TokenType.MINUS) { 00162 unaryMinus = true; 00163 } 00164 else { 00165 mScan.pushToken( t ); 00166 } 00167 Node top = factor(); 00168 if (unaryMinus) { 00169 Node minus = createElement( TokenType.UMINUS.getString() ); 00170 minus.appendChild( top ); 00171 top = minus; 00172 } 00173 return top; 00174 } // unaryExpr


Member Data Documentation

Document xmlexpr.ParseExpToXML.mDoc = null [private]
 

String xmlexpr.ParseExpToXML.mNameSpace = null [private]
 

String xmlexpr.ParseExpToXML.mPrefix = null [private]
 

Scanner xmlexpr.ParseExpToXML.mScan = null [private]
 


The documentation for this class was generated from the following file:
Generated on Sat Aug 28 13:09:43 2004 for XmlExpr by doxygen 1.3.8