1 16 17 package org.apache.taglibs.standard.lang.jpath.expression; 18 19 import javax.servlet.jsp.PageContext ; 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 32 public class EqualsOperator extends SimpleNode { 33 34 41 public EqualsOperator(int id) { 42 super(id); 43 } 44 45 53 public EqualsOperator(Parser p, int id) { 54 super(p, id); 55 } 56 57 67 public String toNormalizedString() { 68 69 String normalized = ""; 70 71 normalized = "(" + jjtGetChild(0).toNormalizedString() + " " 72 + getTokenImage(ParserConstants.EQ) + " " 73 + jjtGetChild(1).toNormalizedString() + ")"; 74 75 return normalized; 76 } 77 78 95 public Object evaluate(PageContext pageContext, IterationContext icontext) 96 throws EvaluationException { 97 98 boolean result; 99 100 try { 101 Object leftSide = jjtGetChild(0).evaluate(pageContext, icontext); 102 Object rightSide = jjtGetChild(1).evaluate(pageContext, icontext); 103 104 if ((leftSide instanceof Boolean ) 105 || (rightSide instanceof Boolean )) { 106 result = (Convert.toBoolean(leftSide).booleanValue() 107 == Convert.toBoolean(rightSide).booleanValue()); 108 } else if ((leftSide instanceof Double ) 109 || (rightSide instanceof Double )) { 110 result = (Convert.toDouble(leftSide).doubleValue() 111 == Convert.toDouble(rightSide).doubleValue()); 112 } else if ((leftSide instanceof String ) 113 || (rightSide instanceof String )) { 114 result = 115 Convert.toString(leftSide) 116 .equals(Convert.toString(rightSide)); 117 } else if ((leftSide instanceof Comparable ) 118 && (rightSide instanceof Comparable )) { 119 try { 120 int comp = ((Comparable ) leftSide).compareTo(rightSide); 121 122 if (comp == 0) { 123 result = true; 124 } else { 125 result = false; 126 } 127 } catch (ClassCastException 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 (result); 145 } 146 } 147 | Popular Tags |