KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > wsm > axis > databinding > AxisTypeLookup


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
21 package org.apache.beehive.wsm.axis.databinding;
22
23 import javax.xml.namespace.QName JavaDoc;
24
25 import org.apache.axis.description.TypeDesc;
26 import org.apache.axis.utils.ClassUtils;
27 import org.apache.axis.wsdl.fromJava.Namespaces;
28 import org.apache.axis.wsdl.fromJava.Types;
29 import org.apache.axis.wsdl.toJava.Utils;
30 import org.apache.beehive.wsm.axis.registration.AxisTypeMappingMetaData;
31 import org.apache.beehive.wsm.databinding.BindingLookupService;
32 import org.apache.log4j.Logger;
33
34 public class AxisTypeLookup implements BindingLookupService {
35     static Logger logger = Logger.getLogger(AxisTypeLookup.class);
36
37     /**
38      * @param tm
39      */

40     public AxisTypeLookup() {
41         super();
42     }
43
44     /*
45      * (non-Javadoc)
46      *
47      * @see TypeLookUpServices#getClassQName(java.lang.Class)
48      */

49     public QName JavaDoc class2qname(Class JavaDoc cls) {
50         if (cls.isArray())
51             cls = cls.getComponentType();
52         TypeDesc td = TypeDesc.getTypeDescForClass(cls); // a class can
53
if (null != td) {
54             return td.getXmlType();
55         } else {
56             String JavaDoc namespace = Namespaces.makeNamespace(cls.getName());
57             if (namespace == null || namespace.endsWith("DefaultNamespace")) {
58                 namespace = "http://no.namespace.specified";
59             }
60             return class2qname(cls, namespace);
61         }
62
63     }
64
65     /**
66      * @param cls
67      * @param namespace
68      * @return
69      */

70     public QName JavaDoc class2qname(Class JavaDoc cls, String JavaDoc namespace) {
71         if (AxisTypeMappingMetaData.isBuiltInType(cls))
72             return AxisTypeMappingMetaData.getBuiltInTypeQname(cls);
73         return new QName JavaDoc(namespace, Types.getLocalNameFromFullName(cls
74                 .getName()));
75     }
76
77     public Class JavaDoc qname2class(QName JavaDoc qType) {
78
79         if (qType == null) {
80             return null;
81         }
82
83         String JavaDoc packageName = getPackageNameFromQName(qType);
84         String JavaDoc className;
85         if (packageName != null && packageName.length() > 0) {
86             className = packageName + "."
87                     + Utils.xmlNameToJavaClass(qType.getLocalPart());
88         } else {
89             className = Utils.xmlNameToJavaClass(qType.getLocalPart());
90         }
91
92         Class JavaDoc javaType = null;
93
94         try {
95             javaType = ClassUtils.forName(className);
96
97         } catch (ClassNotFoundException JavaDoc e) {
98             System.out.println("Failed to find the class: " + className
99                     + " No Axis generated classes was found for qname: "
100                     + qType);
101         }
102
103         if (null != javaType)
104             System.out.println("Found an Axis generated type for qname: "
105                     + qType + " class: " + javaType.getCanonicalName());
106
107         return javaType;
108     }
109
110     private String JavaDoc getPackageNameFromQName(QName JavaDoc qType) {
111
112         // TODO: Later keep a cache of previous conversions, and return the
113
// result
114
// from cache if there is any.
115
String JavaDoc packageName = Utils.makePackageName(qType.getNamespaceURI());
116         packageName = normalizePackageName(packageName, '.');
117         return packageName;
118     }
119
120     private static final char[] pkgSeparators = { '.', ':' };
121
122     private static String JavaDoc normalizePackageName(String JavaDoc pkg, char separator) {
123
124         for (int i = 0; i < pkgSeparators.length; i++) {
125             pkg = pkg.replace(pkgSeparators[i], separator);
126         }
127
128         return pkg;
129     }
130
131 }
132
Popular Tags