KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 package org.objectweb.medor.expression.lib;
19
20 import org.objectweb.medor.expression.api.Expression;
21 import org.objectweb.medor.expression.api.Operand;
22 import org.objectweb.medor.expression.api.ParameterOperand;
23 import org.objectweb.medor.expression.api.ExpressionException;
24 import org.objectweb.medor.expression.api.MalformedExpressionException;
25 import org.objectweb.medor.expression.api.TypingException;
26 import org.objectweb.jorm.type.api.PType;
27 import org.objectweb.jorm.type.api.PTypeSpace;
28
29 /**
30  * Is an operator to upper string operand.
31  *
32  * @author S.Chassande-Barrioz
33  */

34 public class StringUpper extends BasicUnaryOperator {
35
36     public StringUpper() {
37         super(PTypeSpace.STRING);
38     }
39     public StringUpper(Expression strOperand) {
40         super(PTypeSpace.STRING, strOperand);
41     }
42
43     public String JavaDoc getOperatorString() {
44         return STRING_UPPER;
45     }
46
47     public Operand evaluate(ParameterOperand[] pos, Object JavaDoc o)
48         throws ExpressionException {
49         if (expressions[0] == null) {
50             throw new MalformedExpressionException("Null children value");
51         }
52         if (result == null) {
53             throw new ExpressionException("Expression not compiled");
54         }
55         result.setValue(
56                 evaluate(getExpression(0).evaluate(pos, o).getString()));
57         return result;
58     }
59
60     public Operand compileExpression()
61         throws ExpressionException, MalformedExpressionException {
62         if (expressions[0] == null) {
63             throw new MalformedExpressionException("Null children value");
64         }
65         expressions[0].compileExpression();
66         PType opType = expressions[0].getType();
67         if (opType == null) {
68             throw new TypingException("Argument type not defined (null)");
69         }
70         if (opType.getTypeCode() != PType.TYPECODE_STRING) {
71             throw new TypingException("Bad Argument type, expected String, found: "
72                 + expressions[0].getType().getJavaName());
73         }
74         result = new BasicVariableOperand(type);
75         verified = true;
76         return result;
77     }
78
79     public String JavaDoc evaluate(String JavaDoc str) {
80         return (str == null ? null : str.toUpperCase());
81     }
82 }
83
Popular Tags