KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > tools > common > model > JavaType


1 package org.objectweb.celtix.tools.common.model;
2
3 import java.lang.reflect.Constructor JavaDoc;
4 import java.util.*;
5 import javax.xml.namespace.QName JavaDoc;
6 import com.sun.xml.bind.api.TypeReference;
7
8 public class JavaType {
9     
10     public static enum Style { IN, OUT, INOUT }
11     private static Map<String JavaDoc, String JavaDoc> typeMapping = new HashMap<String JavaDoc, String JavaDoc>();
12
13     static {
14         typeMapping.put("boolean", "false");
15         typeMapping.put("int", "0");
16         typeMapping.put("long", "0");
17         typeMapping.put("short", "Short.parseShort(\"0\")");
18         typeMapping.put("byte", "Byte.parseByte(\"0\")");
19         typeMapping.put("float", "0.0f");
20         typeMapping.put("double", "0.0");
21         typeMapping.put("char", "0");
22         
23         typeMapping.put("javax.xml.namespace.QName", "new javax.xml.namespace.QName(\"\", \"\")");
24         typeMapping.put("java.net.URI", "new java.net.URI(\"\")");
25
26         typeMapping.put("java.math.BigInteger", "new java.math.BigInteger(\"0\")");
27         typeMapping.put("java.math.BigDecimal", "new java.math.BigDecimal(\"0\")");
28         typeMapping.put("javax.xml.datatype.XMLGregorianCalendar", "null");
29         // javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar()
30
typeMapping.put("javax.xml.datatype.Duration", "null");
31         // javax.xml.datatype.DatatypeFactory.newInstance().newDuration(\"P1Y35DT60M60.500S\")
32
}
33
34     protected String JavaDoc name;
35     protected String JavaDoc type;
36     protected String JavaDoc className;
37     protected String JavaDoc targetNamespace;
38     protected Style style;
39     protected TypeReference typeRef;
40     protected boolean isHeader;
41     private QName JavaDoc qname;
42
43     public JavaType() {
44     }
45
46     public JavaType(String JavaDoc n, String JavaDoc t, String JavaDoc tns) {
47         this.name = n;
48         this.type = t;
49         this.targetNamespace = tns;
50         this.className = t;
51     }
52
53     public void setQName(QName JavaDoc qn) {
54         this.qname = qn;
55     }
56
57     public QName JavaDoc getQName() {
58         return this.qname;
59     }
60
61     public void setClassName(String JavaDoc clzName) {
62         this.className = clzName;
63     }
64
65     public String JavaDoc getClassName() {
66         return this.className;
67     }
68
69     public String JavaDoc getDefaultTypeValue() {
70         if (this.className.trim().endsWith("[]")) {
71             return "new " + this.className.substring(0, this.className.length() - 2) + "[0]";
72         }
73         if (typeMapping.containsKey(this.className.trim())) {
74             return typeMapping.get(this.className);
75         }
76
77         try {
78             if (hasDefaultConstructor(Class.forName(this.className))) {
79                 return "new " + this.className + "()";
80             }
81         } catch (ClassNotFoundException JavaDoc e) {
82             // DONE
83
}
84         return "null";
85     }
86
87     private boolean hasDefaultConstructor(Class JavaDoc clz) {
88         Constructor JavaDoc[] cons = clz.getConstructors();
89         if (cons.length == 0) {
90             return false;
91         } else {
92             for (int i = 0; i < cons.length; i++) {
93                 if (cons[i].getParameterTypes().length == 0) {
94                     return true;
95                 }
96             }
97         }
98         return false;
99     }
100     
101     public void setTargetNamespace(String JavaDoc tns) {
102         this.targetNamespace = tns;
103     }
104
105     public String JavaDoc getTargetNamespace() {
106         return this.targetNamespace;
107     }
108
109     public String JavaDoc getName() {
110         return name;
111     }
112
113     public void setName(String JavaDoc s) {
114         name = s;
115     }
116
117     public String JavaDoc getType() {
118         return type;
119     }
120
121     public void setType(String JavaDoc t) {
122         type = t;
123     }
124     
125     //
126
// getter and setter for in, out inout style
127
//
128
public JavaType.Style getStyle() {
129         return this.style;
130     }
131
132     public void setStyle(Style s) {
133         this.style = s;
134     }
135
136     public boolean isIN() {
137         return this.style == Style.IN;
138     }
139
140     public boolean isOUT() {
141         return this.style == Style.OUT;
142     }
143
144     public boolean isINOUT() {
145         return this.style == Style.INOUT;
146     }
147     public void setTypeReference(TypeReference ref) {
148         this.typeRef = ref;
149     }
150
151     public TypeReference getTypeReference() {
152         return this.typeRef;
153     }
154
155     public void setHeader(boolean header) {
156         this.isHeader = header;
157     }
158
159     public boolean isHeader() {
160         return this.isHeader;
161     }
162
163     public String JavaDoc toString() {
164         final StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
165         sb.append("\nName: ");
166         sb.append(this.name);
167         sb.append("\nType: ");
168         sb.append(this.type);
169         sb.append("\nClass Name: ");
170         sb.append(this.className);
171         sb.append("\nTargetNamespace: ");
172         sb.append(this.targetNamespace);
173         sb.append("\nStyle: ");
174         sb.append(style);
175         return sb.toString();
176     }
177 }
178
Popular Tags