001: package org.jicengine.expression;
002:
003: import java.util.List;
004: import java.util.ArrayList;
005:
006: /**
007: *
008: * <p>
009: * Copyright (C) 2004 Timo Laitinen
010: * </p>
011: *
012: * @author Timo Laitinen
013: * @created 2004-09-20
014: * @since JICE-0.10
015: *
016: */
017: public final class Utils {
018: public static char METHOD_ARGS_START = '(';
019: public static char METHOD_ARGS_END = ')';
020:
021: /**
022: * 'anything.anything(something, else)' -> ['something','else'].
023: */
024: public static String[] extractArguments(String expression) {
025: return extractArguments(expression, 0, expression.length());
026: }
027:
028: /**
029: *
030: * @param expression String 'anything.anything(something, else)'
031: * @param fromIndex int
032: * @param endIndex int
033: * @return String[] {'something','else'}
034: */
035: public static String[] extractArguments(String expression,
036: int fromIndex, int endIndex) {
037: char[] chars = expression.toCharArray();
038: char character;
039: int paramStart = -1;
040: int paramEnd = -1;
041:
042: // find the first '('
043: for (int i = fromIndex; i < endIndex; i++) {
044: character = chars[i];
045: if (character == METHOD_ARGS_START) {
046: // got it.
047: paramStart = i;
048: break;
049: }
050: }
051:
052: // find the last ')'
053: for (int i = endIndex - 1; fromIndex <= i; i--) {
054: character = chars[i];
055: if (character == METHOD_ARGS_END) {
056: paramEnd = i;
057: break;
058: }
059: }
060:
061: if (paramStart < 0 && paramEnd < 0) {
062: return new String[0];
063: } else {
064: try {
065: return parseArgumentlist(expression.substring(
066: paramStart + 1, paramEnd));
067: } catch (StringIndexOutOfBoundsException e) {
068: throw new IllegalArgumentException(
069: "didn't find correct argument-part from string '"
070: + expression + "' at [" + fromIndex
071: + ".." + endIndex + "]");
072: }
073: }
074: }
075:
076: public static String toParameterList(String[] parameters) {
077: String list = "";
078: for (int i = 0; i < parameters.length; i++) {
079: list += parameters[i];
080: if (i + 1 < parameters.length) {
081: list += ",";
082: }
083: }
084: return list;
085:
086: }
087:
088: /**
089: *
090: * @param list String 'arg1,arg2,arg3'
091: * @return String[] {'arg1','arg2','arg3'}
092: */
093: public static String[] parseArgumentlist(String list) {
094: if (list.indexOf("'") == -1) {
095: java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(
096: list, ",");
097: String[] params = new String[tokenizer.countTokens()];
098: for (int i = 0; i < params.length; i++) {
099: params[i] = tokenizer.nextToken().trim();
100: }
101: return params;
102: } else {
103: boolean inString = false;
104: java.util.StringTokenizer tokenizer = new java.util.StringTokenizer(
105: list, ",'", true);
106: List params = new ArrayList();
107: String token;
108: while (tokenizer.hasMoreElements()) {
109: token = tokenizer.nextToken(",'");
110: if (token.equals("'")) {
111: try {
112: token = tokenizer.nextToken("'");
113: tokenizer.nextToken("'");
114: params.add("'" + token + "'");
115: } catch (java.util.NoSuchElementException e) {
116: throw new IllegalArgumentException(
117: "unclosed string in parameter-list: "
118: + list);
119: }
120: } else if (token.equals(",")) {
121: // nothing..
122: } else {
123: params.add(token.trim());
124: }
125: }
126:
127: return (String[]) params.toArray(new String[0]);
128: }
129:
130: }
131: }
|