|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
NamedNodeMapImpl.java | 75% | 76.7% | 71.4% | 75.5% |
|
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | * | |
17 | * $Id: NamedNodeMapImpl.java 541508 2007-05-25 01:54:12Z vgritsenko $ | |
18 | */ | |
19 | ||
20 | package org.apache.xindice.xml.dom; | |
21 | ||
22 | import org.w3c.dom.DOMException; | |
23 | import org.w3c.dom.NamedNodeMap; | |
24 | import org.w3c.dom.Node; | |
25 | ||
26 | import java.util.HashMap; | |
27 | import java.util.Iterator; | |
28 | ||
29 | /** | |
30 | * NamedNodeMapImpl | |
31 | * | |
32 | * @version $Revision: 541508 $, $Date: 2007-05-24 18:54:12 -0700 (Thu, 24 May 2007) $ | |
33 | */ | |
34 | public final class NamedNodeMapImpl extends NodeListImpl implements NamedNodeMap { | |
35 | private HashMap map = new HashMap(); | |
36 | ||
37 | 276677 | public NamedNodeMapImpl(NodeImpl owner) { |
38 | 276677 | super(owner); |
39 | } | |
40 | ||
41 | /** | |
42 | * Retrieves a node specified by name. | |
43 | * @param name Name of a node to retrieve. | |
44 | * @return A <code>Node</code> (of any type) with the specified name, or | |
45 | * <code>null</code> if the specified name did not identify any node in | |
46 | * the map. | |
47 | */ | |
48 | 898878 | public Node getNamedItem(String name) { |
49 | 898878 | return (Node) map.get(name); |
50 | } | |
51 | ||
52 | /** | |
53 | * Adds a node using its <code>nodeName</code> attribute. | |
54 | * <br>As the <code>nodeName</code> attribute is used to derive the name | |
55 | * which the node must be stored under, multiple nodes of certain types | |
56 | * (those that have a "special" string value) cannot be stored as the names | |
57 | * would clash. This is seen as preferable to allowing nodes to be aliased. | |
58 | * @param arg A node to store in a named node map. The node will later be | |
59 | * accessible using the value of the <code>nodeName</code> attribute of | |
60 | * the node. If a node with that name is already present in the map, it | |
61 | * is replaced by the new one. | |
62 | * @return If the new <code>Node</code> replaces an existing node with the | |
63 | * same name the previously existing <code>Node</code> is returned, | |
64 | * otherwise <code>null</code> is returned. | |
65 | * @exception DOMException | |
66 | * WRONG_DOCUMENT_ERR: Raised if <code>arg</code> was created from a | |
67 | * different document than the one that created the | |
68 | * <code>NamedNodeMap</code>. | |
69 | * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this | |
70 | * <code>NamedNodeMap</code> is readonly. | |
71 | * <br>INUSE_ATTRIBUTE_ERR: Raised if <code>arg</code> is an | |
72 | * <code>Attr</code> that is already an attribute of another | |
73 | * <code>Element</code> object. The DOM user must explicitly clone | |
74 | * <code>Attr</code> nodes to re-use them in other elements. | |
75 | */ | |
76 | 218026 | public Node setNamedItem(Node arg) throws DOMException { |
77 | 218026 | String name = arg.getNodeName(); |
78 | 218027 | Node node = (Node) map.get(name); |
79 | 218027 | if (node != null) { |
80 | 557 | int idx = indexOf(node); |
81 | 557 | set(idx, arg); |
82 | } else { | |
83 | 217470 | add(arg); |
84 | } | |
85 | 218027 | map.put(arg.getNodeName(), arg); |
86 | 218027 | return node; |
87 | } | |
88 | ||
89 | /** | |
90 | * Removes a node specified by name. If the removed node is an | |
91 | * <code>Attr</code> with a default value it is immediately replaced. | |
92 | * @param name The name of a node to remove. | |
93 | * @return The node removed from the map or <code>null</code> if no node | |
94 | * with such a name exists. | |
95 | * @exception DOMException | |
96 | * NOT_FOUND_ERR: Raised if there is no node named <code>name</code> in | |
97 | * the map. | |
98 | */ | |
99 | 0 | public Node removeNamedItem(String name) throws DOMException { |
100 | 0 | Node node = (Node) map.get(name); |
101 | 0 | map.remove(name); |
102 | 0 | remove(node); |
103 | 0 | return node; |
104 | } | |
105 | ||
106 | 354 | public Node getNamedItemNS(String namespaceURI, String localName) { |
107 | 354 | Node node; |
108 | 354 | for (Iterator iter = iterator(); iter.hasNext(); ) { |
109 | 1378 | node = (Node) iter.next(); |
110 | 1378 | String ns = node.getNamespaceURI(); |
111 | 1378 | String ln = node.getLocalName(); |
112 | 1378 | if (ns != null |
113 | && ln != null | |
114 | && ns.equals(namespaceURI) | |
115 | && ln.equals(localName)) { | |
116 | 346 | return node; |
117 | } | |
118 | } | |
119 | 8 | return null; |
120 | } | |
121 | ||
122 | 0 | public Node setNamedItemNS(Node arg) throws DOMException { |
123 | 0 | return setNamedItem(arg); |
124 | } | |
125 | ||
126 | /** | |
127 | * Removes an attribute by local name and namespace URI. If the | |
128 | * removed attribute has a default value it is immediately | |
129 | * replaced. The replacing attribute has the same namespace URI | |
130 | * and local name, as well as the original prefix. as the | |
131 | * original prefix. If the named attribute is not present, this | |
132 | * method has no effect (per clarification in DOM2 errata). | |
133 | * | |
134 | * <br> HTML-only DOM implementations do not need to implement | |
135 | * this method. | |
136 | * | |
137 | * @param namespaceURI The namespace URI of the attribute to remove. | |
138 | * @param localName The local name of the attribute to remove. | |
139 | **/ | |
140 | 2 | public Node removeNamedItemNS(String namespaceURI, String localName) throws DOMException { |
141 | 2 | Node node; |
142 | 2 | for (Iterator iter = iterator(); iter.hasNext(); ) { |
143 | 6 | node = (Node) iter.next(); |
144 | 6 | if (localName.equals(node.getLocalName())) { |
145 | 0 | if ((namespaceURI == null && node.getNamespaceURI() == null) || |
146 | (namespaceURI != null && namespaceURI.equals(node.getNamespaceURI()))) { | |
147 | 0 | return removeNamedItem(node.getNodeName()); |
148 | } | |
149 | } | |
150 | } | |
151 | 2 | return null; |
152 | } | |
153 | } | |
154 |
|