Clover coverage report - dom4j - 1.6.1
Coverage timestamp: ma mei 16 2005 14:23:01 GMT+01:00

file stats: | LOC: | 272 |  | Methods: | 18
| NCLOC: | 113 |  | Classes: | 1

Source file | Conditionals | Statements | Methods | TOTAL

Namespace.java

81,8%

86,8%

83,3%

84,9%

1 |  | /*
2 |  | * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3 |  | *
4 |  | * This software is open source.
5 |  | * See the bottom of this file for the licence.
6 |  | */
7 |  |
8 |  | package org.dom4j;
9 |  |
10 |  | import org.dom4j.tree.AbstractNode;
11 |  | import org.dom4j.tree.DefaultNamespace;
12 |  | import org.dom4j.tree.NamespaceCache;
13 |  |
14 |  | /**
15 |  | * <p>
16 |  | * <code>Namespace</code> is a Flyweight Namespace that can be shared amongst
17 |  | * nodes.
18 |  | * </p>
19 |  | *
20 |  | * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
21 |  | * @version $Revision: 1.22 $
22 |  | */
23 |  | public class Namespace extends AbstractNode {
24 |  | /** Cache of Namespace instances */
25 |  | protected static final NamespaceCache CACHE = new NamespaceCache();
26 |  |
27 |  | /** XML Namespace */
28 |  | public static final Namespace XML_NAMESPACE = CACHE.get("xml",
29 |  | "http://www.w3.org/XML/1998/namespace");
30 |  |
31 |  | /** No Namespace present */
32 |  | public static final Namespace NO_NAMESPACE = CACHE.get("", "");
33 |  |
34 |  | /** The prefix mapped to this namespace */
35 |  | private String prefix;
36 |  |
37 |  | /** The URI for this namespace */
38 |  | private String uri;
39 |  |
40 |  | /** A cached version of the hashcode for efficiency */
41 |  | private int hashCode;
42 |  |
43 |  | /**
44 |  | * DOCUMENT ME!
45 |  | *
46 |  | * @param prefix
47 |  | * is the prefix for this namespace
48 |  | * @param uri
49 |  | * is the URI for this namespace
50 |  | */
51 | 10603 | public Namespace(String prefix, String uri) {
52 | 10603 | this.prefix = (prefix != null) ? prefix : "";
53 | 10603 | this.uri = (uri != null) ? uri : "";
54 |  | }
55 |  |
56 |  | /**
57 |  | * A helper method to return the Namespace instance for the given prefix and
58 |  | * URI
59 |  | *
60 |  | * @param prefix
61 |  | * DOCUMENT ME!
62 |  | * @param uri
63 |  | * DOCUMENT ME!
64 |  | *
65 |  | * @return an interned Namespace object
66 |  | */
67 | 101172 | public static Namespace get(String prefix, String uri) {
68 | 101172 | return CACHE.get(prefix, uri);
69 |  | }
70 |  |
71 |  | /**
72 |  | * A helper method to return the Namespace instance for no prefix and the
73 |  | * URI
74 |  | *
75 |  | * @param uri
76 |  | * DOCUMENT ME!
77 |  | *
78 |  | * @return an interned Namespace object
79 |  | */
80 | 26 | public static Namespace get(String uri) {
81 | 26 | return CACHE.get(uri);
82 |  | }
83 |  |
84 | 732 | public short getNodeType() {
85 | 732 | return NAMESPACE_NODE;
86 |  | }
87 |  |
88 |  | /**
89 |  | * DOCUMENT ME!
90 |  | *
91 |  | * @return the hash code based on the qualified name and the URI of the
92 |  | * namespace.
93 |  | */
94 | 21734 | public int hashCode() {
95 | 21734 | if (hashCode == 0) {
96 | 312 | hashCode = createHashCode();
97 |  | }
98 |  |
99 | 21734 | return hashCode;
100 |  | }
101 |  |
102 |  | /**
103 |  | * Factory method to create the hashcode allowing derived classes to change
104 |  | * the behaviour
105 |  | *
106 |  | * @return DOCUMENT ME!
107 |  | */
108 | 312 | protected int createHashCode() {
109 | 312 | int result = uri.hashCode() ^ prefix.hashCode();
110 |  |
111 | 312 | if (result == 0) {
112 | 8 | result = 0xbabe;
113 |  | }
114 |  |
115 | 312 | return result;
116 |  | }
117 |  |
118 |  | /**
119 |  | * Checks whether this Namespace equals the given Namespace. Two Namespaces
120 |  | * are equals if their URI and prefix are equal.
121 |  | *
122 |  | * @param object
123 |  | * DOCUMENT ME!
124 |  | *
125 |  | * @return DOCUMENT ME!
126 |  | */
127 | 5594 | public boolean equals(Object object) {
128 | 5594 | if (this == object) {
129 | 4783 | return true;
130 | 811 | } else if (object instanceof Namespace) {
131 | 626 | Namespace that = (Namespace) object;
132 |  |
133 |  | // we cache hash codes so this should be quick
134 | 626 | if (hashCode() == that.hashCode()) {
135 | 148 | return uri.equals(that.getURI())
136 |  | && prefix.equals(that.getPrefix());
137 |  | }
138 |  | }
139 |  |
140 | 663 | return false;
141 |  | }
142 |  |
143 | 0 | public String getText() {
144 | 0 | return uri;
145 |  | }
146 |  |
147 | 0 | public String getStringValue() {
148 | 0 | return uri;
149 |  | }
150 |  |
151 |  | /**
152 |  | * DOCUMENT ME!
153 |  | *
154 |  | * @return the prefix for this <code>Namespace</code>.
155 |  | */
156 | 69780 | public String getPrefix() {
157 | 69780 | return prefix;
158 |  | }
159 |  |
160 |  | /**
161 |  | * DOCUMENT ME!
162 |  | *
163 |  | * @return the URI for this <code>Namespace</code>.
164 |  | */
165 | 351051 | public String getURI() {
166 | 351051 | return uri;
167 |  | }
168 |  |
169 | 12 | public String getXPathNameStep() {
170 | 12 | if ((prefix != null) && !"".equals(prefix)) {
171 | 6 | return "namespace::" + prefix;
172 |  | }
173 |  |
174 | 6 | return "namespace::*[name()='']";
175 |  | }
176 |  |
177 | 6 | public String getPath(Element context) {
178 | 6 | StringBuffer path = new StringBuffer(10);
179 | 6 | Element parent = getParent();
180 |  |
181 | 6 | if ((parent != null) && (parent != context)) {
182 | 0 | path.append(parent.getPath(context));
183 | 0 | path.append('/');
184 |  | }
185 |  |
186 | 6 | path.append(getXPathNameStep());
187 |  |
188 | 6 | return path.toString();
189 |  | }
190 |  |
191 | 6 | public String getUniquePath(Element context) {
192 | 6 | StringBuffer path = new StringBuffer(10);
193 | 6 | Element parent = getParent();
194 |  |
195 | 6 | if ((parent != null) && (parent != context)) {
196 | 0 | path.append(parent.getUniquePath(context));
197 | 0 | path.append('/');
198 |  | }
199 |  |
200 | 6 | path.append(getXPathNameStep());
201 |  |
202 | 6 | return path.toString();
203 |  | }
204 |  |
205 | 32 | public String toString() {
206 | 32 | return super.toString() + " [Namespace: prefix " + getPrefix()
207 |  | + " mapped to URI \"" + getURI() + "\"]";
208 |  | }
209 |  |
210 | 26 | public String asXML() {
211 | 26 | StringBuffer asxml = new StringBuffer(10);
212 | 26 | String pref = getPrefix();
213 |  |
214 | 26 | if ((pref != null) && (pref.length() > 0)) {
215 | 14 | asxml.append("xmlns:");
216 | 14 | asxml.append(pref);
217 | 14 | asxml.append("=\"");
218 |  | } else {
219 | 12 | asxml.append("xmlns=\"");
220 |  | }
221 |  |
222 | 26 | asxml.append(getURI());
223 | 26 | asxml.append("\"");
224 |  |
225 | 26 | return asxml.toString();
226 |  | }
227 |  |
228 | 0 | public void accept(Visitor visitor) {
229 | 0 | visitor.visit(this);
230 |  | }
231 |  |
232 | 107 | protected Node createXPathResult(Element parent) {
233 | 107 | return new DefaultNamespace(parent, getPrefix(), getURI());
234 |  | }
235 |  | }
236 |  |
237 |  | /*
238 |  | * Redistribution and use of this software and associated documentation
239 |  | * ("Software"), with or without modification, are permitted provided that the
240 |  | * following conditions are met:
241 |  | *
242 |  | * 1. Redistributions of source code must retain copyright statements and
243 |  | * notices. Redistributions must also contain a copy of this document.
244 |  | *
245 |  | * 2. Redistributions in binary form must reproduce the above copyright notice,
246 |  | * this list of conditions and the following disclaimer in the documentation
247 |  | * and/or other materials provided with the distribution.
248 |  | *
249 |  | * 3. The name "DOM4J" must not be used to endorse or promote products derived
250 |  | * from this Software without prior written permission of MetaStuff, Ltd. For
251 |  | * written permission, please contact dom4j-info@metastuff.com.
252 |  | *
253 |  | * 4. Products derived from this Software may not be called "DOM4J" nor may
254 |  | * "DOM4J" appear in their names without prior written permission of MetaStuff,
255 |  | * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
256 |  | *
257 |  | * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
258 |  | *
259 |  | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
260 |  | * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261 |  | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
262 |  | * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
263 |  | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
264 |  | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
265 |  | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266 |  | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
267 |  | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
268 |  | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
269 |  | * POSSIBILITY OF SUCH DAMAGE.
270 |  | *
271 |  | * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
272 |  | */