KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > expr > Step


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.*;
4 import com.icl.saxon.functions.*;
5 import com.icl.saxon.pattern.NodeTest;
6
7 import java.util.*;
8
9 /**
10 * A step in a path expression
11 */

12
13 public final class Step {
14
15     private byte axis;
16     private NodeTest test;
17
18     private Expression[] filters = new Expression[3]; // list of filter expressions to apply
19
private int numberOfFilters = 0;
20
21     public Step(byte axis, NodeTest nodeTest) {
22         this.axis = axis;
23         this.test = nodeTest;
24     }
25
26     public Step addFilter(Expression exp) {
27         if (numberOfFilters == filters.length) {
28             Expression[] f2 = new Expression[numberOfFilters * 2];
29             System.arraycopy(filters, 0, f2, 0, numberOfFilters);
30             filters = f2;
31         }
32         filters[numberOfFilters++] = exp;
33         return this;
34     }
35
36     public void setFilters(Expression[] filters, int count) {
37         this.filters = filters;
38         this.numberOfFilters = count;
39     }
40
41     public byte getAxis() {
42         return axis;
43     }
44
45     public NodeTest getNodeTest() {
46         return test;
47     }
48
49     public Expression[] getFilters() {
50         return filters;
51     }
52     
53     public int getNumberOfFilters() {
54         return numberOfFilters;
55     }
56
57     /**
58     * Simplify the step. Return either the same step after simplification, or null,
59     * indicating that the step will always give an empty result.
60     */

61
62     public Step simplify() throws XPathException {
63         
64         for (int i=numberOfFilters-1; i>=0; i--) {
65             Expression exp = filters[i].simplify();
66             filters[i] = exp;
67             
68             // look for a filter that is constant true or false (which can arise after
69
// an expression is reduced).
70

71             if (exp instanceof Value && !(exp instanceof NumericValue)) {
72                 if (((Value)exp).asBoolean()) { // filter is constant true
73
// only bother removing it if it's the last
74
if (i==numberOfFilters-1) {
75                         numberOfFilters--;
76                     }
77                 } else { // filter is constant false,
78
// so the wbole path-expression is empty
79
return null;
80                 }
81             }
82             
83             // look for the filter [last()]
84

85             if (exp instanceof Last) {
86                 filters[i] = new IsLastExpression(true);
87             }
88             
89         }
90         return this;
91     }
92
93     /**
94     * Enumerate this step.
95     * @param node: The node from which we want to make the step
96     * @param context: The context for evaluation. Affects the result of positional
97     * filters
98     * @return: an enumeration of nodes that result from applying this step
99     */

100
101     public NodeEnumeration enumerate(NodeInfo node, Context context)
102         throws XPathException {
103
104         NodeEnumeration enum = node.getEnumeration(axis, test);
105         if (enum.hasMoreElements()) { // if there are no nodes, there's nothing to filter
106
for (int i=0; i<numberOfFilters; i++) {
107                 enum = new FilterEnumerator(enum, filters[i],
108                                              context, false);
109             }
110         }
111         return enum;
112                                
113     }
114
115     /**
116     * Diagnostic print of expression structure
117     */

118     
119     public void display(int level) {
120         System.err.println(Expression.indent(level) + "Step " + Axis.axisName[axis] + "::" + test.toString() +
121             (numberOfFilters > 0 ? " [" : ""));
122         for (int f=0; f<numberOfFilters; f++) {
123             filters[f].display(level+1);
124         }
125     }
126         
127         
128 }
129
130
131
132 //
133
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
134
// you may not use this file except in compliance with the License. You may obtain a copy of the
135
// License at http://www.mozilla.org/MPL/
136
//
137
// Software distributed under the License is distributed on an "AS IS" basis,
138
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
139
// See the License for the specific language governing rights and limitations under the License.
140
//
141
// The Original Code is: all this file.
142
//
143
// The Initial Developer of the Original Code is
144
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
145
//
146
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
147
//
148
// Contributor(s): none.
149
//
150
Popular Tags