private void applyOnRefresh()

in impl/src/main/java/org/apache/myfaces/view/facelets/tag/jstl/core/ForEachHandler.java [441:609]


    private void applyOnRefresh(FaceletContext ctx, FaceletCompositionContext fcc, PageContext pctx, 
        UIComponent parent, String uniqueId, Object src, ValueExpression srcVE, IterationState restoredSavedOption)
        throws IOException
    {
        int s = this.getBegin(ctx);
        int e = this.getEnd(ctx);
        int m = this.getStep(ctx);
        Integer sO = this.begin != null ? Integer.valueOf(s) : null;
        Integer eO = this.end != null ? Integer.valueOf(e) : null;
        Integer mO = this.step != null ? Integer.valueOf(m) : null;
        boolean t = this.getTransient(ctx);

        // Refresh, evaluate and synchronize state
        Iterator<?> itr = this.toIterator(src);
        IterationState iterationState = new IterationState();
        iterationState.setCounter(restoredSavedOption.getCounter());
        if (itr != null)
        {
            int i = 0;

            // move to start
            while (i < s && itr.hasNext())
            {
                itr.next();
                i++;
            }

            String v = this.getVarName(ctx);
            String vs = this.getVarStatusName(ctx);
            ValueExpression vO = this.capture(v, pctx);
            ValueExpression vsO = this.capture(vs, pctx);
            int mi = 0;
            Object value = null;
            int stateIndex = 0;
            try
            {
                boolean first = true;
                while (i <= e && itr.hasNext())
                {
                    value = itr.next();
                    Object[] stateValue = null; /*restoredSavedOption.getValueList().get(stateIndex);*/
                    String base = null;
                    boolean found = false;

                    // The important thing here is use the same base for the generated component ids
                    // for each element in the iteration that was used on the restore. To do that
                    // 
                    int stateIndexCheck = stateIndex;
                    for (; stateIndexCheck < restoredSavedOption.getValueList().size(); stateIndexCheck++)
                    {
                        stateValue = restoredSavedOption.getValueList().get(stateIndexCheck);
                        if (value.equals(stateValue[1]))
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                    {
                        stateIndex = stateIndexCheck;
                        base = (String) stateValue[0];
                        stateIndex++;
                    }
                    else
                    {
                        // No state, added item, create new count
                        int count = iterationState.getCounter();
                        base = Integer.toString(count);
                        iterationState.setCounter(count+1);
                        stateValue = null;
                    }

                    if (value instanceof Serializable)
                    {
                        iterationState.getValueList().add(new Object[] {base, value, i});
                    }

                    try
                    {
                        fcc.startComponentUniqueIdSection(base);

                        setVar(ctx, parent, uniqueId, base, t, src, srcVE, value, v, i);

                        boolean last = !itr.hasNext();
                        // set the varStatus
                        if (vs != null)
                        {
                            IterationStatus itrS = new IterationStatus(first, last, i, sO, eO, mO, value);
                            ValueExpression ve;
                            if (t || srcVE == null)
                            {
                                if (srcVE == null)
                                {
                                    ve = null;
                                }
                                else
                                {
                                    ve = ctx.getExpressionFactory().createValueExpression(itrS, Object.class);
                                }
                            }
                            else
                            {
                                ve = new IterationStatusExpression(itrS);
                            }
                            setVar(ctx, parent, uniqueId, base + "_vs", vs, ve, srcVE);
                        }
                        //setVarStatus(ctx, pctx, t, sO, eO, mO, srcVE, value, vs, first, !itr.hasNext(), i);

                        // execute body
                        boolean markInitialState = (stateValue == null);// !restoredSavedOption.equals(i)
                        boolean oldMarkInitialState = false;
                        Boolean isBuildingInitialState = null;
                        try
                        {
                            if (markInitialState && fcc.isUsingPSSOnThisView())
                            {
                                //set markInitialState flag
                                oldMarkInitialState = fcc.isMarkInitialState();
                                fcc.setMarkInitialState(true);
                                isBuildingInitialState = (Boolean) ctx.getFacesContext().getAttributes().put(
                                        StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
                            }                                
                            this.nextHandler.apply(ctx, parent);
                        }
                        finally
                        {
                            if (markInitialState && fcc.isUsingPSSOnThisView())
                            {
                                //unset markInitialState flag
                                if (isBuildingInitialState == null)
                                {
                                    ctx.getFacesContext().getAttributes().remove(
                                            StateManager.IS_BUILDING_INITIAL_STATE);
                                }
                                else
                                {
                                    ctx.getFacesContext().getAttributes().put(
                                            StateManager.IS_BUILDING_INITIAL_STATE, isBuildingInitialState);
                                }
                                fcc.setMarkInitialState(oldMarkInitialState);
                            }
                        }
                    }
                    finally
                    {
                        fcc.endComponentUniqueIdSection(base);
                    }

                    // increment steps
                    mi = 1;
                    while (mi < m && itr.hasNext())
                    {
                        itr.next();
                        mi++;
                        i++;
                    }
                    i++;

                    first = false;
                }
            }
            finally
            {
                removeVarAndVarStatus(pctx, v, vO, vs, vsO);
            }
        }
        FaceletState faceletState = ComponentSupport.getFaceletState(ctx, parent, true);
        faceletState.putState(uniqueId, iterationState);
    }