KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > expression > lib > FirstLocate


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23
24 package org.objectweb.medor.expression.lib;
25
26 import org.objectweb.medor.expression.api.BinaryOperator;
27 import org.objectweb.medor.expression.api.Expression;
28 import org.objectweb.medor.expression.api.ExpressionException;
29 import org.objectweb.medor.expression.api.MalformedExpressionException;
30 import org.objectweb.medor.expression.api.Operand;
31 import org.objectweb.medor.expression.api.Operator;
32 import org.objectweb.medor.expression.api.ParameterOperand;
33 import org.objectweb.medor.expression.api.TypingException;
34 import org.objectweb.jorm.type.api.PType;
35 import org.objectweb.jorm.type.api.PTypeSpace;
36
37 public class FirstLocate extends BasicBinaryOperator implements BinaryOperator {
38
39     /**
40      * Returns the first position of an Expression (first parameter) in another
41      * Expression (second parameter).
42      * <p>Unlike in Java, the index is 1 for the first position, and not zero.
43      * @param substring is the Expression to be tested as a subexpression
44      * @param instring is the Expression which may contain the expressions[0] Expression.
45      */

46     public FirstLocate(Expression substring, Expression instring) {
47         super(substring, instring);
48     }
49
50     public FirstLocate() {
51     }
52
53     public org.objectweb.medor.expression.api.Operand evaluate(ParameterOperand[] pos, Object JavaDoc o)
54         throws ExpressionException {
55         try {
56             result.setValue(
57                     evaluate(
58                             expressions[0].evaluate(pos, o).getString(),
59                             expressions[1].evaluate(pos, o).getString()));
60         }
61         catch (NullPointerException JavaDoc e) {
62             // result == null or missing children objects
63
throw new IllegalStateException JavaDoc("Unevaluable Expression: Not compiled");
64         }
65         return result;
66     }
67
68     public int evaluate(String JavaDoc op1, String JavaDoc op2) {
69         return op2.indexOf(op1) + 1;
70     }
71
72     public Operand compileExpression()
73         throws ExpressionException, MalformedExpressionException {
74         if ((expressions[0] != null) && (expressions[1] != null)) {
75             expressions[0].compileExpression();
76             expressions[1].compileExpression();
77             if (!(expressions[0].getType() == null || expressions[1].getType() == null)) {
78                 if ((expressions[0].getType().getTypeCode() == PType.TYPECODE_STRING) &&
79                     (expressions[1].getType().getTypeCode() == PType.TYPECODE_STRING)) {
80                     type = PTypeSpace.INT;
81                     result = new BasicVariableOperand(type);
82                     verified = true;
83                 }
84                 else
85                 // it is not a string type
86
throw new TypingException("Attempt arg type : String, String");
87             }
88             else
89                 throw new MalformedExpressionException("null children value");
90         }
91         else
92         // Operands not yet assigned
93
throw new MalformedExpressionException("null children value");
94         return result;
95     }
96
97     public String JavaDoc getOperatorString() {
98         return Operator.FIRSTLOCATE;
99     }
100 }
101
Popular Tags