1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * @LastModified: Nov 2017 4 */ 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one or more 7 * contributor license agreements. See the NOTICE file distributed with 8 * this work for additional information regarding copyright ownership. 9 * The ASF licenses this file to You under the Apache License, Version 2.0 10 * (the "License"); you may not use this file except in compliance with 11 * the License. You may obtain a copy of the License at 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 15 * Unless required by applicable law or agreed to in writing, software 16 * distributed under the License is distributed on an "AS IS" BASIS, 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 18 * See the License for the specific language governing permissions and 19 * limitations under the License. 20 */ 21 22 package com.sun.org.apache.xml.internal.dtm.ref; 23 24 import com.sun.org.apache.xml.internal.dtm.DTM; 25 import com.sun.org.apache.xml.internal.dtm.DTMIterator; 26 import org.w3c.dom.Node; 27 28 /** 29 * <code>DTMNodeList</code> gives us an implementation of the DOM's 30 * NodeList interface wrapped around a DTM Iterator. The author 31 * considers this something of an abominations, since NodeList was not 32 * intended to be a general purpose "list of nodes" API and is 33 * generally considered by the DOM WG to have be a mistake... but I'm 34 * told that some of the XPath/XSLT folks say they must have this 35 * solution. 36 * 37 * Please note that this is not necessarily equivlaent to a DOM 38 * NodeList operating over the same document. In particular: 39 * <ul> 40 * 41 * <li>If there are several Text nodes in logical succession (ie, 42 * across CDATASection and EntityReference boundaries), we will return 43 * only the first; the caller is responsible for stepping through 44 * them. 45 * (%REVIEW% Provide a convenience routine here to assist, pending 46 * proposed DOM Level 3 getAdjacentText() operation?) </li> 47 * 48 * <li>Since the whole XPath/XSLT architecture assumes that the source 49 * document is not altered while we're working with it, we do not 50 * promise to implement the DOM NodeList's "live view" response to 51 * document mutation. </li> 52 * 53 * </ul> 54 * 55 * <p>State: In progress!!</p> 56 * */ 57 public class DTMNodeList extends DTMNodeListBase { 58 private DTMIterator m_iter; 59 60 //================================================================ 61 // Methods unique to this class 62 private DTMNodeList() { 63 } 64 65 /** 66 * Public constructor: Wrap a DTMNodeList around an existing 67 * and preconfigured DTMIterator 68 * 69 * WARNING: THIS HAS THE SIDE EFFECT OF ISSUING setShouldCacheNodes(true) 70 * AGAINST THE DTMIterator. 71 * 72 */ 73 public DTMNodeList(DTMIterator dtmIterator) { 74 if (dtmIterator != null) { 75 int pos = dtmIterator.getCurrentPos(); 76 try { 77 m_iter = dtmIterator.cloneWithReset(); 78 } catch(CloneNotSupportedException cnse) { 79 m_iter = dtmIterator; 80 } 81 m_iter.setShouldCacheNodes(true); 82 m_iter.runTo(-1); 83 m_iter.setCurrentPos(pos); 84 } 85 } 86 87 /** 88 * Access the wrapped DTMIterator. I'm not sure whether anyone will 89 * need this or not, but let's write it and think about it. 90 * 91 */ 92 public DTMIterator getDTMIterator() { 93 return m_iter; 94 } 95 96 //================================================================ 97 // org.w3c.dom.NodeList API follows 98 99 /** 100 * Returns the <code>index</code>th item in the collection. If 101 * <code>index</code> is greater than or equal to the number of nodes in 102 * the list, this returns <code>null</code>. 103 * @param index Index into the collection. 104 * @return The node at the <code>index</code>th position in the 105 * <code>NodeList</code>, or <code>null</code> if that is not a valid 106 * index. 107 */ 108 public Node item(int index) 109 { 110 if (m_iter != null) { 111 int handle=m_iter.item(index); 112 if (handle == DTM.NULL) { 113 return null; 114 } 115 return m_iter.getDTM(handle).getNode(handle); 116 } else { 117 return null; 118 } 119 } 120 121 /** 122 * The number of nodes in the list. The range of valid child node indices 123 * is 0 to <code>length-1</code> inclusive. 124 */ 125 public int getLength() { 126 return (m_iter != null) ? m_iter.getLength() : 0; 127 } 128 }