001: /*
002: Copyright (c) 2007, Dennis M. Sosnoski
003: All rights reserved.
004:
005: Redistribution and use in source and binary forms, with or without modification,
006: are permitted provided that the following conditions are met:
007:
008: * Redistributions of source code must retain the above copyright notice, this
009: list of conditions and the following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice,
011: this list of conditions and the following disclaimer in the documentation
012: and/or other materials provided with the distribution.
013: * Neither the name of JiBX nor the names of its contributors may be used
014: to endorse or promote products derived from this software without specific
015: prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
018: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
019: WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
021: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028:
029: package org.jibx.util;
030:
031: import java.util.HashMap;
032:
033: import org.jibx.runtime.QName;
034:
035: /**
036: * Mapping information to and from schema types.
037: */
038: public class Types {
039: public static final String SCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema";
040: public static final QName STRING_QNAME = new QName(
041: SCHEMA_NAMESPACE, "string");
042:
043: /** Set of object types mapped to schema types. */
044: private static HashMap s_objectTypeMap = new HashMap();
045:
046: static {
047: s_objectTypeMap.put("java.lang.Boolean", new QName(
048: SCHEMA_NAMESPACE, "boolean"));
049: s_objectTypeMap.put("java.lang.Byte", new QName(
050: SCHEMA_NAMESPACE, "byte"));
051: s_objectTypeMap.put("java.lang.Char", new QName(
052: SCHEMA_NAMESPACE, "unsignedInt"));
053: s_objectTypeMap.put("java.lang.Double", new QName(
054: SCHEMA_NAMESPACE, "double"));
055: s_objectTypeMap.put("java.lang.Float", new QName(
056: SCHEMA_NAMESPACE, "float"));
057: s_objectTypeMap.put("java.lang.Integer", new QName(
058: SCHEMA_NAMESPACE, "int"));
059: s_objectTypeMap.put("java.lang.Long", new QName(
060: SCHEMA_NAMESPACE, "long"));
061: s_objectTypeMap.put("java.lang.Short", new QName(
062: SCHEMA_NAMESPACE, "short"));
063: s_objectTypeMap.put("java.lang.String", STRING_QNAME);
064: s_objectTypeMap.put("java.math.BigDecimal", new QName(
065: SCHEMA_NAMESPACE, "decimal"));
066: s_objectTypeMap.put("java.math.BigInteger", new QName(
067: SCHEMA_NAMESPACE, "integer"));
068: //#!j2me{
069: s_objectTypeMap.put("java.sql.Date", new QName(
070: SCHEMA_NAMESPACE, "date"));
071: s_objectTypeMap.put("java.sql.Time", new QName(
072: SCHEMA_NAMESPACE, "time"));
073: s_objectTypeMap.put("java.sql.Timestamp", new QName(
074: SCHEMA_NAMESPACE, "dateTime"));
075: //#j2me}
076: s_objectTypeMap.put("java.util.Date", new QName(
077: SCHEMA_NAMESPACE, "dateTime"));
078: s_objectTypeMap.put("byte[]", new QName(SCHEMA_NAMESPACE,
079: "base64"));
080: }
081:
082: /** Set of primitive types mapped to schema types. */
083: private static HashMap s_primitiveTypeMap = new HashMap();
084:
085: static {
086: s_primitiveTypeMap.put("boolean", new QName(SCHEMA_NAMESPACE,
087: "boolean"));
088: s_primitiveTypeMap.put("byte", new QName(SCHEMA_NAMESPACE,
089: "byte"));
090: s_primitiveTypeMap.put("char", new QName(SCHEMA_NAMESPACE,
091: "unsignedInt"));
092: s_primitiveTypeMap.put("double", new QName(SCHEMA_NAMESPACE,
093: "double"));
094: s_primitiveTypeMap.put("float", new QName(SCHEMA_NAMESPACE,
095: "float"));
096: s_primitiveTypeMap.put("int",
097: new QName(SCHEMA_NAMESPACE, "int"));
098: s_primitiveTypeMap.put("long", new QName(SCHEMA_NAMESPACE,
099: "long"));
100: s_primitiveTypeMap.put("short", new QName(SCHEMA_NAMESPACE,
101: "short"));
102: }
103:
104: /**
105: * Check if type represents a simple text value.
106: *
107: * @param type primitive type name, or fully qualified class name
108: * @return simple value flag
109: */
110: public static boolean isSimpleValue(String type) {
111: return s_primitiveTypeMap.containsKey(type)
112: || s_objectTypeMap.containsKey(type)
113: || "void".equals(type);
114: }
115:
116: /**
117: * Get the schema type for a simple text value.
118: *
119: * @param type primitive type name, or fully qualified class name
120: * @return schema type
121: */
122: public static QName schemaType(String type) {
123: QName stype = (QName) s_primitiveTypeMap.get(type);
124: if (stype == null) {
125: stype = (QName) s_objectTypeMap.get(type);
126: }
127: return stype;
128: }
129: }
|