001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.common.model;
019:
020: import java.io.IOException;
021: import java.io.Writer;
022: import java.lang.reflect.Constructor;
023: import java.util.HashMap;
024: import java.util.Map;
025: import javax.xml.namespace.QName;
026:
027: public class JavaType {
028:
029: public static enum Style {
030: IN, OUT, INOUT
031: }
032:
033: private static Map<String, String> typeMapping = new HashMap<String, String>();
034:
035: static {
036: typeMapping.put("boolean", "false");
037: typeMapping.put("int", "0");
038: typeMapping.put("long", "0");
039: typeMapping.put("short", "Short.parseShort(\"0\")");
040: typeMapping.put("byte", "Byte.parseByte(\"0\")");
041: typeMapping.put("float", "0.0f");
042: typeMapping.put("double", "0.0");
043: typeMapping.put("char", "0");
044: typeMapping.put("java.lang.String", "\"\"");
045:
046: typeMapping.put("javax.xml.namespace.QName",
047: "new javax.xml.namespace.QName(\"\", \"\")");
048: typeMapping.put("java.net.URI", "new java.net.URI(\"\")");
049:
050: typeMapping.put("java.math.BigInteger",
051: "new java.math.BigInteger(\"0\")");
052: typeMapping.put("java.math.BigDecimal",
053: "new java.math.BigDecimal(\"0\")");
054: typeMapping.put("javax.xml.datatype.XMLGregorianCalendar",
055: "null");
056: // javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar()
057: typeMapping.put("javax.xml.datatype.Duration", "null");
058: // javax.xml.datatype.DatatypeFactory.newInstance().newDuration(\"P1Y35DT60M60.500S\")
059: }
060:
061: protected String name;
062: protected String type;
063: protected String className;
064: protected String targetNamespace;
065: protected Style style;
066: protected boolean isHeader;
067: private QName qname;
068: private JavaInterface owner;
069: private DefaultValueWriter dvw;
070:
071: public JavaType() {
072: }
073:
074: public JavaType(String n, String t, String tns) {
075: this .name = n;
076: this .type = t;
077: this .targetNamespace = tns;
078: this .className = t;
079: }
080:
081: public void setDefaultValueWriter(DefaultValueWriter w) {
082: dvw = w;
083: }
084:
085: public DefaultValueWriter getDefaultValueWriter() {
086: return dvw;
087: }
088:
089: public void setQName(QName qn) {
090: this .qname = qn;
091: }
092:
093: public QName getQName() {
094: return this .qname;
095: }
096:
097: public void setClassName(String clzName) {
098: this .className = clzName;
099: }
100:
101: public String getClassName() {
102: return this .className;
103: }
104:
105: public void writeDefaultValue(Writer writer, String indent,
106: String opName, String varName) throws IOException {
107: if (dvw != null) {
108: dvw.writeDefaultValue(writer, indent, opName, varName);
109: } else {
110: writer.write(className);
111: writer.write(' ');
112: writer.write(varName);
113: writer.write(" = ");
114: writer.write(getDefaultTypeValue());
115: writer.write(";");
116: }
117: }
118:
119: protected String getDefaultTypeValue() {
120: if (this .className.trim().endsWith("[]")) {
121: return "new "
122: + this .className.substring(0, this .className
123: .length() - 2) + "[0]";
124: }
125: if (typeMapping.containsKey(this .className.trim())) {
126: return typeMapping.get(this .className);
127: }
128:
129: try {
130: if (hasDefaultConstructor(Class.forName(this .className))) {
131: return "new " + this .className + "()";
132: }
133: } catch (ClassNotFoundException e) {
134: // DONE
135: }
136: return "null";
137: }
138:
139: private boolean hasDefaultConstructor(Class clz) {
140: Constructor[] cons = clz.getConstructors();
141: if (cons.length == 0) {
142: return false;
143: } else {
144: for (int i = 0; i < cons.length; i++) {
145: if (cons[i].getParameterTypes().length == 0) {
146: return true;
147: }
148: }
149: }
150: return false;
151: }
152:
153: public void setTargetNamespace(String tns) {
154: this .targetNamespace = tns;
155: }
156:
157: public String getTargetNamespace() {
158: return this .targetNamespace;
159: }
160:
161: public String getRawName() {
162: return this .name;
163: }
164:
165: public String getName() {
166: return this .name;
167: }
168:
169: public void setName(String s) {
170: name = s;
171: }
172:
173: public String getType() {
174: return type;
175: }
176:
177: public void setType(String t) {
178: type = t;
179: }
180:
181: //
182: // getter and setter for in, out inout style
183: //
184: public JavaType.Style getStyle() {
185: return this .style;
186: }
187:
188: public void setStyle(Style s) {
189: this .style = s;
190: }
191:
192: public boolean isIN() {
193: return this .style == Style.IN;
194: }
195:
196: public boolean isOUT() {
197: return this .style == Style.OUT;
198: }
199:
200: public boolean isINOUT() {
201: return this .style == Style.INOUT;
202: }
203:
204: public void setHeader(boolean header) {
205: this .isHeader = header;
206: }
207:
208: public boolean isHeader() {
209: return this .isHeader;
210: }
211:
212: public String toString() {
213: final StringBuffer sb = new StringBuffer();
214: sb.append("\nName: ");
215: sb.append(this .name);
216: sb.append("\nType: ");
217: sb.append(this .type);
218: sb.append("\nClassName: ");
219: sb.append(this .className);
220: sb.append("\nTargetNamespace: ");
221: sb.append(this .targetNamespace);
222: sb.append("\nStyle: ");
223: sb.append(style);
224: return sb.toString();
225: }
226:
227: public JavaInterface getOwner() {
228: return this .owner;
229: }
230:
231: public void setOwner(JavaInterface intf) {
232: this.owner = intf;
233: }
234: }
|