1 9 10 package org.dom4j.tree; 11 12 import org.dom4j.Element; 13 import org.dom4j.Namespace; 14 import org.dom4j.Node; 15 import org.dom4j.Visitor; 16 17 22 public class AbstractNamespace extends AbstractNode implements Namespace { 23 24 25 protected static final NamespaceCache cache = new NamespaceCache(); 26 27 28 public static final AbstractNamespace XML_NAMESPACE 29 = cache.get("xml", "http://www.w3.org/XML/1998/namespace"); 30 31 32 public static final AbstractNamespace NO_NAMESPACE 33 = cache.get("", ""); 34 35 36 37 private String prefix; 38 39 40 private String uri; 41 42 43 private int hashCode; 44 45 46 51 public static AbstractNamespace get(String prefix, String uri) { 52 return cache.get(prefix, uri); 53 } 54 55 60 public static Namespace get(String uri) { 61 return cache.get(uri); 62 } 63 64 67 public AbstractNamespace(String prefix, String uri) { 68 this.prefix = (prefix != null) ? prefix : ""; 69 this.uri = (uri != null) ? uri : "";; 70 } 71 72 73 public short getNodeType() { 74 return NAMESPACE_NODE; 75 } 76 77 80 public int hashCode() { 81 if ( hashCode == 0 ) { 82 hashCode = createHashCode(); 83 } 84 return hashCode; 85 } 86 87 88 protected int createHashCode() { 89 int hashCode = uri.hashCode() ^ prefix.hashCode(); 90 if ( hashCode == 0 ) { 91 hashCode = 0xbabe; 92 } 93 return hashCode; 94 } 95 96 97 public boolean equals(Object object) { 98 if ( this == object ) { 99 return true; 100 } 101 else if ( object instanceof Namespace ) { 102 Namespace that = (Namespace) object; 103 104 if ( hashCode() == that.hashCode() ) { 106 return uri.equals( that.getURI() ) 107 && prefix.equals( that.getPrefix() ); 108 } 109 } 110 return false; 111 } 112 113 public String getText() { 114 return uri; 115 } 116 117 public String getStringValue() { 118 return uri; 119 } 120 121 123 public String getPrefix() { 124 return prefix; 125 } 126 127 129 public String getURI() { 130 return uri; 131 } 132 133 134 public String getXPathNameStep() { 135 if (prefix != null && !"".equals( prefix )) { 136 return "namespace::" + prefix; 137 } 138 return "namespace::*[name()='']"; 139 } 140 141 public String getPath(Element context) { 142 StringBuffer path = new StringBuffer (10); 143 Element parent = getParent(); 144 if (parent != null && parent != context) { 145 path.append( parent.getPath( context ) ); 146 path.append( '/' ); 147 } 148 path.append( getXPathNameStep() ); 149 return path.toString(); 150 } 151 152 public String getUniquePath(Element context) { 153 StringBuffer path = new StringBuffer (10); 154 Element parent = getParent(); 155 if (parent != null && parent != context) { 156 path.append( parent.getUniquePath( context ) ); 157 path.append( '/' ); 158 } 159 path.append( getXPathNameStep() ); 160 return path.toString(); 161 } 162 163 public String toString() { 164 return super.toString() + " [AbstractNamespace: prefix " + getPrefix() 165 + " mapped to URI \"" + getURI() + "\"]"; 166 } 167 168 public String asXML() { 169 StringBuffer asxml = new StringBuffer (10); 170 String prefix = getPrefix(); 171 if ( prefix != null && prefix.length() > 0 ) { 172 asxml.append("xmlns:"); 173 asxml.append(prefix); 174 asxml.append("=\""); 175 } 176 else { 177 asxml.append("xmlns=\""); 178 } 179 asxml.append(getURI()); 180 asxml.append("\""); 181 return asxml.toString(); 182 } 183 184 public void accept(Visitor visitor) { 185 visitor.visit(this); 186 } 187 188 protected Node createXPathResult(Element parent) { 189 return new org.dom4j.tree.DefaultNamespace( parent, getPrefix(), getURI() ); 190 } 191 192 } 193 194 195 196 197 241 | Popular Tags |