1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri.compiler;
18
19 import org.apache.commons.jxpath.NodeSet;
20 import org.apache.commons.jxpath.Pointer;
21 import org.apache.commons.jxpath.ri.EvalContext;
22 import org.apache.commons.jxpath.ri.model.NodePointer;
23 import org.apache.commons.jxpath.ri.QName;
24 import org.apache.commons.jxpath.util.ValueUtils;
25
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.Locale;
29
30
31
32
33
34
35
36
37
38
39
40 public abstract class Expression {
41
42
43 protected static final Double ZERO = new Double(0);
44
45
46 protected static final Double ONE = new Double(1);
47
48
49 protected static final Double NOT_A_NUMBER = new Double(Double.NaN);
50
51 private boolean contextDependencyKnown = false;
52 private boolean contextDependent;
53
54
55
56
57
58
59 public synchronized boolean isContextDependent() {
60 if (!contextDependencyKnown) {
61 contextDependent = computeContextDependent();
62 contextDependencyKnown = true;
63 }
64 return contextDependent;
65 }
66
67
68
69
70
71 public abstract boolean computeContextDependent();
72
73
74
75
76
77
78
79 public abstract Object computeValue(EvalContext context);
80
81
82
83
84
85
86
87 public abstract Object compute(EvalContext context);
88
89
90
91
92
93
94 public Iterator iterate(EvalContext context) {
95 Object result = compute(context);
96 if (result instanceof EvalContext) {
97 return new ValueIterator((EvalContext) result);
98 }
99 if (result instanceof NodeSet) {
100 return new ValueIterator(((NodeSet) result).getPointers().iterator());
101 }
102 return ValueUtils.iterate(result);
103 }
104
105
106
107
108
109
110 public Iterator iteratePointers(EvalContext context) {
111 Object result = compute(context);
112 if (result == null) {
113 return Collections.EMPTY_LIST.iterator();
114 }
115 if (result instanceof EvalContext) {
116 return (EvalContext) result;
117 }
118 if (result instanceof NodeSet) {
119 return new PointerIterator(((NodeSet) result).getPointers().iterator(),
120 new QName(null, "value"),
121 context.getRootContext().getCurrentNodePointer().getLocale());
122 }
123 return new PointerIterator(ValueUtils.iterate(result),
124 new QName(null, "value"),
125 context.getRootContext().getCurrentNodePointer().getLocale());
126 }
127
128
129
130
131 public static class PointerIterator implements Iterator {
132 private Iterator iterator;
133 private QName qname;
134 private Locale locale;
135
136
137
138
139
140
141
142
143
144 public PointerIterator(Iterator it, QName qname, Locale locale) {
145 this.iterator = it;
146 this.qname = qname;
147 this.locale = locale;
148 }
149
150 public boolean hasNext() {
151 return iterator.hasNext();
152 }
153
154 public Object next() {
155 Object o = iterator.next();
156 return o instanceof Pointer ? o : NodePointer.newNodePointer(qname, o, locale);
157 }
158
159
160
161
162 public void remove() {
163 throw new UnsupportedOperationException();
164 }
165 }
166
167
168
169
170 public static class ValueIterator implements Iterator {
171 private Iterator iterator;
172
173
174
175
176
177 public ValueIterator(Iterator it) {
178 this.iterator = it;
179 }
180
181 public boolean hasNext() {
182 return iterator.hasNext();
183 }
184
185 public Object next() {
186 Object o = iterator.next();
187 return o instanceof Pointer ? ((Pointer) o).getValue() : o;
188 }
189
190
191
192
193 public void remove() {
194 throw new UnsupportedOperationException();
195 }
196 }
197 }