1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.xbean.spring.generator;
18
19 import java.beans.PropertyEditor;
20 import java.beans.PropertyEditorManager;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.List;
25 import java.util.ArrayList;
26 import java.util.Iterator;
27
28
29
30
31
32
33 public final class Utils {
34 public static final String XBEAN_ANNOTATION = "org.apache.xbean.XBean";
35 public static final String PROPERTY_ANNOTATION = "org.apache.xbean.Property";
36
37 private Utils() {
38 }
39
40 public static String decapitalise(String value) {
41 if (value == null || value.length() == 0) {
42 return value;
43 }
44 return value.substring(0, 1).toLowerCase() + value.substring(1);
45 }
46
47 public static boolean isSimpleType(Type type) {
48 if (type.isPrimitive()) {
49 return true;
50 }
51 if (type.isCollection()) {
52 return false;
53 }
54
55 String name = type.getName();
56 if (name.equals("java.lang.Class") ||
57 name.equals("javax.xml.namespace.QName")) {
58 return true;
59 }
60 return hasPropertyEditor(name);
61 }
62
63 private static boolean hasPropertyEditor(String type) {
64 Class theClass;
65 try {
66 theClass = loadClass(type);
67
68 PropertyEditor editor = PropertyEditorManager.findEditor(theClass);
69 return editor != null;
70 } catch (Throwable e) {
71 System.out.println("Warning, could not load class: " + type + ": " + e);
72 return false;
73 }
74 }
75
76
77
78
79
80 private static Class loadClass(String name) throws ClassNotFoundException {
81 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
82 if (contextClassLoader != null) {
83 try {
84 return contextClassLoader.loadClass(name);
85 } catch (ClassNotFoundException e) {
86 }
87 }
88 return Utils.class.getClassLoader().loadClass(name);
89 }
90
91 public static String getXsdType(Type type) {
92 String name = type.getName();
93 String xsdType = (String) XSD_TYPES.get(name);
94 if (xsdType == null) {
95 xsdType = "xs:string";
96 }
97 return xsdType;
98 }
99
100 public static final Map XSD_TYPES;
101
102 static {
103
104 Map map = new HashMap();
105 map.put(String.class.getName(), "xs:string");
106 map.put(Boolean.class.getName(), "xs:boolean");
107 map.put(boolean.class.getName(), "xs:boolean");
108 map.put(Byte.class.getName(), "xs:byte");
109 map.put(byte.class.getName(), "xs:byte");
110 map.put(Short.class.getName(), "xs:short");
111 map.put(short.class.getName(), "xs:short");
112 map.put(Integer.class.getName(), "xs:integer");
113 map.put(int.class.getName(), "xs:integer");
114 map.put(Long.class.getName(), "xs:long");
115 map.put(long.class.getName(), "xs:long");
116 map.put(Float.class.getName(), "xs:float");
117 map.put(float.class.getName(), "xs:float");
118 map.put(Double.class.getName(), "xs:double");
119 map.put(double.class.getName(), "xs:double");
120 map.put(java.util.Date.class.getName(), "xs:date");
121 map.put(java.sql.Date.class.getName(), "xs:date");
122 map.put("javax.xml.namespace.QName", "xs:QName");
123 XSD_TYPES = Collections.unmodifiableMap(map);
124 }
125
126 public static List findImplementationsOf(NamespaceMapping namespaceMapping, Type type) {
127 List elements = new ArrayList();
128 String nestedTypeName = type.getName();
129 for (Iterator iter = namespaceMapping.getElements().iterator(); iter.hasNext();) {
130 ElementMapping element = (ElementMapping) iter.next();
131 if (element.getClassName().equals(nestedTypeName) ||
132 element.getInterfaces().contains(nestedTypeName) ||
133 element.getSuperClasses().contains(nestedTypeName))
134 {
135 elements.add(element);
136 }
137 }
138 return elements;
139 }
140 }