KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > util > xml > XMLJavaMappingRegistry


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.util.xml;
59
60 import java.io.*;
61 import java.util.*;
62 import org.w3c.dom.*;
63 import org.apache.soap.util.*;
64 import org.apache.soap.Constants;
65 import org.apache.soap.rpc.SOAPContext;
66 import org.apache.soap.encoding.soapenc.MimePartSerializer;
67 import org.apache.soap.encoding.SOAPMappingRegistry;
68
69 /**
70  * An <code>XMLJavaMappingRegistry</code> ...
71  *
72  * @author Matthew J. Duftler (duftler@us.ibm.com)
73  * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
74  * @author Francisco Curbera (curbera@us.ibm.com)
75  */

76 public class XMLJavaMappingRegistry
77 {
78   private Hashtable sReg = new Hashtable();
79   private Hashtable dsReg = new Hashtable();
80   private Hashtable xml2JavaReg = new Hashtable();
81   private Hashtable java2XMLReg = new Hashtable();
82   private String JavaDoc defaultEncodingStyle = null;
83   
84   /** Set the default encoding style. If the query*() calls
85    * are invoked with a null encodingStyleURI parameter, we'll
86    * use this instead.
87    */

88   public void setDefaultEncodingStyle(String JavaDoc defEncStyle)
89   {
90       defaultEncodingStyle = defEncStyle;
91   }
92
93   // To register the default, set both types to null.
94
public void mapTypes(String JavaDoc encodingStyleURI, QName elementType,
95                        Class JavaDoc javaType, Serializer s, Deserializer ds)
96   {
97     String JavaDoc java2XMLKey = getKey(javaType, encodingStyleURI);
98     String JavaDoc xml2JavaKey = getKey(elementType, encodingStyleURI);
99
100     if (s != null)
101     {
102       sReg.put(java2XMLKey, s);
103     }
104
105     if (ds != null)
106     {
107       dsReg.put(xml2JavaKey, ds);
108     }
109
110     // Only map types if both types are provided.
111
if (elementType != null && javaType != null)
112     {
113       java2XMLReg.put(java2XMLKey, elementType);
114       xml2JavaReg.put(xml2JavaKey, javaType);
115     }
116   }
117
118   /**
119    * This version returns null if the serializer is not found. It is
120    * intended for internal usage (its used for chaining registries,
121    * for example).
122    */

123   protected Serializer querySerializer_(Class JavaDoc javaType,
124                                         String JavaDoc encodingStyleURI)
125   {
126     if (encodingStyleURI == null) {
127       encodingStyleURI = defaultEncodingStyle;
128     }
129     String JavaDoc java2XMLKey = getKey(javaType, encodingStyleURI);
130     Serializer s = (Serializer)sReg.get(java2XMLKey);
131
132     if (s != null)
133     {
134       return s;
135     }
136     else
137     {
138       java2XMLKey = getKey(null, encodingStyleURI);
139       return (Serializer)sReg.get(java2XMLKey);
140     }
141   }
142
143   /**
144    * This version calls the protected method to do the work and if its
145    * not found throws an exception.
146    */

147   public Serializer querySerializer(Class JavaDoc javaType, String JavaDoc encodingStyleURI)
148     throws IllegalArgumentException JavaDoc
149   {
150     Serializer s = querySerializer_(javaType, encodingStyleURI);
151     if (s != null)
152     {
153       return s;
154     }
155     else
156     {
157       throw new IllegalArgumentException JavaDoc("No Serializer found to " +
158                                          "serialize a '" +
159                                          getClassName(javaType) +
160                                          "' using encoding style '" +
161                                          encodingStyleURI + "'.");
162     }
163   }
164
165   /**
166    * This version returns null if the deserializer is not found. It is
167    * intended for internal usage (its used for chaining registries,
168    * for example).
169    */

170   protected Deserializer queryDeserializer_(QName elementType,
171                                             String JavaDoc encodingStyleURI)
172   {
173     if (encodingStyleURI == null) {
174       encodingStyleURI = defaultEncodingStyle;
175     }
176
177     String JavaDoc xml2JavaKey = getKey(elementType, encodingStyleURI);
178     Deserializer ds = (Deserializer)dsReg.get(xml2JavaKey);
179
180     if (ds != null)
181     {
182       return ds;
183     }
184     else
185     {
186       xml2JavaKey = getKey(null, encodingStyleURI);
187       return (Deserializer)dsReg.get(xml2JavaKey);
188     }
189   }
190
191   /**
192    * This version calls the protected method to do the work and if its
193    * not found throws an exception.
194    */

195   public Deserializer queryDeserializer(QName elementType,
196                                         String JavaDoc encodingStyleURI)
197     throws IllegalArgumentException JavaDoc
198   {
199     Deserializer ds = queryDeserializer_(elementType, encodingStyleURI);
200     if (ds != null)
201     {
202       return ds;
203     }
204     else
205     {
206       throw new IllegalArgumentException JavaDoc("No Deserializer found to " +
207                                          "deserialize a '" + elementType +
208                                          "' using encoding style '" +
209                                          encodingStyleURI + "'.");
210     }
211   }
212
213   /**
214    * This version returns null if the element type is not found. It is
215    * intended for internal usage (its used for chaining registries,
216    * for example).
217    */

218   protected QName queryElementType_(Class JavaDoc javaType, String JavaDoc encodingStyleURI)
219   {
220     if (encodingStyleURI == null) {
221       encodingStyleURI = defaultEncodingStyle;
222     }
223
224     String JavaDoc java2XMLkey = getKey(javaType, encodingStyleURI);
225     return (QName)java2XMLReg.get(java2XMLkey);
226   }
227
228   /**
229    * This version calls the protected method to do the work and if its
230    * not found throws an exception.
231    */

232   public QName queryElementType(Class JavaDoc javaType, String JavaDoc encodingStyleURI)
233     throws IllegalArgumentException JavaDoc
234   {
235     QName elementType = queryElementType_(javaType, encodingStyleURI);
236     if (elementType != null)
237     {
238       return elementType;
239     }
240     else
241     {
242       throw new IllegalArgumentException JavaDoc("No mapping found for '" +
243                                          getClassName(javaType) +
244                                          "' using encoding style '" +
245                                          encodingStyleURI + "'.");
246     }
247   }
248
249   /**
250    * This version returns null if the Java type is not found. It is
251    * intended for internal usage (its used for chaining registries,
252    * for example).
253    */

254   protected Class JavaDoc queryJavaType_(QName elementType, String JavaDoc encodingStyleURI)
255   {
256     if (encodingStyleURI == null) {
257       encodingStyleURI = defaultEncodingStyle;
258     }
259
260     String JavaDoc xml2JavaKey = getKey(elementType, encodingStyleURI);
261     return (Class JavaDoc)xml2JavaReg.get(xml2JavaKey);
262   }
263
264   /**
265    * This version calls the protected method to do the work and if its
266    * not found throws an exception.
267    */

268   public Class JavaDoc queryJavaType(QName elementType, String JavaDoc encodingStyleURI)
269     throws IllegalArgumentException JavaDoc
270   {
271     Class JavaDoc javaType = queryJavaType_(elementType, encodingStyleURI);
272     if (javaType != null)
273     {
274       return javaType;
275     }
276     else
277     {
278       throw new IllegalArgumentException JavaDoc("No mapping found for '" +
279                                          elementType +
280                                          "' using encoding style '" +
281                                          encodingStyleURI + "'.");
282     }
283   }
284
285   public void marshall(String JavaDoc inScopeEncStyle, Class JavaDoc javaType, Object JavaDoc src,
286                        Object JavaDoc context, Writer sink, NSStack nsStack,
287                        SOAPContext ctx)
288     throws IllegalArgumentException JavaDoc, IOException
289   {
290     Serializer s = (Serializer)querySerializer(javaType, inScopeEncStyle);
291
292     s.marshall(inScopeEncStyle, javaType, src, context,
293                sink, nsStack, this, ctx);
294   }
295
296   public Bean unmarshall(String JavaDoc inScopeEncStyle, QName elementType,
297                          Node src, SOAPContext ctx)
298     throws IllegalArgumentException JavaDoc
299   {
300       Deserializer ds = null;
301       try {
302           ds = (Deserializer)queryDeserializer(elementType,
303                                                inScopeEncStyle);
304       } catch(IllegalArgumentException JavaDoc iae) {
305           // If the element contains an HREF= parameter and could not be
306
// resolved , use MimePartSerializer.
307
String JavaDoc href = ((Element)src).getAttribute(Constants.ATTR_REFERENCE);
308           if (href != null && !href.equals(""))
309               ds = SOAPMappingRegistry.partSer;
310           else
311               throw iae;
312       }
313
314     return ds.unmarshall(inScopeEncStyle, elementType, src, this, ctx);
315   }
316
317   private static String JavaDoc getKey(Object JavaDoc type, String JavaDoc encodingStyleURI)
318   {
319     return type + " + " + encodingStyleURI;
320   }
321
322   protected static String JavaDoc getClassName(Class JavaDoc javaType)
323   {
324     return javaType != null ? StringUtils.getClassName(javaType) : "null";
325   }
326 }
327
Popular Tags