1 55 package groovy.xml; 56 57 import java.io.IOException ; 58 import java.io.ObjectInputStream ; 59 import java.io.Serializable ; 60 61 72 public class QName implements Serializable { 73 74 75 private static final String emptyString = "".intern(); 76 77 78 private String namespaceURI; 79 80 81 private String localPart; 82 83 84 private String prefix; 85 86 91 public QName(String localPart) { 92 this(emptyString, localPart, emptyString); 93 } 94 95 101 public QName(String namespaceURI, String localPart) { 102 this(namespaceURI, localPart, emptyString); 103 } 104 105 112 public QName(String namespaceURI, String localPart, String prefix) { 113 this.namespaceURI = (namespaceURI == null) 114 ? emptyString 115 : namespaceURI.intern(); 116 if (localPart == null) { 117 throw new IllegalArgumentException ("invalid QName local part"); 118 } else { 119 this.localPart = localPart.intern(); 120 } 121 122 if (prefix == null) { 123 throw new IllegalArgumentException ("invalid QName prefix"); 124 } else { 125 this.prefix = prefix.intern(); 126 } 127 } 128 129 134 public String getNamespaceURI() { 135 return namespaceURI; 136 } 137 138 143 public String getLocalPart() { 144 return localPart; 145 } 146 147 152 public String getPrefix() { 153 return prefix; 154 } 155 156 161 public String getQualifiedName() { 162 163 return ((prefix.equals(emptyString)) 164 ? localPart 165 : prefix + ':' + localPart); 166 } 167 168 173 public String toString() { 174 175 return ((namespaceURI.equals(emptyString)) 176 ? localPart 177 : '{' + namespaceURI + '}' + localPart); 178 } 179 180 199 public final boolean equals(Object obj) { 200 201 if (obj == this) { 202 return true; 203 } 204 205 if (!(obj instanceof QName)) { 206 return false; 207 } 208 209 if ((namespaceURI.equals(((QName) obj).namespaceURI)) 210 && (localPart == ((QName) obj).localPart)) { 211 return true; 212 } 213 214 return false; 215 } 216 217 233 public static QName valueOf(String s) { 234 235 if ((s == null) || s.equals("")) { 236 throw new IllegalArgumentException ("invalid QName literal"); 237 } 238 239 if (s.charAt(0) == '{') { 240 int i = s.indexOf('}'); 241 242 if (i == -1) { 243 throw new IllegalArgumentException ("invalid QName literal"); 244 } 245 246 if (i == s.length() - 1) { 247 throw new IllegalArgumentException ("invalid QName literal"); 248 } else { 249 return new QName(s.substring(1, i), s.substring(i + 1)); 250 } 251 } else { 252 return new QName(s); 253 } 254 } 255 256 264 public final int hashCode() { 265 return namespaceURI.hashCode() ^ localPart.hashCode(); 266 } 267 268 272 private void readObject(ObjectInputStream in) throws 273 IOException , ClassNotFoundException { 274 in.defaultReadObject(); 275 276 namespaceURI = namespaceURI.intern(); 277 localPart = localPart.intern(); 278 prefix = prefix.intern(); 279 } 280 } | Popular Tags |