KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > expression > FieldValueParser


1 package org.jicengine.expression;
2
3 import org.jicengine.operation.FieldValueOperation;
4
5 import org.jicengine.operation.Operation;
6
7 /**
8  *
9  * <p>
10  * Copyright (C) 2004 Timo Laitinen
11  * </p>
12  * @author .timo
13  */

14
15 public class FieldValueParser implements Parser {
16
17     Parser elementParser;
18
19     public FieldValueParser(Parser elementParser)
20     {
21         this.elementParser = elementParser;
22     }
23
24     public Operation parse(String JavaDoc expression) throws SyntaxException
25     {
26         char[] chars = expression.toCharArray();
27         int fieldNameSeparatorIndex = -1;
28         for (int i = chars.length-1; 0 <= i; i--) {
29             if( chars[i] == METHOD_PARAMS_END || chars[i] == METHOD_PARAMS_START || chars[i] == ARRAY_LENGTH_START || chars[i] == ARRAY_LENGTH_END){
30                 // the existence of method-param-brackets signals that this isn't
31
// a field-value-expression.
32
break;
33             }
34             else if( chars[i] == OPERATION_SEPARATOR ){
35                 // we found the field-name
36
fieldNameSeparatorIndex = i;
37                 break;
38             }
39             else {
40                 continue;
41             }
42         }
43
44         if( fieldNameSeparatorIndex != -1 ){
45             // extract the actor-part (= a name of a variable or a class ).
46
String JavaDoc actorName = expression.substring(0, fieldNameSeparatorIndex);
47             String JavaDoc fieldName = expression.substring(fieldNameSeparatorIndex+1);
48
49             Operation actor = elementParser.parse(actorName);
50
51             return new FieldValueOperation(expression, actor, fieldName);
52         }
53         else {
54             // no field name found, I guess the
55
// expression wasn't a field-value expression.
56
return null;
57         }
58     }
59 }
60
Popular Tags