KickJava   Java API By Example, From Geeks To Geeks.

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


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.xml.*;
63 import org.apache.soap.*;
64
65 /**
66  * These static methods can be used to do much of the repetitive and
67  * mechanical work that is required when generating structures using
68  * the <code>SOAP-ENC</code> encoding style.
69  *
70  * @author Matthew J. Duftler (duftler@us.ibm.com)
71  */

72 public class SoapEncUtils
73 {
74   public static void generateNullStructure(String JavaDoc inScopeEncStyle,
75                                            Class JavaDoc javaType, Object JavaDoc context,
76                                            Writer sink, NSStack nsStack,
77                                            XMLJavaMappingRegistry xjmr)
78     throws IllegalArgumentException JavaDoc, IOException
79   {
80     generateStructureHeader(inScopeEncStyle, javaType, context, sink,
81                             nsStack, xjmr, null, null, true);
82   }
83
84   public static void generateNullArray(String JavaDoc inScopeEncStyle,
85                                        Class JavaDoc javaType, Object JavaDoc context,
86                                        Writer sink, NSStack nsStack,
87                                        XMLJavaMappingRegistry xjmr,
88                                        QName arrayElementType,
89                                        String JavaDoc arrayLengthStr)
90     throws IllegalArgumentException JavaDoc, IOException
91   {
92     generateStructureHeader(inScopeEncStyle, javaType, context, sink,
93                             nsStack, xjmr, arrayElementType, arrayLengthStr,
94                             true);
95   }
96
97   public static void generateArrayHeader(String JavaDoc inScopeEncStyle,
98                                          Class JavaDoc javaType, Object JavaDoc context,
99                                          Writer sink, NSStack nsStack,
100                                          XMLJavaMappingRegistry xjmr,
101                                          QName arrayElementType,
102                                          String JavaDoc arrayLengthStr)
103     throws IllegalArgumentException JavaDoc, IOException
104   {
105     generateStructureHeader(inScopeEncStyle, javaType, context, sink,
106                             nsStack, xjmr, arrayElementType, arrayLengthStr,
107                             false);
108   }
109
110   public static void generateStructureHeader(String JavaDoc inScopeEncStyle,
111                                              Class JavaDoc javaType, Object JavaDoc context,
112                                              Writer sink, NSStack nsStack,
113                                              XMLJavaMappingRegistry xjmr)
114     throws IllegalArgumentException JavaDoc, IOException
115   {
116     generateStructureHeader(inScopeEncStyle, javaType, context, sink,
117                             nsStack, xjmr, null, null, false);
118   }
119
120   private static void generateStructureHeader(String JavaDoc inScopeEncStyle,
121                                               Class JavaDoc javaType, Object JavaDoc context,
122                                               Writer sink, NSStack nsStack,
123                                               XMLJavaMappingRegistry xjmr,
124                                               QName arrayElementType,
125                                               String JavaDoc arrayLengthStr,
126                                               boolean isNull)
127     throws IllegalArgumentException JavaDoc, IOException
128   {
129     QName elementType = xjmr.queryElementType(javaType,
130                                               Constants.NS_URI_SOAP_ENC);
131     String JavaDoc namespaceDecl = "";
132
133     if (context instanceof PrefixedName)
134     {
135       PrefixedName pname = (PrefixedName)context;
136       QName qname = pname.getQName();
137
138       if (qname != null)
139       {
140         String JavaDoc namespaceURI = qname.getNamespaceURI();
141
142         if (namespaceURI != null && !namespaceURI.equals(""))
143         {
144           if (pname.getPrefix() == null)
145           {
146             String JavaDoc prefix = nsStack.getPrefixFromURI(namespaceURI);
147
148             if (prefix == null)
149             {
150               prefix = nsStack.addNSDeclaration(namespaceURI);
151               namespaceDecl = " xmlns:" + prefix + "=\"" + namespaceURI + '\"';
152             }
153
154             pname.setPrefix(prefix);
155           }
156         }
157       }
158     }
159
160     sink.write('<' + context.toString() + namespaceDecl);
161
162     // Get prefixes for the needed namespaces.
163
String JavaDoc elementTypeNS = elementType.getNamespaceURI();
164     String JavaDoc xsiNamespaceURI = Constants.NS_URI_CURRENT_SCHEMA_XSI;
165
166     if (elementTypeNS.startsWith("http://www.w3.org/")
167         && elementTypeNS.endsWith("/XMLSchema"))
168     {
169       xsiNamespaceURI = elementTypeNS + "-instance";
170     }
171
172     String JavaDoc xsiNSPrefix = nsStack.getPrefixFromURI(xsiNamespaceURI, sink);
173     String JavaDoc elementTypeNSPrefix = nsStack.getPrefixFromURI(elementTypeNS, sink);
174
175     sink.write(' ' + xsiNSPrefix + ':' + Constants.ATTR_TYPE + "=\"" +
176                elementTypeNSPrefix + ':' +
177                elementType.getLocalPart() + '\"');
178
179     if (inScopeEncStyle == null
180         || !inScopeEncStyle.equals(Constants.NS_URI_SOAP_ENC))
181     {
182       // Determine the prefix associated with the NS_URI_SOAP_ENV
183
// namespace URI.
184
String JavaDoc soapEnvNSPrefix = nsStack.getPrefixFromURI(
185         Constants.NS_URI_SOAP_ENV, sink);
186
187       sink.write(' ' + soapEnvNSPrefix + ':' +
188                  Constants.ATTR_ENCODING_STYLE + "=\"" +
189                  Constants.NS_URI_SOAP_ENC + '\"');
190     }
191
192     if (arrayElementType != null)
193     {
194       String JavaDoc arrayElementTypeNSPrefix = nsStack.getPrefixFromURI(
195         arrayElementType.getNamespaceURI(), sink);
196       String JavaDoc arrayTypeValue = arrayElementTypeNSPrefix + ':' +
197                               arrayElementType.getLocalPart() +
198                               '[' + arrayLengthStr + ']';
199       String JavaDoc soapEncNSPrefix = nsStack.getPrefixFromURI(
200         Constants.NS_URI_SOAP_ENC, sink);
201
202       sink.write(' ' + soapEncNSPrefix + ':' +
203                  Constants.ATTR_ARRAY_TYPE + "=\"" + arrayTypeValue + '\"');
204     }
205
206     if (isNull)
207     {
208       sink.write(' ' + xsiNSPrefix + ':' + nilName(xsiNamespaceURI) + "=\"" +
209                  Constants.ATTRVAL_TRUE + "\"/");
210     }
211
212     sink.write('>');
213   }
214
215   private static String JavaDoc nilName(String JavaDoc currentSchemaXSI)
216   {
217     return (currentSchemaXSI.equals(Constants.NS_URI_2001_SCHEMA_XSI))
218            ? Constants.ATTR_NIL
219            : Constants.ATTR_NULL;
220   }
221
222   public static boolean isNull(Element element)
223   {
224     String JavaDoc nullValue = DOMUtils.getAttributeNS(element,
225                                                Constants.NS_URI_2001_SCHEMA_XSI,
226                                                Constants.ATTR_NIL);
227
228     if (nullValue == null)
229     {
230       nullValue = DOMUtils.getAttributeNS(element,
231                                           Constants.NS_URI_2000_SCHEMA_XSI,
232                                           Constants.ATTR_NULL);
233     }
234
235     if (nullValue == null)
236     {
237       nullValue = DOMUtils.getAttributeNS(element,
238                                           Constants.NS_URI_1999_SCHEMA_XSI,
239                                           Constants.ATTR_NULL);
240     }
241
242     return nullValue != null && decodeBooleanValue(nullValue);
243   }
244
245   public static boolean decodeBooleanValue(String JavaDoc value)
246   {
247     switch (value.charAt(0))
248     {
249       case '0': case 'f': case 'F':
250         return false;
251
252       case '1': case 't': case 'T':
253         return true;
254
255       default:
256         throw new IllegalArgumentException JavaDoc("Invalid boolean value: " + value);
257     }
258   }
259
260   public static QName getAttributeValue(Element el,
261                                         String JavaDoc attrNameNamespaceURI,
262                                         String JavaDoc attrNameLocalPart,
263                                         String JavaDoc elDesc,
264                                         boolean isRequired)
265     throws IllegalArgumentException JavaDoc
266   {
267     String JavaDoc attrValue = DOMUtils.getAttributeNS(el,
268                                                attrNameNamespaceURI,
269                                                attrNameLocalPart);
270
271     if (attrValue != null)
272     {
273       int index = attrValue.indexOf(':');
274
275       if (index != -1)
276       {
277         String JavaDoc attrValuePrefix = attrValue.substring(0, index);
278         String JavaDoc attrValueLocalPart = attrValue.substring(index + 1);
279         String JavaDoc attrValueNamespaceURI =
280           DOMUtils.getNamespaceURIFromPrefix(el, attrValuePrefix);
281
282         if (attrValueNamespaceURI != null)
283         {
284           return new QName(attrValueNamespaceURI, attrValueLocalPart);
285         }
286         else
287         {
288           throw new IllegalArgumentException JavaDoc("Unable to resolve namespace " +
289                                              "URI for '" + attrValuePrefix +
290                                              "'.");
291         }
292       }
293       else
294       {
295         throw new IllegalArgumentException JavaDoc("The value of the '" +
296                                            attrNameNamespaceURI + ':' +
297                                            attrNameLocalPart +
298                                            "' attribute must be " +
299                                            "namespace-qualified.");
300       }
301     }
302     else if (isRequired)
303     {
304       throw new IllegalArgumentException JavaDoc("The '" +
305                                          attrNameNamespaceURI + ':' +
306                                          attrNameLocalPart +
307                                          "' attribute must be " +
308                                          "specified for every " +
309                                          elDesc + '.');
310     }
311     else
312     {
313       return null;
314     }
315   }
316
317   /**
318    * Get the value of the xsi:type attribute, for varying values of
319    * the xsi namespace.
320    */

321   public static QName getTypeQName(Element el)
322     throws IllegalArgumentException JavaDoc
323   {
324     // Try 2001
325
QName typeQName = getAttributeValue(el, Constants.NS_URI_2001_SCHEMA_XSI,
326                                         Constants.ATTR_TYPE, null, false);
327
328     if (typeQName != null)
329       return typeQName;
330
331     // Try 2000
332
typeQName = getAttributeValue(el, Constants.NS_URI_2000_SCHEMA_XSI,
333                                   Constants.ATTR_TYPE, null, false);
334
335     if (typeQName != null)
336       return typeQName;
337
338     // Try 1999
339
typeQName = getAttributeValue(el, Constants.NS_URI_1999_SCHEMA_XSI,
340                                   Constants.ATTR_TYPE, null, false);
341
342     return typeQName;
343   }
344 }
345
Popular Tags