KickJava   Java API By Example, From Geeks To Geeks.

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


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.description.TypeDesc;
20 import org.apache.axis.encoding.Deserializer;
21 import org.apache.axis.utils.BeanPropertyDescriptor;
22 import org.apache.axis.utils.BeanUtils;
23 import org.apache.axis.utils.JavaUtils;
24
25 import javax.xml.namespace.QName JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 /**
31  * DeserializerFactory for Bean
32  *
33  * @author Rich Scheuerle <scheu@us.ibm.com>
34  * @author Sam Ruby <rubys@us.ibm.com>
35  */

36 public class BeanDeserializerFactory extends BaseDeserializerFactory {
37
38     /** Type metadata about this class for XML deserialization */
39     protected transient TypeDesc typeDesc = null;
40     protected transient Map JavaDoc propertyMap = null;
41
42     public BeanDeserializerFactory(Class JavaDoc javaType, QName JavaDoc xmlType) {
43         super(BeanDeserializer.class, xmlType, javaType);
44         
45         // Sometimes an Enumeration class is registered as a Bean.
46
// If this is the case, silently switch to the EnumDeserializer
47
if (JavaUtils.isEnumClass(javaType)) {
48             deserClass = EnumDeserializer.class;
49         }
50
51         typeDesc = TypeDesc.getTypeDescForClass(javaType);
52         propertyMap = getProperties(javaType, typeDesc);
53     }
54
55    /**
56      * Get a list of the bean properties
57      */

58     public static Map JavaDoc getProperties(Class JavaDoc javaType, TypeDesc typeDesc ) {
59         Map JavaDoc propertyMap = null;
60
61         if (typeDesc != null) {
62             propertyMap = typeDesc.getPropertyDescriptorMap();
63         } else {
64             BeanPropertyDescriptor[] pd = BeanUtils.getPd(javaType, null);
65             propertyMap = new HashMap JavaDoc();
66             // loop through properties and grab the names for later
67
for (int i = 0; i < pd.length; i++) {
68                 BeanPropertyDescriptor descriptor = pd[i];
69                 propertyMap.put(descriptor.getName(), descriptor);
70             }
71         }
72
73         return propertyMap;
74     }
75
76    /**
77      * Optimize construction of a BeanDeserializer by caching the
78      * type descriptor and property map.
79      */

80     protected Deserializer getGeneralPurpose(String JavaDoc mechanismType) {
81         if (javaType == null || xmlType == null) {
82            return super.getGeneralPurpose(mechanismType);
83         }
84
85         if (deserClass == EnumDeserializer.class) {
86            return super.getGeneralPurpose(mechanismType);
87         }
88
89         return new BeanDeserializer(javaType, xmlType, typeDesc, propertyMap);
90     }
91
92
93     private void readObject(java.io.ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
94         in.defaultReadObject();
95         typeDesc = TypeDesc.getTypeDescForClass(javaType);
96         propertyMap = getProperties(javaType, typeDesc);
97     }
98 }
99
Popular Tags