KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > NodeListImpl


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xml;
30
31 import com.caucho.util.CharBuffer;
32 import com.caucho.xpath.pattern.NodeListIterator;
33
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36
37 import java.util.ArrayList JavaDoc;
38 import java.util.Collection JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 /**
42  * Generic implementation of a node list.
43  */

44 public class NodeListImpl implements NodeList JavaDoc {
45   private ArrayList JavaDoc<Node JavaDoc> _nodeList = new ArrayList JavaDoc<Node JavaDoc>();
46
47   /**
48    * Creates an empty node list.
49    */

50   public NodeListImpl()
51   {
52   }
53
54   /**
55    * Creates a node list from a collection.
56    */

57   public NodeListImpl(Collection JavaDoc<Node JavaDoc> collection)
58   {
59     _nodeList.addAll(collection);
60   }
61
62   /**
63    * Adds an item.
64    */

65   public void add(Node JavaDoc node)
66   {
67     _nodeList.add(node);
68   }
69
70   /**
71    * Returns the item at the index.
72    */

73   public Node JavaDoc item(int index)
74   {
75     if (index < 0 || _nodeList.size() <= index)
76       return null;
77
78     return _nodeList.get(index);
79   }
80
81   /**
82    * Returns the number of items in the list.
83    */

84   public int getLength()
85   {
86     return _nodeList.size();
87   }
88
89   public String JavaDoc toString()
90   {
91     CharBuffer cb = new CharBuffer();
92
93     cb.append("NodeListImpl[");
94     for (int i = 0; i < getLength(); i++) {
95       if (i != 0)
96     cb.append(", ");
97       cb.append(item(i));
98     }
99     cb.append("]");
100
101     return cb.toString();
102   }
103
104   // for quercus
105
public Iterator<Node JavaDoc> iterator()
106   {
107     return new NodeListIterator(null, this);
108   }
109 }
110
Popular Tags