src/share/classes/com/sun/org/apache/xml/internal/security/utils/HelperNodeList.java

Print this page


   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 }
   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 }