001: /**
002: * org/ozone-db/xml/dom/NamedNodeMapImpl.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.util.*;
027: import java.io.*;
028: import org.w3c.dom.*;
029: import org.ozoneDB.OzoneObject;
030:
031: /**
032: * Implements a collection of nodes that can be accessed by name. Used mostly by
033: * {@link DocumentTypeImpl} to hold collections of element, notation and other
034: * definitions.
035: * <P>
036: * The actual collection of objects is held by some owner node in a {@link
037: * java.util.Dictionary}. This map object provides access to this dictionary
038: * in a manner that is consistent with the DOM. This map can be accessed
039: * concurrently, so the owner need only create one map per dictionary.
040: * <P>
041: * Nodes are not maintained in any particular order, so accessing them by index
042: * can be expected to be a slow operation.
043: *
044: *
045: * @version $Revision: 1.1 $ $Date: 2001/12/18 11:03:24 $
046: * @author <a href="mailto:arkin@trendline.co.il">Assaf Arkin</a>
047: * @see org.w3c.dom.NamedNodeMap
048: */
049: public final class NamedNodeMapImpl extends OzoneObject implements
050: NamedNodeMapProxy, Externalizable {
051:
052: final static long serialVersionUID = 1;
053:
054: public Node getNamedItemNS(java.lang.String namespaceURI,
055: java.lang.String localName) {
056: throw new DOMExceptionImpl(
057: DOMException.NOT_SUPPORTED_ERR,
058: "NamedNode.getNamedItemNS(): ozone's persistent DOM doesn't support DOM level 2 yet.");
059: }
060:
061: public Node setNamedItemNS(Node arg) throws DOMException {
062: throw new DOMExceptionImpl(
063: DOMException.NOT_SUPPORTED_ERR,
064: "NamedNode.getNamedItemNS(): ozone's persistent DOM doesn't support DOM level 2 yet.");
065: }
066:
067: public Node removeNamedItemNS(java.lang.String namespaceURI,
068: java.lang.String localName) throws DOMException {
069: throw new DOMExceptionImpl(
070: DOMException.NOT_SUPPORTED_ERR,
071: "NamedNode.getNamedItemNS(): ozone's persistent DOM doesn't support DOM level 2 yet.");
072: }
073:
074: public synchronized Node getNamedItem(String name) {
075: // Dictionary get by name.
076: return (Node) _dictionary.get(name);
077: }
078:
079: public synchronized Node setNamedItem(Node arg) throws DOMException {
080: // Make sure the DTD is not read only and that the added node is of the
081: // allowed type (Entity, Notation, AttrDecl, ElementDecl).
082: if (_owner.isReadOnly()) {
083: throw new DOMExceptionImpl(
084: DOMException.NO_MODIFICATION_ALLOWED_ERR);
085: }
086: if (arg == null || !(arg instanceof Node)) {
087: throw new DOMExceptionImpl(DOMException.WRONG_DOCUMENT_ERR);
088: }
089: if (!(arg instanceof ElementDeclProxy
090: || arg instanceof Notation || arg instanceof Entity)) {
091: throw new DOMExceptionImpl(DOMException.WRONG_DOCUMENT_ERR);
092: }
093: return (Node) _dictionary.put(arg.getNodeName(), arg);
094: }
095:
096: public synchronized Node removeNamedItem(String name)
097: throws DOMException {
098: if (_owner.isReadOnly()) {
099: throw new DOMExceptionImpl(
100: DOMException.NO_MODIFICATION_ALLOWED_ERR);
101: }
102: return (Node) _dictionary.remove(name);
103: }
104:
105: public synchronized Node item(int index) {
106: Enumeration elem;
107:
108: // Get by index is a long operation, but one that is performed less
109: // than get by name. We opt not to use the JDK 1.2 collections to
110: // speed it up for the sake of portability.
111: elem = _dictionary.elements();
112: while (elem.hasMoreElements()) {
113: if (index == 0) {
114: return (Node) elem.nextElement();
115: }
116: elem.nextElement();
117: --index;
118: }
119: return null;
120: }
121:
122: public int getLength() {
123: return _dictionary.size();
124: }
125:
126: /**
127: * So we lied about the owner managing the dictionary. But just in case
128: * the owner would like to traverse the dictionary list without resorting
129: * to the slower indexed method.
130: *
131: * @return Enumeration of all elements in the dictionary
132: */
133: public Enumeration elements() {
134: return (Enumeration) _dictionary.elements();
135: }
136:
137: /**
138: * Constructor required the owner of this dictionary and a reference to the
139: * dictionary. Once constructed, the map is ready for use.
140: *
141: * @param owner The owner of this dictionary
142: * @param dictionary The dictionary managed by that owner
143: */
144: public NamedNodeMapImpl(NodeProxy owner, Dictionary dictionary) {
145: init(owner, dictionary);
146: }
147:
148: public NamedNodeMapImpl() {
149: super ();
150: }
151:
152: public void init(NodeProxy owner, Dictionary dictionary) {
153: if (owner == null || dictionary == null) {
154: throw new NullPointerException(
155: "Argument 'owner' or 'dictionary' is null.");
156: }
157: _dictionary = dictionary;
158: _owner = owner;
159: }
160:
161: /** */
162: public void writeExternal(ObjectOutput out) throws IOException {
163: // super.writeExternal (out);
164: out.writeObject(_dictionary);
165: out.writeObject(_owner);
166: }
167:
168: /** */
169: public void readExternal(ObjectInput in) throws IOException,
170: ClassNotFoundException {
171: // super.readExternal (in);
172: _dictionary = (Dictionary) in.readObject();
173: _owner = (NodeProxy) in.readObject();
174: }
175:
176: /**
177: * Reference to the dictionary accessed through this node map. The dictionary
178: * is not held by this object but by the {@link #_owner}.
179: */
180: private Dictionary _dictionary;
181:
182: /**
183: * Reference to the owner of this node list. This is another node which contains
184: * {@link #_dictionary} as part of it.
185: */
186: private NodeProxy _owner;
187: }
|