| 1 |  |  | 
| 2 |  |  | 
| 3 |  |  | 
| 4 |  |  | 
| 5 |  |  | 
| 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 |  |  | 
| 16 |  |  | 
| 17 |  |  | 
| 18 |  |  | 
| 19 |  |  | 
| 20 |  |  | 
| 21 |  |  | 
| 22 |  |  | 
| 23 |  | public class Namespace extends AbstractNode { | 
| 24 |  |  | 
| 25 |  | protected static final NamespaceCache CACHE = new NamespaceCache(); | 
| 26 |  |  | 
| 27 |  |  | 
| 28 |  | public static final Namespace XML_NAMESPACE = CACHE.get("xml", | 
| 29 |  | "http://www.w3.org/XML/1998/namespace"); | 
| 30 |  |  | 
| 31 |  |  | 
| 32 |  | public static final Namespace NO_NAMESPACE = CACHE.get("", ""); | 
| 33 |  |  | 
| 34 |  |  | 
| 35 |  | private String prefix; | 
| 36 |  |  | 
| 37 |  |  | 
| 38 |  | private String uri; | 
| 39 |  |  | 
| 40 |  |  | 
| 41 |  | private int hashCode; | 
| 42 |  |  | 
| 43 |  |  | 
| 44 |  |  | 
| 45 |  |  | 
| 46 |  |  | 
| 47 |  |  | 
| 48 |  |  | 
| 49 |  |  | 
| 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 |  |  | 
| 58 |  |  | 
| 59 |  |  | 
| 60 |  |  | 
| 61 |  |  | 
| 62 |  |  | 
| 63 |  |  | 
| 64 |  |  | 
| 65 |  |  | 
| 66 |  |  | 
| 67 | 101172 | public static Namespace get(String prefix, String uri) { | 
| 68 | 101172 | return CACHE.get(prefix, uri); | 
| 69 |  | } | 
| 70 |  |  | 
| 71 |  |  | 
| 72 |  |  | 
| 73 |  |  | 
| 74 |  |  | 
| 75 |  |  | 
| 76 |  |  | 
| 77 |  |  | 
| 78 |  |  | 
| 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 |  |  | 
| 90 |  |  | 
| 91 |  |  | 
| 92 |  |  | 
| 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 |  |  | 
| 104 |  |  | 
| 105 |  |  | 
| 106 |  |  | 
| 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 |  |  | 
| 120 |  |  | 
| 121 |  |  | 
| 122 |  |  | 
| 123 |  |  | 
| 124 |  |  | 
| 125 |  |  | 
| 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 |  |  | 
| 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 |  |  | 
| 153 |  |  | 
| 154 |  |  | 
| 155 |  |  | 
| 156 | 69780 | public String getPrefix() { | 
| 157 | 69780 | return prefix; | 
| 158 |  | } | 
| 159 |  |  | 
| 160 |  |  | 
| 161 |  |  | 
| 162 |  |  | 
| 163 |  |  | 
| 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 |  |  | 
| 239 |  |  | 
| 240 |  |  | 
| 241 |  |  | 
| 242 |  |  | 
| 243 |  |  | 
| 244 |  |  | 
| 245 |  |  | 
| 246 |  |  | 
| 247 |  |  | 
| 248 |  |  | 
| 249 |  |  | 
| 250 |  |  | 
| 251 |  |  | 
| 252 |  |  | 
| 253 |  |  | 
| 254 |  |  | 
| 255 |  |  | 
| 256 |  |  | 
| 257 |  |  | 
| 258 |  |  | 
| 259 |  |  | 
| 260 |  |  | 
| 261 |  |  | 
| 262 |  |  | 
| 263 |  |  | 
| 264 |  |  | 
| 265 |  |  | 
| 266 |  |  | 
| 267 |  |  | 
| 268 |  |  | 
| 269 |  |  | 
| 270 |  |  | 
| 271 |  |  | 
| 272 |  |  |