KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > message > SOAPBody


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.message;
18
19 import org.apache.axis.AxisFault;
20 import org.apache.axis.Constants;
21 import org.apache.axis.components.logger.LogFactory;
22 import org.apache.axis.encoding.DeserializationContext;
23 import org.apache.axis.encoding.SerializationContext;
24 import org.apache.axis.soap.SOAPConstants;
25 import org.apache.axis.utils.Messages;
26 import org.apache.commons.logging.Log;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.xml.sax.Attributes JavaDoc;
30
31 import javax.xml.namespace.QName JavaDoc;
32 import javax.xml.soap.Name JavaDoc;
33 import javax.xml.soap.SOAPElement JavaDoc;
34 import javax.xml.soap.SOAPException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38 import java.util.Locale JavaDoc;
39 import java.util.Vector JavaDoc;
40
41 /**
42  * Holder for body elements.
43  *
44  * @author Glyn Normington (glyn@apache.org)
45  */

46 public class SOAPBody extends MessageElement
47     implements javax.xml.soap.SOAPBody JavaDoc {
48
49     private static Log log = LogFactory.getLog(SOAPBody.class.getName());
50
51     private SOAPConstants soapConstants;
52
53     private boolean disableFormatting = false;
54     private boolean doSAAJEncodingCompliance = false;
55     private static ArrayList JavaDoc knownEncodingStyles = new ArrayList JavaDoc();
56
57     static {
58         knownEncodingStyles.add(Constants.URI_SOAP11_ENC);
59         knownEncodingStyles.add(Constants.URI_SOAP12_ENC);
60         knownEncodingStyles.add("");
61         knownEncodingStyles.add(Constants.URI_SOAP12_NOENC);
62     }
63
64     SOAPBody(SOAPEnvelope env, SOAPConstants soapConsts) {
65        super(soapConsts.getEnvelopeURI(), Constants.ELEM_BODY);
66        soapConstants = soapConsts;
67         try {
68             setParentElement(env);
69         } catch (SOAPException JavaDoc ex) {
70             // class cast should never fail when parent is a SOAPEnvelope
71
log.fatal(Messages.getMessage("exception00"), ex);
72         }
73     }
74
75     public SOAPBody(String JavaDoc namespace, String JavaDoc localPart, String JavaDoc prefix,
76                     Attributes JavaDoc attributes, DeserializationContext context,
77                     SOAPConstants soapConsts) throws AxisFault {
78         super(namespace, localPart, prefix, attributes, context);
79         soapConstants = soapConsts;
80     }
81
82     public void setParentElement(SOAPElement JavaDoc parent) throws SOAPException JavaDoc {
83         if(parent == null) {
84             throw new IllegalArgumentException JavaDoc(Messages.getMessage("nullParent00"));
85         }
86         try {
87             SOAPEnvelope env = (SOAPEnvelope)parent;
88             super.setParentElement(env);
89             setEnvelope(env);
90         } catch (Throwable JavaDoc t) {
91             throw new SOAPException JavaDoc(t);
92         }
93     }
94
95     public void disableFormatting() {
96         this.disableFormatting = true;
97     }
98
99     public void setEncodingStyle(String JavaDoc encodingStyle) throws SOAPException JavaDoc {
100         if (encodingStyle == null) {
101             encodingStyle = "";
102         }
103
104         if (doSAAJEncodingCompliance) {
105             // Make sure this matches a known encodingStyle. This is
106
if (!knownEncodingStyles.contains(encodingStyle))
107                 throw new IllegalArgumentException JavaDoc(Messages.getMessage("badEncodingStyle1", encodingStyle));
108         }
109
110         super.setEncodingStyle(encodingStyle);
111     }
112
113     protected void outputImpl(SerializationContext context) throws Exception JavaDoc {
114         boolean oldPretty = context.getPretty();
115         if (!disableFormatting) {
116              context.setPretty(true);
117         } else {
118              context.setPretty(false);
119         }
120
121         List JavaDoc bodyElements = getChildren();
122
123         if (bodyElements == null || bodyElements.isEmpty()) {
124             // This is a problem.
125
// throw new Exception("No body elements!");
126
// If there are no body elements just return - it's ok that
127
// the body is empty
128
}
129
130         // Output <SOAP-ENV:Body>
131
context.startElement(new QName JavaDoc(soapConstants.getEnvelopeURI(),
132                                        Constants.ELEM_BODY),
133                              getAttributesEx());
134         
135         if (bodyElements != null) {
136             Iterator JavaDoc e = bodyElements.iterator();
137             while (e.hasNext()) {
138                 MessageElement body = (MessageElement)e.next();
139                 body.output(context);
140                 // Output this body element.
141
}
142         }
143         
144         // Output multi-refs if appropriate
145
context.outputMultiRefs();
146         
147         // Output </SOAP-ENV:Body>
148
context.endElement();
149
150         context.setPretty(oldPretty);
151     }
152
153     Vector JavaDoc getBodyElements() throws AxisFault {
154         initializeChildren();
155         return new Vector JavaDoc(getChildren());
156     }
157
158     SOAPBodyElement getFirstBody() throws AxisFault
159     {
160         if (!hasChildNodes())
161             return null;
162         return (SOAPBodyElement)getChildren().get(0);
163     }
164
165     void addBodyElement(SOAPBodyElement element)
166     {
167         if (log.isDebugEnabled())
168             log.debug(Messages.getMessage("addBody00"));
169         try {
170             addChildElement(element);
171         } catch (SOAPException JavaDoc ex) {
172             // class cast should never fail when parent is a SOAPBody
173
log.fatal(Messages.getMessage("exception00"), ex);
174         }
175     }
176
177     void removeBodyElement(SOAPBodyElement element)
178     {
179         if (log.isDebugEnabled())
180             log.debug(Messages.getMessage("removeBody00"));
181         removeChild( (MessageElement)element );
182     }
183
184     void clearBody()
185     {
186         removeContents();
187     }
188
189     SOAPBodyElement getBodyByName(String JavaDoc namespace, String JavaDoc localPart)
190         throws AxisFault
191     {
192         QName JavaDoc name = new QName JavaDoc(namespace, localPart);
193         return (SOAPBodyElement)getChildElement(name);
194     }
195
196     // JAXM methods
197

198     public javax.xml.soap.SOAPBodyElement JavaDoc addBodyElement(Name JavaDoc name)
199         throws SOAPException JavaDoc {
200         SOAPBodyElement bodyElement = new SOAPBodyElement(name);
201         addChildElement(bodyElement);
202         return bodyElement;
203     }
204
205     public javax.xml.soap.SOAPFault JavaDoc addFault(Name JavaDoc name, String JavaDoc s, Locale JavaDoc locale) throws SOAPException JavaDoc {
206         AxisFault af = new AxisFault(new QName JavaDoc(name.getURI(), name.getLocalName()), s, "", new Element JavaDoc[0]);
207         SOAPFault fault = new SOAPFault(af);
208         addChildElement(fault);
209         return fault;
210     }
211
212     public javax.xml.soap.SOAPFault JavaDoc addFault(Name JavaDoc name, String JavaDoc s) throws SOAPException JavaDoc {
213         AxisFault af = new AxisFault(new QName JavaDoc(name.getURI(), name.getLocalName()), s, "", new Element JavaDoc[0]);
214         SOAPFault fault = new SOAPFault(af);
215         addChildElement(fault);
216         return fault;
217     }
218
219     public javax.xml.soap.SOAPBodyElement JavaDoc addDocument(Document JavaDoc document) throws SOAPException JavaDoc {
220         SOAPBodyElement bodyElement = new SOAPBodyElement(document.getDocumentElement());
221         addChildElement(bodyElement);
222         return bodyElement;
223     }
224
225     public javax.xml.soap.SOAPFault JavaDoc addFault() throws SOAPException JavaDoc {
226         
227         AxisFault af = new AxisFault(new QName JavaDoc(Constants.NS_URI_AXIS, Constants.FAULT_SERVER_GENERAL), "", "", new Element JavaDoc[0]);
228         SOAPFault fault = new SOAPFault(af);
229         addChildElement(fault);
230         return fault;
231     }
232
233     public javax.xml.soap.SOAPFault JavaDoc getFault() {
234         List JavaDoc bodyElements = getChildren();
235         if (bodyElements != null) {
236             Iterator JavaDoc e = bodyElements.iterator();
237             while (e.hasNext()) {
238                 Object JavaDoc element = e.next();
239                 if(element instanceof javax.xml.soap.SOAPFault JavaDoc) {
240                     return (javax.xml.soap.SOAPFault JavaDoc) element;
241                 }
242             }
243         }
244         return null;
245     }
246     
247     public boolean hasFault() {
248         return (getFault() != null);
249     }
250
251     // overwrite the one in MessageElement and set envelope
252
public void addChild(MessageElement element) throws SOAPException JavaDoc {
253 // Commented out for SAAJ compatibility - gdaniels, 05/19/2003
254
// if (!(element instanceof javax.xml.soap.SOAPBodyElement)) {
255
// throw new SOAPException(Messages.getMessage("badSOAPBodyElement00"));
256
// }
257
element.setEnvelope(getEnvelope());
258         super.addChild(element);
259     }
260
261     // overwrite the one in MessageElement and sets dirty flag
262
public SOAPElement JavaDoc addChildElement(SOAPElement JavaDoc element)
263         throws SOAPException JavaDoc {
264 // Commented out for SAAJ compatibility - gdaniels, 05/19/2003
265
// if (!(element instanceof javax.xml.soap.SOAPBodyElement)) {
266
// throw new SOAPException(Messages.getMessage("badSOAPBodyElement00"));
267
// }
268
SOAPElement JavaDoc child = super.addChildElement(element);
269         setDirty(true);
270         return child;
271     }
272
273     public SOAPElement JavaDoc addChildElement(Name JavaDoc name) throws SOAPException JavaDoc {
274         SOAPBodyElement child = new SOAPBodyElement(name);
275         addChildElement(child);
276         return child;
277     }
278
279     public SOAPElement JavaDoc addChildElement(String JavaDoc localName) throws SOAPException JavaDoc {
280         // Inherit parent's namespace
281
SOAPBodyElement child = new SOAPBodyElement(getNamespaceURI(),
282                                                     localName);
283         addChildElement(child);
284         return child;
285     }
286
287     public SOAPElement JavaDoc addChildElement(String JavaDoc localName,
288                                        String JavaDoc prefix) throws SOAPException JavaDoc {
289         SOAPBodyElement child =
290             new SOAPBodyElement(getNamespaceURI(prefix), localName);
291         child.setPrefix(prefix);
292         addChildElement(child);
293         return child;
294     }
295
296     public SOAPElement JavaDoc addChildElement(String JavaDoc localName,
297                                        String JavaDoc prefix,
298                                        String JavaDoc uri) throws SOAPException JavaDoc {
299         SOAPBodyElement child = new SOAPBodyElement(uri, localName);
300         child.setPrefix(prefix);
301         child.addNamespaceDeclaration(prefix, uri);
302         addChildElement(child);
303         return child;
304     }
305
306     public void setSAAJEncodingCompliance(boolean comply) {
307         this.doSAAJEncodingCompliance = true;
308     }
309 }
310
Popular Tags