src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/soap/impl/ElementImpl.java

Thu, 24 May 2018 17:55:52 +0800

author
aoqi
date
Thu, 24 May 2018 17:55:52 +0800
changeset 1435
a90b319bae7a
parent 1341
e5cc521294d8
parent 637
9c07ef4934dd
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
aefimov@1341 2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.xml.internal.messaging.saaj.soap.impl;
aoqi@0 27
aoqi@0 28 import java.net.URI;
aoqi@0 29 import java.net.URISyntaxException;
aoqi@0 30 import java.util.*;
aoqi@0 31 import java.util.logging.Level;
aoqi@0 32 import java.util.logging.Logger;
aoqi@0 33
aoqi@0 34 import javax.xml.namespace.QName;
aoqi@0 35 import javax.xml.soap.*;
aoqi@0 36
aoqi@0 37 import org.w3c.dom.*;
aoqi@0 38 import org.w3c.dom.Node;
aoqi@0 39
aoqi@0 40 import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
aoqi@0 41 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
aoqi@0 42 import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
aoqi@0 43 import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
aoqi@0 44 import com.sun.xml.internal.messaging.saaj.util.*;
aoqi@0 45
aoqi@0 46 public class ElementImpl
aoqi@0 47 extends com.sun.org.apache.xerces.internal.dom.ElementNSImpl
aoqi@0 48 implements SOAPElement, SOAPBodyElement {
aoqi@0 49
aoqi@0 50 public static final String DSIG_NS = "http://www.w3.org/2000/09/xmldsig#".intern();
aoqi@0 51 public static final String XENC_NS = "http://www.w3.org/2001/04/xmlenc#".intern();
aoqi@0 52 public static final String WSU_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd".intern();
aoqi@0 53
aoqi@0 54 private AttributeManager encodingStyleAttribute = new AttributeManager();
aoqi@0 55
aoqi@0 56 protected QName elementQName;
aoqi@0 57
aoqi@0 58 protected static final Logger log =
aoqi@0 59 Logger.getLogger(LogDomainConstants.SOAP_IMPL_DOMAIN,
aoqi@0 60 "com.sun.xml.internal.messaging.saaj.soap.impl.LocalStrings");
aoqi@0 61
aoqi@0 62 /**
aoqi@0 63 * XML Information Set REC
aoqi@0 64 * all namespace attributes (including those named xmlns,
aoqi@0 65 * whose [prefix] property has no value) have a namespace URI of http://www.w3.org/2000/xmlns/
aoqi@0 66 */
aoqi@0 67 public final static String XMLNS_URI = "http://www.w3.org/2000/xmlns/".intern();
aoqi@0 68
aoqi@0 69 /**
aoqi@0 70 * The XML Namespace ("http://www.w3.org/XML/1998/namespace"). This is
aoqi@0 71 * the Namespace URI that is automatically mapped to the "xml" prefix.
aoqi@0 72 */
aoqi@0 73 public final static String XML_URI = "http://www.w3.org/XML/1998/namespace".intern();
aoqi@0 74
aoqi@0 75 public ElementImpl(SOAPDocumentImpl ownerDoc, Name name) {
aoqi@0 76 super(
aoqi@0 77 ownerDoc,
aoqi@0 78 name.getURI(),
aoqi@0 79 name.getQualifiedName(),
aoqi@0 80 name.getLocalName());
aoqi@0 81 elementQName = NameImpl.convertToQName(name);
aoqi@0 82 }
aoqi@0 83
aoqi@0 84 public ElementImpl(SOAPDocumentImpl ownerDoc, QName name) {
aoqi@0 85 super(
aoqi@0 86 ownerDoc,
aoqi@0 87 name.getNamespaceURI(),
aoqi@0 88 getQualifiedName(name),
aoqi@0 89 name.getLocalPart());
aoqi@0 90 elementQName = name;
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 public ElementImpl(
aoqi@0 94 SOAPDocumentImpl ownerDoc,
aoqi@0 95 String uri,
aoqi@0 96 String qualifiedName) {
aoqi@0 97
aoqi@0 98 super(ownerDoc, uri, qualifiedName);
aoqi@0 99 elementQName =
aoqi@0 100 new QName(uri, getLocalPart(qualifiedName), getPrefix(qualifiedName));
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 public void ensureNamespaceIsDeclared(String prefix, String uri) {
aoqi@0 104 String alreadyDeclaredUri = getNamespaceURI(prefix);
aoqi@0 105 if (alreadyDeclaredUri == null || !alreadyDeclaredUri.equals(uri)) {
aoqi@0 106 try {
aoqi@0 107 addNamespaceDeclaration(prefix, uri);
aoqi@0 108 } catch (SOAPException e) { /*ignore*/
aoqi@0 109 }
aoqi@0 110 }
aoqi@0 111 }
aoqi@0 112
aoqi@0 113 public Document getOwnerDocument() {
aoqi@0 114 Document doc = super.getOwnerDocument();
aoqi@0 115 if (doc instanceof SOAPDocument)
aoqi@0 116 return ((SOAPDocument) doc).getDocument();
aoqi@0 117 else
aoqi@0 118 return doc;
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 public SOAPElement addChildElement(Name name) throws SOAPException {
aoqi@0 122 return addElement(name);
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 public SOAPElement addChildElement(QName qname) throws SOAPException {
aoqi@0 126 return addElement(qname);
aoqi@0 127 }
aoqi@0 128
aoqi@0 129 public SOAPElement addChildElement(String localName) throws SOAPException {
aefimov@1341 130 String nsUri = getNamespaceURI("");
aefimov@1341 131 Name name = (nsUri == null || nsUri.isEmpty())
aefimov@1341 132 ? NameImpl.createFromUnqualifiedName(localName)
aefimov@1341 133 : NameImpl.createFromQualifiedName(localName, nsUri);
aefimov@1341 134 return addChildElement(name);
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 public SOAPElement addChildElement(String localName, String prefix)
aoqi@0 138 throws SOAPException {
aoqi@0 139 String uri = getNamespaceURI(prefix);
aoqi@0 140 if (uri == null) {
aoqi@0 141 log.log(
aoqi@0 142 Level.SEVERE,
aoqi@0 143 "SAAJ0101.impl.parent.of.body.elem.mustbe.body",
aoqi@0 144 new String[] { prefix });
aoqi@0 145 throw new SOAPExceptionImpl(
aoqi@0 146 "Unable to locate namespace for prefix " + prefix);
aoqi@0 147 }
aoqi@0 148 return addChildElement(localName, prefix, uri);
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 public String getNamespaceURI(String prefix) {
aoqi@0 152
aoqi@0 153 if ("xmlns".equals(prefix)) {
aoqi@0 154 return XMLNS_URI;
aoqi@0 155 }
aoqi@0 156
aoqi@0 157 if("xml".equals(prefix)) {
aoqi@0 158 return XML_URI;
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 if ("".equals(prefix)) {
aoqi@0 162
aoqi@0 163 org.w3c.dom.Node currentAncestor = this;
aoqi@0 164 while (currentAncestor != null &&
aoqi@0 165 !(currentAncestor instanceof Document)) {
aoqi@0 166
aoqi@0 167 if (currentAncestor instanceof ElementImpl) {
aoqi@0 168 QName name = ((ElementImpl) currentAncestor).getElementQName();
aoqi@0 169 /*
aoqi@0 170 if (prefix.equals(name.getPrefix())) {
aoqi@0 171 String uri = name.getNamespaceURI();
aoqi@0 172 if ("".equals(uri)) {
aoqi@0 173 return null;
aoqi@0 174 }
aoqi@0 175 else {
aoqi@0 176 return uri;
aoqi@0 177 }
aoqi@0 178 }*/
aoqi@0 179 if (((Element) currentAncestor).hasAttributeNS(
aoqi@0 180 XMLNS_URI, "xmlns")) {
aoqi@0 181
aoqi@0 182 String uri =
aoqi@0 183 ((Element) currentAncestor).getAttributeNS(
aoqi@0 184 XMLNS_URI, "xmlns");
aoqi@0 185 if ("".equals(uri))
aoqi@0 186 return null;
aoqi@0 187 else {
aoqi@0 188 return uri;
aoqi@0 189 }
aoqi@0 190 }
aoqi@0 191 }
aoqi@0 192 currentAncestor = currentAncestor.getParentNode();
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 } else if (prefix != null) {
aoqi@0 196 // Find if there's an ancester whose name contains this prefix
aoqi@0 197 org.w3c.dom.Node currentAncestor = this;
aoqi@0 198
aoqi@0 199 // String uri = currentAncestor.lookupNamespaceURI(prefix);
aoqi@0 200 // return uri;
aoqi@0 201 while (currentAncestor != null &&
aoqi@0 202 !(currentAncestor instanceof Document)) {
aoqi@0 203
aoqi@0 204 /* if (prefix.equals(currentAncestor.getPrefix())) {
aoqi@0 205 String uri = currentAncestor.getNamespaceURI();
aoqi@0 206 // this is because the javadoc says getNamespaceURI() is not a computed value
aoqi@0 207 // and URI for a non-empty prefix cannot be null
aoqi@0 208 if (uri != null)
aoqi@0 209 return uri;
aoqi@0 210 }*/
aoqi@0 211 //String uri = currentAncestor.lookupNamespaceURI(prefix);
aoqi@0 212 //if (uri != null) {
aoqi@0 213 // return uri;
aoqi@0 214 //}
aoqi@0 215
aoqi@0 216 if (((Element) currentAncestor).hasAttributeNS(
aoqi@0 217 XMLNS_URI, prefix)) {
aoqi@0 218 return ((Element) currentAncestor).getAttributeNS(
aoqi@0 219 XMLNS_URI, prefix);
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 currentAncestor = currentAncestor.getParentNode();
aoqi@0 223 }
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 return null;
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 public SOAPElement setElementQName(QName newName) throws SOAPException {
aoqi@0 230 ElementImpl copy =
aoqi@0 231 new ElementImpl((SOAPDocumentImpl) getOwnerDocument(), newName);
aoqi@0 232 return replaceElementWithSOAPElement(this,copy);
aoqi@0 233 }
aoqi@0 234
aoqi@0 235 public QName createQName(String localName, String prefix)
aoqi@0 236 throws SOAPException {
aoqi@0 237 String uri = getNamespaceURI(prefix);
aoqi@0 238 if (uri == null) {
aoqi@0 239 log.log(Level.SEVERE, "SAAJ0102.impl.cannot.locate.ns",
aoqi@0 240 new Object[] {prefix});
aoqi@0 241 throw new SOAPException("Unable to locate namespace for prefix "
aoqi@0 242 + prefix);
aoqi@0 243 }
aoqi@0 244 return new QName(uri, localName, prefix);
aoqi@0 245 }
aoqi@0 246
aoqi@0 247 public String getNamespacePrefix(String uri) {
aoqi@0 248
aoqi@0 249 NamespaceContextIterator eachNamespace = getNamespaceContextNodes();
aoqi@0 250 while (eachNamespace.hasNext()) {
aoqi@0 251 org.w3c.dom.Attr namespaceDecl = eachNamespace.nextNamespaceAttr();
aoqi@0 252 if (namespaceDecl.getNodeValue().equals(uri)) {
aoqi@0 253 String candidatePrefix = namespaceDecl.getLocalName();
aoqi@0 254 if ("xmlns".equals(candidatePrefix))
aoqi@0 255 return "";
aoqi@0 256 else
aoqi@0 257 return candidatePrefix;
aoqi@0 258 }
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 // Find if any of the ancestors' name has this uri
aoqi@0 262 org.w3c.dom.Node currentAncestor = this;
aoqi@0 263 while (currentAncestor != null &&
aoqi@0 264 !(currentAncestor instanceof Document)) {
aoqi@0 265
aoqi@0 266 if (uri.equals(currentAncestor.getNamespaceURI()))
aoqi@0 267 return currentAncestor.getPrefix();
aoqi@0 268 currentAncestor = currentAncestor.getParentNode();
aoqi@0 269 }
aoqi@0 270
aoqi@0 271 return null;
aoqi@0 272 }
aoqi@0 273
aoqi@0 274 protected org.w3c.dom.Attr getNamespaceAttr(String prefix) {
aoqi@0 275 NamespaceContextIterator eachNamespace = getNamespaceContextNodes();
aoqi@0 276 if (!"".equals(prefix))
aoqi@0 277 prefix = ":"+prefix;
aoqi@0 278 while (eachNamespace.hasNext()) {
aoqi@0 279 org.w3c.dom.Attr namespaceDecl = eachNamespace.nextNamespaceAttr();
aoqi@0 280 if (!"".equals(prefix)) {
aoqi@0 281 if (namespaceDecl.getNodeName().endsWith(prefix))
aoqi@0 282 return namespaceDecl;
aoqi@0 283 } else {
aoqi@0 284 if (namespaceDecl.getNodeName().equals("xmlns"))
aoqi@0 285 return namespaceDecl;
aoqi@0 286 }
aoqi@0 287 }
aoqi@0 288 return null;
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 public NamespaceContextIterator getNamespaceContextNodes() {
aoqi@0 292 return getNamespaceContextNodes(true);
aoqi@0 293 }
aoqi@0 294
aoqi@0 295 public NamespaceContextIterator getNamespaceContextNodes(boolean traverseStack) {
aoqi@0 296 return new NamespaceContextIterator(this, traverseStack);
aoqi@0 297 }
aoqi@0 298
aoqi@0 299 public SOAPElement addChildElement(
aoqi@0 300 String localName,
aoqi@0 301 String prefix,
aoqi@0 302 String uri)
aoqi@0 303 throws SOAPException {
aoqi@0 304
aoqi@0 305 SOAPElement newElement = createElement(NameImpl.create(localName, prefix, uri));
aoqi@0 306 addNode(newElement);
aoqi@0 307 return convertToSoapElement(newElement);
aoqi@0 308 }
aoqi@0 309
aoqi@0 310 public SOAPElement addChildElement(SOAPElement element)
aoqi@0 311 throws SOAPException {
aoqi@0 312
aoqi@0 313 // check if Element falls in SOAP 1.1 or 1.2 namespace.
aoqi@0 314 String elementURI = element.getElementName().getURI();
aoqi@0 315 String localName = element.getLocalName();
aoqi@0 316
aoqi@0 317 if ((SOAPConstants.URI_NS_SOAP_ENVELOPE).equals(elementURI)
aoqi@0 318 || (SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE).equals(elementURI)) {
aoqi@0 319
aoqi@0 320
aoqi@0 321 if ("Envelope".equalsIgnoreCase(localName) ||
aoqi@0 322 "Header".equalsIgnoreCase(localName) || "Body".equalsIgnoreCase(localName)) {
aoqi@0 323 log.severe("SAAJ0103.impl.cannot.add.fragements");
aoqi@0 324 throw new SOAPExceptionImpl(
aoqi@0 325 "Cannot add fragments which contain elements "
aoqi@0 326 + "which are in the SOAP namespace");
aoqi@0 327 }
aoqi@0 328
aoqi@0 329 if ("Fault".equalsIgnoreCase(localName) && !"Body".equalsIgnoreCase(this.getLocalName())) {
aoqi@0 330 log.severe("SAAJ0154.impl.adding.fault.to.nonbody");
aoqi@0 331 throw new SOAPExceptionImpl("Cannot add a SOAPFault as a child of " + this.getLocalName());
aoqi@0 332 }
aoqi@0 333
aoqi@0 334 if ("Detail".equalsIgnoreCase(localName) && !"Fault".equalsIgnoreCase(this.getLocalName())) {
aoqi@0 335 log.severe("SAAJ0155.impl.adding.detail.nonfault");
aoqi@0 336 throw new SOAPExceptionImpl("Cannot add a Detail as a child of " + this.getLocalName());
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 if ("Fault".equalsIgnoreCase(localName)) {
aoqi@0 340 // if body is not empty throw an exception
aoqi@0 341 if (!elementURI.equals(this.getElementName().getURI())) {
aoqi@0 342 log.severe("SAAJ0158.impl.version.mismatch.fault");
aoqi@0 343 throw new SOAPExceptionImpl("SOAP Version mismatch encountered when trying to add SOAPFault to SOAPBody");
aoqi@0 344 }
aoqi@0 345 Iterator it = this.getChildElements();
aoqi@0 346 if (it.hasNext()) {
aoqi@0 347 log.severe("SAAJ0156.impl.adding.fault.error");
aoqi@0 348 throw new SOAPExceptionImpl("Cannot add SOAPFault as a child of a non-Empty SOAPBody");
aoqi@0 349 }
aoqi@0 350 }
aoqi@0 351 }
aoqi@0 352
aoqi@0 353 // preserve the encodingStyle attr as it may get lost in the import
aoqi@0 354 String encodingStyle = element.getEncodingStyle();
aoqi@0 355
aoqi@0 356 ElementImpl importedElement = (ElementImpl) importElement(element);
aoqi@0 357 addNode(importedElement);
aoqi@0 358
aoqi@0 359 if (encodingStyle != null)
aoqi@0 360 importedElement.setEncodingStyle(encodingStyle);
aoqi@0 361
aoqi@0 362 return convertToSoapElement(importedElement);
aoqi@0 363 }
aoqi@0 364
aoqi@0 365 protected Element importElement(Element element) {
aoqi@0 366 Document document = getOwnerDocument();
aoqi@0 367 Document oldDocument = element.getOwnerDocument();
aoqi@0 368 if (!oldDocument.equals(document)) {
aoqi@0 369 return (Element) document.importNode(element, true);
aoqi@0 370 } else {
aoqi@0 371 return element;
aoqi@0 372 }
aoqi@0 373 }
aoqi@0 374
aoqi@0 375 protected SOAPElement addElement(Name name) throws SOAPException {
aoqi@0 376 SOAPElement newElement = createElement(name);
aoqi@0 377 addNode(newElement);
aefimov@1341 378 return newElement;
aoqi@0 379 }
aoqi@0 380
aoqi@0 381 protected SOAPElement addElement(QName name) throws SOAPException {
aoqi@0 382 SOAPElement newElement = createElement(name);
aoqi@0 383 addNode(newElement);
aefimov@1341 384 return newElement;
aoqi@0 385 }
aoqi@0 386
aoqi@0 387 protected SOAPElement createElement(Name name) {
aoqi@0 388
aoqi@0 389 if (isNamespaceQualified(name)) {
aoqi@0 390 return (SOAPElement)
aoqi@0 391 getOwnerDocument().createElementNS(
aoqi@0 392 name.getURI(),
aoqi@0 393 name.getQualifiedName());
aoqi@0 394 } else {
aoqi@0 395 return (SOAPElement)
aoqi@0 396 getOwnerDocument().createElement(name.getQualifiedName());
aoqi@0 397 }
aoqi@0 398 }
aoqi@0 399
aoqi@0 400 protected SOAPElement createElement(QName name) {
aoqi@0 401
aoqi@0 402 if (isNamespaceQualified(name)) {
aoqi@0 403 return (SOAPElement)
aoqi@0 404 getOwnerDocument().createElementNS(
aoqi@0 405 name.getNamespaceURI(),
aoqi@0 406 getQualifiedName(name));
aoqi@0 407 } else {
aoqi@0 408 return (SOAPElement)
aoqi@0 409 getOwnerDocument().createElement(getQualifiedName(name));
aoqi@0 410 }
aoqi@0 411 }
aoqi@0 412
aoqi@0 413 protected void addNode(org.w3c.dom.Node newElement) throws SOAPException {
aoqi@0 414 insertBefore(newElement, null);
aoqi@0 415
aoqi@0 416 if (getOwnerDocument() instanceof DocumentFragment)
aoqi@0 417 return;
aoqi@0 418
aoqi@0 419 if (newElement instanceof ElementImpl) {
aoqi@0 420 ElementImpl element = (ElementImpl) newElement;
aoqi@0 421 QName elementName = element.getElementQName();
aoqi@0 422 if (!"".equals(elementName.getNamespaceURI())) {
aoqi@0 423 element.ensureNamespaceIsDeclared(
aoqi@0 424 elementName.getPrefix(), elementName.getNamespaceURI());
aoqi@0 425 }
aoqi@0 426 }
aoqi@0 427
aoqi@0 428 }
aoqi@0 429
aoqi@0 430 protected SOAPElement findChild(NameImpl name) {
aoqi@0 431 Iterator eachChild = getChildElementNodes();
aoqi@0 432 while (eachChild.hasNext()) {
aoqi@0 433 SOAPElement child = (SOAPElement) eachChild.next();
aoqi@0 434 if (child.getElementName().equals(name)) {
aoqi@0 435 return child;
aoqi@0 436 }
aoqi@0 437 }
aoqi@0 438
aoqi@0 439 return null;
aoqi@0 440 }
aoqi@0 441
aoqi@0 442 public SOAPElement addTextNode(String text) throws SOAPException {
aoqi@0 443 if (text.startsWith(CDATAImpl.cdataUC)
aoqi@0 444 || text.startsWith(CDATAImpl.cdataLC))
aoqi@0 445 return addCDATA(
aoqi@0 446 text.substring(CDATAImpl.cdataUC.length(), text.length() - 3));
aoqi@0 447 return addText(text);
aoqi@0 448 }
aoqi@0 449
aoqi@0 450 protected SOAPElement addCDATA(String text) throws SOAPException {
aoqi@0 451 org.w3c.dom.Text cdata =
aoqi@0 452 (org.w3c.dom.Text) getOwnerDocument().createCDATASection(text);
aoqi@0 453 addNode(cdata);
aoqi@0 454 return this;
aoqi@0 455 }
aoqi@0 456
aoqi@0 457 protected SOAPElement addText(String text) throws SOAPException {
aoqi@0 458 org.w3c.dom.Text textNode =
aoqi@0 459 (org.w3c.dom.Text) getOwnerDocument().createTextNode(text);
aoqi@0 460 addNode(textNode);
aoqi@0 461 return this;
aoqi@0 462 }
aoqi@0 463
aoqi@0 464 public SOAPElement addAttribute(Name name, String value)
aoqi@0 465 throws SOAPException {
aoqi@0 466 addAttributeBare(name, value);
aoqi@0 467 if (!"".equals(name.getURI())) {
aoqi@0 468 ensureNamespaceIsDeclared(name.getPrefix(), name.getURI());
aoqi@0 469 }
aoqi@0 470 return this;
aoqi@0 471 }
aoqi@0 472
aoqi@0 473 public SOAPElement addAttribute(QName qname, String value)
aoqi@0 474 throws SOAPException {
aoqi@0 475 addAttributeBare(qname, value);
aoqi@0 476 if (!"".equals(qname.getNamespaceURI())) {
aoqi@0 477 ensureNamespaceIsDeclared(qname.getPrefix(), qname.getNamespaceURI());
aoqi@0 478 }
aoqi@0 479 return this;
aoqi@0 480 }
aoqi@0 481
aoqi@0 482 private void addAttributeBare(Name name, String value) {
aoqi@0 483 addAttributeBare(
aoqi@0 484 name.getURI(),
aoqi@0 485 name.getPrefix(),
aoqi@0 486 name.getQualifiedName(),
aoqi@0 487 value);
aoqi@0 488 }
aoqi@0 489 private void addAttributeBare(QName name, String value) {
aoqi@0 490 addAttributeBare(
aoqi@0 491 name.getNamespaceURI(),
aoqi@0 492 name.getPrefix(),
aoqi@0 493 getQualifiedName(name),
aoqi@0 494 value);
aoqi@0 495 }
aoqi@0 496
aoqi@0 497 private void addAttributeBare(
aoqi@0 498 String uri,
aoqi@0 499 String prefix,
aoqi@0 500 String qualifiedName,
aoqi@0 501 String value) {
aoqi@0 502
aoqi@0 503 uri = uri.length() == 0 ? null : uri;
aoqi@0 504 if (qualifiedName.equals("xmlns")) {
aoqi@0 505 uri = XMLNS_URI;
aoqi@0 506 }
aoqi@0 507
aoqi@0 508 if (uri == null) {
aoqi@0 509 setAttribute(qualifiedName, value);
aoqi@0 510 } else {
aoqi@0 511 setAttributeNS(uri, qualifiedName, value);
aoqi@0 512 }
aoqi@0 513 }
aoqi@0 514
aoqi@0 515 public SOAPElement addNamespaceDeclaration(String prefix, String uri)
aoqi@0 516 throws SOAPException {
aoqi@0 517 if (prefix.length() > 0) {
aoqi@0 518 setAttributeNS(XMLNS_URI, "xmlns:" + prefix, uri);
aoqi@0 519 } else {
aoqi@0 520 setAttributeNS(XMLNS_URI, "xmlns", uri);
aoqi@0 521 }
aoqi@0 522 //Fix for CR:6474641
aoqi@0 523 //tryToFindEncodingStyleAttributeName();
aoqi@0 524 return this;
aoqi@0 525 }
aoqi@0 526
aoqi@0 527 public String getAttributeValue(Name name) {
aoqi@0 528 return getAttributeValueFrom(this, name);
aoqi@0 529 }
aoqi@0 530
aoqi@0 531 public String getAttributeValue(QName qname) {
aoqi@0 532 return getAttributeValueFrom(
aoqi@0 533 this,
aoqi@0 534 qname.getNamespaceURI(),
aoqi@0 535 qname.getLocalPart(),
aoqi@0 536 qname.getPrefix(),
aoqi@0 537 getQualifiedName(qname));
aoqi@0 538 }
aoqi@0 539
aoqi@0 540 public Iterator getAllAttributes() {
aoqi@0 541 Iterator i = getAllAttributesFrom(this);
aoqi@0 542 ArrayList list = new ArrayList();
aoqi@0 543 while (i.hasNext()) {
aoqi@0 544 Name name = (Name) i.next();
aoqi@0 545 if (!"xmlns".equalsIgnoreCase(name.getPrefix()))
aoqi@0 546 list.add(name);
aoqi@0 547 }
aoqi@0 548 return list.iterator();
aoqi@0 549 }
aoqi@0 550
aoqi@0 551 public Iterator getAllAttributesAsQNames() {
aoqi@0 552 Iterator i = getAllAttributesFrom(this);
aoqi@0 553 ArrayList list = new ArrayList();
aoqi@0 554 while (i.hasNext()) {
aoqi@0 555 Name name = (Name) i.next();
aoqi@0 556 if (!"xmlns".equalsIgnoreCase(name.getPrefix())) {
aoqi@0 557 list.add(NameImpl.convertToQName(name));
aoqi@0 558 }
aoqi@0 559 }
aoqi@0 560 return list.iterator();
aoqi@0 561 }
aoqi@0 562
aoqi@0 563
aoqi@0 564 public Iterator getNamespacePrefixes() {
aoqi@0 565 return doGetNamespacePrefixes(false);
aoqi@0 566 }
aoqi@0 567
aoqi@0 568 public Iterator getVisibleNamespacePrefixes() {
aoqi@0 569 return doGetNamespacePrefixes(true);
aoqi@0 570 }
aoqi@0 571
aoqi@0 572 protected Iterator doGetNamespacePrefixes(final boolean deep) {
aoqi@0 573 return new Iterator() {
aoqi@0 574 String next = null;
aoqi@0 575 String last = null;
aoqi@0 576 NamespaceContextIterator eachNamespace =
aoqi@0 577 getNamespaceContextNodes(deep);
aoqi@0 578
aoqi@0 579 void findNext() {
aoqi@0 580 while (next == null && eachNamespace.hasNext()) {
aoqi@0 581 String attributeKey =
aoqi@0 582 eachNamespace.nextNamespaceAttr().getNodeName();
aoqi@0 583 if (attributeKey.startsWith("xmlns:")) {
aoqi@0 584 next = attributeKey.substring("xmlns:".length());
aoqi@0 585 }
aoqi@0 586 }
aoqi@0 587 }
aoqi@0 588
aoqi@0 589 public boolean hasNext() {
aoqi@0 590 findNext();
aoqi@0 591 return next != null;
aoqi@0 592 }
aoqi@0 593
aoqi@0 594 public Object next() {
aoqi@0 595 findNext();
aoqi@0 596 if (next == null) {
aoqi@0 597 throw new NoSuchElementException();
aoqi@0 598 }
aoqi@0 599
aoqi@0 600 last = next;
aoqi@0 601 next = null;
aoqi@0 602 return last;
aoqi@0 603 }
aoqi@0 604
aoqi@0 605 public void remove() {
aoqi@0 606 if (last == null) {
aoqi@0 607 throw new IllegalStateException();
aoqi@0 608 }
aoqi@0 609 eachNamespace.remove();
aoqi@0 610 next = null;
aoqi@0 611 last = null;
aoqi@0 612 }
aoqi@0 613 };
aoqi@0 614 }
aoqi@0 615
aoqi@0 616 public Name getElementName() {
aoqi@0 617 return NameImpl.convertToName(elementQName);
aoqi@0 618 }
aoqi@0 619
aoqi@0 620 public QName getElementQName() {
aoqi@0 621 return elementQName;
aoqi@0 622 }
aoqi@0 623
aoqi@0 624 public boolean removeAttribute(Name name) {
aoqi@0 625 return removeAttribute(name.getURI(), name.getLocalName());
aoqi@0 626 }
aoqi@0 627
aoqi@0 628 public boolean removeAttribute(QName name) {
aoqi@0 629 return removeAttribute(name.getNamespaceURI(), name.getLocalPart());
aoqi@0 630 }
aoqi@0 631
aoqi@0 632 private boolean removeAttribute(String uri, String localName) {
aoqi@0 633 String nonzeroLengthUri =
aoqi@0 634 (uri == null || uri.length() == 0) ? null : uri;
aoqi@0 635 org.w3c.dom.Attr attribute =
aoqi@0 636 getAttributeNodeNS(nonzeroLengthUri, localName);
aoqi@0 637 if (attribute == null) {
aoqi@0 638 return false;
aoqi@0 639 }
aoqi@0 640 removeAttributeNode(attribute);
aoqi@0 641 return true;
aoqi@0 642 }
aoqi@0 643
aoqi@0 644 public boolean removeNamespaceDeclaration(String prefix) {
aoqi@0 645 org.w3c.dom.Attr declaration = getNamespaceAttr(prefix);
aoqi@0 646 if (declaration == null) {
aoqi@0 647 return false;
aoqi@0 648 }
aoqi@0 649 try {
aoqi@0 650 removeAttributeNode(declaration);
aoqi@0 651 } catch (DOMException de) {
aoqi@0 652 // ignore
aoqi@0 653 }
aoqi@0 654 return true;
aoqi@0 655 }
aoqi@0 656
aoqi@0 657 public Iterator getChildElements() {
aoqi@0 658 return getChildElementsFrom(this);
aoqi@0 659 }
aoqi@0 660
aoqi@0 661 protected SOAPElement convertToSoapElement(Element element) {
aoqi@0 662 if (element instanceof SOAPElement) {
aoqi@0 663 return (SOAPElement) element;
aoqi@0 664 } else {
aoqi@0 665 return replaceElementWithSOAPElement(
aoqi@0 666 element,
aoqi@0 667 (ElementImpl) createElement(NameImpl.copyElementName(element)));
aoqi@0 668 }
aoqi@0 669 }
aoqi@0 670
aoqi@0 671 protected static SOAPElement replaceElementWithSOAPElement(
aoqi@0 672 Element element,
aoqi@0 673 ElementImpl copy) {
aoqi@0 674
aoqi@0 675 Iterator eachAttribute = getAllAttributesFrom(element);
aoqi@0 676 while (eachAttribute.hasNext()) {
aoqi@0 677 Name name = (Name) eachAttribute.next();
aoqi@0 678 copy.addAttributeBare(name, getAttributeValueFrom(element, name));
aoqi@0 679 }
aoqi@0 680
aoqi@0 681 Iterator eachChild = getChildElementsFrom(element);
aoqi@0 682 while (eachChild.hasNext()) {
aoqi@0 683 Node nextChild = (Node) eachChild.next();
aoqi@0 684 copy.insertBefore(nextChild, null);
aoqi@0 685 }
aoqi@0 686
aoqi@0 687 Node parent = element.getParentNode();
aoqi@0 688 if (parent != null) {
aoqi@0 689 parent.replaceChild(copy, element);
aoqi@0 690 } // XXX else throw an exception?
aoqi@0 691
aoqi@0 692 return copy;
aoqi@0 693 }
aoqi@0 694
aoqi@0 695 protected Iterator getChildElementNodes() {
aoqi@0 696 return new Iterator() {
aoqi@0 697 Iterator eachNode = getChildElements();
aoqi@0 698 Node next = null;
aoqi@0 699 Node last = null;
aoqi@0 700
aoqi@0 701 public boolean hasNext() {
aoqi@0 702 if (next == null) {
aoqi@0 703 while (eachNode.hasNext()) {
aoqi@0 704 Node node = (Node) eachNode.next();
aoqi@0 705 if (node instanceof SOAPElement) {
aoqi@0 706 next = node;
aoqi@0 707 break;
aoqi@0 708 }
aoqi@0 709 }
aoqi@0 710 }
aoqi@0 711 return next != null;
aoqi@0 712 }
aoqi@0 713
aoqi@0 714 public Object next() {
aoqi@0 715 if (hasNext()) {
aoqi@0 716 last = next;
aoqi@0 717 next = null;
aoqi@0 718 return last;
aoqi@0 719 }
aoqi@0 720 throw new NoSuchElementException();
aoqi@0 721 }
aoqi@0 722
aoqi@0 723 public void remove() {
aoqi@0 724 if (last == null) {
aoqi@0 725 throw new IllegalStateException();
aoqi@0 726 }
aoqi@0 727 Node target = last;
aoqi@0 728 last = null;
aoqi@0 729 removeChild(target);
aoqi@0 730 }
aoqi@0 731 };
aoqi@0 732 }
aoqi@0 733
aoqi@0 734 public Iterator getChildElements(final Name name) {
aoqi@0 735 return getChildElements(name.getURI(), name.getLocalName());
aoqi@0 736 }
aoqi@0 737
aoqi@0 738 public Iterator getChildElements(final QName qname) {
aoqi@0 739 return getChildElements(qname.getNamespaceURI(), qname.getLocalPart());
aoqi@0 740 }
aoqi@0 741
aoqi@0 742 private Iterator getChildElements(final String nameUri, final String nameLocal) {
aoqi@0 743 return new Iterator() {
aoqi@0 744 Iterator eachElement = getChildElementNodes();
aoqi@0 745 Node next = null;
aoqi@0 746 Node last = null;
aoqi@0 747
aoqi@0 748 public boolean hasNext() {
aoqi@0 749 if (next == null) {
aoqi@0 750 while (eachElement.hasNext()) {
aoqi@0 751 Node element = (Node) eachElement.next();
aoqi@0 752 String elementUri = element.getNamespaceURI();
aoqi@0 753 elementUri = elementUri == null ? "" : elementUri;
aoqi@0 754 String elementName = element.getLocalName();
aoqi@0 755 if (elementUri.equals(nameUri)
aoqi@0 756 && elementName.equals(nameLocal)) {
aoqi@0 757 next = element;
aoqi@0 758 break;
aoqi@0 759 }
aoqi@0 760 }
aoqi@0 761 }
aoqi@0 762 return next != null;
aoqi@0 763 }
aoqi@0 764
aoqi@0 765 public Object next() {
aoqi@0 766 if (!hasNext()) {
aoqi@0 767 throw new NoSuchElementException();
aoqi@0 768 }
aoqi@0 769 last = next;
aoqi@0 770 next = null;
aoqi@0 771 return last;
aoqi@0 772 }
aoqi@0 773
aoqi@0 774 public void remove() {
aoqi@0 775 if (last == null) {
aoqi@0 776 throw new IllegalStateException();
aoqi@0 777 }
aoqi@0 778 Node target = last;
aoqi@0 779 last = null;
aoqi@0 780 removeChild(target);
aoqi@0 781 }
aoqi@0 782 };
aoqi@0 783 }
aoqi@0 784
aoqi@0 785 public void removeContents() {
aoqi@0 786 Node currentChild = getFirstChild();
aoqi@0 787
aoqi@0 788 while (currentChild != null) {
aoqi@0 789 Node temp = currentChild.getNextSibling();
aoqi@0 790 if (currentChild instanceof javax.xml.soap.Node) {
aoqi@0 791 ((javax.xml.soap.Node) currentChild).detachNode();
aoqi@0 792 } else {
aoqi@0 793 Node parent = currentChild.getParentNode();
aoqi@0 794 if (parent != null) {
aoqi@0 795 parent.removeChild(currentChild);
aoqi@0 796 }
aoqi@0 797
aoqi@0 798 }
aoqi@0 799 currentChild = temp;
aoqi@0 800 }
aoqi@0 801 }
aoqi@0 802
aoqi@0 803 public void setEncodingStyle(String encodingStyle) throws SOAPException {
aoqi@0 804 if (!"".equals(encodingStyle)) {
aoqi@0 805 try {
aoqi@0 806 new URI(encodingStyle);
aoqi@0 807 } catch (URISyntaxException m) {
aoqi@0 808 log.log(
aoqi@0 809 Level.SEVERE,
aoqi@0 810 "SAAJ0105.impl.encoding.style.mustbe.valid.URI",
aoqi@0 811 new String[] { encodingStyle });
aoqi@0 812 throw new IllegalArgumentException(
aoqi@0 813 "Encoding style (" + encodingStyle + ") should be a valid URI");
aoqi@0 814 }
aoqi@0 815 }
aoqi@0 816 encodingStyleAttribute.setValue(encodingStyle);
aoqi@0 817 tryToFindEncodingStyleAttributeName();
aoqi@0 818 }
aoqi@0 819
aoqi@0 820 public String getEncodingStyle() {
aoqi@0 821 String encodingStyle = encodingStyleAttribute.getValue();
aoqi@0 822 if (encodingStyle != null)
aoqi@0 823 return encodingStyle;
aoqi@0 824 String soapNamespace = getSOAPNamespace();
aoqi@0 825 if (soapNamespace != null) {
aoqi@0 826 Attr attr = getAttributeNodeNS(soapNamespace, "encodingStyle");
aoqi@0 827 if (attr != null) {
aoqi@0 828 encodingStyle = attr.getValue();
aoqi@0 829 try {
aoqi@0 830 setEncodingStyle(encodingStyle);
aoqi@0 831 } catch (SOAPException se) {
aoqi@0 832 // has to be ignored
aoqi@0 833 }
aoqi@0 834 return encodingStyle;
aoqi@0 835 }
aoqi@0 836 }
aoqi@0 837 return null;
aoqi@0 838 }
aoqi@0 839
aoqi@0 840 // Node methods
aoqi@0 841 public String getValue() {
aoqi@0 842 javax.xml.soap.Node valueNode = getValueNode();
aoqi@0 843 return valueNode == null ? null : valueNode.getValue();
aoqi@0 844 }
aoqi@0 845
aoqi@0 846 public void setValue(String value) {
aoqi@0 847 Node valueNode = getValueNodeStrict();
aoqi@0 848 if (valueNode != null) {
aoqi@0 849 valueNode.setNodeValue(value);
aoqi@0 850 } else {
aoqi@0 851 try {
aoqi@0 852 addTextNode(value);
aoqi@0 853 } catch (SOAPException e) {
aoqi@0 854 throw new RuntimeException(e.getMessage());
aoqi@0 855 }
aoqi@0 856 }
aoqi@0 857 }
aoqi@0 858
aoqi@0 859 protected Node getValueNodeStrict() {
aoqi@0 860 Node node = getFirstChild();
aoqi@0 861 if (node != null) {
aoqi@0 862 if (node.getNextSibling() == null
aoqi@0 863 && node.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
aoqi@0 864 return node;
aoqi@0 865 } else {
aoqi@0 866 log.severe("SAAJ0107.impl.elem.child.not.single.text");
aoqi@0 867 throw new IllegalStateException();
aoqi@0 868 }
aoqi@0 869 }
aoqi@0 870
aoqi@0 871 return null;
aoqi@0 872 }
aoqi@0 873
aoqi@0 874 protected javax.xml.soap.Node getValueNode() {
aoqi@0 875 Iterator i = getChildElements();
aoqi@0 876 while (i.hasNext()) {
aoqi@0 877 javax.xml.soap.Node n = (javax.xml.soap.Node) i.next();
aoqi@0 878 if (n.getNodeType() == org.w3c.dom.Node.TEXT_NODE ||
aoqi@0 879 n.getNodeType() == org.w3c.dom.Node.CDATA_SECTION_NODE) {
aoqi@0 880 // TODO: Hack to fix text node split into multiple lines.
aoqi@0 881 normalize();
aoqi@0 882 // Should remove the normalization step when this gets fixed in
aoqi@0 883 // DOM/Xerces.
aoqi@0 884 return (javax.xml.soap.Node) n;
aoqi@0 885 }
aoqi@0 886 }
aoqi@0 887 return null;
aoqi@0 888 }
aoqi@0 889
aoqi@0 890 public void setParentElement(SOAPElement element) throws SOAPException {
aoqi@0 891 if (element == null) {
aoqi@0 892 log.severe("SAAJ0106.impl.no.null.to.parent.elem");
aoqi@0 893 throw new SOAPException("Cannot pass NULL to setParentElement");
aoqi@0 894 }
aoqi@0 895 element.addChildElement(this);
aoqi@0 896 findEncodingStyleAttributeName();
aoqi@0 897 }
aoqi@0 898
aoqi@0 899 protected void findEncodingStyleAttributeName() throws SOAPException {
aoqi@0 900 String soapNamespace = getSOAPNamespace();
aoqi@0 901 if (soapNamespace != null) {
aoqi@0 902 String soapNamespacePrefix = getNamespacePrefix(soapNamespace);
aoqi@0 903 if (soapNamespacePrefix != null) {
aoqi@0 904 setEncodingStyleNamespace(soapNamespace, soapNamespacePrefix);
aoqi@0 905 }
aoqi@0 906 }
aoqi@0 907 }
aoqi@0 908
aoqi@0 909 protected void setEncodingStyleNamespace(
aoqi@0 910 String soapNamespace,
aoqi@0 911 String soapNamespacePrefix)
aoqi@0 912 throws SOAPException {
aoqi@0 913 Name encodingStyleAttributeName =
aoqi@0 914 NameImpl.create(
aoqi@0 915 "encodingStyle",
aoqi@0 916 soapNamespacePrefix,
aoqi@0 917 soapNamespace);
aoqi@0 918 encodingStyleAttribute.setName(encodingStyleAttributeName);
aoqi@0 919 }
aoqi@0 920
aoqi@0 921 public SOAPElement getParentElement() {
aoqi@0 922 Node parentNode = getParentNode();
aoqi@0 923 if (parentNode instanceof SOAPDocument) {
aoqi@0 924 return null;
aoqi@0 925 }
aoqi@0 926 return (SOAPElement) parentNode;
aoqi@0 927 }
aoqi@0 928
aoqi@0 929 protected String getSOAPNamespace() {
aoqi@0 930 String soapNamespace = null;
aoqi@0 931
aoqi@0 932 SOAPElement antecedent = this;
aoqi@0 933 while (antecedent != null) {
aoqi@0 934 Name antecedentName = antecedent.getElementName();
aoqi@0 935 String antecedentNamespace = antecedentName.getURI();
aoqi@0 936
aoqi@0 937 if (NameImpl.SOAP11_NAMESPACE.equals(antecedentNamespace)
aoqi@0 938 || NameImpl.SOAP12_NAMESPACE.equals(antecedentNamespace)) {
aoqi@0 939
aoqi@0 940 soapNamespace = antecedentNamespace;
aoqi@0 941 break;
aoqi@0 942 }
aoqi@0 943
aoqi@0 944 antecedent = antecedent.getParentElement();
aoqi@0 945 }
aoqi@0 946
aoqi@0 947 return soapNamespace;
aoqi@0 948 }
aoqi@0 949
aoqi@0 950 public void detachNode() {
aoqi@0 951 Node parent = getParentNode();
aoqi@0 952 if (parent != null) {
aoqi@0 953 parent.removeChild(this);
aoqi@0 954 }
aoqi@0 955 encodingStyleAttribute.clearNameAndValue();
aoqi@0 956 // Fix for CR: 6474641
aoqi@0 957 //tryToFindEncodingStyleAttributeName();
aoqi@0 958 }
aoqi@0 959
aoqi@0 960 public void tryToFindEncodingStyleAttributeName() {
aoqi@0 961 try {
aoqi@0 962 findEncodingStyleAttributeName();
aoqi@0 963 } catch (SOAPException e) { /*okay to fail*/
aoqi@0 964 }
aoqi@0 965 }
aoqi@0 966
aoqi@0 967 public void recycleNode() {
aoqi@0 968 detachNode();
aoqi@0 969 // TBD
aoqi@0 970 // - add this to the factory so subsequent
aoqi@0 971 // creations can reuse this object.
aoqi@0 972 }
aoqi@0 973
aoqi@0 974 class AttributeManager {
aoqi@0 975 Name attributeName = null;
aoqi@0 976 String attributeValue = null;
aoqi@0 977
aoqi@0 978 public void setName(Name newName) throws SOAPException {
aoqi@0 979 clearAttribute();
aoqi@0 980 attributeName = newName;
aoqi@0 981 reconcileAttribute();
aoqi@0 982 }
aoqi@0 983 public void clearName() {
aoqi@0 984 clearAttribute();
aoqi@0 985 attributeName = null;
aoqi@0 986 }
aoqi@0 987 public void setValue(String value) throws SOAPException {
aoqi@0 988 attributeValue = value;
aoqi@0 989 reconcileAttribute();
aoqi@0 990 }
aoqi@0 991 public Name getName() {
aoqi@0 992 return attributeName;
aoqi@0 993 }
aoqi@0 994 public String getValue() {
aoqi@0 995 return attributeValue;
aoqi@0 996 }
aoqi@0 997
aoqi@0 998 /** Note: to be used only in detachNode method */
aoqi@0 999 public void clearNameAndValue() {
aoqi@0 1000 attributeName = null;
aoqi@0 1001 attributeValue = null;
aoqi@0 1002 }
aoqi@0 1003
aoqi@0 1004 private void reconcileAttribute() throws SOAPException {
aoqi@0 1005 if (attributeName != null) {
aoqi@0 1006 removeAttribute(attributeName);
aoqi@0 1007 if (attributeValue != null) {
aoqi@0 1008 addAttribute(attributeName, attributeValue);
aoqi@0 1009 }
aoqi@0 1010 }
aoqi@0 1011 }
aoqi@0 1012 private void clearAttribute() {
aoqi@0 1013 if (attributeName != null) {
aoqi@0 1014 removeAttribute(attributeName);
aoqi@0 1015 }
aoqi@0 1016 }
aoqi@0 1017 }
aoqi@0 1018
aoqi@0 1019 protected static org.w3c.dom.Attr getNamespaceAttrFrom(
aoqi@0 1020 Element element,
aoqi@0 1021 String prefix) {
aoqi@0 1022 NamespaceContextIterator eachNamespace =
aoqi@0 1023 new NamespaceContextIterator(element);
aoqi@0 1024 while (eachNamespace.hasNext()) {
aoqi@0 1025 org.w3c.dom.Attr namespaceDecl = eachNamespace.nextNamespaceAttr();
aoqi@0 1026 String declaredPrefix =
aoqi@0 1027 NameImpl.getLocalNameFromTagName(namespaceDecl.getNodeName());
aoqi@0 1028 if (declaredPrefix.equals(prefix)) {
aoqi@0 1029 return namespaceDecl;
aoqi@0 1030 }
aoqi@0 1031 }
aoqi@0 1032 return null;
aoqi@0 1033 }
aoqi@0 1034
aoqi@0 1035 protected static Iterator getAllAttributesFrom(final Element element) {
aoqi@0 1036 final NamedNodeMap attributes = element.getAttributes();
aoqi@0 1037
aoqi@0 1038 return new Iterator() {
aoqi@0 1039 int attributesLength = attributes.getLength();
aoqi@0 1040 int attributeIndex = 0;
aoqi@0 1041 String currentName;
aoqi@0 1042
aoqi@0 1043 public boolean hasNext() {
aoqi@0 1044 return attributeIndex < attributesLength;
aoqi@0 1045 }
aoqi@0 1046
aoqi@0 1047 public Object next() {
aoqi@0 1048 if (!hasNext()) {
aoqi@0 1049 throw new NoSuchElementException();
aoqi@0 1050 }
aoqi@0 1051 Node current = attributes.item(attributeIndex++);
aoqi@0 1052 currentName = current.getNodeName();
aoqi@0 1053
aoqi@0 1054 String prefix = NameImpl.getPrefixFromTagName(currentName);
aoqi@0 1055 if (prefix.length() == 0) {
aoqi@0 1056 return NameImpl.createFromUnqualifiedName(currentName);
aoqi@0 1057 } else {
aoqi@0 1058 Name attributeName =
aoqi@0 1059 NameImpl.createFromQualifiedName(
aoqi@0 1060 currentName,
aoqi@0 1061 current.getNamespaceURI());
aoqi@0 1062 return attributeName;
aoqi@0 1063 }
aoqi@0 1064 }
aoqi@0 1065
aoqi@0 1066 public void remove() {
aoqi@0 1067 if (currentName == null) {
aoqi@0 1068 throw new IllegalStateException();
aoqi@0 1069 }
aoqi@0 1070 attributes.removeNamedItem(currentName);
aoqi@0 1071 }
aoqi@0 1072 };
aoqi@0 1073 }
aoqi@0 1074
aoqi@0 1075 protected static String getAttributeValueFrom(Element element, Name name) {
aoqi@0 1076 return getAttributeValueFrom(
aoqi@0 1077 element,
aoqi@0 1078 name.getURI(),
aoqi@0 1079 name.getLocalName(),
aoqi@0 1080 name.getPrefix(),
aoqi@0 1081 name.getQualifiedName());
aoqi@0 1082 }
aoqi@0 1083
aoqi@0 1084 private static String getAttributeValueFrom(
aoqi@0 1085 Element element,
aoqi@0 1086 String uri,
aoqi@0 1087 String localName,
aoqi@0 1088 String prefix,
aoqi@0 1089 String qualifiedName) {
aoqi@0 1090
aoqi@0 1091 String nonzeroLengthUri =
aoqi@0 1092 (uri == null || uri.length() == 0) ? null : uri;
aoqi@0 1093
aoqi@0 1094 boolean mustUseGetAttributeNodeNS = (nonzeroLengthUri != null);
aoqi@0 1095
aoqi@0 1096 if (mustUseGetAttributeNodeNS) {
aoqi@0 1097
aoqi@0 1098 if (!element.hasAttributeNS(uri, localName)) {
aoqi@0 1099 return null;
aoqi@0 1100 }
aoqi@0 1101
aoqi@0 1102 String attrValue =
aoqi@0 1103 element.getAttributeNS(nonzeroLengthUri, localName);
aoqi@0 1104
aoqi@0 1105 return attrValue;
aoqi@0 1106 }
aoqi@0 1107
aoqi@0 1108 Attr attribute = null;
aoqi@0 1109 attribute = element.getAttributeNode(qualifiedName);
aoqi@0 1110
aoqi@0 1111 return attribute == null ? null : attribute.getValue();
aoqi@0 1112 }
aoqi@0 1113
aoqi@0 1114 protected static Iterator getChildElementsFrom(final Element element) {
aoqi@0 1115 return new Iterator() {
aoqi@0 1116 Node next = element.getFirstChild();
aoqi@0 1117 Node nextNext = null;
aoqi@0 1118 Node last = null;
aoqi@0 1119
aoqi@0 1120 public boolean hasNext() {
aoqi@0 1121 if (next != null) {
aoqi@0 1122 return true;
aoqi@0 1123 }
aoqi@0 1124 if (next == null && nextNext != null) {
aoqi@0 1125 next = nextNext;
aoqi@0 1126 }
aoqi@0 1127
aoqi@0 1128 return next != null;
aoqi@0 1129 }
aoqi@0 1130
aoqi@0 1131 public Object next() {
aoqi@0 1132 if (hasNext()) {
aoqi@0 1133 last = next;
aoqi@0 1134 next = null;
aoqi@0 1135
aoqi@0 1136 if ((element instanceof ElementImpl)
aoqi@0 1137 && (last instanceof Element)) {
aoqi@0 1138 last =
aoqi@0 1139 ((ElementImpl) element).convertToSoapElement(
aoqi@0 1140 (Element) last);
aoqi@0 1141 }
aoqi@0 1142
aoqi@0 1143 nextNext = last.getNextSibling();
aoqi@0 1144 return last;
aoqi@0 1145 }
aoqi@0 1146 throw new NoSuchElementException();
aoqi@0 1147 }
aoqi@0 1148
aoqi@0 1149 public void remove() {
aoqi@0 1150 if (last == null) {
aoqi@0 1151 throw new IllegalStateException();
aoqi@0 1152 }
aoqi@0 1153 Node target = last;
aoqi@0 1154 last = null;
aoqi@0 1155 element.removeChild(target);
aoqi@0 1156 }
aoqi@0 1157 };
aoqi@0 1158 }
aoqi@0 1159
aoqi@0 1160 public static String getQualifiedName(QName name) {
aoqi@0 1161 String prefix = name.getPrefix();
aoqi@0 1162 String localName = name.getLocalPart();
aoqi@0 1163 String qualifiedName = null;
aoqi@0 1164
aoqi@0 1165 if (prefix != null && prefix.length() > 0) {
aoqi@0 1166 qualifiedName = prefix + ":" + localName;
aoqi@0 1167 } else {
aoqi@0 1168 qualifiedName = localName;
aoqi@0 1169 }
aoqi@0 1170 return qualifiedName;
aoqi@0 1171 }
aoqi@0 1172
aoqi@0 1173 public static String getLocalPart(String qualifiedName) {
aoqi@0 1174 if (qualifiedName == null) {
aoqi@0 1175 // Log
aoqi@0 1176 throw new IllegalArgumentException("Cannot get local name for a \"null\" qualified name");
aoqi@0 1177 }
aoqi@0 1178
aoqi@0 1179 int index = qualifiedName.indexOf(':');
aoqi@0 1180 if (index < 0)
aoqi@0 1181 return qualifiedName;
aoqi@0 1182 else
aoqi@0 1183 return qualifiedName.substring(index + 1);
aoqi@0 1184 }
aoqi@0 1185
aoqi@0 1186 public static String getPrefix(String qualifiedName) {
aoqi@0 1187 if (qualifiedName == null) {
aoqi@0 1188 // Log
aoqi@0 1189 throw new IllegalArgumentException("Cannot get prefix for a \"null\" qualified name");
aoqi@0 1190 }
aoqi@0 1191
aoqi@0 1192 int index = qualifiedName.indexOf(':');
aoqi@0 1193 if (index < 0)
aoqi@0 1194 return "";
aoqi@0 1195 else
aoqi@0 1196 return qualifiedName.substring(0, index);
aoqi@0 1197 }
aoqi@0 1198
aoqi@0 1199 protected boolean isNamespaceQualified(Name name) {
aoqi@0 1200 return !"".equals(name.getURI());
aoqi@0 1201 }
aoqi@0 1202
aoqi@0 1203 protected boolean isNamespaceQualified(QName name) {
aoqi@0 1204 return !"".equals(name.getNamespaceURI());
aoqi@0 1205 }
aoqi@0 1206
aoqi@0 1207 //TODO: This is a temporary SAAJ workaround for optimizing XWS
aoqi@0 1208 // should be removed once the corresponding JAXP bug is fixed
aoqi@0 1209 // It appears the bug will be fixed in JAXP 1.4 (not by Appserver 9 timeframe)
aoqi@0 1210 public void setAttributeNS(
aoqi@0 1211 String namespaceURI,String qualifiedName, String value) {
aoqi@0 1212 int index = qualifiedName.indexOf(':');
aoqi@0 1213 String localName;
aoqi@0 1214 if (index < 0)
aoqi@0 1215 localName = qualifiedName;
aoqi@0 1216 else
aoqi@0 1217 localName = qualifiedName.substring(index + 1);
aoqi@0 1218
aoqi@0 1219 // Workaround for bug 6467808 - This needs to be fixed in JAXP
aoqi@0 1220
aoqi@0 1221 // Rolling back this fix, this is a wrong fix, infact its causing other regressions in JAXWS tck and
aoqi@0 1222 // other tests, because of this change the namespace declarations on soapenv:Fault element are never
aoqi@0 1223 // picked up. The fix for bug 6467808 should be in JAXP.
aoqi@0 1224 // if(elementQName.getLocalPart().equals("Fault") &&
aoqi@0 1225 // (SOAPConstants.URI_NS_SOAP_1_1_ENVELOPE.equals(value) ||
aoqi@0 1226 // SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE.equals(value)))
aoqi@0 1227 // return;
aoqi@0 1228
aoqi@0 1229 super.setAttributeNS(namespaceURI,qualifiedName,value);
aoqi@0 1230 //String tmpLocalName = this.getLocalName();
aoqi@0 1231 String tmpURI = this.getNamespaceURI();
aoqi@0 1232 boolean isIDNS = false;
aoqi@0 1233 if( tmpURI != null && (tmpURI.equals(DSIG_NS) || tmpURI.equals(XENC_NS))){
aoqi@0 1234 isIDNS = true;
aoqi@0 1235 }
aoqi@0 1236 //No need to check for Signature/encryption element
aoqi@0 1237 //just check for namespace.
aoqi@0 1238 if(localName.equals("Id")){
aoqi@0 1239 if(namespaceURI == null || namespaceURI.equals("")){
aoqi@0 1240 setIdAttribute(localName,true);
aoqi@0 1241 }else if(isIDNS || WSU_NS.equals(namespaceURI)){
aoqi@0 1242 setIdAttributeNS(namespaceURI,localName,true);
aoqi@0 1243 }
aoqi@0 1244 }
aoqi@0 1245
aoqi@0 1246 }
aoqi@0 1247
aoqi@0 1248 }

mercurial