001: /* Generated by Together */
002:
003: package com.rimfaxe.xml.xmlreader.xpath;
004:
005: import java.io.*;
006:
007: /**
008: * One of the steps which, separated by slashes, make up an XPath
009: * expression.
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: 2002/12/06 23:39:07 $ $Revision: 1.2 $
022: @author Eamonn O'Brien-Strain
023: */
024: public class Step {
025:
026: public static Step DOT = new Step(ThisNodeTest.INSTANCE,
027: TrueExpr.INSTANCE);
028:
029: Step(NodeTest nodeTest, BooleanExpr predicate) {
030: nodeTest_ = nodeTest;
031: predicate_ = predicate;
032: multiLevel_ = false;
033: }
034:
035: /**
036: @precondition current token is 1st token in the node_test production.
037: @postcondition current tok is tok after last tok of step production */
038: Step(XPath xpath, boolean multiLevel, StreamTokenizer toks)
039: throws XPathException, IOException {
040: multiLevel_ = multiLevel;
041:
042: switch (toks.ttype) {
043: case '.':
044: if (toks.nextToken() == '.')
045: nodeTest_ = ParentNodeTest.INSTANCE;
046: else {
047: toks.pushBack();
048: nodeTest_ = ThisNodeTest.INSTANCE;
049: }
050: break;
051: case '*':
052: nodeTest_ = AllElementTest.INSTANCE;
053: break;
054: case '@':
055: if (toks.nextToken() != StreamTokenizer.TT_WORD)
056: throw new XPathException(xpath, "after @ in node test",
057: toks, "name");
058: nodeTest_ = new AttrTest(toks.sval);
059: break;
060: case StreamTokenizer.TT_WORD:
061: if (toks.sval.equals("text")) {
062: if (toks.nextToken() != '(' || toks.nextToken() != ')')
063: throw new XPathException(xpath, "after text", toks,
064: "()");
065: nodeTest_ = TextTest.INSTANCE;
066: } else
067: nodeTest_ = new ElementTest(toks.sval);
068: break;
069: default:
070: throw new XPathException(xpath, "at begininning of step",
071: toks, "'.' or '*' or name");
072: }
073: if (toks.nextToken() == '[') {
074: toks.nextToken();
075: //current token is first token in expr production
076: predicate_ = ExprFactory.createExpr(xpath, toks);
077: //current token is 1st token after expr production
078: if (toks.ttype != ']')
079: throw new XPathException(xpath,
080: "after predicate expression", toks, "]");
081: toks.nextToken();
082: } else
083: predicate_ = TrueExpr.INSTANCE;
084: //current token is token after step production
085: }
086:
087: public String toString() {
088: return nodeTest_.toString() + predicate_.toString();
089: }
090:
091: /** Is this step preceeded by a '//' ? */
092: public boolean isMultiLevel() {
093: return multiLevel_;
094: }
095:
096: /** Does this step evaluate to a string values (attribute values
097: or text() nodes)*/
098: public boolean isStringValue() {
099: return nodeTest_.isStringValue();
100: }
101:
102: public NodeTest getNodeTest() {
103: return nodeTest_;
104: }
105:
106: public BooleanExpr getPredicate() {
107: return predicate_;
108: }
109:
110: /**
111: * @link aggregationByValue
112: * @directed
113: */
114: private final NodeTest nodeTest_;
115:
116: /**
117: * @link aggregationByValue
118: * @directed
119: * @label predicate
120: */
121: private final BooleanExpr predicate_;
122:
123: private final boolean multiLevel_;
124: }
125:
126: // $Log: Step.java,v $
127: // Revision 1.2 2002/12/06 23:39:07 eobrain
128: // Make objects that are always the same follow the Flyweight Pattern.
129: //
130: // Revision 1.1.1.1 2002/08/19 05:04:04 eobrain
131: // import from HP Labs internal CVS
132: //
133: // Revision 1.6 2002/08/18 23:38:59 eob
134: // Add copyright and other formatting and commenting in preparation for
135: // release to SourceForge.
136: //
137: // Revision 1.5 2002/06/14 19:41:29 eob
138: // Add handling of "text()" in XPath expressions.
139: //
140: // Revision 1.4 2002/06/04 05:28:00 eob
141: // Simplify use of visitor pattern to make code easier to understand.
142: //
143: // Revision 1.3 2002/05/23 21:13:43 eob
144: // Better error reporting.
145: //
146: // Revision 1.2 2002/02/04 22:12:14 eob
147: // Add handling of nodetest for attribute.
148: //
149: // Revision 1.1 2002/02/01 02:04:23 eob
150: // initial
|