KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > dom > ElementListImpl


1 /**
2  * org/ozone-db/xml/dom/ElementListImpl.java
3  *
4  * The contents of this file are subject to the OpenXML Public
5  * License Version 1.0; you may not use this file except in compliance
6  * with the License. You may obtain a copy of the License at
7  * http://www.openxml.org/license.html
8  *
9  * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
10  * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
11  * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
12  * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
13  * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
14  * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
15  *
16  * The Initial Developer of this code under the License is Assaf Arkin.
17  * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
18  * All Rights Reserved.
19  */

20
21 /**
22  * Changes for Persistent DOM running with ozone are
23  * Copyright 1999 by SMB GmbH. All rights reserved.
24  */

25
26 package org.ozoneDB.xml.dom;
27
28 import java.io.*;
29 import java.util.*;
30 import org.w3c.dom.*;
31
32
33 /**
34  * Implements a list of elements extracted based on their tag name.
35  * The constructor recieves the root element and tag name. It then obtains
36  * all the elements contained within that element that match the tag name,
37  * or all of them if the tag name is "*". The list is then accessible
38  * through the {@link #item} method.
39  * <P>
40  * The returned list is a snapshot of the element's contents at the time
41  * of calling. Subsequent updates to the element are not reflected in the
42  * list. This might result in inaccuracies when working from multiple threads.
43  *
44  *
45  * @version $Revision: 1.1 $ $Date: 2003/11/02 17:26:14 $
46  * @author <a HREF="mailto:arkin@trendline.co.il">Assaf Arkin</a>
47  * @see org.w3c.dom.NodeList
48  * @see ElementImpl
49  */

50 final class ElementListImpl implements NodeList, Externalizable {
51     
52     final static long serialVersionUID = 1;
53     
54     
55     public ElementListImpl() {
56     }
57     
58     
59     public Node item( int index ) {
60         if (index < 0 || index >= _elements.size()) {
61             return null;
62         } else {
63             return (Node)_elements.elementAt( index );
64         }
65     }
66     
67     
68     public int getLength() {
69         return _elements.size();
70     }
71     
72     
73     /**
74      * Add a single element to the list of elements.
75      *
76      * @param newElem The element to add
77      */

78     void addElement( Element newElem ) {
79         _elements.addElement( newElem );
80     }
81     
82     
83     /**
84      * Add all the elements contained in the root element and matching the
85      * tag name. If the tag name is "*", all elements are added. Each element
86      * is added by calling {@link #addElement} and the method is recursed on
87      * all sub-elements.
88      *
89      * @param element The root element from which to extract all sub elements
90      * @param tagName The tag name to match or "*" to match all tags
91      */

92     void addElements( Node element, String JavaDoc tagName ) {
93         Node node;
94         
95         // If tag name is "*" use null.
96
if (tagName.equals( "*" )) {
97             tagName = null;
98         }
99         // Traverse all the child nodes of this element. Each node that is
100
// an element is added to the list if its tag name matches and this
101
// method is recursed on that element.
102
node = element.getFirstChild();
103         while (node != null) {
104             if (node instanceof ElementProxy) {
105                 if (tagName == null || node.getNodeName().equals( tagName )) {
106                     addElement( (Element)node );
107                 }
108                 addElements( node, tagName );
109             }
110             node = node.getNextSibling();
111         }
112     }
113     
114     
115     /**
116      * Constructor receieves an element and tag name and extracts all the
117      * matching sub elements. After construction this object is ready for
118      * element retrieval.
119      *
120      * @param element The root element from which to extract all sub elements
121      * @param tagName The tag name to match or "*" to match all tags
122      */

123     ElementListImpl( Node element, String JavaDoc tagName ) {
124         init( element, tagName );
125     }
126     
127     
128     public void init( Node element, String JavaDoc tagName ) {
129         if (tagName == null) {
130             throw new NullPointerException JavaDoc( "Argument 'tagName' is null." );
131         }
132         _elements = new Vector();
133         addElements( element, tagName );
134     }
135     
136     
137     public void init( NodeProxy node ) {
138         throw new NullPointerException JavaDoc( "Argument 'tagName' is null." );
139     }
140     
141     /**
142      * Holds a list of all the matching elements. This list is not live, updates
143      * to the node tree are not reflected in this list.
144      */

145     private Vector _elements;
146     
147     
148     public void writeExternal( ObjectOutput out ) throws IOException {
149         out.writeObject( _elements );
150     }
151     
152     
153     public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
154         _elements = (Vector)in.readObject();
155     }
156  
157 }
158
Popular Tags