1 package org.apache.velocity.runtime.parser.node; 2 3 18 19 import org.apache.velocity.context.InternalContextAdapter; 20 import org.apache.velocity.runtime.parser.Parser; 21 22 import org.apache.velocity.exception.MethodInvocationException; 23 24 34 public class ASTDivNode extends SimpleNode 35 { 36 public ASTDivNode(int id) 37 { 38 super(id); 39 } 40 41 public ASTDivNode(Parser p, int id) 42 { 43 super(p, id); 44 } 45 46 47 public Object jjtAccept(ParserVisitor visitor, Object data) 48 { 49 return visitor.visit(this, data); 50 } 51 52 57 public Object value( InternalContextAdapter context) 58 throws MethodInvocationException 59 { 60 63 64 Object left = jjtGetChild(0).value( context ); 65 Object right = jjtGetChild(1).value( context ); 66 67 70 71 if (left == null || right == null) 72 { 73 rsvc.error( ( left == null ? "Left" : "Right" ) 74 + " side (" 75 + jjtGetChild( (left == null? 0 : 1) ).literal() 76 + ") of division operation has null value." 77 + " Operation not possible. " 78 + context.getCurrentTemplateName() 79 + " [line " + getLine() 80 + ", column " + getColumn() + "]"); 81 return null; 82 } 83 84 87 88 if ( !( left instanceof Integer ) || !( right instanceof Integer )) 89 { 90 rsvc.error( ( !( left instanceof Integer ) ? "Left" : "Right" ) 91 + " side of division operation is not a valid type. " 92 + "Currently only integers (1,2,3...) and Integer type is supported. " 93 + context.getCurrentTemplateName() + " [line " + getLine() 94 + ", column " + getColumn() + "]"); 95 96 return null; 97 } 98 99 102 103 if ( ( (Integer ) right).intValue() == 0 ) 104 { 105 rsvc.error( "Right side of division operation is zero. Must be non-zero. " 106 + context.getCurrentTemplateName() + " [line " + getLine() 107 + ", column " + getColumn() + "]"); 108 109 return null; 110 } 111 112 return new Integer ( ( (Integer ) left ).intValue() / ( (Integer ) right ).intValue() ); 113 } 114 } 115 | Popular Tags |