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