001: /*--
002:
003: Copyright (C) 2002-2005 Adrian Price.
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: 1. Redistributions of source code must retain the above copyright
011: notice, this list of conditions, and the following disclaimer.
012:
013: 2. Redistributions in binary form must reproduce the above copyright
014: notice, this list of conditions, and the disclaimer that follows
015: these conditions in the documentation and/or other materials
016: provided with the distribution.
017:
018: 3. The names "OBE" and "Open Business Engine" must not be used to
019: endorse or promote products derived from this software without prior
020: written permission. For written permission, please contact
021: adrianprice@sourceforge.net.
022:
023: 4. Products derived from this software may not be called "OBE" or
024: "Open Business Engine", nor may "OBE" or "Open Business Engine"
025: appear in their name, without prior written permission from
026: Adrian Price (adrianprice@users.sourceforge.net).
027:
028: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
029: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
030: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
031: DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
032: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
033: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
034: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
035: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
036: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
037: IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
038: POSSIBILITY OF SUCH DAMAGE.
039:
040: For more information on OBE, please see
041: <http://obe.sourceforge.net/>.
042:
043: */
044:
045: package org.obe.xpdl.model.data;
046:
047: import org.obe.util.AbstractEnum;
048: import org.obe.xpdl.XPDLNames;
049: import org.w3c.dom.Document;
050:
051: import java.util.Date;
052: import java.util.HashMap;
053: import java.util.List;
054: import java.util.Map;
055:
056: /**
057: * @author Adrian Price
058: */
059: public final class DataTypes extends AbstractEnum {
060: private static final long serialVersionUID = 8024954267852036171L;
061:
062: public static final DataTypes BOOLEAN = new DataTypes(
063: XPDLNames.BOOLEAN_KIND, Type.BOOLEAN_TYPE);
064: public static final DataTypes PERFORMER = new DataTypes(
065: XPDLNames.PERFORMER_KIND, Type.PERFORMER_TYPE);
066: public static final DataTypes STRING = new DataTypes(
067: XPDLNames.STRING_KIND, Type.STRING_TYPE);
068: public static final DataTypes FLOAT = new DataTypes(
069: XPDLNames.FLOAT_KIND, Type.FLOAT_TYPE);
070: public static final DataTypes INTEGER = new DataTypes(
071: XPDLNames.INTEGER_KIND, Type.INTEGER_TYPE);
072: public static final DataTypes REFERENCE = new DataTypes(
073: XPDLNames.REFERENCE_KIND, Type.REFERENCE_TYPE);
074: public static final DataTypes DATETIME = new DataTypes(
075: XPDLNames.DATETIME_KIND, Type.DATETIME_TYPE);
076: public static final DataTypes UNION = new DataTypes(
077: XPDLNames.UNION_KIND, Type.UNION_TYPE);
078: public static final DataTypes ENUMERATION = new DataTypes(
079: XPDLNames.ENUMERATION_KIND, Type.ENUMERATION_TYPE);
080: public static final DataTypes ARRAY = new DataTypes(
081: XPDLNames.ARRAY_KIND, Type.ARRAY_TYPE);
082: public static final DataTypes LIST = new DataTypes(
083: XPDLNames.LIST_KIND, Type.LIST_TYPE);
084: public static final DataTypes EXTERNAL_REFERENCE = new DataTypes(
085: XPDLNames.EXTERNAL_REFERENCE_KIND,
086: Type.EXTERNAL_REFERENCE_TYPE);
087: public static final DataTypes SCHEMA = new DataTypes(
088: XPDLNames.SCHEMA_KIND, Type.SCHEMA_TYPE);
089:
090: /**
091: * Array of all DataTypes.
092: */
093: private static final DataTypes[] _values = { BOOLEAN, PERFORMER,
094: STRING, FLOAT, INTEGER, REFERENCE, DATETIME, UNION,
095: ENUMERATION, ARRAY, LIST, EXTERNAL_REFERENCE, SCHEMA };
096: private static final Map _tagMap = new HashMap();
097: public static final List VALUES = clinit(_values, _tagMap);
098:
099: /**
100: * Lookup table of classes that correspond to a particular type:
101: * <p/>
102: * <code>Class c = DataTypes.classForType(type.value());</code>
103: * <p/>
104: */
105: private static final Class[] _xpdlToJava = { String.class, // STRING_TYPE
106: Double.class, // FLOAT_TYPE
107: Integer.class, // INTEGER_TYPE
108: Object.class, // REFERENCE_TYPE
109: Date.class, // DATETIME_TYPE
110: Boolean.class, // BOOLEAN_TYPE
111: String.class, // PERFORMER_TYPE
112: null, // DECLARED_TYPE (always mapped to another type)
113: Document.class, // SCHEMA_TYPE
114: Object.class, // EXTERNAL_REFERENCE_TYPE
115: Object.class, // RECORD_TYPE
116: Object.class, // UNION_TYPE
117: Object.class, // ENUMERATION_TYPE
118: Object.class, // ARRAY_TYPE
119: List.class // LIST_TYPE
120: };
121:
122: private static final Object[][] _javaToXpdl = {
123: { boolean.class, new Integer(Type.BOOLEAN_TYPE) },
124: { Boolean.class, new Integer(Type.BOOLEAN_TYPE) },
125: { byte.class, new Integer(Type.INTEGER_TYPE) },
126: { Byte.class, new Integer(Type.INTEGER_TYPE) },
127: { char.class, new Integer(Type.INTEGER_TYPE) },
128: { Character.class, new Integer(Type.INTEGER_TYPE) },
129: { double.class, new Integer(Type.FLOAT_TYPE) },
130: { Double.class, new Integer(Type.FLOAT_TYPE) },
131: { float.class, new Integer(Type.FLOAT_TYPE) },
132: { Float.class, new Integer(Type.FLOAT_TYPE) },
133: { int.class, new Integer(Type.INTEGER_TYPE) },
134: { Integer.class, new Integer(Type.INTEGER_TYPE) },
135: { short.class, new Integer(Type.INTEGER_TYPE) },
136: { Short.class, new Integer(Type.INTEGER_TYPE) },
137: { Date.class, new Integer(Type.DATETIME_TYPE) },
138: { Document.class, new Integer(Type.SCHEMA_TYPE) },
139: { String.class, new Integer(Type.STRING_TYPE) }, };
140: private static final Map _javaToXpdlMap = new HashMap();
141:
142: static {
143: for (int i = 0; i < _javaToXpdl.length; i++)
144: _javaToXpdlMap.put(_javaToXpdl[i][0], _javaToXpdl[i][1]);
145: }
146:
147: /**
148: * Returns the XPDL ordinal data type for the specified class.
149: *
150: * @param type The Java class to map to an XPDL type.
151: * @return Ordinal data type.
152: * @see DataTypes#typeForClass
153: */
154: public static Class classForType(int type) {
155: return _xpdlToJava[type];
156: }
157:
158: /**
159: * Returns the class for the specified data type.
160: *
161: * @param dataType The XPDL data type definition.
162: * @return Run-time value class for the data type.
163: */
164: public static Class classForDataType(DataType dataType) {
165: return classForType(dataType.getType());
166: }
167:
168: /**
169: * Returns the class for the specified data type.
170: *
171: * @param type The XPDL data type.
172: * @return Run-time value class for the data type.
173: */
174: public static Class classForType(Type type) {
175: return classForType(type.getImpliedType().value());
176: }
177:
178: /**
179: * Looks up the type ordinal that corresponds to a particular class.
180: * <p/>
181: * <code>int type = DataTypes.typeForClass(clazz);</code>
182: * <p/>
183: */
184: public static int typeForClass(Class clazz) {
185: Integer type = (Integer) _javaToXpdlMap.get(clazz);
186: return type == null ? Type.EXTERNAL_REFERENCE_TYPE : type
187: .intValue();
188: }
189:
190: public static DataType dataTypeForClass(Class parmType) {
191: Type type;
192: int ordinal = typeForClass(parmType);
193: switch (ordinal) {
194: case Type.STRING_TYPE:
195: case Type.FLOAT_TYPE:
196: case Type.INTEGER_TYPE:
197: case Type.DATETIME_TYPE:
198: case Type.BOOLEAN_TYPE:
199: case Type.PERFORMER_TYPE:
200: type = BasicType.valueOf(ordinal);
201: break;
202: case Type.SCHEMA_TYPE:
203: // No way for us to tell what the schema itself should be.
204: type = new SchemaType();
205: break;
206: case Type.EXTERNAL_REFERENCE_TYPE:
207: type = new ExternalReference("java:" + parmType.getName());
208: break;
209: default:
210: // All other types are deprecated or could never be returned.
211: throw new IllegalArgumentException(parmType.toString());
212: }
213: return new DataType(type);
214: }
215:
216: public static DataTypes valueOf(String tag) {
217: DataTypes dataTypes = (DataTypes) _tagMap.get(tag);
218: if (dataTypes == null && tag != null)
219: throw new IllegalArgumentException(tag);
220: return dataTypes;
221: }
222:
223: /**
224: * Construct a new DataTypes object.
225: *
226: * @param name
227: * @param ordinal The int value
228: */
229: private DataTypes(String name, int ordinal) {
230: super (name, ordinal);
231: }
232:
233: public List family() {
234: return VALUES;
235: }
236: }
|