1 /*
2 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
3 */
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 /*
21 * $Id: KeyIndex.java,v 1.6 2006/06/19 19:49:02 spericas Exp $
22 */
98 }
99
100 IntegerArray nodes = _index.get(value);
101
102 if (nodes == null) {
103 nodes = new IntegerArray();
104 _index.put(value, nodes);
105 nodes.add(node);
106
107 // Because nodes are added in document order,
108 // duplicates can be eliminated easily at this stage.
109 } else if (node != nodes.at(nodes.cardinality() - 1)) {
110 nodes.add(node);
111 }
112 }
113
114 /**
115 * Merge the current value's nodeset set by lookupKey() with _nodes.
116 * @deprecated
117 */
118 public void merge(KeyIndex other) {
119 if (other == null) return;
120
121 if (other._nodes != null) {
122 if (_nodes == null) {
123 _nodes = (IntegerArray)other._nodes.clone();
124 }
125 else {
126 _nodes.merge(other._nodes);
127 }
128 }
129 }
130
131 /**
132 * This method must be called by the code generated by the id() function
133 * prior to returning the node iterator. The lookup code for key() and
134 * id() differ in the way the lookup value can be whitespace separated
135 * list of tokens for the id() function, but a single string for the
136 * key() function.
137 * @deprecated
138 */
139 public void lookupId(Object value) {
140 // Clear _nodes array
141 _nodes = null;
142
143 final StringTokenizer values = new StringTokenizer((String) value,
144 " \n\t");
145 while (values.hasMoreElements()) {
146 final String token = (String) values.nextElement();
147 IntegerArray nodes = _index.get(token);
148
149 if (nodes == null && _enhancedDOM != null
150 && _enhancedDOM.hasDOMSource()) {
151 nodes = getDOMNodeById(token);
152 }
153
154 if (nodes == null) continue;
155
156 if (_nodes == null) {
157 nodes = (IntegerArray)nodes.clone();
158 _nodes = nodes;
188
189 if (nodes == null) {
190 nodes = new IntegerArray();
191 index.put(id, nodes);
192 }
193
194 nodes.add(_enhancedDOM.getNodeHandle(ident));
195 }
196 }
197
198 return nodes;
199 }
200
201 /**
202 * <p>This method must be called by the code generated by the key() function
203 * prior to returning the node iterator.</p>
204 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
205 * <b>deprecated.</b></em></p>
206 * @deprecated
207 */
208 public void lookupKey(Object value) {
209 IntegerArray nodes = _index.get(value);
210 _nodes = (nodes != null) ? (IntegerArray) nodes.clone() : null;
211 _position = 0;
212 }
213
214 /**
215 * <p>Callers should not call next() after it returns END.</p>
216 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
217 * <b>deprecated.</b></em></p>
218 * @deprecated
219 */
220 public int next() {
221 if (_nodes == null) return DTMAxisIterator.END;
222
223 return (_position < _nodes.cardinality()) ?
224 _dom.getNodeHandle(_nodes.at(_position++)) : DTMAxisIterator.END;
225 }
226
227 /**
228 * Given a context node and the argument to the XPath <code>id</code>
229 * function, checks whether the context node is in the set of nodes that
230 * results from that reference to the <code>id</code> function. This is
231 * used in the implementation of <code>id</code> patterns.
232 *
233 * @param node The context node
234 * @param value The argument to the <code>id</code> function
235 * @return <code>1</code> if the context node is in the set of nodes
236 * returned by the reference to the <code>id</code> function;
237 * <code>0</code>, otherwise
238 */
239 public int containsID(int node, Object value) {
296 Map<String,IntegerArray> index =
297 _rootToIndexMap.get(new Integer(rootHandle));
298
299 // Check whether the context node is present in the set of nodes
300 // returned by the key function
301 if (index != null) {
302 final IntegerArray nodes = index.get(value);
303 return (nodes != null && nodes.indexOf(node) >= 0) ? 1 : 0;
304 }
305
306 // The particular key name identifies no nodes in this document
307 return 0;
308 }
309
310 /**
311 * <p>Resets the iterator to the last start node.</p>
312 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
313 * <b>deprecated.</b></em></p>
314 * @deprecated
315 */
316 public DTMAxisIterator reset() {
317 _position = 0;
318 return this;
319 }
320
321 /**
322 * <p>Returns the number of elements in this iterator.</p>
323 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
324 * <b>deprecated.</b></em></p>
325 * @deprecated
326 */
327 public int getLast() {
328 return (_nodes == null) ? 0 : _nodes.cardinality();
329 }
330
331 /**
332 * <p>Returns the position of the current node in the set.</p>
333 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
334 * <b>deprecated.</b></em></p>
335 * @deprecated
336 */
337 public int getPosition() {
338 return _position;
339 }
340
341 /**
342 * <p>Remembers the current node for the next call to gotoMark().</p>
343 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
344 * <b>deprecated.</b></em></p>
345 * @deprecated
346 */
347 public void setMark() {
348 _markedPosition = _position;
349 }
350
351 /**
352 * <p>Restores the current node remembered by setMark().</p>
353 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
354 * <b>deprecated.</b></em></p>
355 * @deprecated
356 */
357 public void gotoMark() {
358 _position = _markedPosition;
359 }
360
361 /**
362 * <p>Set start to END should 'close' the iterator,
363 * i.e. subsequent call to next() should return END.</p>
364 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
365 * <b>deprecated.</b></em></p>
366 * @deprecated
367 */
368 public DTMAxisIterator setStartNode(int start) {
369 if (start == DTMAxisIterator.END) {
370 _nodes = null;
371 }
372 else if (_nodes != null) {
373 _position = 0;
374 }
375 return (DTMAxisIterator) this;
376 }
377
378 /**
379 * <p>Get start to END should 'close' the iterator,
380 * i.e. subsequent call to next() should return END.</p>
381 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
382 * <b>deprecated.</b></em></p>
383 * @deprecated
384 */
385 public int getStartNode() {
386 return 0;
387 }
388
389 /**
390 * <p>True if this iterator has a reversed axis.</p>
391 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
392 * <b>deprecated.</b></em></p>
393 * @deprecated
394 */
395 public boolean isReverse() {
396 return(false);
397 }
398
399 /**
400 * <p>Returns a deep copy of this iterator.</p>
401 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
402 * <b>deprecated.</b></em></p>
403 * @deprecated
404 */
405 public DTMAxisIterator cloneIterator() {
406 KeyIndex other = new KeyIndex(0);
407 other._index = _index;
408 other._rootToIndexMap = _rootToIndexMap;
409 other._nodes = _nodes;
410 other._position = _position;
411 return (DTMAxisIterator) other;
412 }
413
414 public void setDom(DOM dom, int node) {
415 _dom = dom;
416
417 // If a MultiDOM, ensure _enhancedDOM is correctly set
418 // so that getElementById() works in lookupNodes below
419 if (dom instanceof MultiDOM) {
420 dom = ((MultiDOM) dom).getDTM(node);
421 }
422
423 if (dom instanceof DOMEnhancedForDTM) {
424 _enhancedDOM = (DOMEnhancedForDTM)dom;
|
1 /*
2 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
3 */
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 /*
21 * $Id: KeyIndex.java,v 1.6 2006/06/19 19:49:02 spericas Exp $
22 */
98 }
99
100 IntegerArray nodes = _index.get(value);
101
102 if (nodes == null) {
103 nodes = new IntegerArray();
104 _index.put(value, nodes);
105 nodes.add(node);
106
107 // Because nodes are added in document order,
108 // duplicates can be eliminated easily at this stage.
109 } else if (node != nodes.at(nodes.cardinality() - 1)) {
110 nodes.add(node);
111 }
112 }
113
114 /**
115 * Merge the current value's nodeset set by lookupKey() with _nodes.
116 * @deprecated
117 */
118 @Deprecated
119 public void merge(KeyIndex other) {
120 if (other == null) return;
121
122 if (other._nodes != null) {
123 if (_nodes == null) {
124 _nodes = (IntegerArray)other._nodes.clone();
125 }
126 else {
127 _nodes.merge(other._nodes);
128 }
129 }
130 }
131
132 /**
133 * This method must be called by the code generated by the id() function
134 * prior to returning the node iterator. The lookup code for key() and
135 * id() differ in the way the lookup value can be whitespace separated
136 * list of tokens for the id() function, but a single string for the
137 * key() function.
138 * @deprecated
139 */
140 @Deprecated
141 public void lookupId(Object value) {
142 // Clear _nodes array
143 _nodes = null;
144
145 final StringTokenizer values = new StringTokenizer((String) value,
146 " \n\t");
147 while (values.hasMoreElements()) {
148 final String token = (String) values.nextElement();
149 IntegerArray nodes = _index.get(token);
150
151 if (nodes == null && _enhancedDOM != null
152 && _enhancedDOM.hasDOMSource()) {
153 nodes = getDOMNodeById(token);
154 }
155
156 if (nodes == null) continue;
157
158 if (_nodes == null) {
159 nodes = (IntegerArray)nodes.clone();
160 _nodes = nodes;
190
191 if (nodes == null) {
192 nodes = new IntegerArray();
193 index.put(id, nodes);
194 }
195
196 nodes.add(_enhancedDOM.getNodeHandle(ident));
197 }
198 }
199
200 return nodes;
201 }
202
203 /**
204 * <p>This method must be called by the code generated by the key() function
205 * prior to returning the node iterator.</p>
206 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
207 * <b>deprecated.</b></em></p>
208 * @deprecated
209 */
210 @Deprecated
211 public void lookupKey(Object value) {
212 IntegerArray nodes = _index.get(value);
213 _nodes = (nodes != null) ? (IntegerArray) nodes.clone() : null;
214 _position = 0;
215 }
216
217 /**
218 * <p>Callers should not call next() after it returns END.</p>
219 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
220 * <b>deprecated.</b></em></p>
221 * @deprecated
222 */
223 @Deprecated
224 public int next() {
225 if (_nodes == null) return DTMAxisIterator.END;
226
227 return (_position < _nodes.cardinality()) ?
228 _dom.getNodeHandle(_nodes.at(_position++)) : DTMAxisIterator.END;
229 }
230
231 /**
232 * Given a context node and the argument to the XPath <code>id</code>
233 * function, checks whether the context node is in the set of nodes that
234 * results from that reference to the <code>id</code> function. This is
235 * used in the implementation of <code>id</code> patterns.
236 *
237 * @param node The context node
238 * @param value The argument to the <code>id</code> function
239 * @return <code>1</code> if the context node is in the set of nodes
240 * returned by the reference to the <code>id</code> function;
241 * <code>0</code>, otherwise
242 */
243 public int containsID(int node, Object value) {
300 Map<String,IntegerArray> index =
301 _rootToIndexMap.get(new Integer(rootHandle));
302
303 // Check whether the context node is present in the set of nodes
304 // returned by the key function
305 if (index != null) {
306 final IntegerArray nodes = index.get(value);
307 return (nodes != null && nodes.indexOf(node) >= 0) ? 1 : 0;
308 }
309
310 // The particular key name identifies no nodes in this document
311 return 0;
312 }
313
314 /**
315 * <p>Resets the iterator to the last start node.</p>
316 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
317 * <b>deprecated.</b></em></p>
318 * @deprecated
319 */
320 @Deprecated
321 public DTMAxisIterator reset() {
322 _position = 0;
323 return this;
324 }
325
326 /**
327 * <p>Returns the number of elements in this iterator.</p>
328 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
329 * <b>deprecated.</b></em></p>
330 * @deprecated
331 */
332 @Deprecated
333 public int getLast() {
334 return (_nodes == null) ? 0 : _nodes.cardinality();
335 }
336
337 /**
338 * <p>Returns the position of the current node in the set.</p>
339 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
340 * <b>deprecated.</b></em></p>
341 * @deprecated
342 */
343 @Deprecated
344 public int getPosition() {
345 return _position;
346 }
347
348 /**
349 * <p>Remembers the current node for the next call to gotoMark().</p>
350 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
351 * <b>deprecated.</b></em></p>
352 * @deprecated
353 */
354 @Deprecated
355 public void setMark() {
356 _markedPosition = _position;
357 }
358
359 /**
360 * <p>Restores the current node remembered by setMark().</p>
361 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
362 * <b>deprecated.</b></em></p>
363 * @deprecated
364 */
365 @Deprecated
366 public void gotoMark() {
367 _position = _markedPosition;
368 }
369
370 /**
371 * <p>Set start to END should 'close' the iterator,
372 * i.e. subsequent call to next() should return END.</p>
373 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
374 * <b>deprecated.</b></em></p>
375 * @deprecated
376 */
377 @Deprecated
378 public DTMAxisIterator setStartNode(int start) {
379 if (start == DTMAxisIterator.END) {
380 _nodes = null;
381 }
382 else if (_nodes != null) {
383 _position = 0;
384 }
385 return (DTMAxisIterator) this;
386 }
387
388 /**
389 * <p>Get start to END should 'close' the iterator,
390 * i.e. subsequent call to next() should return END.</p>
391 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
392 * <b>deprecated.</b></em></p>
393 * @deprecated
394 */
395 @Deprecated
396 public int getStartNode() {
397 return 0;
398 }
399
400 /**
401 * <p>True if this iterator has a reversed axis.</p>
402 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
403 * <b>deprecated.</b></em></p>
404 * @deprecated
405 */
406 @Deprecated
407 public boolean isReverse() {
408 return(false);
409 }
410
411 /**
412 * <p>Returns a deep copy of this iterator.</p>
413 * <p><em>Use of an instance of this class as a {@link DTMAxisIterator} is
414 * <b>deprecated.</b></em></p>
415 * @deprecated
416 */
417 @Deprecated
418 public DTMAxisIterator cloneIterator() {
419 KeyIndex other = new KeyIndex(0);
420 other._index = _index;
421 other._rootToIndexMap = _rootToIndexMap;
422 other._nodes = _nodes;
423 other._position = _position;
424 return (DTMAxisIterator) other;
425 }
426
427 public void setDom(DOM dom, int node) {
428 _dom = dom;
429
430 // If a MultiDOM, ensure _enhancedDOM is correctly set
431 // so that getElementById() works in lookupNodes below
432 if (dom instanceof MultiDOM) {
433 dom = ((MultiDOM) dom).getDTM(node);
434 }
435
436 if (dom instanceof DOMEnhancedForDTM) {
437 _enhancedDOM = (DOMEnhancedForDTM)dom;
|