1 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 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 51 public Namespace(String prefix, String uri) { 52 this.prefix = (prefix != null) ? prefix : ""; 53 this.uri = (uri != null) ? uri : ""; 54 } 55 56 67 public static Namespace get(String prefix, String uri) { 68 return CACHE.get(prefix, uri); 69 } 70 71 80 public static Namespace get(String uri) { 81 return CACHE.get(uri); 82 } 83 84 public short getNodeType() { 85 return NAMESPACE_NODE; 86 } 87 88 94 public int hashCode() { 95 if (hashCode == 0) { 96 hashCode = createHashCode(); 97 } 98 99 return hashCode; 100 } 101 102 108 protected int createHashCode() { 109 int result = uri.hashCode() ^ prefix.hashCode(); 110 111 if (result == 0) { 112 result = 0xbabe; 113 } 114 115 return result; 116 } 117 118 127 public boolean equals(Object object) { 128 if (this == object) { 129 return true; 130 } else if (object instanceof Namespace) { 131 Namespace that = (Namespace) object; 132 133 if (hashCode() == that.hashCode()) { 135 return uri.equals(that.getURI()) 136 && prefix.equals(that.getPrefix()); 137 } 138 } 139 140 return false; 141 } 142 143 public String getText() { 144 return uri; 145 } 146 147 public String getStringValue() { 148 return uri; 149 } 150 151 156 public String getPrefix() { 157 return prefix; 158 } 159 160 165 public String getURI() { 166 return uri; 167 } 168 169 public String getXPathNameStep() { 170 if ((prefix != null) && !"".equals(prefix)) { 171 return "namespace::" + prefix; 172 } 173 174 return "namespace::*[name()='']"; 175 } 176 177 public String getPath(Element context) { 178 StringBuffer path = new StringBuffer (10); 179 Element parent = getParent(); 180 181 if ((parent != null) && (parent != context)) { 182 path.append(parent.getPath(context)); 183 path.append('/'); 184 } 185 186 path.append(getXPathNameStep()); 187 188 return path.toString(); 189 } 190 191 public String getUniquePath(Element context) { 192 StringBuffer path = new StringBuffer (10); 193 Element parent = getParent(); 194 195 if ((parent != null) && (parent != context)) { 196 path.append(parent.getUniquePath(context)); 197 path.append('/'); 198 } 199 200 path.append(getXPathNameStep()); 201 202 return path.toString(); 203 } 204 205 public String toString() { 206 return super.toString() + " [Namespace: prefix " + getPrefix() 207 + " mapped to URI \"" + getURI() + "\"]"; 208 } 209 210 public String asXML() { 211 StringBuffer asxml = new StringBuffer (10); 212 String pref = getPrefix(); 213 214 if ((pref != null) && (pref.length() > 0)) { 215 asxml.append("xmlns:"); 216 asxml.append(pref); 217 asxml.append("=\""); 218 } else { 219 asxml.append("xmlns=\""); 220 } 221 222 asxml.append(getURI()); 223 asxml.append("\""); 224 225 return asxml.toString(); 226 } 227 228 public void accept(Visitor visitor) { 229 visitor.visit(this); 230 } 231 232 protected Node createXPathResult(Element parent) { 233 return new DefaultNamespace(parent, getPrefix(), getURI()); 234 } 235 } 236 237 273 | Popular Tags |