001: /* Generated by Together */
002:
003: package com.rimfaxe.xml.xmlreader.xpath;
004:
005: import java.io.*;
006:
007: /**
008: * A utility that parses a stream of tokens and creates the
009: * appropriate sub-class of BooleanExpr.
010:
011: <blockquote><small> Copyright (C) 2002 Hewlett-Packard Company.
012: This file is part of Sparta, an XML Parser, DOM, and XPath library.
013: This library is free software; you can redistribute it and/or
014: modify it under the terms of the <a href="doc-files/LGPL.txt">GNU
015: Lesser General Public License</a> as published by the Free Software
016: Foundation; either version 2.1 of the License, or (at your option)
017: any later version. This library is distributed in the hope that it
018: will be useful, but WITHOUT ANY WARRANTY; without even the implied
019: warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
020: PURPOSE. </small></blockquote>
021: @version $Date: 2003/01/09 01:12:37 $ $Revision: 1.6 $
022: @author Eamonn O'Brien-Strain
023: * @stereotype factory */
024: public class ExprFactory {
025:
026: /** @precondition current token is first token in expr production.
027: @postcondition current token is first token after expr production */
028: static BooleanExpr createExpr(XPath xpath, StreamTokenizer toks)
029: throws XPathException, IOException {
030: switch (toks.ttype) {
031:
032: default:
033: throw new XPathException(xpath,
034: "at beginning of expression", toks,
035: "@, number, or text()");
036:
037: case StreamTokenizer.TT_NUMBER:
038: int position = (int) toks.nval;
039: toks.nextToken();
040: return new PositionEqualsExpr(position);
041:
042: case '@':
043:
044: if (toks.nextToken() != StreamTokenizer.TT_WORD)
045: throw new XPathException(xpath, "after @", toks, "name");
046: String name = toks.sval;
047: String value;
048: double valueN;
049: switch (toks.nextToken()) {
050: case '=':
051: toks.nextToken();
052: if (toks.ttype != '\"' && toks.ttype != '\'')
053: throw new XPathException(xpath,
054: "right hand side of equals", toks,
055: "quoted string");
056: value = toks.sval;
057: toks.nextToken();
058: return new AttrEqualsExpr(name, value);
059: case '<':
060: toks.nextToken();
061: if (toks.ttype == '\"' || toks.ttype == '\'') {
062: // use jdk1.1 API to make the code work with PersonalJava
063: // valueN = Double.parseDouble(toks.sval);
064: valueN = Double.valueOf(toks.sval).doubleValue();
065: } else if (toks.ttype == toks.TT_NUMBER)
066: valueN = toks.nval;
067: else
068: throw new XPathException(xpath,
069: "right hand side of less-than", toks,
070: "quoted string or number");
071: toks.nextToken();
072: return new AttrLessExpr(name, valueN);
073: case '>':
074: toks.nextToken();
075: if (toks.ttype == '\"' || toks.ttype == '\'') {
076: // use jdk1.1 API to make the code work with PersonalJava
077: // valueN = Double.parseDouble(toks.sval);
078: valueN = Double.valueOf(toks.sval).doubleValue();
079: } else if (toks.ttype == toks.TT_NUMBER)
080: valueN = toks.nval;
081: else
082: throw new XPathException(xpath,
083: "right hand side of greater-than", toks,
084: "quoted string or number");
085: toks.nextToken();
086: return new AttrGreaterExpr(name, valueN);
087: case '!':
088: toks.nextToken();
089: if (toks.ttype != '=')
090: throw new XPathException(xpath, "after !", toks,
091: "=");
092: toks.nextToken();
093: if (toks.ttype != '\"' && toks.ttype != '\'')
094: throw new XPathException(xpath,
095: "right hand side of !=", toks,
096: "quoted string");
097: value = toks.sval;
098: toks.nextToken();
099: return new AttrNotEqualsExpr(name, value);
100: default:
101: return new AttrExistsExpr(name);
102: }
103: case StreamTokenizer.TT_WORD:
104: if (!toks.sval.equals("text"))
105: throw new XPathException(xpath,
106: "at beginning of expression", toks, "text()");
107:
108: if (toks.nextToken() != '(')
109: throw new XPathException(xpath, "after text", toks, "(");
110: if (toks.nextToken() != ')')
111: throw new XPathException(xpath, "after text(", toks,
112: ")");
113: String tValue;
114: switch (toks.nextToken()) {
115: case '=':
116: toks.nextToken();
117: if (toks.ttype != '\"' && toks.ttype != '\'')
118: throw new XPathException(xpath,
119: "right hand side of equals", toks,
120: "quoted string");
121: tValue = toks.sval;
122: toks.nextToken();
123: return new TextEqualsExpr(tValue);
124: case '!':
125: toks.nextToken();
126: if (toks.ttype != '=')
127: throw new XPathException(xpath, "after !", toks,
128: "=");
129: toks.nextToken();
130: if (toks.ttype != '\"' && toks.ttype != '\'')
131: throw new XPathException(xpath,
132: "right hand side of !=", toks,
133: "quoted string");
134: tValue = toks.sval;
135: toks.nextToken();
136: return new TextNotEqualsExpr(tValue);
137: default:
138: return TextExistsExpr.INSTANCE;
139: }
140: }
141: }
142:
143: }
144:
145: // $Log: ExprFactory.java,v $
146: // Revision 1.6 2003/01/09 01:12:37 yuhongx
147: // Use JDK1.1 API to make the code work with PersonalJava.
148: //
149: // Revision 1.5 2002/12/06 23:37:37 eobrain
150: // Make objects that are always the same follow the Flyweight Pattern.
151: //
152: // Revision 1.4 2002/12/05 04:34:38 eobrain
153: // Add support for greater than and less than relational expressions in predicates.
154: //
155: // Revision 1.3 2002/10/30 16:30:16 eobrain
156: // Feature request [ 630127 ] Support /a/b[text()='foo']
157: // http://sourceforge.net/projects/sparta-xml/
158: //
159: // Revision 1.2 2002/09/18 05:30:21 eobrain
160: // Support xpath predicates of the form [1], [2], ...
161: //
162: // Revision 1.1.1.1 2002/08/19 05:04:04 eobrain
163: // import from HP Labs internal CVS
164: //
165: // Revision 1.3 2002/08/18 23:38:41 eob
166: // Add copyright and other formatting and commenting in preparation for
167: // release to SourceForge.
168: //
169: // Revision 1.2 2002/05/23 21:12:10 eob
170: // Better error reporting.
171: //
172: // Revision 1.1 2002/02/01 02:01:08 eob
173: // initial
|