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: */
019: package org.apache.axis2.description.java2wsdl;
020:
021: import org.apache.axiom.om.OMElement;
022:
023: import javax.xml.namespace.QName;
024: import java.util.*;
025:
026: public class TypeTable {
027:
028: private static HashMap simpleTypetoxsd;
029: public static final QName ANY_TYPE = new QName(
030: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "anyType", "xs");
031:
032: private HashMap complexTypeMap;
033:
034: public TypeTable() {
035: //complex type table is resetted every time this is
036: //instantiated
037: complexTypeMap = new HashMap();
038: }
039:
040: /* statically populate the simple type map - this is not likely to
041: * change and we need not populate it over and over */
042: static {
043: populateSimpleTypes();
044: }
045:
046: /* populate the simpletype hashmap */
047: private static void populateSimpleTypes() {
048: simpleTypetoxsd = new HashMap();
049: //todo pls use the types from org.apache.ws.commons.schema.constants.Constants
050: simpleTypetoxsd.put("int", new QName(
051: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "int", "xs"));
052: simpleTypetoxsd
053: .put("java.lang.String", new QName(
054: Java2WSDLConstants.URI_2001_SCHEMA_XSD,
055: "string", "xs"));
056: simpleTypetoxsd.put("boolean",
057: new QName(Java2WSDLConstants.URI_2001_SCHEMA_XSD,
058: "boolean", "xs"));
059: simpleTypetoxsd.put("float", new QName(
060: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "float", "xs"));
061: simpleTypetoxsd
062: .put("double", new QName(
063: Java2WSDLConstants.URI_2001_SCHEMA_XSD,
064: "double", "xs"));
065: simpleTypetoxsd.put("short", new QName(
066: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "short", "xs"));
067: simpleTypetoxsd.put("long", new QName(
068: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "long", "xs"));
069: simpleTypetoxsd.put("byte", new QName(
070: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "byte", "xs"));
071: simpleTypetoxsd.put("char", ANY_TYPE);
072: simpleTypetoxsd.put("java.lang.Integer", new QName(
073: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "int", "xs"));
074: simpleTypetoxsd
075: .put("java.lang.Double", new QName(
076: Java2WSDLConstants.URI_2001_SCHEMA_XSD,
077: "double", "xs"));
078: simpleTypetoxsd.put("java.lang.Float", new QName(
079: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "float", "xs"));
080: simpleTypetoxsd.put("java.lang.Long", new QName(
081: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "long", "xs"));
082: simpleTypetoxsd.put("java.lang.Character", ANY_TYPE);
083: simpleTypetoxsd.put("java.lang.Boolean",
084: new QName(Java2WSDLConstants.URI_2001_SCHEMA_XSD,
085: "boolean", "xs"));
086: simpleTypetoxsd.put("java.lang.Byte", new QName(
087: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "byte", "xs"));
088: simpleTypetoxsd.put("java.lang.Short", new QName(
089: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "short", "xs"));
090: simpleTypetoxsd.put("java.util.Date", new QName(
091: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "dateTime",
092: "xs"));
093: simpleTypetoxsd.put("java.util.Calendar", new QName(
094: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "dateTime",
095: "xs"));
096:
097: // SQL date time
098: simpleTypetoxsd.put("java.sql.Date", new QName(
099: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "dateTime",
100: "xs"));
101: simpleTypetoxsd.put("java.sql.Time", new QName(
102: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "time", "xs"));
103: simpleTypetoxsd.put("java.sql.Timestamp", new QName(
104: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "dateTime",
105: "xs"));
106:
107: //consider BigDecimal, BigInteger, Day, Duration, Month, MonthDay,
108: //Time, Year, YearMonth as SimpleType as well
109: simpleTypetoxsd.put("java.math.BigDecimal",
110: new QName(Java2WSDLConstants.URI_2001_SCHEMA_XSD,
111: "decimal", "xs"));
112: simpleTypetoxsd.put("java.math.BigInteger",
113: new QName(Java2WSDLConstants.URI_2001_SCHEMA_XSD,
114: "integer", "xs"));
115: simpleTypetoxsd.put("org.apache.axis2.databinding.types.Day",
116: new QName(Java2WSDLConstants.URI_2001_SCHEMA_XSD,
117: "gDay", "xs"));
118: simpleTypetoxsd.put(
119: "org.apache.axis2.databinding.types.Duration",
120: new QName(Java2WSDLConstants.URI_2001_SCHEMA_XSD,
121: "duration", "xs"));
122: simpleTypetoxsd.put("org.apache.axis2.databinding.types.Month",
123: new QName(Java2WSDLConstants.URI_2001_SCHEMA_XSD,
124: "gMonth", "xs"));
125: simpleTypetoxsd.put(
126: "org.apache.axis2.databinding.types.MonthDay",
127: new QName(Java2WSDLConstants.URI_2001_SCHEMA_XSD,
128: "gMonthDay", "xs"));
129: simpleTypetoxsd.put("org.apache.axis2.databinding.types.Time",
130: new QName(Java2WSDLConstants.URI_2001_SCHEMA_XSD,
131: "time", "xs"));
132: simpleTypetoxsd.put("org.apache.axis2.databinding.types.Year",
133: new QName(Java2WSDLConstants.URI_2001_SCHEMA_XSD,
134: "gYear", "xs"));
135: simpleTypetoxsd.put(
136: "org.apache.axis2.databinding.types.YearMonth",
137: new QName(Java2WSDLConstants.URI_2001_SCHEMA_XSD,
138: "gYearMonth", "xs"));
139:
140: simpleTypetoxsd.put("java.lang.Object", ANY_TYPE);
141:
142: // Any types
143: simpleTypetoxsd.put(OMElement.class.getName(), ANY_TYPE);
144: simpleTypetoxsd.put(ArrayList.class.getName(), ANY_TYPE);
145: simpleTypetoxsd.put(Vector.class.getName(), ANY_TYPE);
146: simpleTypetoxsd.put(List.class.getName(), ANY_TYPE);
147: simpleTypetoxsd.put(HashMap.class.getName(), ANY_TYPE);
148: simpleTypetoxsd.put(Hashtable.class.getName(), ANY_TYPE);
149: //byteArrat
150: simpleTypetoxsd.put("base64Binary", new QName(
151: Java2WSDLConstants.URI_2001_SCHEMA_XSD, "base64Binary",
152: "xs"));
153: }
154:
155: /**
156: * Return the schema type QName given the type class name
157: * @param typeName the name of the type
158: * @return the name of the simple type or null if it is not a simple type
159: */
160: public QName getSimpleSchemaTypeName(String typeName) {
161: QName qName = (QName) simpleTypetoxsd.get(typeName);
162: if (qName == null) {
163: if ((typeName.startsWith("java.lang") || typeName
164: .startsWith("javax."))
165: && !Exception.class.getName().equals(typeName)) {
166: return ANY_TYPE;
167: }
168: }
169: return qName;
170: }
171:
172: /**
173: * Return whether the given type is a simple type or not
174: * @param typeName the name of the type
175: * @return true if the type is a simple type
176: */
177: public boolean isSimpleType(String typeName) {
178:
179: if (simpleTypetoxsd.keySet().contains(typeName)) {
180: return true;
181: } else if (typeName.startsWith("java.lang")
182: || typeName.startsWith("javax.")) {
183: return true;
184: }
185: return false;
186: }
187:
188: /**
189: * Return the complex type map
190: * @return the map with complex types
191: */
192: public Map getComplexSchemaMap() {
193: return complexTypeMap;
194: }
195:
196: public void addComplexSchema(String name, QName schemaType) {
197: complexTypeMap.put(name, schemaType);
198: }
199:
200: public QName getComplexSchemaType(String name) {
201: return (QName) complexTypeMap.get(name);
202: }
203:
204: /**
205: * Get the qname for a type
206: * first try the simple types if not try the complex types
207: * @param typeName name of the type
208: * @return the Qname for this type
209: */
210: public QName getQNamefortheType(String typeName) {
211: QName type = getSimpleSchemaTypeName(typeName);
212: if (type == null) {
213: type = getComplexSchemaType(typeName);
214: }
215: return type;
216: }
217: }
|