1 package org.apache.velocity.runtime.parser.node; 2 3 18 19 20 29 30 import java.util.ArrayList ; 31 32 import org.apache.velocity.context.InternalContextAdapter; 33 import org.apache.velocity.runtime.parser.Parser; 34 35 import org.apache.velocity.exception.MethodInvocationException; 36 37 public class ASTIntegerRange extends SimpleNode { 38 39 public ASTIntegerRange(int id) 40 { 41 super(id); 42 } 43 44 public ASTIntegerRange(Parser p, int id) 45 { 46 super(p, id); 47 } 48 49 50 public Object jjtAccept(ParserVisitor visitor, Object data) 51 { 52 return visitor.visit(this, data); 53 } 54 55 62 public Object value( InternalContextAdapter context) 63 throws MethodInvocationException 64 { 65 68 69 Object left = jjtGetChild(0).value( context ); 70 Object right = jjtGetChild(1).value( context ); 71 72 75 76 if (left == null || right == null) 77 { 78 rsvc.error( ( left == null ? "Left" : "Right" ) + " side of range operator [n..m] has null value." 79 + " Operation not possible. " 80 + context.getCurrentTemplateName() + " [line " + getLine() 81 + ", column " + getColumn() + "]"); 82 return null; 83 } 84 85 88 89 if ( !( left instanceof Integer ) || !( right instanceof Integer )) 90 { 91 rsvc.error( ( !( left instanceof Integer ) ? "Left" : "Right" ) 92 + " side of range operator is not a valid type. " 93 + "Currently only integers (1,2,3...) and Integer type is supported. " 94 + context.getCurrentTemplateName() + " [line " + getLine() 95 + ", column " + getColumn() + "]"); 96 97 return null; 98 } 99 100 101 104 105 int l = ( (Integer ) left ).intValue() ; 106 int r = ( (Integer ) right ).intValue(); 107 108 111 112 int num = Math.abs( l - r ); 113 num += 1; 114 115 118 119 int delta = ( l >= r ) ? -1 : 1; 120 121 124 125 ArrayList foo = new ArrayList (); 126 int val = l; 127 128 for(int i =0; i < num; i++) 129 { 130 foo.add( new Integer ( val )); 131 val += delta; 132 } 133 134 return foo; 135 } 136 } 137 138 | Popular Tags |