| 1 9 10 package org.ozoneDB.xml.dom4j.o3impl; 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 71 public DefaultQName(String name, NodeFactory factory) { 72 this(name, factory.getNoNamespace(), factory); 73 setNodeFactory(factory); 74 } 75 76 public DefaultQName(String name, Namespace namespace, NodeFactory nodeFactory) { 77 this.name = (name == null) ? "" : name; 78 this.namespace = (namespace == null) ? nodeFactory.getNoNamespace() : namespace; 79 setNodeFactory(nodeFactory); 80 } 81 82 public DefaultQName(String name, Namespace namespace, String qualifiedName, NodeFactory nodeFactory) { 83 this.name = (name == null) ? "" : name; 84 this.qualifiedName = qualifiedName; 85 this.namespace = (namespace == null) ? nodeFactory.getNoNamespace() : namespace; 86 setNodeFactory(nodeFactory); 87 } 88 89 90 92 public String getName() { 93 return name; 94 } 95 96 98 public String getQualifiedName() { 99 if (qualifiedName == null) { 100 String prefix = getNamespacePrefix(); 101 if (prefix != null && prefix.length() > 0) { 102 qualifiedName = prefix + ":" + name; 103 } else { 104 qualifiedName = name; 105 } 106 } 107 return qualifiedName; 108 } 109 110 112 public Namespace getNamespace() { 113 return namespace; 114 } 115 116 118 public String getNamespacePrefix() { 119 if (namespace == null) { 120 return ""; 121 } 122 return namespace.getPrefix(); 123 } 124 125 127 public String getNamespaceURI() { 128 if (namespace == null) { 129 return ""; 130 } 131 return namespace.getURI(); 132 } 133 134 135 138 public int hashCode() { 139 if (hashCode == 0) { 140 hashCode = getName().hashCode() 141 ^ getNamespaceURI().hashCode(); 142 if (hashCode == 0) { 143 hashCode = 0xbabe; 144 } 145 } 146 return hashCode; 147 } 148 149 public boolean equals(Object object) { 150 if (this == object) { 151 return true; 152 } else if (object instanceof QName) { 153 QName that = (QName) object; 154 if (hashCode() == that.hashCode()) { 156 return getName().equals(that.getName()) 157 && getNamespaceURI().equals(that.getNamespaceURI()); 158 } 159 } 160 return false; 161 } 162 163 public String toString() { 164 return super.toString() + " [name: " + getName() 165 + " namespace: \"" + getNamespace() + "\"]"; 166 } 167 168 171 public DocumentFactory getDocumentFactory() { 172 if (getNodeFactory() instanceof DocumentFactory) { 173 return (DocumentFactory) nodeFactory; 174 } else { 175 return null; 176 } 177 } 178 179 public NodeFactory getNodeFactory() { 180 return nodeFactory; 181 } 182 183 186 public void setDocumentFactory(DocumentFactory documentFactory) { 187 this.nodeFactory = documentFactory; 188 } 189 190 public void setNodeFactory(NodeFactory nodeFactory) { 191 this.nodeFactory = nodeFactory; 192 } 193 194 private void writeObject(ObjectOutputStream out) throws IOException { 195 196 out.writeObject(namespace.getPrefix()); 199 out.writeObject(namespace.getURI()); 200 201 out.defaultWriteObject(); 202 } 203 204 private void readObject(ObjectInputStream in) throws IOException , ClassNotFoundException { 205 206 String prefix = (String ) in.readObject(); 207 String uri = (String ) in.readObject(); 208 209 in.defaultReadObject(); 210 211 namespace = nodeFactory.createNamespace(prefix, uri); 213 } 214 215 216 private static QNameCache getCache() { 217 QNameCache cache = (QNameCache) cachePerThread.get(); 218 if (cache == null) { 219 cache = new QNameCache(); 220 cachePerThread.set(cache); 221 } 222 return cache; 223 } 224 225 } 226 227 228 272 | Popular Tags |