/*
*
* Copyright 2001-2004 The Apache Software Foundation.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.apache.beehive.wsm.axis.databinding;
import javax.xml.namespace.QName;
import org.apache.axis.description.TypeDesc;
import org.apache.axis.utils.ClassUtils;
import org.apache.axis.wsdl.fromJava.Namespaces;
import org.apache.axis.wsdl.fromJava.Types;
import org.apache.axis.wsdl.toJava.Utils;
import org.apache.beehive.wsm.axis.registration.AxisTypeMappingMetaData;
import org.apache.beehive.wsm.databinding.BindingLookupService;
import org.apache.log4j.Logger;
public class AxisTypeLookup implements BindingLookupService {
static Logger logger = Logger.getLogger(AxisTypeLookup.class);
/**
* @param tm
*/
public AxisTypeLookup() {
super();
}
/*
* (non-Javadoc)
*
* @see TypeLookUpServices#getClassQName(java.lang.Class)
*/
public QName class2qname(Class cls) {
if (cls.isArray())
cls = cls.getComponentType();
TypeDesc td = TypeDesc.getTypeDescForClass(cls); // a class can
if (null != td) {
return td.getXmlType();
} else {
String namespace = Namespaces.makeNamespace(cls.getName());
if (namespace == null || namespace.endsWith("DefaultNamespace")) {
namespace = "http://no.namespace.specified";
}
return class2qname(cls, namespace);
}
}
/**
* @param cls
* @param namespace
* @return
*/
public QName class2qname(Class cls, String namespace) {
if (AxisTypeMappingMetaData.isBuiltInType(cls))
return AxisTypeMappingMetaData.getBuiltInTypeQname(cls);
return new QName(namespace, Types.getLocalNameFromFullName(cls
.getName()));
}
public Class qname2class(QName qType) {
if (qType == null) {
return null;
}
String packageName = getPackageNameFromQName(qType);
String className;
if (packageName != null && packageName.length() > 0) {
className = packageName + "."
+ Utils.xmlNameToJavaClass(qType.getLocalPart());
} else {
className = Utils.xmlNameToJavaClass(qType.getLocalPart());
}
Class javaType = null;
try {
javaType = ClassUtils.forName(className);
} catch (ClassNotFoundException e) {
System.out.println("Failed to find the class: " + className
+ " No Axis generated classes was found for qname: "
+ qType);
}
if (null != javaType)
System.out.println("Found an Axis generated type for qname: "
+ qType + " class: " + javaType.getCanonicalName());
return javaType;
}
private String getPackageNameFromQName(QName qType) {
// TODO: Later keep a cache of previous conversions, and return the
// result
// from cache if there is any.
String packageName = Utils.makePackageName(qType.getNamespaceURI());
packageName = normalizePackageName(packageName, '.');
return packageName;
}
private static final char[] pkgSeparators = { '.', ':' };
private static String normalizePackageName(String pkg, char separator) {
for (int i = 0; i < pkgSeparators.length; i++) {
pkg = pkg.replace(pkgSeparators[i], separator);
}
return pkg;
}
}