1 package org.apache.velocity.runtime.parser.node; 2 3 18 19 20 29 import org.apache.velocity.context.InternalContextAdapter; 30 import org.apache.velocity.runtime.parser.Parser; 31 32 import org.apache.velocity.exception.MethodInvocationException; 33 34 public class ASTModNode extends SimpleNode 35 { 36 public ASTModNode(int id) 37 { 38 super(id); 39 } 40 41 public ASTModNode(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 public Object value( InternalContextAdapter context) 53 throws MethodInvocationException 54 { 55 58 59 Object left = jjtGetChild(0).value( context ); 60 Object right = jjtGetChild(1).value( context ); 61 62 65 66 if (left == null || right == null) 67 { 68 rsvc.error( ( left == null ? "Left" : "Right" ) + " side (" 69 + jjtGetChild( (left == null? 0 : 1) ).literal() 70 + ") of modulus operation has null value." 71 + " Operation not possible. " 72 + context.getCurrentTemplateName() + " [line " + getLine() 73 + ", column " + getColumn() + "]"); 74 return null; 75 } 76 77 80 81 if ( !( left instanceof Integer ) || !( right instanceof Integer )) 82 { 83 rsvc.error( ( !( left instanceof Integer ) ? "Left" : "Right" ) 84 + " side of modulus operation is not a valid type. " 85 + "Currently only integers (1,2,3...) and Integer type is supported. " 86 + context.getCurrentTemplateName() + " [line " + getLine() 87 + ", column " + getColumn() + "]"); 88 89 return null; 90 } 91 92 95 96 if ( ( (Integer ) right).intValue() == 0 ) 97 { 98 rsvc.error( "Right side of modulus operation is zero. Must be non-zero. " 99 + context.getCurrentTemplateName() + " [line " + getLine() 100 + ", column " + getColumn() + "]"); 101 102 return null; 103 } 104 105 return new Integer ( ( (Integer ) left ).intValue() % ( (Integer ) right ).intValue() ); 106 } 107 } 108 109 | Popular Tags |