| 1 | package org.apache.velocity.runtime.parser.node; |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2000-2001,2004 The Apache Software Foundation. |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * handles the range 'operator' [ n .. m ] |
| 22 | * |
| 23 | * Please look at the Parser.jjt file which is |
| 24 | * what controls the generation of this class. |
| 25 | * |
| 26 | * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> |
| 27 | * @version $Id: ASTIntegerRange.java,v 1.4.10.1 2004/03/03 23:22:58 geirm Exp $ |
| 28 | */ |
| 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 | /** Accept the visitor. **/ |
| 50 | public Object jjtAccept(ParserVisitor visitor, Object data) |
| 51 | { |
| 52 | return visitor.visit(this, data); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * does the real work. Creates an Vector of Integers with the |
| 57 | * right value range |
| 58 | * |
| 59 | * @param context app context used if Left or Right of .. is a ref |
| 60 | * @return Object array of Integers |
| 61 | */ |
| 62 | public Object value( InternalContextAdapter context) |
| 63 | throws MethodInvocationException |
| 64 | { |
| 65 | /* |
| 66 | * get the two range ends |
| 67 | */ |
| 68 | |
| 69 | Object left = jjtGetChild(0).value( context ); |
| 70 | Object right = jjtGetChild(1).value( context ); |
| 71 | |
| 72 | /* |
| 73 | * if either is null, lets log and bail |
| 74 | */ |
| 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 | /* |
| 86 | * if not an Integer, not much we can do either |
| 87 | */ |
| 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 | /* |
| 102 | * get the two integer values of the ends of the range |
| 103 | */ |
| 104 | |
| 105 | int l = ( (Integer) left ).intValue() ; |
| 106 | int r = ( (Integer) right ).intValue(); |
| 107 | |
| 108 | /* |
| 109 | * find out how many there are |
| 110 | */ |
| 111 | |
| 112 | int num = Math.abs( l - r ); |
| 113 | num += 1; |
| 114 | |
| 115 | /* |
| 116 | * see if your increment is Pos or Neg |
| 117 | */ |
| 118 | |
| 119 | int delta = ( l >= r ) ? -1 : 1; |
| 120 | |
| 121 | /* |
| 122 | * make the vector and fill it |
| 123 | */ |
| 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 | |