001: package org.objectweb.celtix.tools.common.model;
002:
003: import java.lang.reflect.Constructor;
004: import java.util.*;
005: import javax.xml.namespace.QName;
006: import com.sun.xml.bind.api.TypeReference;
007:
008: public class JavaType {
009:
010: public static enum Style {
011: IN, OUT, INOUT
012: }
013:
014: private static Map<String, String> typeMapping = new HashMap<String, String>();
015:
016: static {
017: typeMapping.put("boolean", "false");
018: typeMapping.put("int", "0");
019: typeMapping.put("long", "0");
020: typeMapping.put("short", "Short.parseShort(\"0\")");
021: typeMapping.put("byte", "Byte.parseByte(\"0\")");
022: typeMapping.put("float", "0.0f");
023: typeMapping.put("double", "0.0");
024: typeMapping.put("char", "0");
025:
026: typeMapping.put("javax.xml.namespace.QName",
027: "new javax.xml.namespace.QName(\"\", \"\")");
028: typeMapping.put("java.net.URI", "new java.net.URI(\"\")");
029:
030: typeMapping.put("java.math.BigInteger",
031: "new java.math.BigInteger(\"0\")");
032: typeMapping.put("java.math.BigDecimal",
033: "new java.math.BigDecimal(\"0\")");
034: typeMapping.put("javax.xml.datatype.XMLGregorianCalendar",
035: "null");
036: // javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar()
037: typeMapping.put("javax.xml.datatype.Duration", "null");
038: // javax.xml.datatype.DatatypeFactory.newInstance().newDuration(\"P1Y35DT60M60.500S\")
039: }
040:
041: protected String name;
042: protected String type;
043: protected String className;
044: protected String targetNamespace;
045: protected Style style;
046: protected TypeReference typeRef;
047: protected boolean isHeader;
048: private QName qname;
049:
050: public JavaType() {
051: }
052:
053: public JavaType(String n, String t, String tns) {
054: this .name = n;
055: this .type = t;
056: this .targetNamespace = tns;
057: this .className = t;
058: }
059:
060: public void setQName(QName qn) {
061: this .qname = qn;
062: }
063:
064: public QName getQName() {
065: return this .qname;
066: }
067:
068: public void setClassName(String clzName) {
069: this .className = clzName;
070: }
071:
072: public String getClassName() {
073: return this .className;
074: }
075:
076: public String getDefaultTypeValue() {
077: if (this .className.trim().endsWith("[]")) {
078: return "new "
079: + this .className.substring(0, this .className
080: .length() - 2) + "[0]";
081: }
082: if (typeMapping.containsKey(this .className.trim())) {
083: return typeMapping.get(this .className);
084: }
085:
086: try {
087: if (hasDefaultConstructor(Class.forName(this .className))) {
088: return "new " + this .className + "()";
089: }
090: } catch (ClassNotFoundException e) {
091: // DONE
092: }
093: return "null";
094: }
095:
096: private boolean hasDefaultConstructor(Class clz) {
097: Constructor[] cons = clz.getConstructors();
098: if (cons.length == 0) {
099: return false;
100: } else {
101: for (int i = 0; i < cons.length; i++) {
102: if (cons[i].getParameterTypes().length == 0) {
103: return true;
104: }
105: }
106: }
107: return false;
108: }
109:
110: public void setTargetNamespace(String tns) {
111: this .targetNamespace = tns;
112: }
113:
114: public String getTargetNamespace() {
115: return this .targetNamespace;
116: }
117:
118: public String getName() {
119: return name;
120: }
121:
122: public void setName(String s) {
123: name = s;
124: }
125:
126: public String getType() {
127: return type;
128: }
129:
130: public void setType(String t) {
131: type = t;
132: }
133:
134: //
135: // getter and setter for in, out inout style
136: //
137: public JavaType.Style getStyle() {
138: return this .style;
139: }
140:
141: public void setStyle(Style s) {
142: this .style = s;
143: }
144:
145: public boolean isIN() {
146: return this .style == Style.IN;
147: }
148:
149: public boolean isOUT() {
150: return this .style == Style.OUT;
151: }
152:
153: public boolean isINOUT() {
154: return this .style == Style.INOUT;
155: }
156:
157: public void setTypeReference(TypeReference ref) {
158: this .typeRef = ref;
159: }
160:
161: public TypeReference getTypeReference() {
162: return this .typeRef;
163: }
164:
165: public void setHeader(boolean header) {
166: this .isHeader = header;
167: }
168:
169: public boolean isHeader() {
170: return this .isHeader;
171: }
172:
173: public String toString() {
174: final StringBuffer sb = new StringBuffer();
175: sb.append("\nName: ");
176: sb.append(this .name);
177: sb.append("\nType: ");
178: sb.append(this .type);
179: sb.append("\nClass Name: ");
180: sb.append(this .className);
181: sb.append("\nTargetNamespace: ");
182: sb.append(this .targetNamespace);
183: sb.append("\nStyle: ");
184: sb.append(style);
185: return sb.toString();
186: }
187: }
|