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

Sun, 15 Dec 2013 23:35:45 +0100

author
mkos
date
Sun, 15 Dec 2013 23:35:45 +0100
changeset 494
2fcd3ddb57a6
parent 368
0989ad8c0860
child 637
9c07ef4934dd
child 1341
e5cc521294d8
permissions
-rw-r--r--

8025152: Enhance activation set up
8028388: 9 jaxws tests failed in nightly build with java.lang.ClassCastException
Summary: fix also reviewed by Bill Shannon, Alexander Fomin
Reviewed-by: dfuchs, hawtin, mgrebac
Contributed-by: bill.shannon@oracle.com

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

mercurial