001: /*
002: * $Id: BodyImpl.java,v 1.41 2006/01/27 12:49:34 vj135062 Exp $
003: * $Revision: 1.41 $
004: * $Date: 2006/01/27 12:49:34 $
005: */
006:
007: /*
008: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
009: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
010: *
011: * This code is free software; you can redistribute it and/or modify it
012: * under the terms of the GNU General Public License version 2 only, as
013: * published by the Free Software Foundation. Sun designates this
014: * particular file as subject to the "Classpath" exception as provided
015: * by Sun in the LICENSE file that accompanied this code.
016: *
017: * This code is distributed in the hope that it will be useful, but WITHOUT
018: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
019: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
020: * version 2 for more details (a copy is included in the LICENSE file that
021: * accompanied this code).
022: *
023: * You should have received a copy of the GNU General Public License version
024: * 2 along with this work; if not, write to the Free Software Foundation,
025: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
026: *
027: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
028: * CA 95054 USA or visit www.sun.com if you need additional information or
029: * have any questions.
030: */
031: package com.sun.xml.internal.messaging.saaj.soap.impl;
032:
033: import java.util.Iterator;
034: import java.util.Locale;
035: import java.util.logging.Level;
036:
037: import javax.xml.namespace.QName;
038: import javax.xml.soap.*;
039: import javax.xml.parsers.DocumentBuilder;
040: import javax.xml.parsers.DocumentBuilderFactory;
041:
042: import org.w3c.dom.*;
043:
044: import com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl;
045: import com.sun.xml.internal.messaging.saaj.soap.SOAPDocument;
046: import com.sun.xml.internal.messaging.saaj.soap.SOAPDocumentImpl;
047: import com.sun.xml.internal.messaging.saaj.soap.name.NameImpl;
048:
049: /**
050: * The implementation of SOAP-ENV:BODY or the SOAPBody abstraction.
051: *
052: * @author Anil Vijendran (anil@sun.com)
053: */
054: public abstract class BodyImpl extends ElementImpl implements SOAPBody {
055: private SOAPFault fault;
056:
057: protected BodyImpl(SOAPDocumentImpl ownerDoc, NameImpl bodyName) {
058: super (ownerDoc, bodyName);
059: }
060:
061: protected abstract NameImpl getFaultName(String name);
062:
063: protected abstract boolean isFault(SOAPElement child);
064:
065: protected abstract SOAPBodyElement createBodyElement(Name name);
066:
067: protected abstract SOAPBodyElement createBodyElement(QName name);
068:
069: protected abstract SOAPFault createFaultElement();
070:
071: protected abstract QName getDefaultFaultCode();
072:
073: public SOAPFault addFault() throws SOAPException {
074: if (hasFault()) {
075: log.severe("SAAJ0110.impl.fault.already.exists");
076: throw new SOAPExceptionImpl("Error: Fault already exists");
077: }
078:
079: fault = createFaultElement();
080:
081: addNode(fault);
082:
083: fault.setFaultCode(getDefaultFaultCode());
084: fault
085: .setFaultString("Fault string, and possibly fault code, not set");
086:
087: return fault;
088: }
089:
090: public SOAPFault addFault(Name faultCode, String faultString,
091: Locale locale) throws SOAPException {
092:
093: SOAPFault fault = addFault();
094: fault.setFaultCode(faultCode);
095: fault.setFaultString(faultString, locale);
096: return fault;
097: }
098:
099: public SOAPFault addFault(QName faultCode, String faultString,
100: Locale locale) throws SOAPException {
101:
102: SOAPFault fault = addFault();
103: fault.setFaultCode(faultCode);
104: fault.setFaultString(faultString, locale);
105: return fault;
106: }
107:
108: public SOAPFault addFault(Name faultCode, String faultString)
109: throws SOAPException {
110:
111: SOAPFault fault = addFault();
112: fault.setFaultCode(faultCode);
113: fault.setFaultString(faultString);
114: return fault;
115: }
116:
117: public SOAPFault addFault(QName faultCode, String faultString)
118: throws SOAPException {
119:
120: SOAPFault fault = addFault();
121: fault.setFaultCode(faultCode);
122: fault.setFaultString(faultString);
123: return fault;
124: }
125:
126: void initializeFault() {
127: FaultImpl flt = (FaultImpl) findFault();
128: fault = flt;
129: }
130:
131: protected SOAPElement findFault() {
132: Iterator eachChild = getChildElementNodes();
133: while (eachChild.hasNext()) {
134: SOAPElement child = (SOAPElement) eachChild.next();
135: if (isFault(child)) {
136: return child;
137: }
138: }
139:
140: return null;
141: }
142:
143: public boolean hasFault() {
144: initializeFault();
145: return fault != null;
146: }
147:
148: public SOAPFault getFault() {
149: if (hasFault())
150: return fault;
151: return null;
152: }
153:
154: public SOAPBodyElement addBodyElement(Name name)
155: throws SOAPException {
156: SOAPBodyElement newBodyElement = (SOAPBodyElement) ElementFactory
157: .createNamedElement(((SOAPDocument) getOwnerDocument())
158: .getDocument(), name.getLocalName(), name
159: .getPrefix(), name.getURI());
160: if (newBodyElement == null) {
161: newBodyElement = createBodyElement(name);
162: }
163: addNode(newBodyElement);
164: return newBodyElement;
165: }
166:
167: public SOAPBodyElement addBodyElement(QName qname)
168: throws SOAPException {
169: SOAPBodyElement newBodyElement = (SOAPBodyElement) ElementFactory
170: .createNamedElement(((SOAPDocument) getOwnerDocument())
171: .getDocument(), qname.getLocalPart(), qname
172: .getPrefix(), qname.getNamespaceURI());
173: if (newBodyElement == null) {
174: newBodyElement = createBodyElement(qname);
175: }
176: addNode(newBodyElement);
177: return newBodyElement;
178: }
179:
180: public void setParentElement(SOAPElement element)
181: throws SOAPException {
182:
183: if (!(element instanceof SOAPEnvelope)) {
184: log.severe("SAAJ0111.impl.body.parent.must.be.envelope");
185: throw new SOAPException(
186: "Parent of SOAPBody has to be a SOAPEnvelope");
187: }
188: super .setParentElement(element);
189: }
190:
191: protected SOAPElement addElement(Name name) throws SOAPException {
192: return addBodyElement(name);
193: }
194:
195: protected SOAPElement addElement(QName name) throws SOAPException {
196: return addBodyElement(name);
197: }
198:
199: // public Node insertBefore(Node newElement, Node ref) throws DOMException {
200: // if (!(newElement instanceof SOAPBodyElement) && (newElement instanceof SOAPElement)) {
201: // newElement = new ElementWrapper((ElementImpl) newElement);
202: // }
203: // return super.insertBefore(newElement, ref);
204: // }
205: //
206: // public Node replaceChild(Node newElement, Node ref) throws DOMException {
207: // if (!(newElement instanceof SOAPBodyElement) && (newElement instanceof SOAPElement)) {
208: // newElement = new ElementWrapper((ElementImpl) newElement);
209: // }
210: // return super.replaceChild(newElement, ref);
211: // }
212:
213: public SOAPBodyElement addDocument(Document document)
214: throws SOAPException {
215: /*
216:
217: Element rootNode =
218: document.getDocumentElement();
219: // Causes all deferred nodes to be inflated
220: rootNode.normalize();
221: adoptElement(rootNode);
222: SOAPBodyElement bodyElement = (SOAPBodyElement) convertToSoapElement(rootNode);
223: addNode(bodyElement);
224: return bodyElement;
225: */
226: ///*
227: SOAPBodyElement newBodyElement = null;
228: DocumentFragment docFrag = document.createDocumentFragment();
229: Element rootElement = document.getDocumentElement();
230: if (rootElement != null) {
231: docFrag.appendChild(rootElement);
232:
233: Document ownerDoc = getOwnerDocument();
234: // This copies the whole tree which could be very big so it's slow.
235: // However, it does have the advantage of actually working.
236: org.w3c.dom.Node replacingNode = ownerDoc.importNode(
237: docFrag, true);
238: // Adding replacingNode at the last of the children list of body
239: addNode(replacingNode);
240: Iterator i = getChildElements(NameImpl
241: .copyElementName(rootElement));
242: // Return the child element with the required name which is at the
243: // end of the list
244: while (i.hasNext())
245: newBodyElement = (SOAPBodyElement) i.next();
246: }
247: return newBodyElement;
248: //*/
249: }
250:
251: protected SOAPElement convertToSoapElement(Element element) {
252: if ((element instanceof SOAPBodyElement) &&
253: //this check is required because ElementImpl currently
254: // implements SOAPBodyElement
255: !(element.getClass().equals(ElementImpl.class))) {
256: return (SOAPElement) element;
257: } else {
258: return replaceElementWithSOAPElement(element,
259: (ElementImpl) createBodyElement(NameImpl
260: .copyElementName(element)));
261: }
262: }
263:
264: public SOAPElement setElementQName(QName newName)
265: throws SOAPException {
266: log.log(Level.SEVERE,
267: "SAAJ0146.impl.invalid.name.change.requested",
268: new Object[] { elementQName.getLocalPart(),
269: newName.getLocalPart() });
270: throw new SOAPException("Cannot change name for "
271: + elementQName.getLocalPart() + " to "
272: + newName.getLocalPart());
273: }
274:
275: public Document extractContentAsDocument() throws SOAPException {
276:
277: Iterator eachChild = getChildElements();
278: javax.xml.soap.Node firstBodyElement = null;
279:
280: while (eachChild.hasNext()
281: && !(firstBodyElement instanceof SOAPElement))
282: firstBodyElement = (javax.xml.soap.Node) eachChild.next();
283:
284: boolean exactlyOneChildElement = true;
285: if (firstBodyElement == null)
286: exactlyOneChildElement = false;
287: else {
288: for (org.w3c.dom.Node node = firstBodyElement
289: .getNextSibling(); node != null; node = node
290: .getNextSibling()) {
291:
292: if (node instanceof Element) {
293: exactlyOneChildElement = false;
294: break;
295: }
296: }
297: }
298:
299: if (!exactlyOneChildElement) {
300: log.log(Level.SEVERE,
301: "SAAJ0250.impl.body.should.have.exactly.one.child");
302: throw new SOAPException("Cannot extract Document from body");
303: }
304:
305: Document document = null;
306: try {
307: DocumentBuilderFactory factory = new com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl();
308: factory.setNamespaceAware(true);
309: DocumentBuilder builder = factory.newDocumentBuilder();
310: document = builder.newDocument();
311:
312: Element rootElement = (Element) document.importNode(
313: firstBodyElement, true);
314:
315: document.appendChild(rootElement);
316:
317: } catch (Exception e) {
318: log.log(Level.SEVERE,
319: "SAAJ0251.impl.cannot.extract.document.from.body");
320: throw new SOAPExceptionImpl(
321: "Unable to extract Document from body", e);
322: }
323:
324: firstBodyElement.detachNode();
325:
326: return document;
327: }
328:
329: }
|