KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jpath > expression > AdditionOperator


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.lang.jpath.expression;
18
19 import java.math.BigDecimal JavaDoc;
20
21 import javax.servlet.jsp.PageContext JavaDoc;
22
23 import org.apache.taglibs.standard.lang.jpath.adapter.ConversionException;
24 import org.apache.taglibs.standard.lang.jpath.adapter.Convert;
25 import org.apache.taglibs.standard.lang.jpath.adapter.IterationContext;
26
27 /**
28  * The AdditionOperator class
29  *
30  *
31  * @author <a HREF='mailto:scott.hasse@isthmusgroup.com'>Scott Hasse</a>
32  * @version
33  */

34 public class AdditionOperator extends SimpleNode {
35
36     /**
37      * Used to create an instance of the AdditionOperator class
38      *
39      *
40      * @param id
41      *
42      */

43     public AdditionOperator(int id) {
44         super(id);
45     }
46
47     /**
48      * Used to create an instance of the AdditionOperator class
49      *
50      *
51      * @param p
52      * @param id
53      *
54      */

55     public AdditionOperator(Parser p, int id) {
56         super(p, id);
57     }
58
59     /**
60      * Provides a method to print a normalized version of the original
61      * expression. The normalized version has standardized spacing and
62      * parenthesis, and can be used to compare expressions formatted
63      * in different ways to see if they are actually the same expression.
64      *
65      *
66      * @return The normalized version of the original expression
67      *
68      */

69     public String JavaDoc toNormalizedString() {
70
71         String JavaDoc normalized = "";
72
73         normalized = "(" + jjtGetChild(0).toNormalizedString() + " "
74                 + getTokenImage(ParserConstants.PLUS) + " "
75                 + jjtGetChild(1).toNormalizedString() + ")";
76
77         return normalized;
78     }
79
80     /**
81      * This method evaluates this node of the expression and all child nodes.
82      * It returns the result of the
83      * evaluation as an <tt>Object</tt>. If any problems are encountered
84      * during the evaluation, an <tt>EvaluationException</tt> is thrown.
85      * This method uses the <tt>BigDecimal</tt> class with conversions
86      * to ensure proper results from the operation.
87      *
88      *
89      * @param pageContext the current JSP PageContext
90      *
91      * @param icontext the Iteration Context of the expression. If there is
92      * no interation context, this should be null.
93      *
94      * @return the result of the expression evaluation as an object
95      *
96      * @throws EvaluationException if a problem is encountered during the
97      * evaluation
98      */

99     public Object JavaDoc evaluate(PageContext JavaDoc pageContext, IterationContext icontext)
100             throws EvaluationException {
101
102         Double JavaDoc result;
103
104         try {
105             BigDecimal JavaDoc leftSide = new BigDecimal JavaDoc(
106                 Convert.toDouble(jjtGetChild(0).evaluate(pageContext,
107                     icontext)).toString());
108             BigDecimal JavaDoc rightSide = new BigDecimal JavaDoc(
109                 Convert.toDouble(jjtGetChild(1).evaluate(pageContext,
110                     icontext)).toString());
111
112             result = new Double JavaDoc((leftSide.add(rightSide)).doubleValue());
113         } catch (ConversionException ce) {
114             throw new EvaluationException(this, ce.getMessage());
115         }
116
117         return result;
118     }
119 }
120
Popular Tags