KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > message > NamedNodeMapImpl


1 /*
2
3  * Copyright 2001-2004 The Apache Software Foundation.
4
5  *
6
7  * Licensed under the Apache License, Version 2.0 (the "License");
8
9  * you may not use this file except in compliance with the License.
10
11  * You may obtain a copy of the License at
12
13  *
14
15  * http://www.apache.org/licenses/LICENSE-2.0
16
17  *
18
19  * Unless required by applicable law or agreed to in writing, software
20
21  * distributed under the License is distributed on an "AS IS" BASIS,
22
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
25  * See the License for the specific language governing permissions and
26
27  * limitations under the License.
28
29  */

30
31
32 package org.jboss.axis.message;
33
34
35 import org.w3c.dom.Attr JavaDoc;
36 import org.w3c.dom.DOMException JavaDoc;
37 import org.w3c.dom.Document JavaDoc;
38 import org.w3c.dom.NamedNodeMap JavaDoc;
39 import org.w3c.dom.Node JavaDoc;
40
41 import java.util.Iterator JavaDoc;
42 import java.util.Vector JavaDoc;
43
44
45 /**
46  * A W3C simple DOM NameNodeMap implementation
47  *
48  * @author Heejune Ahn (cityboy@tmax.co.kr)
49  */

50
51 public class NamedNodeMapImpl implements NamedNodeMap JavaDoc
52 {
53
54
55    /**
56     * Nodes.
57     */

58
59    protected Vector JavaDoc nodes;
60
61
62    static private Document JavaDoc doc = null;
63
64    static
65    {
66
67       try
68       {
69
70          org.w3c.dom.Document JavaDoc doc = org.jboss.axis.utils.XMLUtils.newDocument();
71
72       }
73       catch (javax.xml.parsers.ParserConfigurationException JavaDoc e)
74       {
75
76          throw new org.jboss.axis.InternalException(e);
77
78       }
79
80    }
81
82
83    public NamedNodeMapImpl()
84
85    {
86
87       nodes = new Vector JavaDoc();
88
89    }
90
91    /**
92     * Retrieves a node specified by name.
93     *
94     * @param name The <code>nodeName</code> of a node to retrieve.
95     * @return A <code>Node</code> (of any type) with the specified
96     * <p/>
97     * <code>nodeName</code>, or <code>null</code> if it does not identify
98     * <p/>
99     * any node in this map.
100     */

101
102    public Node JavaDoc getNamedItem(String JavaDoc name)
103    {
104
105       if (name == null)
106       {
107
108          Thread.dumpStack();
109
110          throw new java.lang.IllegalArgumentException JavaDoc("local name = null");
111
112       }
113
114
115       for (Iterator JavaDoc iter = nodes.iterator(); iter.hasNext();)
116       {
117
118          Attr JavaDoc attr = (Attr JavaDoc)iter.next();
119
120          if (name.equals(attr.getName()))
121          {
122
123             return attr;
124
125          }
126
127       }
128
129       return null;
130
131    }
132
133
134    /**
135     * Adds a node using its <code>nodeName</code> attribute. If a node with
136     * <p/>
137     * that name is already present in this map, it is replaced by the new
138     * <p/>
139     * one.
140     * <p/>
141     * <br>As the <code>nodeName</code> attribute is used to derive the name
142     * <p/>
143     * which the node must be stored under, multiple nodes of certain types
144     * <p/>
145     * (those that have a "special" string value) cannot be stored as the
146     * <p/>
147     * names would clash. This is seen as preferable to allowing nodes to be
148     * <p/>
149     * aliased.
150     *
151     * @param arg A node to store in this map. The node will later be
152     * <p/>
153     * accessible using the value of its <code>nodeName</code> attribute.
154     * @return If the new <code>Node</code> replaces an existing node the
155     * <p/>
156     * replaced <code>Node</code> is returned, otherwise <code>null</code>
157     * <p/>
158     * is returned.
159     * @throws DOMException WRONG_DOCUMENT_ERR: Raised if <code>arg</code> was created from a
160     * <p/>
161     * different document than the one that created this map.
162     * <p/>
163     * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
164     * <p/>
165     * <br>INUSE_ATTRIBUTE_ERR: Raised if <code>arg</code> is an
166     * <p/>
167     * <code>Attr</code> that is already an attribute of another
168     * <p/>
169     * <code>Element</code> object. The DOM user must explicitly clone
170     * <p/>
171     * <code>Attr</code> nodes to re-use them in other elements.
172     * <p/>
173     * <br>HIERARCHY_REQUEST_ERR: Raised if an attempt is made to add a node
174     * <p/>
175     * doesn't belong in this NamedNodeMap. Examples would include trying
176     * <p/>
177     * to insert something other than an Attr node into an Element's map
178     * <p/>
179     * of attributes, or a non-Entity node into the DocumentType's map of
180     * <p/>
181     * Entities.
182     */

183
184    public Node JavaDoc setNamedItem(Node JavaDoc arg) throws DOMException JavaDoc
185
186    {
187
188
189       String JavaDoc name = arg.getNodeName();
190
191
192       if (name == null)
193       {
194
195          Thread.dumpStack();
196
197          throw new java.lang.IllegalArgumentException JavaDoc("local name = null");
198
199       }
200
201
202       for (int i = 0; i < nodes.size(); i++)
203       {
204
205          Attr JavaDoc attr = (Attr JavaDoc)nodes.get(i);
206
207          // search if we have already
208

209          if (name.equals(attr.getName()))
210          {
211
212             nodes.remove(i);
213
214             nodes.add(i, arg);
215
216             return attr; // return old one
217

218          }
219
220       }
221
222
223
224       // if cannot found
225

226       nodes.add(arg);
227
228       return null;
229
230    }
231
232
233    /**
234     * Removes a node specified by name. When this map contains the attributes
235     * <p/>
236     * attached to an element, if the removed attribute is known to have a
237     * <p/>
238     * default value, an attribute immediately appears containing the
239     * <p/>
240     * default value as well as the corresponding namespace URI, local name,
241     * <p/>
242     * and prefix when applicable.
243     *
244     * @param name The <code>nodeName</code> of the node to remove.
245     * @return The node removed from this map if a node with such a name
246     * <p/>
247     * exists.
248     * @throws DOMException NOT_FOUND_ERR: Raised if there is no node named <code>name</code> in
249     * <p/>
250     * this map.
251     * <p/>
252     * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
253     */

254
255    public Node JavaDoc removeNamedItem(String JavaDoc name) throws DOMException JavaDoc
256
257    {
258
259       if (name == null)
260       {
261
262          Thread.dumpStack();
263
264          throw new java.lang.IllegalArgumentException JavaDoc("local name = null");
265
266       }
267
268       for (int i = 0; i < nodes.size(); i++)
269       {
270
271          Attr JavaDoc attr = (Attr JavaDoc)nodes.get(i);
272
273          // search if we have already
274

275          if (name.equals(attr.getLocalName()))
276          {
277
278             nodes.remove(i);
279
280             return attr; // return old one
281

282          }
283
284       }
285
286       return null;
287
288    }
289
290
291    /**
292     * Returns the <code>index</code>th item in the map. If <code>index</code>
293     * <p/>
294     * is greater than or equal to the number of nodes in this map, this
295     * <p/>
296     * returns <code>null</code>.
297     *
298     * @param index Index into this map.
299     * @return The node at the <code>index</code>th position in the map, or
300     * <p/>
301     * <code>null</code> if that is not a valid index.
302     */

303
304    public Node JavaDoc item(int index)
305    {
306
307       return (nodes != null && index < nodes.size()) ?
308
309               (Node JavaDoc)(nodes.elementAt(index)) : null;
310
311    }
312
313
314    /**
315     * The number of nodes in this map. The range of valid child node indices
316     * <p/>
317     * is <code>0</code> to <code>length-1</code> inclusive.
318     */

319
320    public int getLength()
321    {
322
323       return (nodes != null) ? nodes.size() : 0;
324
325    }
326
327
328    /**
329     * Retrieves a node specified by local name and namespace URI.
330     * <p/>
331     * <br>Documents which do not support the "XML" feature will permit only
332     * <p/>
333     * the DOM Level 1 calls for creating/setting elements and attributes.
334     * <p/>
335     * Hence, if you specify a non-null namespace URI, these DOMs will never
336     * <p/>
337     * find a matching node.
338     *
339     * @param namespaceURI The namespace URI of the node to retrieve.
340     * @param localName The local name of the node to retrieve.
341     * @return A <code>Node</code> (of any type) with the specified local
342     * <p/>
343     * name and namespace URI, or <code>null</code> if they do not
344     * <p/>
345     * identify any node in this map.
346     * @since DOM Level 2
347     */

348
349    public Node JavaDoc getNamedItemNS(String JavaDoc namespaceURI, String JavaDoc localName)
350    {
351
352
353       if (namespaceURI == null) namespaceURI = "";
354
355       if (localName == null)
356       {
357
358          Thread.dumpStack();
359
360          throw new java.lang.IllegalArgumentException JavaDoc("local name = null");
361
362       }
363
364
365       for (Iterator JavaDoc iter = nodes.iterator(); iter.hasNext();)
366       {
367
368          Attr JavaDoc attr = (Attr JavaDoc)iter.next();
369
370          if (namespaceURI.equals(attr.getNamespaceURI()) &&
371
372                  namespaceURI.equals(attr.getLocalName()))
373          {
374
375             return attr;
376
377          }
378
379       }
380
381       return null;
382
383    }
384
385
386    /**
387     * Adds a node using its <code>namespaceURI</code> and
388     * <p/>
389     * <code>localName</code>. If a node with that namespace URI and that
390     * <p/>
391     * local name is already present in this map, it is replaced by the new
392     * <p/>
393     * one.
394     *
395     * @param arg A node to store in this map. The node will later be
396     * <p/>
397     * accessible using the value of its <code>namespaceURI</code> and
398     * <p/>
399     * <code>localName</code> attributes.
400     * @return If the new <code>Node</code> replaces an existing node the
401     * <p/>
402     * replaced <code>Node</code> is returned, otherwise <code>null</code>
403     * <p/>
404     * is returned.
405     * @throws DOMException WRONG_DOCUMENT_ERR: Raised if <code>arg</code> was created from a
406     * <p/>
407     * different document than the one that created this map.
408     * <p/>
409     * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
410     * <p/>
411     * <br>INUSE_ATTRIBUTE_ERR: Raised if <code>arg</code> is an
412     * <p/>
413     * <code>Attr</code> that is already an attribute of another
414     * <p/>
415     * <code>Element</code> object. The DOM user must explicitly clone
416     * <p/>
417     * <code>Attr</code> nodes to re-use them in other elements.
418     * <p/>
419     * <br>HIERARCHY_REQUEST_ERR: Raised if an attempt is made to add a node
420     * <p/>
421     * doesn't belong in this NamedNodeMap. Examples would include trying
422     * <p/>
423     * to insert something other than an Attr node into an Element's map
424     * <p/>
425     * of attributes, or a non-Entity node into the DocumentType's map of
426     * <p/>
427     * Entities.
428     * <p/>
429     * <br>NOT_SUPPORTED_ERR: Always thrown if the current document does not
430     * <p/>
431     * support the <code>"XML"</code> feature, since namespaces were
432     * <p/>
433     * defined by XML.
434     * @since DOM Level 2
435     */

436
437    public Node JavaDoc setNamedItemNS(Node JavaDoc arg) throws DOMException JavaDoc
438
439    {
440
441       String JavaDoc namespaceURI = arg.getNamespaceURI();
442
443       String JavaDoc localName = arg.getLocalName();
444
445
446       if (namespaceURI == null) namespaceURI = "";
447
448       if (localName == null)
449       {
450
451          Thread.dumpStack();
452
453          throw new java.lang.IllegalArgumentException JavaDoc("local name = null");
454
455       }
456
457
458       for (int i = 0; i < nodes.size(); i++)
459       {
460
461          Attr JavaDoc attr = (Attr JavaDoc)nodes.get(i);
462
463          // search if we have already
464

465          if (namespaceURI.equals(attr.getNamespaceURI()) &&
466
467                  namespaceURI.equals(attr.getLocalName()))
468          {
469
470             nodes.remove(i);
471
472             nodes.add(i, arg);
473
474             return attr; // return old one
475

476          }
477
478       }
479
480
481
482       // if cannot found
483

484       nodes.add(arg);
485
486       return null;
487
488    }
489
490
491    /**
492     * Removes a node specified by local name and namespace URI. A removed
493     * <p/>
494     * attribute may be known to have a default value when this map contains
495     * <p/>
496     * the attributes attached to an element, as returned by the attributes
497     * <p/>
498     * attribute of the <code>Node</code> interface. If so, an attribute
499     * <p/>
500     * immediately appears containing the default value as well as the
501     * <p/>
502     * corresponding namespace URI, local name, and prefix when applicable.
503     * <p/>
504     * <br>Documents which do not support the "XML" feature will permit only
505     * <p/>
506     * the DOM Level 1 calls for creating/setting elements and attributes.
507     * <p/>
508     * Hence, if you specify a non-null namespace URI, these DOMs will never
509     * <p/>
510     * find a matching node.
511     *
512     * @param namespaceURI The namespace URI of the node to remove.
513     * @param localName The local name of the node to remove.
514     * @return The node removed from this map if a node with such a local
515     * <p/>
516     * name and namespace URI exists.
517     * @throws DOMException NOT_FOUND_ERR: Raised if there is no node with the specified
518     * <p/>
519     * <code>namespaceURI</code> and <code>localName</code> in this map.
520     * <p/>
521     * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this map is readonly.
522     * @since DOM Level 2
523     */

524
525    public Node JavaDoc removeNamedItemNS(String JavaDoc namespaceURI, String JavaDoc localName) throws DOMException JavaDoc
526    {
527
528
529       if (namespaceURI == null) namespaceURI = "";
530
531       if (localName == null)
532       {
533
534          Thread.dumpStack();
535
536          throw new java.lang.IllegalArgumentException JavaDoc("local name = null");
537
538       }
539
540
541       for (int i = 0; i < nodes.size(); i++)
542       {
543
544          Attr JavaDoc attr = (Attr JavaDoc)nodes.get(i);
545
546          // search if we have already
547

548          if (namespaceURI.equals(attr.getNamespaceURI()) &&
549
550                  localName.equals(attr.getLocalName()))
551          {
552
553             nodes.remove(i);
554
555             return attr; // return old one
556

557          }
558
559       }
560
561       return null;
562
563
564    }
565
566
567 }
568
569
Popular Tags