/** * Iterates collection. */ protected void iterateCollection(final Collection collection, final int from, final int count, final PageContext pageContext) throws JspException { JspFragment body = getJspBody(); Iterator iter = collection.iterator(); int i = 0; int to = calculateTo(from, count, collection.size()); while (i < to) { Object item = iter.next(); if (i >= from) { if (status != null) { iteratorStatus.next(!iter.hasNext()); } TagUtil.setScopeAttribute(var, item, scope, pageContext); TagUtil.invokeBody(body); } i++; } }
/** * Iterates arrays. */ protected void iterateArray(final Object[] array, final int from, final int count, final PageContext pageContext) throws JspException { JspFragment body = getJspBody(); int len = array.length; int to = calculateTo(from, count, len); int last = to - 1; for (int i = from; i < to; i++) { Object item = array[i]; if (status != null) { iteratorStatus.next(i == last); } TagUtil.setScopeAttribute(var, item, scope, pageContext); TagUtil.invokeBody(body); } }
@Override public void doTag() throws JspException { if (items == null) { return; } JspFragment body = getJspBody(); if (body == null) { return; } PageContext pageContext = (PageContext) getJspContext(); // create an iterator status if the status attribute was set. if (status != null) { iteratorStatus = new IteratorStatus(this.modulus); TagUtil.setScopeAttribute(status, iteratorStatus, scope, pageContext); } if (items instanceof Collection) { iterateCollection((Collection) items, from, count, pageContext); } else if (items.getClass().isArray()) { iterateArray((Object[]) items, from, count, pageContext); } else if (items instanceof String) { iterateArray(Converter.get().toStringArray(items), from, count, pageContext); } else { throw new JspException("Provided items are not iterable"); } // cleanup if (status != null) { TagUtil.removeScopeAttribute(status, scope, pageContext); } TagUtil.removeScopeAttribute(var, scope, pageContext); }