001: package org.jicengine.expression;
002:
003: import org.jicengine.operation.StaticValue;
004: import java.util.Map;
005: import java.util.HashMap;
006:
007: /**
008: *
009: *
010: * <p>
011: * Copyright (C) 2004 Timo Laitinen
012: * </p>
013: * @author Timo Laitinen
014: * @created 2004-09-20
015: * @since JICE-0.10
016: *
017: */
018:
019: public class ClassParser extends StaticValueParser {
020:
021: public static final char INNERCLASS_SEPARATOR = '$';
022: public static final String ARRAY_BRACKETS = "[]";
023:
024: public static final ClassParser INSTANCE = new ClassParser();
025:
026: private static final Map primitives = new HashMap();
027: static {
028: primitives.put("int", Integer.TYPE);
029: primitives.put("double", Double.TYPE);
030: primitives.put("long", Long.TYPE);
031: primitives.put("float", Float.TYPE);
032: primitives.put("boolean", Boolean.TYPE);
033: primitives.put("char", Character.TYPE);
034: primitives.put("byte", Byte.TYPE);
035: // shortcut for string defined here
036: primitives.put("String", String.class);
037: primitives.put("short", Short.TYPE);
038: }
039:
040: private static final Map primitiveArrays = new HashMap();
041: static {
042: primitiveArrays.put("int[]", new int[0].getClass());
043: primitiveArrays.put("double[]", new double[0].getClass());
044: primitiveArrays.put("long[]", new long[0].getClass());
045: primitiveArrays.put("float[]", new float[0].getClass());
046: primitiveArrays.put("boolean[]", new boolean[0].getClass());
047: primitiveArrays.put("char[]", new char[0].getClass());
048: primitiveArrays.put("byte[]", new byte[0].getClass());
049: primitiveArrays.put("short[]", new short[0].getClass());
050:
051: // shortcut for string arrays defined here
052: primitiveArrays.put("String[]", new String[0].getClass());
053: }
054:
055: public Object parseValue(String expression) throws SyntaxException {
056: char[] chars = expression.toCharArray();
057: char character;
058: if (Character.isLetter(chars[0])) {
059: for (int i = 1; i < chars.length; i++) {
060: character = chars[i];
061: if (character == OPERATION_SEPARATOR
062: || Character.isLetterOrDigit(character)
063: || character == '_'
064: || character == INNERCLASS_SEPARATOR) {
065: // ok.
066: } else {
067: return null;
068: }
069: }
070: try {
071: return toClass(expression);
072: } catch (ClassNotFoundException e) {
073: throw new SyntaxException(e.toString());
074: }
075: } else {
076: return null;
077: }
078: }
079:
080: public static Class toClass(String classExpression)
081: throws ClassNotFoundException {
082: Class parsedClass;
083: if (classExpression.endsWith(ARRAY_BRACKETS)) {
084: // it is an array
085:
086: Class primitiveArray = (Class) primitiveArrays
087: .get(classExpression);
088: if (primitiveArray != null) {
089: parsedClass = primitiveArray;
090: } else {
091: // any object-array
092: // transform the class to the format understood by Java.
093: String elementClassName = classExpression.substring(0,
094: classExpression.lastIndexOf("[]"));
095: String parsedArrayClassName = "[L" + elementClassName
096: + ";";
097: parsedClass = toClass(parsedArrayClassName);
098: }
099: } else {
100: // not an array
101: Class primitive = (Class) primitives.get(classExpression);
102: if (primitive != null) {
103: parsedClass = primitive;
104: } else {
105: ClassLoader loader = ClassLoaderResolver
106: .getClassLoader(ClassParser.class);
107: parsedClass = Class.forName(classExpression, true,
108: loader);
109: }
110: }
111: return parsedClass;
112: }
113: }
|