KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.om.Axis;
4 import com.icl.saxon.om.NodeInfo;
5 import com.icl.saxon.om.NodeEnumeration;
6 import com.icl.saxon.om.AxisEnumeration;
7 import com.icl.saxon.pattern.NodeTest;
8 import com.icl.saxon.sort.LocalOrderComparer;
9
10 /**
11 * An AxisExpression is always obtained by simplifying a PathExpression.
12 * It represents a PathExpression that starts at the context node, and uses
13 * a simple node-test with no filters. For example "*", "title", "./item",
14 * "@*", or "ancestor::chapter*".
15 */

16
17 final class AxisExpression extends NodeSetExpression {
18
19     private byte axis;
20     private NodeTest test;
21     private NodeInfo contextNode = null;
22
23     /**
24     * Constructor
25     * @param start A node-set expression denoting the absolute or relative set of nodes from which the
26     * navigation path should start.
27     * @param step The step to be followed from each node in the start expression to yield a new
28     * node-set
29     */

30
31     public AxisExpression(byte axis, NodeTest nodeTest) {
32         this.axis = axis;
33         this.test = nodeTest;
34     }
35
36     /**
37     * Simplify an expression
38     * @return the simplified expression
39     */

40
41     public Expression simplify() {
42         return this;
43     }
44
45     /**
46     * Determine which aspects of the context the expression depends on. The result is
47     * a bitwise-or'ed value composed from constants such as Context.VARIABLES and
48     * Context.CURRENT_NODE
49     */

50
51     public int getDependencies() {
52         if (contextNode==null) {
53             return Context.CONTEXT_NODE;
54         } else {
55             return 0;
56         }
57     }
58
59     /**
60     * Determine, in the case of an expression whose data type is Value.NODESET,
61     * whether all the nodes in the node-set are guaranteed to come from the same
62     * document as the context node. Used for optimization.
63     */

64     
65     public boolean isContextDocumentNodeSet() {
66         return true;
67     }
68
69     /**
70     * Perform a partial evaluation of the expression, by eliminating specified dependencies
71     * on the context.
72     * @param dep The dependencies to be removed
73     * @param context The context to be used for the partial evaluation
74     * @return a new expression that does not have any of the specified
75     * dependencies
76     */

77
78     public Expression reduce(int dep, Context context) throws XPathException {
79         if (contextNode == null && (dep & Context.CONTEXT_NODE) != 0) {
80             AxisExpression exp2 = new AxisExpression(axis, test);
81             exp2.contextNode = context.getContextNodeInfo();
82             return exp2;
83         } else {
84             return this;
85         }
86     }
87
88     /**
89     * Evaluate the path-expression in a given context to return a NodeSet
90     * @param context the evaluation context
91     * @param sort true if the returned nodes must be in document order
92     */

93
94     public NodeEnumeration enumerate(Context context, boolean sort) throws XPathException {
95         NodeInfo start;
96         if (contextNode==null) {
97             start = context.getContextNodeInfo();
98         } else {
99             start = contextNode;
100         }
101         AxisEnumeration enum = start.getEnumeration(axis, test);
102         if (sort && !enum.isSorted()) {
103             NodeSetExtent ns = new NodeSetExtent(enum, LocalOrderComparer.getInstance());
104             ns.sort();
105             return ns.enumerate();
106         }
107         return enum;
108     }
109
110     /**
111     * Evaluate the expression
112     * (typically used if the result is to be stored in a variable)
113     */

114
115     public Value evaluate(Context context) throws XPathException {
116         NodeSetIntent nsi = new NodeSetIntent(
117                     (NodeSetExpression)reduce(Context.CONTEXT_NODE, context),
118                     context.getController());
119         nsi.setSorted(Axis.isForwards[axis]);
120         return nsi;
121     }
122     
123     /**
124     * Diagnostic print of expression structure
125     */

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