1 package org.objectweb.celtix.tools.common.model; 2 3 import java.lang.reflect.Constructor ; 4 import java.util.*; 5 import javax.xml.namespace.QName ; 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 , String > typeMapping = new HashMap<String , String >(); 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 typeMapping.put("javax.xml.datatype.Duration", "null"); 31 } 33 34 protected String name; 35 protected String type; 36 protected String className; 37 protected String targetNamespace; 38 protected Style style; 39 protected TypeReference typeRef; 40 protected boolean isHeader; 41 private QName qname; 42 43 public JavaType() { 44 } 45 46 public JavaType(String n, String t, String tns) { 47 this.name = n; 48 this.type = t; 49 this.targetNamespace = tns; 50 this.className = t; 51 } 52 53 public void setQName(QName qn) { 54 this.qname = qn; 55 } 56 57 public QName getQName() { 58 return this.qname; 59 } 60 61 public void setClassName(String clzName) { 62 this.className = clzName; 63 } 64 65 public String getClassName() { 66 return this.className; 67 } 68 69 public String 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 e) { 82 } 84 return "null"; 85 } 86 87 private boolean hasDefaultConstructor(Class clz) { 88 Constructor [] 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 tns) { 102 this.targetNamespace = tns; 103 } 104 105 public String getTargetNamespace() { 106 return this.targetNamespace; 107 } 108 109 public String getName() { 110 return name; 111 } 112 113 public void setName(String s) { 114 name = s; 115 } 116 117 public String getType() { 118 return type; 119 } 120 121 public void setType(String t) { 122 type = t; 123 } 124 125 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 toString() { 164 final StringBuffer sb = new StringBuffer (); 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 |