KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > util > XmlBeanTypeMappingUtil


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

22 package org.apache.beehive.wsm.util;
23
24 import java.lang.reflect.Method JavaDoc;
25
26 import javax.xml.namespace.QName JavaDoc;
27
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  *
37  * @author Jonathan Colwell
38  */

39 public class XmlBeanTypeMappingUtil implements TypeMappingUtil {
40
41     public QName JavaDoc registerType(Class JavaDoc cls) throws InvalidTypeMappingException {
42        throw new InvalidTypeMappingException
43            ("this implementation does not support type registration");
44     }
45
46     public QName JavaDoc registerType(Class JavaDoc cls, QName JavaDoc expectedType)
47         throws InvalidTypeMappingException {
48         
49         throw new InvalidTypeMappingException
50             ("this implementation does not support type registration");
51     }
52
53     public QName JavaDoc generateQName(Class JavaDoc type, String JavaDoc defaultNS) {
54
55         if (XmlObject.class.isAssignableFrom(type)) {
56             return XmlBeans.typeForClass(type).getName();
57         }
58         else {
59             return null;
60         }
61     }
62
63     public Class JavaDoc q2Class(QName JavaDoc qType) {
64
65         SchemaTypeLoader stl = XmlBeans.getContextTypeLoader();
66         SchemaType st = stl.findType(qType);
67         if (st == null) {
68             SchemaField sf = stl.findElement(qType);
69             if (sf != null) {
70                 st = sf.getType();
71             }
72         }
73
74         if (st != null) {
75             Class JavaDoc xmlClass = st.getJavaClass();
76
77             //String clName = xmlClass.getName();
78
if (st.isBuiltinType()) {
79                 Method JavaDoc[] declared = xmlClass.getDeclaredMethods();
80                 Class JavaDoc natural =
81                     scanDeclaredMethodsForViableReturnType(declared);
82                 if (natural != null) {
83                     return natural;
84                 }
85                 else {
86                     // NOTE jcolwell@bea.com 2004-Nov-12 --
87
// XmlString declares no methods
88

89                     if (xmlClass.isInterface()) {
90                         for (Class JavaDoc cl : xmlClass.getInterfaces()) {
91                             natural = scanDeclaredMethodsForViableReturnType
92                                 (cl.getDeclaredMethods());
93                             if (natural != null) {
94                                 return natural;
95                             }
96                         }
97                     }
98                     else {
99                         declared = xmlClass.getSuperclass().getDeclaredMethods();
100                         natural = scanDeclaredMethodsForViableReturnType
101                             (declared);
102                         if (natural != null) {
103                             return natural;
104                         }
105                     }
106                 }
107             }
108             /*
109             else if (st.isSimpleType()){
110                 System.out.println(clName + " is simple but not built in");
111             }
112             else {
113                 System.out.println(clName + " is NOT built in");
114             }
115             System.out.println("resulting class: " + xmlClass.getName());
116             */

117             return xmlClass;
118         }
119         else {
120             // NOTE jcolwell@bea.com 2004-Nov-30 --
121
// keep in mind that a real TMU based on a viable SOAP stack should
122
// be used and this pure XmlBean implementation is just a fallback.
123
System.out.println("no schematype found for " + qType);
124             return Object JavaDoc.class;
125         }
126     }
127
128     private Class JavaDoc scanDeclaredMethodsForViableReturnType
129         (Method JavaDoc[] declared) {
130         
131         for (Method JavaDoc meth : declared) {
132                 
133             Class JavaDoc returnType = meth.getReturnType();
134             //System.out.println(returnType.getName());
135
if (!returnType.equals(Void.TYPE)) {
136                 /*
137                  * NOTE jcolwell@bea.com 2004-Nov-12 --
138                  * built-in XmlBeans types may be of the following natural
139                  * types:
140                  * primitives, byte arrays, Strings, Calendars, BigIntegers
141                  * and BigDecimals
142                  */

143                 if (returnType.isArray()
144                     || returnType.isPrimitive()
145                     || returnType.equals(String JavaDoc.class)
146                     || returnType.equals(QName JavaDoc.class)
147                     || returnType.equals(java.util.Calendar JavaDoc.class)
148                     || returnType.equals(java.math.BigDecimal JavaDoc.class)
149                     || returnType.equals(java.math.BigInteger JavaDoc.class)) {
150
151                     return returnType;
152                 }
153             }
154         }
155         return null;
156     }
157 }
158
Popular Tags