KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > encoding > soapenc > ParameterSerializer


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "SOAP" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap.encoding.soapenc;
59
60 import java.io.*;
61 import org.w3c.dom.*;
62 import org.apache.soap.util.*;
63 import org.apache.soap.util.xml.*;
64 import org.apache.soap.*;
65 import org.apache.soap.rpc.*;
66
67 /**
68  * A <code>ParameterSerializer</code> is used to serialize and deserialize
69  * parameters using the <code>SOAP-ENC</code> encoding style.
70  *
71  * @author Matthew J. Duftler (duftler@us.ibm.com)
72  * @author Sander Brienen (sander.brienen@capgemini.nl)
73  * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
74  */

75 public class ParameterSerializer implements Serializer, Deserializer
76 {
77   public void marshall(String JavaDoc inScopeEncStyle, Class JavaDoc javaType, Object JavaDoc src,
78                        Object JavaDoc context, Writer sink, NSStack nsStack,
79                        XMLJavaMappingRegistry xjmr, SOAPContext ctx)
80     throws IllegalArgumentException JavaDoc, IOException
81   {
82     nsStack.pushScope();
83
84     Parameter param = (Parameter)src;
85     Class JavaDoc type = param.getType();
86     String JavaDoc name = param.getName();
87     Object JavaDoc value = param.getValue();
88
89     if (!(context instanceof PrefixedName))
90     {
91       context = name;
92     }
93
94     if (value == null && !type.isArray())
95     {
96       SoapEncUtils.generateNullStructure(inScopeEncStyle, type, context,
97                                          sink, nsStack, xjmr);
98     }
99     else
100     {
101       String JavaDoc declEncStyle = param.getEncodingStyleURI();
102       String JavaDoc actualEncStyle = declEncStyle != null
103                               ? declEncStyle
104                               : inScopeEncStyle;
105       Serializer s = xjmr.querySerializer(type, actualEncStyle);
106
107       s.marshall(inScopeEncStyle, type, value, context,
108                  sink, nsStack, xjmr, ctx);
109     }
110
111     nsStack.popScope();
112   }
113
114   public Bean unmarshall(String JavaDoc inScopeEncStyle, QName elementType, Node src,
115                          XMLJavaMappingRegistry xjmr, SOAPContext ctx)
116     throws IllegalArgumentException JavaDoc
117   {
118     Element paramEl = (Element)src;
119     String JavaDoc name = paramEl.getTagName();
120     Bean bean = null;
121
122     /*
123       If the element contains an HREF= parameter, shortcut to
124       MimePartSerializer.
125     */

126     String JavaDoc href = DOMUtils.getAttribute(paramEl, Constants.ATTR_REFERENCE);
127
128     if (href != null)
129     {
130       // First, check to see if it's a local ref.
131
if (href.length() > 0 && href.charAt(0) == '#')
132       {
133         href = href.substring(1);
134
135         Element el =
136           DOMUtils.getElementByID(src.getOwnerDocument().getDocumentElement(),
137                                   href);
138
139         if (el == null)
140         {
141           throw new IllegalArgumentException JavaDoc("No such ID '" + href + "'.");
142         }
143
144         QName soapType = SoapEncUtils.getTypeQName(el);
145
146         if (soapType == null)
147         {
148           /*
149             No xsi:type attribute found: determine the type as the
150             qualified name of the parameter element (if the parameter
151             element is qualified) or as the qname formed by an empty
152             namespace URI and the tag name of the parameter element.
153             Is that a legal qname???
154           */

155           String JavaDoc paramNamespaceURI = paramEl.getNamespaceURI();
156
157           if (paramNamespaceURI != null)
158           {
159             soapType = new QName(paramNamespaceURI, name);
160           }
161           else
162           {
163             soapType = new QName("", name);
164           }
165         }
166
167         bean = xjmr.unmarshall(inScopeEncStyle, soapType, el, ctx);
168       }
169       else
170       {
171         bean = (new MimePartSerializer()).unmarshall(inScopeEncStyle,
172                                                      elementType, src,
173                                                      xjmr, ctx);
174       }
175     }
176     else
177     {
178       QName soapType = SoapEncUtils.getTypeQName(paramEl);
179
180       if (soapType == null)
181       {
182         /*
183           No xsi:type attribute found: determine the type as the
184           qualified name of the parameter element (if the parameter
185           element is qualified) or as the qname formed by an empty
186           namespace URI and the tag name of the parameter element.
187           Is that a legal qname???
188         */

189         String JavaDoc paramNamespaceURI = paramEl.getNamespaceURI();
190
191         if (paramNamespaceURI != null)
192         {
193           soapType = new QName(paramNamespaceURI, name);
194         }
195         else
196         {
197           soapType = new QName("", name);
198         }
199       }
200
201       bean = (SoapEncUtils.isNull(paramEl)
202               && !new QName(Constants.NS_URI_SOAP_ENC,
203                             "Array").equals(soapType)
204               ? new Bean(xjmr.queryJavaType(soapType, inScopeEncStyle),
205                          null)
206               : xjmr.unmarshall(inScopeEncStyle, soapType, paramEl, ctx));
207     }
208     
209     Parameter parameter = new Parameter(name, bean.type,
210                                         bean.value, null);
211     
212     return new Bean(Parameter.class, parameter);
213   }
214 }
215
Popular Tags