1 16 17 package org.apache.xerces.dom; 18 19 import org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl; 20 import org.apache.xerces.xni.NamespaceContext; 21 import org.apache.xerces.xs.XSSimpleTypeDefinition; 22 import org.w3c.dom.DOMException ; 23 24 38 public class AttrNSImpl 39 extends AttrImpl { 40 41 45 46 static final long serialVersionUID = -781906615369795414L; 47 48 static final String xmlnsURI = "http://www.w3.org/2000/xmlns/"; 49 static final String xmlURI = "http://www.w3.org/XML/1998/namespace"; 50 51 55 56 protected String namespaceURI; 57 58 59 protected String localName; 60 61 64 public AttrNSImpl(){} 65 66 69 protected AttrNSImpl(CoreDocumentImpl ownerDocument, 70 String namespaceURI, 71 String qualifiedName) { 72 73 super(ownerDocument, qualifiedName); 74 setName(namespaceURI, qualifiedName); 75 } 76 77 private void setName(String namespaceURI, String qname){ 78 CoreDocumentImpl ownerDocument = ownerDocument(); 79 String prefix; 80 this.namespaceURI = namespaceURI; 82 if (namespaceURI !=null) { 83 this.namespaceURI = (namespaceURI.length() == 0)? null 84 : namespaceURI; 85 86 } 87 int colon1 = qname.indexOf(':'); 88 int colon2 = qname.lastIndexOf(':'); 89 ownerDocument.checkNamespaceWF(qname, colon1, colon2); 90 if (colon1 < 0) { 91 localName = qname; 93 if (ownerDocument.errorChecking) { 94 ownerDocument.checkQName(null, localName); 95 96 if (qname.equals("xmlns") && (namespaceURI == null 97 || !namespaceURI.equals(NamespaceContext.XMLNS_URI)) 98 || (namespaceURI!=null && namespaceURI.equals(NamespaceContext.XMLNS_URI) 99 && !qname.equals("xmlns"))) { 100 String msg = 101 DOMMessageFormatter.formatMessage( 102 DOMMessageFormatter.DOM_DOMAIN, 103 "NAMESPACE_ERR", 104 null); 105 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 106 } 107 } 108 } 109 else { 110 prefix = qname.substring(0, colon1); 111 localName = qname.substring(colon2+1); 112 ownerDocument.checkQName(prefix, localName); 113 ownerDocument.checkDOMNSErr(prefix, namespaceURI); 114 } 115 } 116 117 public AttrNSImpl(CoreDocumentImpl ownerDocument, 119 String namespaceURI, 120 String qualifiedName, 121 String localName) { 122 super(ownerDocument, qualifiedName); 123 124 this.localName = localName; 125 this.namespaceURI = namespaceURI; 126 } 127 128 protected AttrNSImpl(CoreDocumentImpl ownerDocument, 130 String value) { 131 super(ownerDocument, value); 132 } 133 134 void rename(String namespaceURI, String qualifiedName) { 139 if (needsSyncData()) { 140 synchronizeData(); 141 } 142 this.name = qualifiedName; 143 setName(namespaceURI, qualifiedName); 144 } 145 146 154 public void setValues (CoreDocumentImpl ownerDocument, 155 String namespaceURI, 156 String qualifiedName, 157 String localName){ 158 159 super.textNode = null; 160 super.flags = 0; 161 isSpecified(true); 162 hasStringValue(true); 163 super.setOwnerDocument(ownerDocument); 164 this.localName = localName; 165 this.namespaceURI = namespaceURI; 166 super.name = qualifiedName; 167 super.value = null; 168 } 169 170 174 187 public String getNamespaceURI() 188 { 189 if (needsSyncData()) { 190 synchronizeData(); 191 } 192 return namespaceURI; 196 } 197 198 208 public String getPrefix() 209 { 210 if (needsSyncData()) { 211 synchronizeData(); 212 } 213 int index = name.indexOf(':'); 214 return index < 0 ? null : name.substring(0, index); 215 } 216 217 232 public void setPrefix(String prefix) 233 throws DOMException 234 { 235 if (needsSyncData()) { 236 synchronizeData(); 237 } 238 if (ownerDocument().errorChecking) { 239 if (isReadOnly()) { 240 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 241 throw new DOMException (DOMException.NO_MODIFICATION_ALLOWED_ERR, msg); 242 } 243 if (prefix != null && prefix.length() != 0) { 244 245 if (!CoreDocumentImpl.isXMLName(prefix,ownerDocument().isXML11Version())) { 246 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); 247 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 248 } 249 if (namespaceURI == null || prefix.indexOf(':') >=0) { 250 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null); 251 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 252 253 } 254 if (prefix.equals("xmlns")) { 255 if (!namespaceURI.equals(xmlnsURI)){ 256 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null); 257 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 258 } 259 } else if (prefix.equals("xml")) { 260 if (!namespaceURI.equals(xmlURI)) { 261 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null); 262 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 263 } 264 }else if (name.equals("xmlns")) { 265 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null); 266 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 267 } 268 } 269 } 270 271 if (prefix !=null && prefix.length() != 0) { 273 name = prefix + ":" + localName; 274 } 275 else { 276 name = localName; 277 } 278 } 279 280 286 public String getLocalName() 287 { 288 if (needsSyncData()) { 289 synchronizeData(); 290 } 291 return localName; 292 } 293 294 295 298 public String getTypeName() { 299 if (type !=null){ 300 if (type instanceof XSSimpleTypeDecl){ 301 return ((XSSimpleTypeDecl)type).getName(); 302 } 303 return (String )type; 304 } 305 return null; 306 } 307 308 323 public boolean isDerivedFrom(String typeNamespaceArg, 324 String typeNameArg, 325 int derivationMethod) { 326 if (type != null) { 327 if (type instanceof XSSimpleTypeDefinition) { 328 return ((XSSimpleTypeDecl) type).isDOMDerivedFrom( 329 typeNamespaceArg, typeNameArg, derivationMethod); 330 } 331 } 332 return false; 333 } 334 335 338 public String getTypeNamespace() { 339 if (type !=null) { 340 if (type instanceof XSSimpleTypeDecl){ 341 return ((XSSimpleTypeDecl)type).getNamespace(); 342 } 343 return DTD_URI; 344 } 345 return null; 346 } 347 348 } 349 | Popular Tags |