KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * org/ozone-db/xml/dom/NamedNodeMapImpl.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.util.*;
29 import java.io.*;
30 import org.w3c.dom.*;
31 import org.ozoneDB.OzoneObject;
32
33
34 /**
35  * Implements a collection of nodes that can be accessed by name. Used mostly by
36  * {@link DocumentTypeImpl} to hold collections of element, notation and other
37  * definitions.
38  * <P>
39  * The actual collection of objects is held by some owner node in a {@link
40  * java.util.Dictionary}. This map object provides access to this dictionary
41  * in a manner that is consistent with the DOM. This map can be accessed
42  * concurrently, so the owner need only create one map per dictionary.
43  * <P>
44  * Nodes are not maintained in any particular order, so accessing them by index
45  * can be expected to be a slow operation.
46  *
47  *
48  * @version $Revision: 1.2 $ $Date: 2003/11/20 23:18:42 $
49  * @author <a HREF="mailto:arkin@trendline.co.il">Assaf Arkin</a>
50  * @see org.w3c.dom.NamedNodeMap
51  */

52 public final class NamedNodeMapImpl extends OzoneObject implements NamedNodeMapProxy, Externalizable {
53
54     final static long serialVersionUID = 1;
55
56
57     public Node getNamedItemNS( java.lang.String JavaDoc namespaceURI, java.lang.String JavaDoc localName ) {
58         throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR,
59                 "NamedNode.getNamedItemNS(): ozone's persistent DOM doesn't support DOM level 2 yet." );
60     }
61
62
63     public Node setNamedItemNS( Node arg ) throws DOMException {
64         throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR,
65                 "NamedNode.getNamedItemNS(): ozone's persistent DOM doesn't support DOM level 2 yet." );
66     }
67
68
69     public Node removeNamedItemNS( java.lang.String JavaDoc namespaceURI, java.lang.String JavaDoc localName ) throws DOMException {
70         throw new DOMExceptionImpl( DOMException.NOT_SUPPORTED_ERR,
71                 "NamedNode.getNamedItemNS(): ozone's persistent DOM doesn't support DOM level 2 yet." );
72     }
73
74
75     public synchronized Node getNamedItem( String JavaDoc name ) {
76         // Dictionary get by name.
77
return (Node)_dictionary.get( name );
78     }
79
80
81     public synchronized Node setNamedItem( Node arg ) throws DOMException {
82         // Make sure the DTD is not read only and that the added node is of the
83
// allowed type (Entity, Notation, AttrDecl, ElementDecl).
84
if (_owner.isReadOnly()) {
85             throw new DOMExceptionImpl( DOMException.NO_MODIFICATION_ALLOWED_ERR );
86         }
87         if (arg == null || !(arg instanceof Node)) {
88             throw new DOMExceptionImpl( DOMException.WRONG_DOCUMENT_ERR );
89         }
90         if (!(arg instanceof ElementDeclProxy || arg instanceof Notation || arg instanceof Entity)) {
91             throw new DOMExceptionImpl( DOMException.WRONG_DOCUMENT_ERR );
92         }
93         return (Node)_dictionary.put( arg.getNodeName(), arg );
94     }
95
96
97     public synchronized Node removeNamedItem( String JavaDoc name ) throws DOMException {
98         if (_owner.isReadOnly()) {
99             throw new DOMExceptionImpl( DOMException.NO_MODIFICATION_ALLOWED_ERR );
100         }
101         return (Node)_dictionary.remove( name );
102     }
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
123     public int getLength() {
124         return _dictionary.size();
125     }
126
127
128     /**
129      * So we lied about the owner managing the dictionary. But just in case
130      * the owner would like to traverse the dictionary list without resorting
131      * to the slower indexed method.
132      *
133      * @return Enumeration of all elements in the dictionary
134      */

135     public Enumeration elements() {
136         return _dictionary.elements();
137     }
138
139
140     /**
141      * Constructor required the owner of this dictionary and a reference to the
142      * dictionary. Once constructed, the map is ready for use.
143      *
144      * @param owner The owner of this dictionary
145      * @param dictionary The dictionary managed by that owner
146      */

147     public NamedNodeMapImpl( NodeProxy owner, Dictionary dictionary ) {
148         init( owner, dictionary );
149     }
150
151
152     public NamedNodeMapImpl() {
153         super();
154     }
155
156
157     public void init( NodeProxy owner, Dictionary dictionary ) {
158         if (owner == null || dictionary == null) {
159             throw new NullPointerException JavaDoc( "Argument 'owner' or 'dictionary' is null." );
160         }
161         _dictionary = dictionary;
162         _owner = owner;
163     }
164
165
166     /** */
167     public void writeExternal( ObjectOutput out ) throws IOException {
168         // super.writeExternal (out);
169
out.writeObject( _dictionary );
170         out.writeObject( _owner );
171     }
172
173
174     /** */
175     public void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException JavaDoc {
176         // super.readExternal (in);
177
_dictionary = (Dictionary)in.readObject();
178         _owner = (NodeProxy)in.readObject();
179     }
180
181     /**
182      * Reference to the dictionary accessed through this node map. The dictionary
183      * is not held by this object but by the {@link #_owner}.
184      */

185     private Dictionary _dictionary;
186
187
188     /**
189      * Reference to the owner of this node list. This is another node which contains
190      * {@link #_dictionary} as part of it.
191      */

192     private NodeProxy _owner;
193 }
194
Popular Tags