1 56 57 package org.jdom; 58 59 import java.util.*; 60 61 74 public final class Namespace { 75 76 79 86 private static final String CVS_ID = 87 "@(#) $RCSfile: Namespace.java,v $ $Revision: 1.42 $ $Date: 2004/12/11 00:46:02 $ $Name: $"; 88 89 94 private static HashMap namespaces; 95 96 97 public static final Namespace NO_NAMESPACE = new Namespace("", ""); 98 99 100 public static final Namespace XML_NAMESPACE = 101 new Namespace("xml", "http://www.w3.org/XML/1998/namespace"); 102 103 104 private String prefix; 105 106 107 private String uri; 108 109 113 static { 114 namespaces = new HashMap(16); 115 116 namespaces.put(new NamespaceKey(NO_NAMESPACE), NO_NAMESPACE); 118 namespaces.put(new NamespaceKey(XML_NAMESPACE), XML_NAMESPACE); 119 } 120 121 132 public static Namespace getNamespace(String prefix, String uri) { 133 if ((prefix == null) || (prefix.trim().equals(""))) { 135 if ((uri == null) || (uri.trim().equals(""))) { 137 return NO_NAMESPACE; 138 } 139 prefix = ""; 140 } 141 else if ((uri == null) || (uri.trim().equals(""))) { 142 uri = ""; 143 } 144 145 NamespaceKey lookup = new NamespaceKey(prefix, uri); 150 Namespace preexisting = (Namespace) namespaces.get(lookup); 151 if (preexisting != null) { 152 return preexisting; 153 } 154 155 String reason; 157 if ((reason = Verifier.checkNamespacePrefix(prefix)) != null) { 158 throw new IllegalNameException(prefix, "Namespace prefix", reason); 159 } 160 if ((reason = Verifier.checkNamespaceURI(uri)) != null) { 161 throw new IllegalNameException(uri, "Namespace URI", reason); 162 } 163 164 if ((!prefix.equals("")) && (uri.equals(""))) { 166 throw new IllegalNameException("", "namespace", 167 "Namespace URIs must be non-null and non-empty Strings"); 168 } 169 170 if (prefix.equals("xml")) { 177 throw new IllegalNameException(prefix, "Namespace prefix", 178 "The xml prefix can only be bound to " + 179 "http://www.w3.org/XML/1998/namespace"); 180 } 181 182 if (uri.equals("http://www.w3.org/XML/1998/namespace")) { 185 throw new IllegalNameException(uri, "Namespace URI", 186 "The http://www.w3.org/XML/1998/namespace must be bound to " + 187 "the xml prefix."); 188 } 189 190 Namespace ns = new Namespace(prefix, uri); 192 namespaces.put(lookup, ns); 193 return ns; 194 } 195 196 204 public static Namespace getNamespace(String uri) { 205 return getNamespace("", uri); 206 } 207 208 216 private Namespace(String prefix, String uri) { 217 this.prefix = prefix; 218 this.uri = uri; 219 } 220 221 226 public String getPrefix() { 227 return prefix; 228 } 229 230 235 public String getURI() { 236 return uri; 237 } 238 239 247 public boolean equals(Object ob) { 248 if (this == ob) { 249 return true; 250 } 251 if (ob instanceof Namespace) { return uri.equals(((Namespace)ob).uri); 253 } 254 return false; 255 } 256 257 263 public String toString() { 264 return "[Namespace: prefix \"" + prefix + "\" is mapped to URI \"" + 265 uri + "\"]"; 266 } 267 268 275 public int hashCode() { 276 return uri.hashCode(); 277 } 278 } 279 | Popular Tags |