1 /* 2 * reserved comment block 3 * DO NOT REMOVE OR ALTER! 4 */ 5 /** 6 * Licensed to the Apache Software Foundation (ASF) under one 7 * or more contributor license agreements. See the NOTICE file 8 * distributed with this work for additional information 9 * regarding copyright ownership. The ASF licenses this file 10 * to you under the Apache License, Version 2.0 (the 11 * "License"); you may not use this file except in compliance 12 * with the License. You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, 17 * software distributed under the License is distributed on an 18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19 * KIND, either express or implied. See the License for the 20 * specific language governing permissions and limitations 21 * under the License. 22 */ 23 package com.sun.org.apache.xml.internal.security.utils; 24 25 import java.util.ArrayList; 26 import java.util.List; 27 28 import org.w3c.dom.Document; 29 import org.w3c.dom.Node; 30 import org.w3c.dom.NodeList; 31 32 /** 33 * @author Christian Geuer-Pollmann 34 */ 35 public class HelperNodeList implements NodeList { 36 37 /** Field nodes */ 38 List<Node> nodes = new ArrayList<Node>(); 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 index i 61 */ 62 public Node item(int index) { 63 return nodes.get(index); 64 } 65 66 /** 67 * Method getLength 68 * 69 * @return length of the list 70 */ 71 public int getLength() { 72 return nodes.size(); 73 } 74 75 /** 76 * Method appendChild 77 * 78 * @param node 79 * @throws IllegalArgumentException 80 */ 81 public void appendChild(Node node) throws IllegalArgumentException { 82 if (this.allNodesMustHaveSameParent && this.getLength() > 0 83 && this.item(0).getParentNode() != node.getParentNode()) { 84 throw new IllegalArgumentException("Nodes have not the same Parent"); 85 } 86 nodes.add(node); 87 } 88 89 /** 90 * @return the document that contains this nodelist 91 */ 92 public Document getOwnerDocument() { 93 if (this.getLength() == 0) { 94 return null; 95 } 96 return XMLUtils.getOwnerDocument(this.item(0)); 97 } 98 }