KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.jsp.PageContext JavaDoc;
20
21 import org.apache.taglibs.standard.lang.jpath.adapter.ConversionException;
22 import org.apache.taglibs.standard.lang.jpath.adapter.Convert;
23 import org.apache.taglibs.standard.lang.jpath.adapter.IterationContext;
24
25 /**
26  * The NotEqualsOperator class
27  *
28  *
29  * @author <a HREF='mailto:scott.hasse@isthmusgroup.com'>Scott Hasse</a>
30  * @version
31  */

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

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

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

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

95     public Object JavaDoc evaluate(PageContext JavaDoc pageContext, IterationContext icontext)
96             throws EvaluationException {
97
98         boolean result;
99
100         try {
101             Object JavaDoc leftSide = jjtGetChild(0).evaluate(pageContext, icontext);
102             Object JavaDoc rightSide = jjtGetChild(1).evaluate(pageContext, icontext);
103
104             if ((leftSide instanceof Boolean JavaDoc)
105                     || (rightSide instanceof Boolean JavaDoc)) {
106                 result = (Convert.toBoolean(leftSide).booleanValue()
107                         != Convert.toBoolean(rightSide).booleanValue());
108             } else if ((leftSide instanceof Double JavaDoc)
109                     || (rightSide instanceof Double JavaDoc)) {
110                 result = (Convert.toDouble(leftSide).doubleValue()
111                         != Convert.toDouble(rightSide).doubleValue());
112             } else if ((leftSide instanceof String JavaDoc)
113                     || (rightSide instanceof String JavaDoc)) {
114                 result =
115                     !Convert.toString(leftSide)
116                         .equals(Convert.toString(rightSide));
117             } else if ((leftSide instanceof Comparable JavaDoc)
118                     && (rightSide instanceof Comparable JavaDoc)) {
119                 try {
120                     int comp = ((Comparable JavaDoc) leftSide).compareTo(rightSide);
121
122                     if (comp != 0) {
123                         result = true;
124                     } else {
125                         result = false;
126                     }
127                 } catch (ClassCastException JavaDoc cce) {
128                     throw new EvaluationException(this,
129                             "child nodes ["
130                             + jjtGetChild(0).toNormalizedString() + "] and ["
131                             + jjtGetChild(1).toNormalizedString()
132                             + "] cannot be compared");
133                 }
134             } else {
135                 throw new EvaluationException(this,
136                         "child nodes [" + jjtGetChild(0).toNormalizedString()
137                         + "] and [" + jjtGetChild(1).toNormalizedString()
138                         + "] cannot be compared");
139             }
140         } catch (ConversionException ce) {
141             throw new EvaluationException(this, ce.getMessage());
142         }
143
144         return new Boolean JavaDoc(result);
145     }
146 }
147
Popular Tags