KickJava   Java API By Example, From Geeks To Geeks.

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


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.Expression;
27 import org.objectweb.medor.expression.api.ExpressionException;
28 import org.objectweb.medor.expression.api.MalformedExpressionException;
29 import org.objectweb.medor.expression.api.Operand;
30 import org.objectweb.medor.expression.api.Operator;
31 import org.objectweb.medor.expression.api.ParameterOperand;
32 import org.objectweb.medor.expression.api.TypingException;
33 import org.objectweb.jorm.type.api.PType;
34 import org.objectweb.jorm.type.api.PTypeSpace;
35
36 /**
37  * Operator representing the search for a substring within a given input string,
38  * starting at a given index.
39  */

40 public class IndexedLocate extends BasicOperator {
41
42     /**
43      * Constructs an IndexedLocate operator, representing the search for a
44      * substring in an input string, starting at a given index.
45      * <p>It is the equivalent of the Java indexOf String manipulation method.
46      * <p>If the substring argument occurs as a substring within the
47      * inputString at a starting index no smaller than
48      * <code>fromIndex</code>, then the index of the first character
49      * of the first such substring is returned. If it does not occur
50      * as a substring starting at <code>fromIndex</code> or beyond,
51      * <code>-1</code> is returned.
52      * <p>Unlike in Java, the index starts with 1 and not 0.
53      * @param inputString the String in which to look.
54      * @param substring the substring to search for.
55      * @param fromIndex the index to start the search from.
56
57      */

58     public IndexedLocate(Expression inputString, Expression substring,
59                          Expression fromIndex) {
60         super(PTypeSpace.INT, new Expression[]{inputString, substring, fromIndex});
61     }
62
63     public IndexedLocate() {
64         super(PTypeSpace.INT);
65     }
66
67     public Operand evaluate(ParameterOperand[] pos, Object JavaDoc o)
68         throws ExpressionException {
69         try {
70             result.setValue(
71                     evaluate(
72                             getExpression(0).evaluate(pos, o).getString(),
73                             getExpression(1).evaluate(pos, o).getString(),
74                             getExpression(2).evaluate(pos, o).getInt()));
75         }
76         catch (NullPointerException JavaDoc e) {
77             // result == null or missing children objects
78
throw new ExpressionException("Unevaluable Expression: May not be compiled");
79         }
80         return result;
81     }
82
83     public Operand compileExpression()
84         throws ExpressionException, MalformedExpressionException {
85         if ((getExpression(0) != null)
86                 && (getExpression(1) != null)
87                 && (getExpression(2) != null)) {
88             getExpression(0).compileExpression();
89             getExpression(1).compileExpression();
90             getExpression(2).compileExpression();
91             if ((getExpression(0).getType().getTypeCode() ==
92                 PType.TYPECODE_STRING) &&
93                 (getExpression(1).getType().getTypeCode() ==
94                 PType.TYPECODE_STRING) &&
95                 (getExpression(2).getType().isa(PTypeSpace.INT))) {
96                 result = new BasicVariableOperand(type);
97                 verified = true;
98             }
99             else
100             //it is not a boolean type
101
throw new TypingException("Attempted argument types: String, String, int");
102         }
103         else
104         //Operands not yet assigned
105
throw new MalformedExpressionException("Null children value");
106         return result;
107     }
108
109     public int evaluate(String JavaDoc inputString, String JavaDoc subString, int fromIndex) {
110         return inputString.indexOf(subString, fromIndex - 1);
111     }
112
113     public String JavaDoc getOperatorString() {
114         return Operator.INDEXEDLOCATE;
115     }
116 }
Popular Tags