KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > message > SOAPBodyAxisImpl


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.jboss.axis.message;
18
19 import org.jboss.axis.AxisFault;
20 import org.jboss.axis.Constants;
21 import org.jboss.axis.encoding.DeserializationContext;
22 import org.jboss.axis.encoding.SerializationContext;
23 import org.jboss.axis.soap.SOAPConstants;
24 import org.jboss.axis.utils.Messages;
25 import org.jboss.logging.Logger;
26 import org.w3c.dom.Attr JavaDoc;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.NamedNodeMap JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32 import org.xml.sax.Attributes JavaDoc;
33
34 import javax.xml.namespace.QName JavaDoc;
35 import javax.xml.rpc.JAXRPCException JavaDoc;
36 import javax.xml.soap.Name JavaDoc;
37 import javax.xml.soap.SOAPBodyElement JavaDoc;
38 import javax.xml.soap.SOAPElement JavaDoc;
39 import javax.xml.soap.SOAPException JavaDoc;
40 import javax.xml.soap.SOAPFault JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Locale JavaDoc;
45
46 /**
47  * Holder for body elements.
48  *
49  * @author Glyn Normington (glyn@apache.org)
50  */

51 public class SOAPBodyAxisImpl extends SOAPBodyImpl
52 {
53
54    private static Logger log = Logger.getLogger(SOAPBodyAxisImpl.class.getName());
55
56    private SOAPConstants soapConstants;
57
58    private boolean disableFormatting;
59    private boolean doSAAJEncodingCompliance;
60    private static ArrayList JavaDoc knownEncodingStyles = new ArrayList JavaDoc();
61
62    static
63    {
64       knownEncodingStyles.add(Constants.URI_SOAP11_ENC);
65       knownEncodingStyles.add(Constants.URI_SOAP12_ENC);
66       knownEncodingStyles.add("");
67       knownEncodingStyles.add(Constants.URI_SOAP12_NOENC);
68    }
69
70    SOAPBodyAxisImpl(SOAPEnvelopeAxisImpl env, SOAPConstants soapConsts)
71    {
72       super(soapConsts.getEnvelopeURI(), Constants.ELEM_BODY);
73       soapConstants = soapConsts;
74       try
75       {
76          setParentElement(env);
77       }
78       catch (SOAPException JavaDoc ex)
79       {
80          // class cast should never fail when parent is a SOAPEnvelope
81
log.fatal(Messages.getMessage("exception00"), ex);
82       }
83    }
84
85    public SOAPBodyAxisImpl(String JavaDoc namespace, String JavaDoc localPart, String JavaDoc prefix, Attributes JavaDoc attributes,
86                            DeserializationContext context, SOAPConstants soapConsts) throws AxisFault
87    {
88       super(namespace, localPart, prefix, attributes, context);
89       soapConstants = soapConsts;
90    }
91
92    public void setParentElement(SOAPElement JavaDoc parent) throws SOAPException JavaDoc
93    {
94       if (parent == null)
95          throw new IllegalArgumentException JavaDoc(Messages.getMessage("nullParent00"));
96
97       try
98       {
99          SOAPEnvelopeAxisImpl env = (SOAPEnvelopeAxisImpl)parent;
100          super.setParentElement(env);
101          setEnvelope(env);
102       }
103       catch (Throwable JavaDoc t)
104       {
105          throw new SOAPException JavaDoc(t);
106       }
107    }
108
109    public void disableFormatting()
110    {
111       this.disableFormatting = true;
112    }
113
114    public void setEncodingStyle(String JavaDoc encodingStyle) throws SOAPException JavaDoc
115    {
116       if (encodingStyle == null)
117       {
118          encodingStyle = "";
119       }
120
121       if (doSAAJEncodingCompliance)
122       {
123          // Make sure this matches a known encodingStyle. This is
124
if (!knownEncodingStyles.contains(encodingStyle))
125             throw new IllegalArgumentException JavaDoc(Messages.getMessage("badEncodingStyle1", encodingStyle));
126       }
127
128       super.setEncodingStyle(encodingStyle);
129    }
130
131    protected void outputImpl(SerializationContext context) throws Exception JavaDoc
132    {
133       boolean oldPretty = context.getPretty();
134       context.setPretty(!disableFormatting);
135
136       if (getChildren().isEmpty())
137       {
138          // This is a problem.
139
// throw new Exception("No body elements!");
140
// If there are no body elements just return - it's ok that
141
// the body is empty
142
}
143
144       // Output <SOAP-ENV:Body>
145
context.startElement(new QName JavaDoc(soapConstants.getEnvelopeURI(),
146               Constants.ELEM_BODY), getAttributesEx());
147 /*
148         Enumeration enumeration = bodyElements.elements();
149         while (enumeration.hasMoreElements()) {
150             SOAPBodyElement body = (SOAPBodyElement)enumeration.nextElement();
151                                        Constants.ELEM_BODY), getAttributes());
152 */

153       for (Iterator JavaDoc it = getChildElements(); it.hasNext();)
154       {
155          Node JavaDoc childNode = (Node JavaDoc)it.next();
156          if (childNode instanceof SOAPElementAxisImpl)
157             ((SOAPElementAxisImpl)childNode).output(context);
158          else if (childNode instanceof TextImpl)
159             context.writeString(childNode.getNodeValue());
160       }
161
162       // Output multi-refs if appropriate
163
context.outputMultiRefs();
164
165       // Output </SOAP-ENV:Body>
166
context.endElement();
167
168       context.setPretty(oldPretty);
169    }
170
171    List JavaDoc getBodyElements() throws AxisFault
172    {
173       return getChildren();
174    }
175
176    void addBodyElement(SOAPBodyElementAxisImpl element)
177    {
178       if (log.isDebugEnabled())
179          log.debug(Messages.getMessage("addBody00"));
180       try
181       {
182          element.setParentElement(this);
183       }
184       catch (SOAPException JavaDoc ex)
185       {
186          // class cast should never fail when parent is a SOAPBody
187
log.fatal(Messages.getMessage("exception00"), ex);
188       }
189    }
190
191    void clearBody()
192    {
193       removeContents();
194    }
195
196    void removeBodyElement(SOAPBodyElementAxisImpl element)
197    {
198       if (log.isDebugEnabled())
199          log.debug(Messages.getMessage("removeBody00"));
200       removeChild(element);
201    }
202
203    SOAPBodyElementAxisImpl getBodyByName(String JavaDoc namespace, String JavaDoc localPart)
204            throws AxisFault
205    {
206       return (SOAPBodyElementAxisImpl)findElement(getChildren(),
207               namespace,
208               localPart);
209    }
210
211    protected SOAPElementAxisImpl findElement(List JavaDoc list, String JavaDoc namespace, String JavaDoc localPart)
212    {
213       if (list.isEmpty())
214          return null;
215
216       QName JavaDoc qname = new QName JavaDoc(namespace, localPart);
217       Iterator JavaDoc it = list.iterator();
218       while (it.hasNext())
219       {
220          SOAPElementAxisImpl element = (SOAPElementAxisImpl)it.next();
221          if (element.getQName().equals(qname))
222             return element;
223       }
224
225       return null;
226    }
227
228    SOAPBodyElementAxisImpl getFirstBody() throws AxisFault
229    {
230       if (getChildren().isEmpty())
231          return null;
232 /*
233         return (SOAPBodyElement)bodyElements.elementAt(0);
234 */

235       return (SOAPBodyElementAxisImpl)getChildren().get(0);
236    }
237
238    // JAXM methods
239

240    public SOAPBodyElement JavaDoc addBodyElement(Name JavaDoc name)
241            throws SOAPException JavaDoc
242    {
243       SOAPBodyElementAxisImpl bodyElement = new SOAPBodyElementAxisImpl(name);
244       addBodyElement(bodyElement);
245       return bodyElement;
246    }
247
248    public SOAPFault JavaDoc addFault(Name JavaDoc name, String JavaDoc s, Locale JavaDoc locale) throws SOAPException JavaDoc
249    {
250       AxisFault af = new AxisFault(new QName JavaDoc(name.getURI(), name.getLocalName()), s, "", new Element JavaDoc[0]);
251       SOAPFaultImpl fault = new SOAPFaultImpl(af);
252       addBodyElement(fault);
253       return fault;
254    }
255
256    public SOAPFault JavaDoc addFault(Name JavaDoc name, String JavaDoc s) throws SOAPException JavaDoc
257    {
258       AxisFault af = new AxisFault(new QName JavaDoc(name.getURI(), name.getLocalName()), s, "", new Element JavaDoc[0]);
259       SOAPFaultImpl fault = new SOAPFaultImpl(af);
260       addBodyElement(fault);
261       return fault;
262    }
263
264    public SOAPBodyElement JavaDoc addDocument(Document JavaDoc document) throws SOAPException JavaDoc
265    {
266       return (SOAPBodyElement JavaDoc)importDOMElement(this, document.getDocumentElement());
267    }
268
269    public SOAPFault JavaDoc addFault() throws SOAPException JavaDoc
270    {
271
272       AxisFault af = new AxisFault(new QName JavaDoc(Constants.NS_URI_AXIS, Constants.FAULT_SERVER_GENERAL), "", "", new Element JavaDoc[0]);
273       SOAPFaultImpl fault = new SOAPFaultImpl(af);
274       addBodyElement(fault);
275       return fault;
276    }
277
278    public SOAPFault JavaDoc getFault()
279    {
280       Iterator JavaDoc it = getChildren().iterator();
281       while (it.hasNext())
282       {
283          Object JavaDoc element = it.next();
284          if (element instanceof SOAPFault JavaDoc)
285          {
286             return (SOAPFault JavaDoc)element;
287          }
288       }
289       return null;
290    }
291
292    public boolean hasFault()
293    {
294       Iterator JavaDoc it = getChildren().iterator();
295       while (it.hasNext())
296       {
297          if (it.next() instanceof SOAPFault JavaDoc)
298          {
299             return true;
300          }
301       }
302       return false;
303    }
304
305    public void setSAAJEncodingCompliance(boolean comply)
306    {
307       this.doSAAJEncodingCompliance = true;
308    }
309
310    public SOAPElement JavaDoc addChildElement(Name JavaDoc name) throws SOAPException JavaDoc
311    {
312       SOAPBodyElementAxisImpl soapBodyElement = new SOAPBodyElementAxisImpl(name);
313       super.addChildElement(soapBodyElement);
314       return soapBodyElement;
315    }
316
317    /** Recursive function
318     */

319    private SOAPElementAxisImpl importDOMElement(SOAPElementAxisImpl soapParent, Element JavaDoc domElement)
320    {
321       try
322       {
323          NameImpl name;
324          if (domElement.getNamespaceURI() != null)
325             name = new NameImpl(domElement.getLocalName(), domElement.getPrefix(), domElement.getNamespaceURI());
326          else
327             name = new NameImpl(domElement.getLocalName());
328
329          SOAPElementAxisImpl soapChild = (SOAPElementAxisImpl)soapParent.addChildElement(name);
330
331          NamedNodeMap JavaDoc attrs = ((Element JavaDoc)domElement).getAttributes();
332          for (int i = 0; i < attrs.getLength(); i++)
333          {
334             Attr JavaDoc att = (Attr JavaDoc)attrs.item(i);
335             String JavaDoc nsURI = att.getNamespaceURI();
336             String JavaDoc localName = att.getLocalName();
337             String JavaDoc value = att.getValue();
338
339             if (nsURI != null)
340                soapChild.setAttributeNS(nsURI, localName, value);
341             else
342                soapChild.setAttribute(localName, value);
343          }
344
345          StringBuffer JavaDoc content = new StringBuffer JavaDoc();
346
347          NodeList JavaDoc children = domElement.getChildNodes();
348          for (int i = 0; i < children.getLength(); i++)
349          {
350             Node JavaDoc domChild = children.item(i);
351             if (domChild.getNodeType() == Node.ELEMENT_NODE)
352                importDOMElement(soapChild, (Element JavaDoc)domChild);
353
354             if (domChild.getNodeType() == Node.TEXT_NODE && domChild.getNodeValue().trim().length() > 0)
355                content.append(domChild.getNodeValue());
356          }
357
358          if (content.length() > 0)
359          {
360             String JavaDoc value = content.toString();
361             soapChild.addTextNode(value);
362          }
363
364          return soapChild;
365       }
366       catch (RuntimeException JavaDoc e)
367       {
368          throw e;
369       }
370       catch (Exception JavaDoc e)
371       {
372          throw new JAXRPCException JavaDoc(e);
373       }
374    }
375 }
376
Popular Tags