1 7 8 package org.dom4j; 9 10 import java.io.IOException ; 11 import java.io.ObjectInputStream ; 12 import java.io.ObjectOutputStream ; 13 import java.io.Serializable ; 14 15 import org.dom4j.tree.QNameCache; 16 import org.dom4j.util.SingletonStrategy; 17 18 27 public class QName implements Serializable { 28 29 private static SingletonStrategy singleton = null; 30 31 static { 32 try { 33 String defaultSingletonClass = "org.dom4j.util.SimpleSingleton"; 34 Class clazz = null; 35 try { 36 String singletonClass = defaultSingletonClass; 37 singletonClass = System.getProperty( 38 "org.dom4j.QName.singleton.strategy", singletonClass); 39 clazz = Class.forName(singletonClass); 40 } catch (Exception exc1) { 41 try { 42 String singletonClass = defaultSingletonClass; 43 clazz = Class.forName(singletonClass); 44 } catch (Exception exc2) { 45 } 46 } 47 singleton = (SingletonStrategy) clazz.newInstance(); 48 singleton.setSingletonClassName(QNameCache.class.getName()); 49 } catch (Exception exc3) { 50 } 51 } 52 53 54 private String name; 55 56 57 private String qualifiedName; 58 59 60 private transient Namespace namespace; 61 62 63 private int hashCode; 64 65 66 private DocumentFactory documentFactory; 67 68 public QName(String name) { 69 this(name, Namespace.NO_NAMESPACE); 70 } 71 72 public QName(String name, Namespace namespace) { 73 this.name = (name == null) ? "" : name; 74 this.namespace = (namespace == null) ? Namespace.NO_NAMESPACE 75 : namespace; 76 } 77 78 public QName(String name, Namespace namespace, String qualifiedName) { 79 this.name = (name == null) ? "" : name; 80 this.qualifiedName = qualifiedName; 81 this.namespace = (namespace == null) ? Namespace.NO_NAMESPACE 82 : namespace; 83 } 84 85 public static QName get(String name) { 86 return getCache().get(name); 87 } 88 89 public static QName get(String name, Namespace namespace) { 90 return getCache().get(name, namespace); 91 } 92 93 public static QName get(String name, String prefix, String uri) { 94 if (((prefix == null) || (prefix.length() == 0)) && (uri == null)) { 95 return QName.get(name); 96 } else if ((prefix == null) || (prefix.length() == 0)) { 97 return getCache().get(name, Namespace.get(uri)); 98 } else if (uri == null) { 99 return QName.get(name); 100 } else { 101 return getCache().get(name, Namespace.get(prefix, uri)); 102 } 103 } 104 105 public static QName get(String qualifiedName, String uri) { 106 if (uri == null) { 107 return getCache().get(qualifiedName); 108 } else { 109 return getCache().get(qualifiedName, uri); 110 } 111 } 112 113 public static QName get(String localName, Namespace namespace, 114 String qualifiedName) { 115 return getCache().get(localName, namespace, qualifiedName); 116 } 117 118 123 public String getName() { 124 return name; 125 } 126 127 132 public String getQualifiedName() { 133 if (qualifiedName == null) { 134 String prefix = getNamespacePrefix(); 135 136 if ((prefix != null) && (prefix.length() > 0)) { 137 qualifiedName = prefix + ":" + name; 138 } else { 139 qualifiedName = name; 140 } 141 } 142 143 return qualifiedName; 144 } 145 146 151 public Namespace getNamespace() { 152 return namespace; 153 } 154 155 160 public String getNamespacePrefix() { 161 if (namespace == null) { 162 return ""; 163 } 164 165 return namespace.getPrefix(); 166 } 167 168 173 public String getNamespaceURI() { 174 if (namespace == null) { 175 return ""; 176 } 177 178 return namespace.getURI(); 179 } 180 181 187 public int hashCode() { 188 if (hashCode == 0) { 189 hashCode = getName().hashCode() ^ getNamespaceURI().hashCode(); 190 191 if (hashCode == 0) { 192 hashCode = 0xbabe; 193 } 194 } 195 196 return hashCode; 197 } 198 199 public boolean equals(Object object) { 200 if (this == object) { 201 return true; 202 } else if (object instanceof QName) { 203 QName that = (QName) object; 204 205 if (hashCode() == that.hashCode()) { 207 return getName().equals(that.getName()) 208 && getNamespaceURI().equals(that.getNamespaceURI()); 209 } 210 } 211 212 return false; 213 } 214 215 public String toString() { 216 return super.toString() + " [name: " + getName() + " namespace: \"" 217 + getNamespace() + "\"]"; 218 } 219 220 225 public DocumentFactory getDocumentFactory() { 226 return documentFactory; 227 } 228 229 public void setDocumentFactory(DocumentFactory documentFactory) { 230 this.documentFactory = documentFactory; 231 } 232 233 private void writeObject(ObjectOutputStream out) throws IOException { 234 out.writeObject(namespace.getPrefix()); 237 out.writeObject(namespace.getURI()); 238 239 out.defaultWriteObject(); 240 } 241 242 private void readObject(ObjectInputStream in) throws IOException , 243 ClassNotFoundException { 244 String prefix = (String ) in.readObject(); 245 String uri = (String ) in.readObject(); 246 247 in.defaultReadObject(); 248 249 namespace = Namespace.get(prefix, uri); 250 } 251 252 private static QNameCache getCache() { 253 QNameCache cache = (QNameCache) singleton.instance(); 254 return cache; 255 } 256 } 257 258 259 260 | Popular Tags |