1 57 58 package com.sun.org.apache.xerces.internal.dom; 59 60 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition; 61 import com.sun.org.apache.xerces.internal.util.URI; 62 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 63 import org.w3c.dom.Attr ; 64 import org.w3c.dom.DOMException ; 65 import org.w3c.dom.TypeInfo ; 66 67 68 69 79 public class ElementNSImpl 80 extends ElementImpl { 81 82 86 87 static final long serialVersionUID = -9142310625494392642L; 88 static final String xmlURI = "http://www.w3.org/XML/1998/namespace"; 89 90 94 95 protected String namespaceURI; 96 97 98 protected String localName; 99 100 protected ElementNSImpl() { 101 super(); 102 } 103 106 protected ElementNSImpl(CoreDocumentImpl ownerDocument, 107 String namespaceURI, 108 String qualifiedName) 109 throws DOMException 110 { 111 super(ownerDocument, qualifiedName); 112 setName(namespaceURI, qualifiedName); 113 } 114 115 private void setName(String namespaceURI, String qname) { 116 117 String prefix; 118 this.namespaceURI = namespaceURI; 120 if (namespaceURI != null) { 121 this.namespaceURI = (namespaceURI.length() == 0) ? null : namespaceURI; 123 } 124 125 int colon1, colon2 ; 126 127 if(qname == null){ 132 String msg = 133 DOMMessageFormatter.formatMessage( 134 DOMMessageFormatter.DOM_DOMAIN, 135 "NAMESPACE_ERR", 136 null); 137 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 138 } 139 else{ 140 colon1 = qname.indexOf(':'); 141 colon2 = qname.lastIndexOf(':'); 142 } 143 ownerDocument().checkNamespaceWF(qname, colon1, colon2); 144 if (colon1 < 0) { 145 localName = qname; 147 ownerDocument().checkQName(null, localName); 148 if (qname.equals("xmlns") 149 && (namespaceURI == null 150 || !namespaceURI.equals(NamespaceContext.XMLNS_URI)) 151 || (namespaceURI!=null && namespaceURI.equals(NamespaceContext.XMLNS_URI) 152 && !qname.equals("xmlns"))) { 153 String msg = 154 DOMMessageFormatter.formatMessage( 155 DOMMessageFormatter.DOM_DOMAIN, 156 "NAMESPACE_ERR", 157 null); 158 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 159 } 160 } else { 162 prefix = qname.substring(0, colon1); 163 164 167 170 if( namespaceURI == null || ( prefix.equals("xml") && !namespaceURI.equals(NamespaceContext.XML_URI) )){ 171 String msg = 172 DOMMessageFormatter.formatMessage( 173 DOMMessageFormatter.DOM_DOMAIN, 174 "NAMESPACE_ERR", 175 null); 176 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 177 } 178 179 localName = qname.substring(colon2 + 1); 180 ownerDocument().checkQName(prefix, localName); 181 ownerDocument().checkDOMNSErr(prefix, namespaceURI); 182 } 183 } 184 185 protected ElementNSImpl(CoreDocumentImpl ownerDocument, 187 String namespaceURI, String qualifiedName, 188 String localName) 189 throws DOMException 190 { 191 super(ownerDocument, qualifiedName); 192 193 this.localName = localName; 194 this.namespaceURI = namespaceURI; 195 } 196 197 protected ElementNSImpl(CoreDocumentImpl ownerDocument, 199 String value) { 200 super(ownerDocument, value); 201 } 202 203 void rename(String namespaceURI, String qualifiedName) 207 { 208 if (needsSyncData()) { 209 synchronizeData(); 210 } 211 this.name = qualifiedName; 212 setName(namespaceURI, qualifiedName); 213 reconcileDefaultAttributes(); 214 } 215 216 224 protected void setValues (CoreDocumentImpl ownerDocument, 225 String namespaceURI, String qualifiedName, 226 String localName){ 227 228 firstChild = null; 230 previousSibling = null; 231 nextSibling = null; 232 fNodeListCache = null; 233 234 attributes = null; 236 super.flags = 0; 237 setOwnerDocument(ownerDocument); 238 239 needsSyncData(true); 241 super.name = qualifiedName; 242 this.localName = localName; 243 this.namespaceURI = namespaceURI; 244 245 } 246 247 251 252 253 257 270 public String getNamespaceURI() 271 { 272 if (needsSyncData()) { 273 synchronizeData(); 274 } 275 return namespaceURI; 276 } 277 278 288 public String getPrefix() 289 { 290 291 if (needsSyncData()) { 292 synchronizeData(); 293 } 294 int index = name.indexOf(':'); 295 return index < 0 ? null : name.substring(0, index); 296 } 297 298 313 public void setPrefix(String prefix) 314 throws DOMException 315 { 316 if (needsSyncData()) { 317 synchronizeData(); 318 } 319 if (ownerDocument().errorChecking) { 320 if (isReadOnly()) { 321 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null); 322 throw new DOMException ( 323 DOMException.NO_MODIFICATION_ALLOWED_ERR, 324 msg); 325 } 326 if (prefix != null && prefix.length() != 0) { 327 if (!CoreDocumentImpl.isXMLName(prefix,ownerDocument().isXML11Version())) { 328 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null); 329 throw new DOMException (DOMException.INVALID_CHARACTER_ERR, msg); 330 } 331 if (namespaceURI == null || prefix.indexOf(':') >=0) { 332 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null); 333 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 334 } else if (prefix.equals("xml")) { 335 if (!namespaceURI.equals(xmlURI)) { 336 String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null); 337 throw new DOMException (DOMException.NAMESPACE_ERR, msg); 338 } 339 } 340 } 341 } 342 if (prefix !=null && prefix.length() != 0) { 344 name = prefix + ":" + localName; 345 } 346 else { 347 name = localName; 348 } 349 } 350 351 357 public String getLocalName() 358 { 359 if (needsSyncData()) { 360 synchronizeData(); 361 } 362 return localName; 363 } 364 365 366 370 public String getBaseURI() { 371 372 if (needsSyncData()) { 373 synchronizeData(); 374 } 375 376 378 380 if (attributes != null) { 381 Attr attrNode = (Attr )attributes.getNamedItemNS("http://www.w3.org/XML/1998/namespace", "base"); 382 if (attrNode != null) { 383 String uri = attrNode.getNodeValue(); 384 if (uri.length() != 0 ) { try { 386 uri = new URI(uri).toString(); 387 } 388 catch (com.sun.org.apache.xerces.internal.util.URI.MalformedURIException e){ 389 return null; 391 } 392 return uri; 393 } 394 } 395 } 396 397 String parentElementBaseURI = (this.parentNode() != null) ? this.parentNode().getBaseURI() : null ; 400 if(parentElementBaseURI != null){ 402 try { 403 return new URI(parentElementBaseURI).toString(); 405 } 406 catch (com.sun.org.apache.xerces.internal.util.URI.MalformedURIException e){ 407 return null; 409 } 410 } 411 413 String baseURI = (this.ownerNode != null) ? this.ownerNode.getBaseURI() : null ; 414 415 if(baseURI != null){ 416 try { 417 return new URI(baseURI).toString(); 419 } 420 catch (com.sun.org.apache.xerces.internal.util.URI.MalformedURIException e){ 421 return null; 423 } 424 } 425 426 return null; 427 } 428 429 433 public void setType(TypeInfo type) { 434 this.type = type; 435 } 436 437 438 } 439 | Popular Tags |