1 4 package gnu.mapping; 5 import java.io.*; 6 7 13 15 16 23 24 public class Symbol 25 26 28 implements 29 EnvironmentKey, 30 31 Comparable , 32 33 Externalizable 34 { 35 36 protected String name; 37 38 Namespace namespace; 39 40 public final Symbol getKeySymbol () { return this; } 41 public final Object getKeyProperty () { return null; } 42 public boolean matches (EnvironmentKey key) 43 { 44 return equals(key.getKeySymbol(), this) && key.getKeyProperty() == null; 45 } 46 public boolean matches (Symbol symbol, Object property) 47 { 48 return equals(symbol, this) && property == null; 49 } 50 51 52 public final String getNamespaceURI() 53 { 54 Namespace ns = getNamespace(); 55 return ns == null ? null : ns.getName(); 56 } 57 58 public final String getLocalPart() 59 { 60 return name; 61 } 62 63 public final String getPrefix () 64 { 65 Namespace ns = namespace; 66 return ns == null ? "" : ns.prefix; 67 } 68 69 70 public final boolean hasEmptyNamespace () 71 { 72 Namespace ns = getNamespace(); 73 String nsname; 74 return (ns == null 75 || (nsname = ns.getName()) == null || nsname.length() == 0); 76 } 77 78 80 public final String getLocalName() 81 { 82 83 85 return name; 86 87 } 88 89 public final String getName() 90 { 91 92 94 return name; 95 96 } 97 98 103 public static Symbol make (String uri, String name, String prefix) 104 { 105 return Namespace.make(uri, prefix).getSymbol(name.intern()); 106 } 107 108 114 public static Symbol make (Object namespace, String name) 115 { 116 Namespace ns = namespace instanceof String 117 ? Namespace.getInstance((String ) namespace) 118 : (Namespace) namespace; 119 if (ns == null || name == null) 120 return makeUninterned(name); 121 return ns.getSymbol(name.intern()); 122 } 123 124 137 public static Symbol parse (String symbol) 138 { 139 if (symbol.length() > 0 && symbol.charAt(0) == '{') 140 { 141 int rbrace = symbol.lastIndexOf('}'); 142 if (rbrace <= 0) 143 { 144 throw new RuntimeException ("missing '}' in property name '"+symbol+"'"); 145 } 146 return Symbol.make(symbol.substring(1, rbrace), symbol.substring(rbrace+1), ""); 147 } 148 int colon = symbol.indexOf(':'); 149 if (colon > 0) 150 { 151 return Symbol.makeWithUnknownNamespace(symbol.substring(colon+1), 152 symbol.substring(0, colon)); 153 } 154 else 155 { 156 return Symbol.make("", symbol, ""); 157 } 158 } 159 160 167 public static Symbol makeWithUnknownNamespace (String local, String prefix) 168 { 169 return Namespace.makeUnknownNamespace(prefix).getSymbol(local.intern()); 170 } 171 172 public Symbol () 173 { 174 175 177 } 178 179 public static Symbol makeUninterned (String name) 180 { 181 182 197 return new Symbol(null, name); 198 199 } 200 201 205 public Symbol (Namespace ns, String name) 206 { 207 208 210 this.name = name; 211 212 this.namespace = ns; 213 } 214 215 public int compareTo(Object o) 216 { 217 Symbol other = (Symbol) o; 218 if (getNamespaceURI() != other.getNamespaceURI()) 219 throw new IllegalArgumentException ("comparing Symbols in different namespaces"); 220 return getLocalName().compareTo(other.getLocalName()); 221 } 222 223 public static boolean equals (Symbol sym1, Symbol sym2) 224 { 225 if (sym1 == sym2) 226 return true; 227 if (sym1 == null || sym2 == null) 228 return false; 229 230 232 if (sym1.name == sym2.name) 233 234 { 235 Namespace namespace1 = sym1.namespace; 236 Namespace namespace2 = sym2.namespace; 237 return (namespace1 != null && namespace2 != null 238 && (namespace1 == namespace2 239 || namespace1.name == namespace2.name)); 240 } 241 return false; 242 } 243 244 245 247 public final boolean equals (Object o) 248 { 249 return o instanceof Symbol && equals(this, (Symbol) o); 250 } 251 252 public int hashCode () 253 { 254 return name == null ? 0 : name.hashCode(); 255 } 256 257 258 public final Namespace getNamespace() 259 { 260 return namespace; 261 } 262 263 public final void setNamespace (Namespace ns) 264 { 265 namespace = ns; 266 } 267 268 269 public static final Symbol FUNCTION = makeUninterned("(function)"); 270 271 281 public static final Symbol PLIST = makeUninterned("(property-list)"); 282 283 public String toString() 284 { 285 String uri = getNamespaceURI(); 287 if (uri == null || uri.length() == 0) 288 return getName(); 289 StringBuffer sbuf = new StringBuffer (); 290 String prefix = getPrefix(); 291 if (prefix == null || prefix.length() == 0) 292 { 293 sbuf.append('{'); 294 sbuf.append(getNamespaceURI()); 295 sbuf.append('}'); 296 } 297 else 298 { 299 sbuf.append(prefix); 300 sbuf.append(':'); 301 } 302 sbuf.append(getName()); 303 return sbuf.toString(); 304 } 305 306 public void writeExternal(ObjectOutput out) throws IOException 307 { 308 Namespace ns = getNamespace(); 309 out.writeObject(ns); 310 out.writeObject(getName()); 311 } 312 313 public void readExternal(ObjectInput in) 314 throws IOException, ClassNotFoundException 315 { 316 317 319 namespace = (Namespace) in.readObject(); 320 name = (String ) in.readObject(); 321 322 } 323 324 public Object readResolve() throws ObjectStreamException 325 { 326 if (namespace == null) 327 return this; 328 return make(namespace, getName()); 329 } 330 } 331 | Popular Tags |