1 16 17 package org.apache.xerces.dom; 18 19 import org.apache.xerces.xs.XSSimpleTypeDefinition; 20 import org.apache.xerces.xs.XSTypeDefinition; 21 import org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl; 22 import org.apache.xerces.impl.xs.XSComplexTypeDecl; 23 import org.apache.xerces.util.URI; 24 import org.apache.xerces.xni.NamespaceContext; 25 import org.w3c.dom.Attr ; 26 import org.w3c.dom.DOMException ; 27 28 29 30 43 public class ElementNSImpl 44 extends ElementImpl { 45 46 50 51 static final long serialVersionUID = -9142310625494392642L; 52 static final String xmlURI = "http://www.w3.org/XML/1998/namespace"; 53 54 58 59 protected String namespaceURI; 60 61 62 protected String localName; 63 64 65 transient XSTypeDefinition type; 67 68 protected ElementNSImpl() { 69 super(); 70 } 71 74 protected ElementNSImpl(CoreDocumentImpl ownerDocument, 75 String namespaceURI, 76 String qualifiedName) 77 throws DOMException 78 { 79 super(ownerDocument, qualifiedName); 80 setName(namespaceURI, qualifiedName); 81 } 82 83 private void setName(String namespaceURI, String qname) { 84 85 String prefix; 86 this.namespaceURI = namespaceURI; 88 if (namespaceURI != null) { 89 this.namespaceURI = (namespaceURI.length() == 0) ? null : namespaceURI; 91 } 92 93 int colon1, colon2 ; 94 95 if(qname == null){ 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 else{ 108 colon1 = qname.indexOf(':'); 109 colon2 = qname.lastIndexOf(':'); 110 } 111 112 ownerDocument.checkNamespaceWF(qname, colon1, colon2); 113 if (colon1 < 0) { 114 localName = qname; 116 if (ownerDocument.errorChecking) { 117 ownerDocument.checkQName(null, localName); 118 if (qname.equals("xmlns") 119 && (namespaceURI == null 120 || !namespaceURI.equals(NamespaceContext.XMLNS_URI)) 121 || (namespaceURI!=null && namespaceURI.equals(NamespaceContext.XMLNS_URI) 122 && !qname.equals("xmlns"))) { 123 String msg = 124 DOMMessageFormatter.formatMessage( 125 DOMMessageFormatter.DOM_DOMAIN, 126 "NAMESPACE_ERR", 127 null); 128 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 129 } 130 } 131 } else { 133 prefix = qname.substring(0, colon1); 134 localName = qname.substring(colon2 + 1); 135 136 139 142 if (ownerDocument.errorChecking) { 143 if( namespaceURI == null || ( prefix.equals("xml") && !namespaceURI.equals(NamespaceContext.XML_URI) )){ 144 String msg = 145 DOMMessageFormatter.formatMessage( 146 DOMMessageFormatter.DOM_DOMAIN, 147 "NAMESPACE_ERR", 148 null); 149 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 150 } 151 152 ownerDocument.checkQName(prefix, localName); 153 ownerDocument.checkDOMNSErr(prefix, namespaceURI); 154 } 155 } 156 } 157 158 protected ElementNSImpl(CoreDocumentImpl ownerDocument, 160 String namespaceURI, String qualifiedName, 161 String localName) 162 throws DOMException 163 { 164 super(ownerDocument, qualifiedName); 165 166 this.localName = localName; 167 this.namespaceURI = namespaceURI; 168 } 169 170 protected ElementNSImpl(CoreDocumentImpl ownerDocument, 172 String value) { 173 super(ownerDocument, value); 174 } 175 176 void rename(String namespaceURI, String qualifiedName) 180 { 181 if (needsSyncData()) { 182 synchronizeData(); 183 } 184 this.name = qualifiedName; 185 setName(namespaceURI, qualifiedName); 186 reconcileDefaultAttributes(); 187 } 188 189 197 protected void setValues (CoreDocumentImpl ownerDocument, 198 String namespaceURI, String qualifiedName, 199 String localName){ 200 201 firstChild = null; 203 previousSibling = null; 204 nextSibling = null; 205 fNodeListCache = null; 206 207 attributes = null; 209 super.flags = 0; 210 setOwnerDocument(ownerDocument); 211 212 needsSyncData(true); 214 super.name = qualifiedName; 215 this.localName = localName; 216 this.namespaceURI = namespaceURI; 217 218 } 219 220 224 225 226 230 243 public String getNamespaceURI() 244 { 245 if (needsSyncData()) { 246 synchronizeData(); 247 } 248 return namespaceURI; 249 } 250 251 261 public String getPrefix() 262 { 263 264 if (needsSyncData()) { 265 synchronizeData(); 266 } 267 int index = name.indexOf(':'); 268 return index < 0 ? null : name.substring(0, index); 269 } 270 271 286 public void setPrefix(String prefix) 287 throws DOMException 288 { 289 if (needsSyncData()) { 290 synchronizeData(); 291 } 292 if (ownerDocument.errorChecking) { 293 if (isReadOnly()) { 294 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 295 throw new DOMException ( 296 DOMException.NO_MODIFICATION_ALLOWED_ERR, 297 msg); 298 } 299 if (prefix != null && prefix.length() != 0) { 300 if (!CoreDocumentImpl.isXMLName(prefix,ownerDocument.isXML11Version())) { 301 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); 302 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 303 } 304 if (namespaceURI == null || prefix.indexOf(':') >=0) { 305 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null); 306 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 307 } else if (prefix.equals("xml")) { 308 if (!namespaceURI.equals(xmlURI)) { 309 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null); 310 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 311 } 312 } 313 } 314 315 } 316 if (prefix !=null && prefix.length() != 0) { 318 name = prefix + ":" + localName; 319 } 320 else { 321 name = localName; 322 } 323 } 324 325 331 public String getLocalName() 332 { 333 if (needsSyncData()) { 334 synchronizeData(); 335 } 336 return localName; 337 } 338 339 340 344 public String getBaseURI() { 345 346 if (needsSyncData()) { 347 synchronizeData(); 348 } 349 351 353 if (attributes != null) { 354 Attr attrNode = (Attr )attributes.getNamedItemNS("http://www.w3.org/XML/1998/namespace", "base"); 355 if (attrNode != null) { 356 String uri = attrNode.getNodeValue(); 357 if (uri.length() != 0 ) { try { 359 uri = new URI(uri).toString(); 360 } 361 catch (org.apache.xerces.util.URI.MalformedURIException e) { 362 364 NodeImpl parentOrOwner = (parentNode() != null) ? parentNode() : ownerNode; 366 367 String parentBaseURI = (parentOrOwner != null) ? parentOrOwner.getBaseURI() : null; 369 370 if (parentBaseURI != null) { 371 try { 372 uri = new URI(new URI(parentBaseURI), uri).toString(); 373 } 374 catch (org.apache.xerces.util.URI.MalformedURIException ex){ 375 return null; 377 } 378 return uri; 379 } 380 return null; 382 } 383 return uri; 384 } 385 } 386 } 387 388 String parentElementBaseURI = (this.parentNode() != null) ? this.parentNode().getBaseURI() : null ; 391 if(parentElementBaseURI != null){ 393 try { 394 return new URI(parentElementBaseURI).toString(); 396 } 397 catch (org.apache.xerces.util.URI.MalformedURIException e){ 398 return null; 400 } 401 } 402 404 String baseURI = (this.ownerNode != null) ? this.ownerNode.getBaseURI() : null ; 405 406 if(baseURI != null){ 407 try { 408 return new URI(baseURI).toString(); 410 } 411 catch (org.apache.xerces.util.URI.MalformedURIException e){ 412 return null; 414 } 415 } 416 417 return null; 418 419 } 420 421 422 425 public String getTypeName() { 426 if (type !=null){ 427 if (type instanceof XSSimpleTypeDefinition) { 428 return ((XSSimpleTypeDecl) type).getTypeName(); 429 } else { 430 return ((XSComplexTypeDecl) type).getTypeName(); 431 } 432 } 433 return null; 434 } 435 436 439 public String getTypeNamespace() { 440 if (type !=null){ 441 return type.getNamespace(); 442 } 443 return null; 444 } 445 446 461 public boolean isDerivedFrom(String typeNamespaceArg, String typeNameArg, 462 int derivationMethod) { 463 if(needsSyncData()) { 464 synchronizeData(); 465 } 466 if (type != null) { 467 if (type instanceof XSSimpleTypeDefinition) { 468 return ((XSSimpleTypeDecl) type).isDOMDerivedFrom( 469 typeNamespaceArg, typeNameArg, derivationMethod); 470 } else { 471 return ((XSComplexTypeDecl) type).isDOMDerivedFrom( 472 typeNamespaceArg, typeNameArg, derivationMethod); 473 } 474 } 475 return false; 476 } 477 478 482 public void setType(XSTypeDefinition type) { 483 this.type = type; 484 } 485 } 486 | Popular Tags |