< prev index next >

src/java.xml.ws/share/classes/com/sun/xml/internal/messaging/saaj/soap/impl/BodyImpl.java

Print this page


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 114         fault.setFaultCode(faultCode);
 115         fault.setFaultString(faultString);
 116         return fault;
 117     }
 118 
 119     public SOAPFault addFault(QName faultCode, String faultString)
 120         throws SOAPException {
 121 
 122         SOAPFault fault = addFault();
 123         fault.setFaultCode(faultCode);
 124         fault.setFaultString(faultString);
 125         return fault;
 126     }
 127 
 128     void initializeFault() {
 129         FaultImpl flt = (FaultImpl) findFault();
 130         fault = flt;
 131     }
 132 
 133     protected SOAPElement findFault() {
 134         Iterator eachChild = getChildElementNodes();
 135         while (eachChild.hasNext()) {
 136             SOAPElement child = (SOAPElement) eachChild.next();
 137             if (isFault(child)) {
 138                 return child;
 139             }
 140         }
 141 
 142         return null;
 143     }
 144 
 145     public boolean hasFault() {
 146         QName payloadQName = getPayloadQName();
 147         return getFaultQName().equals(payloadQName);
 148     }
 149 
 150     private Object getFaultQName() {
 151         return new QName(getNamespaceURI(), "Fault");
 152     }
 153 
 154     public SOAPFault getFault() {


 230                 // Causes all deferred nodes to be inflated
 231                 rootNode.normalize();
 232                 adoptElement(rootNode);
 233                 SOAPBodyElement bodyElement = (SOAPBodyElement) convertToSoapElement(rootNode);
 234                 addNode(bodyElement);
 235                 return bodyElement;
 236         */
 237         ///*
 238         SOAPBodyElement newBodyElement = null;
 239         DocumentFragment docFrag = document.createDocumentFragment();
 240         Element rootElement = document.getDocumentElement();
 241         if(rootElement != null) {
 242             docFrag.appendChild(rootElement);
 243 
 244             Document ownerDoc = getOwnerDocument();
 245             // This copies the whole tree which could be very big so it's slow.
 246             // However, it does have the advantage of actually working.
 247             org.w3c.dom.Node replacingNode = ownerDoc.importNode(docFrag, true);
 248             // Adding replacingNode at the last of the children list of body
 249             addNode(replacingNode);
 250             Iterator i =
 251                 getChildElements(NameImpl.copyElementName(rootElement));
 252             // Return the child element with the required name which is at the
 253             // end of the list
 254             while(i.hasNext())
 255                 newBodyElement = (SOAPBodyElement) i.next();
 256         }
 257         return newBodyElement;
 258         //*/
 259     }
 260 
 261     protected SOAPElement convertToSoapElement(Element element) {
 262         if ((element instanceof SOAPBodyElement) &&
 263             //this check is required because ElementImpl currently
 264             // implements SOAPBodyElement
 265             !(element.getClass().equals(ElementImpl.class))) {
 266             return (SOAPElement) element;
 267         } else {
 268             return replaceElementWithSOAPElement(
 269                 element,
 270                 (ElementImpl) createBodyElement(NameImpl
 271                     .copyElementName(element)));
 272         }
 273     }
 274 
 275     public SOAPElement setElementQName(QName newName) throws SOAPException {
 276         log.log(Level.SEVERE,
 277                 "SAAJ0146.impl.invalid.name.change.requested",
 278                 new Object[] {elementQName.getLocalPart(),
 279                               newName.getLocalPart()});
 280         throw new SOAPException("Cannot change name for "
 281                                 + elementQName.getLocalPart() + " to "
 282                                 + newName.getLocalPart());
 283     }
 284 
 285     public Document extractContentAsDocument() throws SOAPException {
 286 
 287         Iterator eachChild = getChildElements();
 288         javax.xml.soap.Node firstBodyElement = null;
 289 
 290         while (eachChild.hasNext() &&
 291                !(firstBodyElement instanceof SOAPElement))
 292             firstBodyElement = (javax.xml.soap.Node) eachChild.next();
 293 
 294         boolean exactlyOneChildElement = true;
 295         if (firstBodyElement == null)
 296             exactlyOneChildElement = false;
 297         else {
 298             for (org.w3c.dom.Node node = firstBodyElement.getNextSibling();
 299                  node != null;
 300                  node = node.getNextSibling()) {
 301 
 302                 if (node instanceof Element) {
 303                     exactlyOneChildElement = false;
 304                     break;
 305                 }
 306             }
 307         }
 308 
 309         if(!exactlyOneChildElement) {
 310             log.log(Level.SEVERE,
 311                     "SAAJ0250.impl.body.should.have.exactly.one.child");
 312             throw new SOAPException("Cannot extract Document from body");
 313         }
 314 
 315         Document document = null;
 316         try {
 317             DocumentBuilderFactory factory =
 318                 new com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl();
 319             factory.setNamespaceAware(true);
 320             DocumentBuilder builder = factory.newDocumentBuilder();
 321             document = builder.newDocument();
 322 
 323             Element rootElement = (Element) document.importNode(
 324                                                 firstBodyElement,
 325                                                 true);
 326 
 327             document.appendChild(rootElement);
 328 
 329         } catch(Exception e) {
 330             log.log(Level.SEVERE,
 331                     "SAAJ0251.impl.cannot.extract.document.from.body");
 332             throw new SOAPExceptionImpl(
 333                 "Unable to extract Document from body", e);
 334         }
 335 
 336         firstBodyElement.detachNode();
 337 
 338         return document;


   1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 114         fault.setFaultCode(faultCode);
 115         fault.setFaultString(faultString);
 116         return fault;
 117     }
 118 
 119     public SOAPFault addFault(QName faultCode, String faultString)
 120         throws SOAPException {
 121 
 122         SOAPFault fault = addFault();
 123         fault.setFaultCode(faultCode);
 124         fault.setFaultString(faultString);
 125         return fault;
 126     }
 127 
 128     void initializeFault() {
 129         FaultImpl flt = (FaultImpl) findFault();
 130         fault = flt;
 131     }
 132 
 133     protected SOAPElement findFault() {
 134         Iterator<Node> eachChild = getChildElementNodes();
 135         while (eachChild.hasNext()) {
 136             SOAPElement child = (SOAPElement) eachChild.next();
 137             if (isFault(child)) {
 138                 return child;
 139             }
 140         }
 141 
 142         return null;
 143     }
 144 
 145     public boolean hasFault() {
 146         QName payloadQName = getPayloadQName();
 147         return getFaultQName().equals(payloadQName);
 148     }
 149 
 150     private Object getFaultQName() {
 151         return new QName(getNamespaceURI(), "Fault");
 152     }
 153 
 154     public SOAPFault getFault() {


 230                 // Causes all deferred nodes to be inflated
 231                 rootNode.normalize();
 232                 adoptElement(rootNode);
 233                 SOAPBodyElement bodyElement = (SOAPBodyElement) convertToSoapElement(rootNode);
 234                 addNode(bodyElement);
 235                 return bodyElement;
 236         */
 237         ///*
 238         SOAPBodyElement newBodyElement = null;
 239         DocumentFragment docFrag = document.createDocumentFragment();
 240         Element rootElement = document.getDocumentElement();
 241         if(rootElement != null) {
 242             docFrag.appendChild(rootElement);
 243 
 244             Document ownerDoc = getOwnerDocument();
 245             // This copies the whole tree which could be very big so it's slow.
 246             // However, it does have the advantage of actually working.
 247             org.w3c.dom.Node replacingNode = ownerDoc.importNode(docFrag, true);
 248             // Adding replacingNode at the last of the children list of body
 249             addNode(replacingNode);
 250             Iterator<Node> i =
 251                 getChildElements(NameImpl.copyElementName(rootElement));
 252             // Return the child element with the required name which is at the
 253             // end of the list
 254             while(i.hasNext())
 255                 newBodyElement = (SOAPBodyElement) i.next();
 256         }
 257         return newBodyElement;
 258         //*/
 259     }
 260 
 261     protected SOAPElement convertToSoapElement(Element element) {
 262         if ((element instanceof SOAPBodyElement) &&
 263             //this check is required because ElementImpl currently
 264             // implements SOAPBodyElement
 265             !(element.getClass().equals(ElementImpl.class))) {
 266             return (SOAPElement) element;
 267         } else {
 268             return replaceElementWithSOAPElement(
 269                 element,
 270                 (ElementImpl) createBodyElement(NameImpl
 271                     .copyElementName(element)));
 272         }
 273     }
 274 
 275     public SOAPElement setElementQName(QName newName) throws SOAPException {
 276         log.log(Level.SEVERE,
 277                 "SAAJ0146.impl.invalid.name.change.requested",
 278                 new Object[] {elementQName.getLocalPart(),
 279                               newName.getLocalPart()});
 280         throw new SOAPException("Cannot change name for "
 281                                 + elementQName.getLocalPart() + " to "
 282                                 + newName.getLocalPart());
 283     }
 284 
 285     public Document extractContentAsDocument() throws SOAPException {
 286 
 287         Iterator<Node> eachChild = getChildElements();
 288         javax.xml.soap.Node firstBodyElement = null;
 289 
 290         while (eachChild.hasNext() &&
 291                !(firstBodyElement instanceof SOAPElement))
 292             firstBodyElement = (javax.xml.soap.Node) eachChild.next();
 293 
 294         boolean exactlyOneChildElement = true;
 295         if (firstBodyElement == null)
 296             exactlyOneChildElement = false;
 297         else {
 298             for (org.w3c.dom.Node node = firstBodyElement.getNextSibling();
 299                  node != null;
 300                  node = node.getNextSibling()) {
 301 
 302                 if (node instanceof Element) {
 303                     exactlyOneChildElement = false;
 304                     break;
 305                 }
 306             }
 307         }
 308 
 309         if(!exactlyOneChildElement) {
 310             log.log(Level.SEVERE,
 311                     "SAAJ0250.impl.body.should.have.exactly.one.child");
 312             throw new SOAPException("Cannot extract Document from body");
 313         }
 314 
 315         Document document = null;
 316         try {
 317             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

 318             factory.setNamespaceAware(true);
 319             DocumentBuilder builder = factory.newDocumentBuilder();
 320             document = builder.newDocument();
 321 
 322             Element rootElement = (Element) document.importNode(
 323                                                 firstBodyElement,
 324                                                 true);
 325 
 326             document.appendChild(rootElement);
 327 
 328         } catch(Exception e) {
 329             log.log(Level.SEVERE,
 330                     "SAAJ0251.impl.cannot.extract.document.from.body");
 331             throw new SOAPExceptionImpl(
 332                 "Unable to extract Document from body", e);
 333         }
 334 
 335         firstBodyElement.detachNode();
 336 
 337         return document;


< prev index next >