KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > encoding > ser > SimpleDeserializerFactory


1 /*
2  * Copyright 2001-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.encoding.ser;
18
19 import org.apache.axis.utils.BeanUtils;
20 import org.apache.axis.utils.BeanPropertyDescriptor;
21 import org.apache.axis.utils.JavaUtils;
22
23 import javax.xml.namespace.QName JavaDoc;
24 import javax.xml.rpc.JAXRPCException JavaDoc;
25 import java.lang.reflect.Constructor JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 /**
29  * A deserializer for any simple type with a (String) constructor. Note:
30  * this class is designed so that subclasses need only override the makeValue
31  * method in order to construct objects of their own type.
32  *
33  * @author Glen Daniels (gdaniels@apache.org)
34  * @author Sam Ruby (rubys@us.ibm.com)
35  * Modified for JAX-RPC @author Rich Scheuerle (scheu@us.ibm.com)
36  */

37 public class SimpleDeserializerFactory extends BaseDeserializerFactory {
38
39     private static final Class JavaDoc[] STRING_STRING_CLASS =
40         new Class JavaDoc [] {String JavaDoc.class, String JavaDoc.class};
41     
42     private static final Class JavaDoc[] STRING_CLASS =
43         new Class JavaDoc [] {String JavaDoc.class};
44
45     private transient Constructor JavaDoc constructor = null;
46     private boolean isBasicType = false;
47     /**
48      * Note that the factory is constructed with the QName and xmlType. This is important
49      * to allow distinction between primitive values and java.lang wrappers.
50      **/

51     public SimpleDeserializerFactory(Class JavaDoc javaType, QName JavaDoc xmlType) {
52         super(SimpleDeserializer.class, xmlType, javaType);
53         this.isBasicType = JavaUtils.isBasic(javaType);
54         initConstructor(javaType);
55     }
56
57     private void initConstructor(Class JavaDoc javaType) {
58         if (!this.isBasicType) {
59             // discover the constructor for non basic types
60
try {
61                 if (QName JavaDoc.class.isAssignableFrom(javaType)) {
62                     constructor =
63                         javaType.getDeclaredConstructor(STRING_STRING_CLASS);
64                 } else {
65                     constructor =
66                         javaType.getDeclaredConstructor(STRING_CLASS);
67                 }
68             } catch (NoSuchMethodException JavaDoc e) {
69                 try {
70                     constructor =
71                         javaType.getDeclaredConstructor(new Class JavaDoc[]{});
72                     BeanPropertyDescriptor[] pds = BeanUtils.getPd(javaType);
73                     if(pds != null) {
74                         if(BeanUtils.getSpecificPD(pds,"_value")!=null){
75                             return;
76                         }
77                     }
78                     throw new IllegalArgumentException JavaDoc(e.toString());
79                 } catch (NoSuchMethodException JavaDoc ex) {
80                     throw new IllegalArgumentException JavaDoc(ex.toString());
81                 }
82             }
83         }
84     }
85
86     /**
87      * Get the Deserializer and the set the Constructor so the
88      * deserializer does not have to do introspection.
89      */

90     public javax.xml.rpc.encoding.Deserializer JavaDoc getDeserializerAs(String JavaDoc mechanismType)
91         throws JAXRPCException JavaDoc {
92         if (javaType == java.lang.Object JavaDoc.class) {
93             return null;
94         }
95         if (this.isBasicType) {
96             return new SimpleDeserializer(javaType, xmlType);
97         } else {
98             // XXX: don't think we can always expect to be SimpleDeserializer
99
// since getSpecialized() might return a different type
100
SimpleDeserializer deser =
101                 (SimpleDeserializer) super.getDeserializerAs(mechanismType);
102             if (deser != null) {
103                 deser.setConstructor(constructor);
104             }
105             return deser;
106         }
107     }
108
109     private void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
110         in.defaultReadObject();
111         initConstructor(javaType);
112     }
113
114 }
115
Popular Tags