001: /**
002: * org/ozone-db/xml/dom/ElementListImpl.java
003: *
004: * The contents of this file are subject to the OpenXML Public
005: * License Version 1.0; you may not use this file except in compliance
006: * with the License. You may obtain a copy of the License at
007: * http://www.openxml.org/license.html
008: *
009: * THIS SOFTWARE IS DISTRIBUTED ON AN "AS IS" BASIS WITHOUT WARRANTY
010: * OF ANY KIND, EITHER EXPRESSED OR IMPLIED. THE INITIAL DEVELOPER
011: * AND ALL CONTRIBUTORS SHALL NOT BE LIABLE FOR ANY DAMAGES AS A
012: * RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
013: * DERIVATIVES. SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING
014: * RIGHTS AND LIMITATIONS UNDER THE LICENSE.
015: *
016: * The Initial Developer of this code under the License is Assaf Arkin.
017: * Portions created by Assaf Arkin are Copyright (C) 1998, 1999.
018: * All Rights Reserved.
019: */
020:
021: /**
022: * Changes for Persistent DOM running with ozone are
023: * Copyright 1999 by SMB GmbH. All rights reserved.
024: */package org.ozoneDB.xml.dom;
025:
026: import java.io.*;
027: import java.util.*;
028: import org.w3c.dom.*;
029:
030: /**
031: * Implements a list of elements extracted based on their tag name.
032: * The constructor recieves the root element and tag name. It then obtains
033: * all the elements contained within that element that match the tag name,
034: * or all of them if the tag name is "*". The list is then accessible
035: * through the {@link #item} method.
036: * <P>
037: * The returned list is a snapshot of the element's contents at the time
038: * of calling. Subsequent updates to the element are not reflected in the
039: * list. This might result in inaccuracies when working from multiple threads.
040: *
041: *
042: * @version $Revision: 1.1 $ $Date: 2001/12/18 11:03:24 $
043: * @author <a href="mailto:arkin@trendline.co.il">Assaf Arkin</a>
044: * @see org.w3c.dom.NodeList
045: * @see ElementImpl
046: */
047: final class ElementListImpl implements NodeList, Externalizable {
048:
049: final static long serialVersionUID = 1;
050:
051: public ElementListImpl() {
052: }
053:
054: public Node item(int index) {
055: if (index < 0 || index >= _elements.size()) {
056: return null;
057: } else {
058: return (Node) _elements.elementAt(index);
059: }
060: }
061:
062: public int getLength() {
063: return _elements.size();
064: }
065:
066: /**
067: * Add a single element to the list of elements.
068: *
069: * @param newElem The element to add
070: */
071: void addElement(Element newElem) {
072: _elements.addElement(newElem);
073: }
074:
075: /**
076: * Add all the elements contained in the root element and matching the
077: * tag name. If the tag name is "*", all elements are added. Each element
078: * is added by calling {@link #addElement} and the method is recursed on
079: * all sub-elements.
080: *
081: * @param element The root element from which to extract all sub elements
082: * @param tagName The tag name to match or "*" to match all tags
083: */
084: void addElements(Node element, String tagName) {
085: Node node;
086:
087: // If tag name is "*" use null.
088: if (tagName.equals("*")) {
089: tagName = null;
090: }
091: // Traverse all the child nodes of this element. Each node that is
092: // an element is added to the list if its tag name matches and this
093: // method is recursed on that element.
094: node = element.getFirstChild();
095: while (node != null) {
096: if (node instanceof ElementProxy) {
097: if (tagName == null
098: || node.getNodeName().equals(tagName)) {
099: addElement((Element) node);
100: }
101: addElements(node, tagName);
102: }
103: node = node.getNextSibling();
104: }
105: }
106:
107: /**
108: * Constructor receieves an element and tag name and extracts all the
109: * matching sub elements. After construction this object is ready for
110: * element retrieval.
111: *
112: * @param element The root element from which to extract all sub elements
113: * @param tagName The tag name to match or "*" to match all tags
114: */
115: ElementListImpl(Node element, String tagName) {
116: init(element, tagName);
117: }
118:
119: public void init(Node element, String tagName) {
120: if (tagName == null) {
121: throw new NullPointerException(
122: "Argument 'tagName' is null.");
123: }
124: _elements = new Vector();
125: addElements(element, tagName);
126: }
127:
128: public void init(NodeProxy node) {
129: throw new NullPointerException("Argument 'tagName' is null.");
130: }
131:
132: /**
133: * Holds a list of all the matching elements. This list is not live, updates
134: * to the node tree are not reflected in this list.
135: */
136: private Vector _elements;
137:
138: public void writeExternal(ObjectOutput out) throws IOException {
139: out.writeObject(_elements);
140: }
141:
142: public void readExternal(ObjectInput in) throws IOException,
143: ClassNotFoundException {
144: _elements = (Vector) in.readObject();
145: }
146:
147: }
|