1 9 10 package org.dom4j.tree; 11 12 13 import org.dom4j.DocumentFactory; 14 import org.dom4j.Namespace; 15 import org.dom4j.NodeFactory; 16 import org.dom4j.QName; 17 18 import java.io.IOException ; 19 import java.io.ObjectInputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.io.Serializable ; 22 23 30 public class DefaultQName implements Serializable , QName { 31 32 protected transient static ThreadLocal cachePerThread = new ThreadLocal (); 34 35 36 private String name; 37 38 39 private String qualifiedName; 40 41 42 private transient Namespace namespace; 43 44 45 private int hashCode; 46 47 48 private NodeFactory nodeFactory; 49 50 51 public static QName get(String name) { 52 return getCache().get(name); 53 } 54 55 public static QName get(String name, Namespace namespace) { 56 return getCache().get(name, namespace); 57 } 58 59 public static QName get(String name, String prefix, String uri) { 60 return getCache().get(name, AbstractNamespace.get( prefix, uri )); 61 } 62 63 public static QName get(String qualifiedName, String uri) { 64 return getCache().get(qualifiedName, uri); 65 } 66 67 public static QName get(String localName, Namespace namespace, String qualifiedName) { 68 return getCache().get(localName, namespace, qualifiedName); 69 } 70 71 public DefaultQName(String name) { 72 this( name, AbstractNamespace.NO_NAMESPACE ); 73 } 74 75 public DefaultQName(String name, Namespace namespace) { 76 this.name = (name == null) ? "" : name; 77 this.namespace = (namespace == null) ? AbstractNamespace.NO_NAMESPACE : namespace; 78 } 79 80 public DefaultQName(String name, Namespace namespace, String qualifiedName) { 81 this.name = (name == null) ? "" : name; 82 this.qualifiedName = qualifiedName; 83 this.namespace = (namespace == null) ? AbstractNamespace.NO_NAMESPACE : namespace; 84 } 85 86 87 89 public String getName() { 90 return name; 91 } 92 93 95 public String getQualifiedName() { 96 if ( qualifiedName == null ) { 97 String prefix = getNamespacePrefix(); 98 if ( prefix != null && prefix.length() > 0 ) { 99 qualifiedName = prefix + ":" + name; 100 } 101 else { 102 qualifiedName = name; 103 } 104 } 105 return qualifiedName; 106 } 107 108 110 public Namespace getNamespace() { 111 return namespace; 112 } 113 114 116 public String getNamespacePrefix() { 117 if ( namespace == null ) { 118 return ""; 119 } 120 return namespace.getPrefix(); 121 } 122 123 125 public String getNamespaceURI() { 126 if ( namespace == null ) { 127 return ""; 128 } 129 return namespace.getURI(); 130 } 131 132 133 136 public int hashCode() { 137 if ( hashCode == 0 ) { 138 hashCode = getName().hashCode() 139 ^ getNamespaceURI().hashCode(); 140 if ( hashCode == 0 ) { 141 hashCode = 0xbabe; 142 } 143 } 144 return hashCode; 145 } 146 147 public boolean equals(Object object) { 148 if ( this == object ) { 149 return true; 150 } 151 else if ( object instanceof QName ) { 152 QName that = (QName) object; 153 if ( hashCode() == that.hashCode() ) { 155 return getName().equals( that.getName() ) 156 && getNamespaceURI().equals( that.getNamespaceURI()); 157 } 158 } 159 return false; 160 } 161 162 public String toString() { 163 return super.toString() + " [name: " + getName() 164 + " namespace: \"" + getNamespace() + "\"]"; 165 } 166 167 170 public DocumentFactory getDocumentFactory() { 171 if (getNodeFactory() instanceof DocumentFactory) { 172 return (DocumentFactory) nodeFactory; 173 } else { 174 return null; 175 } 176 } 177 178 public NodeFactory getNodeFactory() { 179 return nodeFactory; 180 } 181 182 185 public void setDocumentFactory(DocumentFactory documentFactory) { 186 this.nodeFactory = documentFactory; 187 } 188 189 public void setNodeFactory(NodeFactory nodeFactory) { 190 this.nodeFactory = nodeFactory; 191 } 192 193 private void writeObject(ObjectOutputStream out) throws IOException { 194 195 out.writeObject(namespace.getPrefix()); 198 out.writeObject(namespace.getURI()); 199 200 out.defaultWriteObject(); 201 } 202 203 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 204 205 String prefix = (String ) in.readObject(); 206 String uri = (String ) in.readObject(); 207 208 in.defaultReadObject(); 209 210 namespace = AbstractNamespace.get( prefix, uri ); 211 } 212 213 214 private static QNameCache getCache() { 215 QNameCache cache = (QNameCache) cachePerThread.get(); 216 if (cache==null) { 217 cache = new QNameCache(); 218 cachePerThread.set(cache); 219 } 220 return cache; 221 } 222 223 } 224 225 226 227 228 272 | Popular Tags |