From: Keats K. <ke...@us...> - 2006-03-28 23:44:31
|
Update of /cvsroot/webmacro/webmacro/src/org/webmacro/directive In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1753 Modified Files: CountDirective.java Log Message: Fixed infinite loop bug when an argument was undefined. Refactored to use EEH. Index: CountDirective.java =================================================================== RCS file: /cvsroot/webmacro/webmacro/src/org/webmacro/directive/CountDirective.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** CountDirective.java 1 Nov 2005 04:08:04 -0000 1.7 --- CountDirective.java 28 Mar 2006 23:44:26 -0000 1.8 *************** *** 29,32 **** --- 29,33 ---- import org.webmacro.engine.BuildContext; import org.webmacro.engine.BuildException; + import org.webmacro.engine.UndefinedMacro; import org.webmacro.engine.Variable; *************** *** 34,42 **** /** - * Allows a template to count - * according to a range. - * <pre> * #count $i from 1 to 100 [step 1] ! * </pre> * @author Eric B. Ridge (eb...@tc...) * @since 1.1b1 --- 35,40 ---- /** * #count $i from 1 to 100 [step 1] ! * * @author Eric B. Ridge (eb...@tc...) * @since 1.1b1 *************** *** 143,215 **** { int start = _start, end = _end, step = _step; ! Object tmp; ! // if necessary, do run-time evaluation of our // start, end, and step objects. ! if ((tmp = _objStart) != null) ! { ! while (tmp instanceof Macro) ! tmp = ((Macro) tmp).evaluate(context); ! if (tmp != null) ! start = Integer.parseInt(tmp.toString()); ! else ! { ! writeWarning("#count: Starting value cannot be null. Not counting", context, out); ! return; ! } ! } ! ! if ((tmp = _objEnd) != null) ! { ! while (tmp instanceof Macro) ! tmp = ((Macro) tmp).evaluate(context); ! if (tmp != null) ! end = Integer.parseInt(tmp.toString()); ! else ! { ! writeWarning("#count: Ending value cannot be null. Not counting", context, out); ! return; ! } ! } ! ! if ((tmp = _objStep) != null) ! { ! while (tmp instanceof Macro) ! tmp = ((Macro) tmp).evaluate(context); ! if (tmp != null) ! step = Integer.parseInt(tmp.toString()); ! else ! { ! writeWarning("#count: Starting value cannot be null. Not counting", context, out); ! return; ! } } ! ! // Check if no step explicitly assigned, if so auto-detect it ! if (step == Integer.MAX_VALUE ) { ! step = (start > end) ? -1 : +1; } ! if (step > 0) ! { ! for (; start <= end; start += step) ! { ! _iterator.setValue(context, new Integer(start)); ! _body.write(out, context); ! } ! } ! else if (step < 0) ! { ! for (; start >= end; start += step) ! { ! _iterator.setValue(context, new Integer(start)); ! _body.write(out, context); ! } ! } ! else { ! writeWarning("#count: step cannot be 0. Not counting", context, out); } } ! } \ No newline at end of file --- 141,227 ---- { int start = _start, end = _end, step = _step; ! boolean error = false; ! // if necessary, do run-time evaluation of our // start, end, and step objects. ! // If object is null then it has been fully resolved during build. ! try { ! if (_objStart != null) ! start = evalAsInt(context, _objStart); ! if (_objEnd != null) ! end = evalAsInt(context, _objEnd); ! if (_objStep != null) ! step = evalAsInt(context, _objStep); } ! catch (Exception e) { ! out.write(context.getEvaluationExceptionHandler().expand(_iterator, context, e)); ! error = true; } ! if (!error) { ! // Check if no step explicitly assigned, if so auto-detect it ! if (step == Integer.MAX_VALUE ) ! { ! step = (start > end) ? -1 : +1; ! } ! ! if (step > 0) ! { ! for (; start <= end; start += step) ! { ! _iterator.setValue(context, new Integer(start)); ! _body.write(out, context); ! } ! } ! else if (step < 0) ! { ! for (; start >= end; start += step) ! { ! _iterator.setValue(context, new Integer(start)); ! _body.write(out, context); ! } ! } ! else ! { ! PropertyException pe = new PropertyException.UndefinedVariableException(); ! pe.setMessage("#count: step cannot be 0."); ! out.write(context.getEvaluationExceptionHandler().expand(_iterator, context, pe)); ! } } } ! ! private int evalAsInt(Context context, Object o) throws PropertyException ! { ! if (o != null) ! { ! while (o instanceof Macro && !(o == UndefinedMacro.getInstance())) ! { ! o = ((Macro)o).evaluate(context); ! } ! if (o == UndefinedMacro.getInstance()){ ! PropertyException pe = new PropertyException.UndefinedVariableException(); ! pe.setMessage("Undefined expression in #count directive."); ! throw pe; ! } ! } ! if (o == null) ! { ! PropertyException pe = new PropertyException.UndefinedVariableException(); ! pe.setMessage("Null expression in #count directive."); ! throw pe; ! } ! if (o instanceof Number) ! return ((Number) o).intValue(); ! try { ! return Integer.parseInt(o.toString()); ! } ! catch (NumberFormatException e) ! { ! PropertyException pe = new PropertyException.UndefinedVariableException(); ! pe.setMessage("Invalid numeric expression in #count directive: [" + o + "]"); ! throw pe; ! } ! } ! } |