1 package org.apache.velocity.runtime.parser.node;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.velocity.context.InternalContextAdapter;
26 import org.apache.velocity.exception.MethodInvocationException;
27 import org.apache.velocity.runtime.parser.Parser;
28 import org.apache.velocity.runtime.parser.ParserVisitor;
29
30
31
32
33
34
35
36
37
38 public class ASTIntegerRange extends SimpleNode
39 {
40
41
42
43 public ASTIntegerRange(int id)
44 {
45 super(id);
46 }
47
48
49
50
51
52 public ASTIntegerRange(Parser p, int id)
53 {
54 super(p, id);
55 }
56
57
58
59
60 public Object jjtAccept(ParserVisitor visitor, Object data)
61 {
62 return visitor.visit(this, data);
63 }
64
65
66
67
68
69
70
71
72
73 public Object value( InternalContextAdapter context)
74 throws MethodInvocationException
75 {
76
77
78
79
80 Object left = jjtGetChild(0).value( context );
81 Object right = jjtGetChild(1).value( context );
82
83
84
85
86
87 if (left == null || right == null)
88 {
89 log.error((left == null ? "Left" : "Right")
90 + " side of range operator [n..m] has null value."
91 + " Operation not possible. "
92 + context.getCurrentTemplateName() + " [line " + getLine()
93 + ", column " + getColumn() + "]");
94 return null;
95 }
96
97
98
99
100
101 if ( !( left instanceof Integer ) || !( right instanceof Integer ))
102 {
103 log.error((!(left instanceof Integer) ? "Left" : "Right")
104 + " side of range operator is not a valid type. "
105 + "Currently only integers (1,2,3...) and Integer type is supported. "
106 + context.getCurrentTemplateName() + " [line " + getLine()
107 + ", column " + getColumn() + "]");
108
109 return null;
110 }
111
112
113
114
115
116
117 int l = ( (Integer) left ).intValue() ;
118 int r = ( (Integer) right ).intValue();
119
120
121
122
123
124 int nbrElements = Math.abs( l - r );
125 nbrElements += 1;
126
127
128
129
130
131 int delta = ( l >= r ) ? -1 : 1;
132
133
134
135
136
137 List elements = new ArrayList(nbrElements);
138 int value = l;
139
140 for (int i = 0; i < nbrElements; i++)
141 {
142
143 elements.add(new Integer(value));
144 value += delta;
145 }
146
147 return elements;
148 }
149 }