KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > databinding > xmlbeans > XmlBeanTypeLookup


1 /*
2  *
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  *
19  */

20 package org.apache.beehive.wsm.databinding.xmlbeans;
21
22 import java.lang.reflect.Method JavaDoc;
23
24 import javax.xml.namespace.QName JavaDoc;
25
26 import org.apache.beehive.wsm.databinding.BindingLookupService;
27 import org.apache.log4j.Logger;
28 import org.apache.xmlbeans.SchemaField;
29 import org.apache.xmlbeans.SchemaType;
30 import org.apache.xmlbeans.SchemaTypeLoader;
31 import org.apache.xmlbeans.XmlBeans;
32 import org.apache.xmlbeans.XmlObject;
33
34
35
36 public class XmlBeanTypeLookup implements BindingLookupService {
37     static Logger logger = Logger.getLogger(XmlBeanTypeLookup.class);
38     static XmlBeanTypeLookup theLookup = new XmlBeanTypeLookup();
39     
40     public QName JavaDoc class2qname( Class JavaDoc cls) {
41         if (XmlObject.class.isAssignableFrom(cls)) {
42             return XmlBeans.typeForClass(cls).getName();
43         }
44         return null;
45     }
46     
47     public QName JavaDoc class2qname(Class JavaDoc cls, String JavaDoc defaultnamespace) {
48         QName JavaDoc res = class2qname(cls);
49         // for xmlbeans we can ignore default name space.
50
return res;
51     }
52
53     /* (non-Javadoc)
54      * @see TypeLookUpServices#Qname2Class(javax.xml.namespace.QName)
55      */

56     public Class JavaDoc qname2class(QName JavaDoc qname) {
57         logger.debug("Get XMLBeans class for Qname: " + qname);
58             SchemaTypeLoader stl = XmlBeans.getContextTypeLoader();
59             SchemaType st = stl.findType(qname);
60             if (st == null) {
61                 SchemaField sf = stl.findElement(qname);
62                 if (sf != null) {
63                     st = sf.getType();
64                 }
65             }
66
67             if (st != null) {
68                 Class JavaDoc xmlClass = st.getJavaClass();
69
70                 //String clName = xmlClass.getName();
71
if (st.isBuiltinType()) {
72                     Method JavaDoc[] declared = xmlClass.getDeclaredMethods();
73                     Class JavaDoc natural =
74                         scanDeclaredMethodsForViableReturnType(declared);
75                     if (natural != null) {
76                         return natural;
77                     }
78                     else {
79                         // NOTE jcolwell@bea.com 2004-Nov-12 --
80
// XmlString declares no methods
81

82                         if (xmlClass.isInterface()) {
83                             for (Class JavaDoc cl : xmlClass.getInterfaces()) {
84                                 natural = scanDeclaredMethodsForViableReturnType
85                                     (cl.getDeclaredMethods());
86                                 if (natural != null) {
87                                     return natural;
88                                 }
89                             }
90                         }
91                         else {
92                             declared = xmlClass.getSuperclass().getDeclaredMethods();
93                             natural = scanDeclaredMethodsForViableReturnType
94                                 (declared);
95                             if (natural != null) {
96                                 return natural;
97                             }
98                         }
99                     }
100                 }
101                 return xmlClass;
102             }
103             else {
104                 // NOTE jcolwell@bea.com 2004-Nov-30 --
105
// keep in mind that a real TMU based on a viable SOAP stack should
106
// be used and this pure XmlBean implementation is just a fallback.
107
return null;
108             }
109         }
110     
111        private Class JavaDoc scanDeclaredMethodsForViableReturnType
112        (Method JavaDoc[] declared) {
113        /* TODO: Does this make sense? IT looks as if the type of the class is determined based on the
114         * method return types.
115         */

116        for (Method JavaDoc meth : declared) {
117                
118            Class JavaDoc returnType = meth.getReturnType();
119            //System.out.println(returnType.getName());
120
if (!returnType.equals(Void.TYPE)) {
121                /*
122                 * NOTE jcolwell@bea.com 2004-Nov-12 --
123                 * built-in XmlBeans types may be of the following natural
124                 * types:
125                 * primitives, byte arrays, Strings, Calendars, BigIntegers
126                 * and BigDecimals
127                 */

128                if (returnType.isArray()
129                    || returnType.isPrimitive()
130                    || returnType.equals(String JavaDoc.class)
131                    || returnType.equals(QName JavaDoc.class)
132                    || returnType.equals(java.util.Calendar JavaDoc.class)
133                    || returnType.equals(java.math.BigDecimal JavaDoc.class)
134                    || returnType.equals(java.math.BigInteger JavaDoc.class)) {
135
136                    return returnType;
137                }
138            }
139        }
140        return null;
141    }
142
143     /**
144      * @param q
145      * @return Class
146      */

147     public static Class JavaDoc q2Class(QName JavaDoc q) {
148         return theLookup.qname2class(q);
149     }
150     
151 }
152
Popular Tags