1 22 package org.apache.beehive.wsm.util; 23 24 import java.lang.reflect.Method ; 25 26 import javax.xml.namespace.QName ; 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 39 public class XmlBeanTypeMappingUtil implements TypeMappingUtil { 40 41 public QName registerType(Class cls) throws InvalidTypeMappingException { 42 throw new InvalidTypeMappingException 43 ("this implementation does not support type registration"); 44 } 45 46 public QName registerType(Class cls, QName expectedType) 47 throws InvalidTypeMappingException { 48 49 throw new InvalidTypeMappingException 50 ("this implementation does not support type registration"); 51 } 52 53 public QName generateQName(Class type, String 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 q2Class(QName 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 xmlClass = st.getJavaClass(); 76 77 if (st.isBuiltinType()) { 79 Method [] declared = xmlClass.getDeclaredMethods(); 80 Class natural = 81 scanDeclaredMethodsForViableReturnType(declared); 82 if (natural != null) { 83 return natural; 84 } 85 else { 86 89 if (xmlClass.isInterface()) { 90 for (Class 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 117 return xmlClass; 118 } 119 else { 120 System.out.println("no schematype found for " + qType); 124 return Object .class; 125 } 126 } 127 128 private Class scanDeclaredMethodsForViableReturnType 129 (Method [] declared) { 130 131 for (Method meth : declared) { 132 133 Class returnType = meth.getReturnType(); 134 if (!returnType.equals(Void.TYPE)) { 136 143 if (returnType.isArray() 144 || returnType.isPrimitive() 145 || returnType.equals(String .class) 146 || returnType.equals(QName .class) 147 || returnType.equals(java.util.Calendar .class) 148 || returnType.equals(java.math.BigDecimal .class) 149 || returnType.equals(java.math.BigInteger .class)) { 150 151 return returnType; 152 } 153 } 154 } 155 return null; 156 } 157 } 158 | Popular Tags |