1 /*
   2  * Copyright (c) 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 package com.sun.org.apache.xml.internal.dtm.ref;
  22 
  23 import com.sun.org.apache.xml.internal.dtm.DTM;
  24 import com.sun.org.apache.xml.internal.dtm.DTMIterator;
  25 import org.w3c.dom.Node;
  26 
  27 /**
  28  * <code>DTMNodeList</code> gives us an implementation of the DOM's
  29  * NodeList interface wrapped around a DTM Iterator. The author
  30  * considers this something of an abominations, since NodeList was not
  31  * intended to be a general purpose "list of nodes" API and is
  32  * generally considered by the DOM WG to have be a mistake... but I'm
  33  * told that some of the XPath/XSLT folks say they must have this
  34  * solution.
  35  *
  36  * Please note that this is not necessarily equivlaent to a DOM
  37  * NodeList operating over the same document. In particular:
  38  * <ul>
  39  *
  40  * <li>If there are several Text nodes in logical succession (ie,
  41  * across CDATASection and EntityReference boundaries), we will return
  42  * only the first; the caller is responsible for stepping through
  43  * them.
  44  * (%REVIEW% Provide a convenience routine here to assist, pending
  45  * proposed DOM Level 3 getAdjacentText() operation?) </li>
  46  *
  47  * <li>Since the whole XPath/XSLT architecture assumes that the source
  48  * document is not altered while we're working with it, we do not
  49  * promise to implement the DOM NodeList's "live view" response to
  50  * document mutation. </li>
  51  *
  52  * </ul>
  53  *
  54  * <p>State: In progress!!</p>
  55  *
  56  * @LastModified: Nov 2017
  57  */
  58 public class DTMNodeList extends DTMNodeListBase {
  59     private DTMIterator m_iter;
  60 
  61     //================================================================
  62     // Methods unique to this class
  63     private DTMNodeList() {
  64     }
  65 
  66     /**
  67      * Public constructor: Wrap a DTMNodeList around an existing
  68      * and preconfigured DTMIterator
  69      *
  70      * WARNING: THIS HAS THE SIDE EFFECT OF ISSUING setShouldCacheNodes(true)
  71      * AGAINST THE DTMIterator.
  72      *
  73      */
  74     public DTMNodeList(DTMIterator dtmIterator) {
  75         if (dtmIterator != null) {
  76             int pos = dtmIterator.getCurrentPos();
  77             try {
  78                 m_iter = dtmIterator.cloneWithReset();
  79             } catch(CloneNotSupportedException cnse) {
  80                 m_iter = dtmIterator;
  81             }
  82             m_iter.setShouldCacheNodes(true);
  83             m_iter.runTo(-1);
  84             m_iter.setCurrentPos(pos);
  85         }
  86     }
  87 
  88     /**
  89      * Access the wrapped DTMIterator. I'm not sure whether anyone will
  90      * need this or not, but let's write it and think about it.
  91      *
  92      */
  93     public DTMIterator getDTMIterator() {
  94         return m_iter;
  95     }
  96 
  97     //================================================================
  98     // org.w3c.dom.NodeList API follows
  99 
 100     /**
 101      * Returns the <code>index</code>th item in the collection. If
 102      * <code>index</code> is greater than or equal to the number of nodes in
 103      * the list, this returns <code>null</code>.
 104      * @param index Index into the collection.
 105      * @return The node at the <code>index</code>th position in the
 106      *   <code>NodeList</code>, or <code>null</code> if that is not a valid
 107      *   index.
 108      */
 109     public Node item(int index)
 110     {
 111         if (m_iter != null) {
 112             int handle=m_iter.item(index);
 113             if (handle == DTM.NULL) {
 114                 return null;
 115             }
 116             return m_iter.getDTM(handle).getNode(handle);
 117         } else {
 118             return null;
 119         }
 120     }
 121 
 122     /**
 123      * The number of nodes in the list. The range of valid child node indices
 124      * is 0 to <code>length-1</code> inclusive.
 125      */
 126     public int getLength() {
 127         return (m_iter != null) ? m_iter.getLength() : 0;
 128     }
 129 }