KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > expression > parser > lib > ReplaceStringPlusByConcat


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.parser.lib;
19
20 import org.objectweb.medor.expression.api.Expression;
21 import org.objectweb.medor.expression.api.Operator;
22 import org.objectweb.medor.expression.api.MalformedExpressionException;
23 import org.objectweb.medor.expression.lib.Plus;
24 import org.objectweb.medor.expression.lib.Concat;
25 import org.objectweb.medor.expression.lib.TypeConverter;
26 import org.objectweb.jorm.type.api.PTypeSpace;
27
28 /**
29  * This class is a rewriter of Expression, replacing the plus operator between
30  * string to the concat operator.
31  *
32  * @author S.Chassande-Barrioz
33  */

34 public class ReplaceStringPlusByConcat {
35
36
37     /**
38      * replaces the plus operator between string to the concat operator.
39      * @param e is the expression to rewrite
40      * @return the new expression
41      */

42     public Expression rewrite(Expression e) throws MalformedExpressionException {
43         if (e instanceof Operator) {
44             Operator op = (Operator) e;
45             for(int i=op.getOperandNumber()-1; i>=0; i--) {
46                 op.setExpression(i, rewrite(op.getExpression(i)));
47             }
48             if (op instanceof Plus) {
49                 Expression left = op.getExpression(0);
50                 Expression right = op.getExpression(1);
51                 if (left.getType() == PTypeSpace.STRING
52                     || right.getType() == PTypeSpace.STRING) {
53                     return new Concat(
54                         convertToString(left),
55                         convertToString(right));
56                 }
57             }
58         }
59         return e;
60     }
61
62     /**
63      * Converts an expression to a string.
64      */

65     private Expression convertToString(Expression e) throws MalformedExpressionException {
66         return (e.getType() == PTypeSpace.STRING
67             ? e
68             : new TypeConverter(e, PTypeSpace.STRING));
69     }
70
71 }
72
Popular Tags