1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright  1999-2004 The Apache Software Foundation.
   7  *
   8  *  Licensed under the Apache License, Version 2.0 (the "License");
   9  *  you may not use this file except in compliance with the License.
  10  *  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.security.utils;
  22 
  23 import java.util.ArrayList;
  24 import java.util.List;
  25 
  26 import org.w3c.dom.Document;
  27 import org.w3c.dom.Node;
  28 import org.w3c.dom.NodeList;
  29 
  30 /**
  31  *
  32  * @author Christian Geuer-Pollmann
  33  *
  34  */
  35 public class HelperNodeList implements NodeList {
  36 
  37    /** Field nodes */
  38    List<Node> nodes = new ArrayList<Node>(20);
  39    boolean _allNodesMustHaveSameParent = false;
  40 
  41    /**
  42     *
  43     */
  44    public HelperNodeList() {
  45       this(false);
  46    }
  47 
  48 
  49    /**
  50     * @param allNodesMustHaveSameParent
  51     */
  52    public HelperNodeList(boolean allNodesMustHaveSameParent) {
  53       this._allNodesMustHaveSameParent = allNodesMustHaveSameParent;
  54    }
  55 
  56    /**
  57     * Method item
  58     *
  59     * @param index
  60     * @return node with inde i
  61     */
  62    public Node item(int index) {
  63 
  64       // log.log(java.util.logging.Level.FINE, "item(" + index + ") of " + this.getLength() + " nodes");
  65 
  66       return nodes.get(index);
  67    }
  68 
  69    /**
  70     * Method getLength
  71     *
  72     *  @return length of the list
  73     */
  74    public int getLength() {
  75       return nodes.size();
  76    }
  77 
  78    /**
  79     * Method appendChild
  80     *
  81     * @param node
  82     * @throws IllegalArgumentException
  83     */
  84    public void appendChild(Node node) throws IllegalArgumentException {
  85       if (this._allNodesMustHaveSameParent && this.getLength() > 0) {
  86          if (this.item(0).getParentNode() != node.getParentNode()) {
  87             throw new IllegalArgumentException("Nodes have not the same Parent");
  88          }
  89       }
  90       nodes.add(node);
  91    }
  92 
  93    /**
  94     * @return the document that contains this nodelist
  95     */
  96    public Document getOwnerDocument() {
  97       if (this.getLength() == 0) {
  98          return null;
  99       }
 100       return XMLUtils.getOwnerDocument(this.item(0));
 101    }
 102 }