KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.Context;
3
4
5 /**
6 * PositionRange: a boolean expression that tests whether the position() is
7 * within a certain range. This expression can occur in any context but it is
8 * optimized when it appears as a predicate (see FilterEnumerator)
9 */

10
11 class PositionRange extends Expression {
12
13     private int minPosition;
14     private int maxPosition;
15
16     /**
17     * Create a position range
18     */

19
20     public PositionRange(int min, int max) {
21         minPosition = min;
22         maxPosition = max;
23     }
24
25     /**
26     * Simplify an expression
27     * @return the simplified expression
28     */

29
30     public Expression simplify() throws XPathException {
31         return this;
32     }
33
34     /**
35     * Evaluate the expression in a given context
36     * @param c the given context for evaluation
37     * @return a BooleanValue representing the result of the numeric comparison of the two operands
38     */

39
40     public Value evaluate(Context c) throws XPathException {
41         return new BooleanValue(evaluateAsBoolean(c));
42     }
43
44     /**
45     * Evaluate the expression in a given context
46     * @param c the given context for evaluation
47     * @return a boolean representing the result of the numeric comparison of the two operands
48     */

49
50     public boolean evaluateAsBoolean(Context c) throws XPathException {
51         int p = c.getContextPosition();
52         return p >= minPosition && p <= maxPosition;
53     }
54
55     /**
56     * Determine the data type of the expression
57     * @return Value.BOOLEAN
58     */

59
60     public int getDataType() {
61         return Value.BOOLEAN;
62     }
63
64     /**
65     * Get the dependencies
66     */

67     
68     public int getDependencies() {
69         return Context.POSITION;
70     }
71     
72     /**
73     * Perform a partial evaluation of the expression, by eliminating specified dependencies
74     * on the context.
75     * @param dependencies The dependencies to be removed
76     * @param context The context to be used for the partial evaluation
77     * @return a new expression that does not have any of the specified
78     * dependencies
79     */

80
81     public Expression reduce(int dependencies, Context context) throws XPathException {
82
83         if ((Context.POSITION & dependencies) != 0 ) {
84             return evaluate(context);
85         }
86         return this;
87     }
88
89     /**
90     * Get the minimum position
91     */

92     
93     protected int getMinPosition() {
94         return minPosition;
95     }
96
97     /**
98     * Get the maximum position
99     */

100     
101     protected int getMaxPosition() {
102         return maxPosition;
103     }
104
105     /**
106     * Diagnostic print of expression structure
107     */

108     
109     public void display(int level) {
110         System.err.println(indent(level) + "positionRange(" + minPosition + "," + maxPosition + ")");
111     }
112 }
113
114 //
115
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
116
// you may not use this file except in compliance with the License. You may obtain a copy of the
117
// License at http://www.mozilla.org/MPL/
118
//
119
// Software distributed under the License is distributed on an "AS IS" basis,
120
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
121
// See the License for the specific language governing rights and limitations under the License.
122
//
123
// The Original Code is: all this file.
124
//
125
// The Initial Developer of the Original Code is
126
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
127
//
128
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
129
//
130
// Contributor(s): none.
131
//
132
Popular Tags