Search in sources :

Example 1 with TemplateContextImpl

use of org.apache.myfaces.view.facelets.impl.TemplateContextImpl in project myfaces by apache.

the class IncludeHandler method apply.

/*
     * (non-Javadoc)
     * 
     * @see jakarta.faces.view.facelets.FaceletHandler#apply(jakarta.faces.view.facelets.FaceletContext, jakarta.faces.component.UIComponent)
     */
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, ELException {
    AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
    FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
    String path;
    boolean markInitialState = false;
    String uniqueId = null;
    if (!src.isLiteral()) {
        uniqueId = actx.generateUniqueFaceletTagId(fcc.startComponentUniqueIdSection(), tagId);
    } else if (_params != null) {
        uniqueId = actx.generateUniqueFaceletTagId(fcc.generateUniqueComponentId(), tagId);
    }
    if (!src.isLiteral()) {
        String restoredPath = (String) ComponentSupport.restoreInitialTagState(ctx, fcc, parent, uniqueId);
        if (restoredPath != null) {
            // evaluated and if is not equals, trigger markInitialState stuff.
            if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId())) {
                path = this.src.getValue(ctx);
                if (StringUtils.isBlank(path)) {
                    return;
                }
                if (!path.equals(restoredPath)) {
                    markInitialState = true;
                }
            } else {
                path = restoredPath;
            }
        } else {
            // No state restored, calculate path
            path = this.src.getValue(ctx);
        }
        ComponentSupport.saveInitialTagState(ctx, fcc, parent, uniqueId, path);
    } else {
        path = this.src.getValue(ctx);
    }
    try {
        if (StringUtils.isBlank(path)) {
            return;
        }
        VariableMapper orig = ctx.getVariableMapper();
        ctx.setVariableMapper(new VariableMapperWrapper(orig));
        try {
            URL url = null;
            boolean oldMarkInitialState = false;
            Boolean isBuildingInitialState = null;
            // we should include the default error page
            if (ctx.getFacesContext().isProjectStage(ProjectStage.Development) && ERROR_PAGE_INCLUDE_PATH.equals(path)) {
                url = ClassUtils.getResource(ERROR_FACELET);
            }
            if (markInitialState) {
                // set markInitialState flag
                oldMarkInitialState = fcc.isMarkInitialState();
                fcc.setMarkInitialState(true);
                isBuildingInitialState = (Boolean) ctx.getFacesContext().getAttributes().put(StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
            }
            try {
                if (_params != null) {
                    // ui:include defines a new TemplateContext, but ui:param EL expressions
                    // defined inside should be built before the new context is setup, to
                    // apply then after. The final effect is EL expressions will be resolved
                    // correctly when nested ui:params with the same name or based on other
                    // ui:params are used.
                    String[] names = new String[_params.length];
                    ValueExpression[] values = new ValueExpression[_params.length];
                    for (int i = 0; i < _params.length; i++) {
                        names[i] = _params[i].getName(ctx);
                        values[i] = _params[i].getValue(ctx);
                    }
                    actx.pushTemplateContext(new TemplateContextImpl());
                    for (int i = 0; i < _params.length; i++) {
                        _params[i].apply(ctx, parent, names[i], values[i], uniqueId);
                    }
                } else {
                    actx.pushTemplateContext(new TemplateContextImpl());
                }
                if (url == null) {
                    ctx.includeFacelet(parent, path);
                } else {
                    ctx.includeFacelet(parent, url);
                }
            } finally {
                if (markInitialState) {
                    // 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);
                }
                actx.popTemplateContext();
            }
        } finally {
            ctx.setVariableMapper(orig);
        }
    } finally {
        if (!src.isLiteral()) {
            fcc.endComponentUniqueIdSection();
            if (fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() && !fcc.isRefreshingTransientBuild()) {
                // Mark the parent component to be saved and restored fully.
                ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent);
            }
            if (fcc.isDynamicComponentSection()) {
                ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent);
            }
        }
    }
}
Also used : FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) VariableMapper(jakarta.el.VariableMapper) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext) URL(java.net.URL) ValueExpression(jakarta.el.ValueExpression) TemplateContextImpl(org.apache.myfaces.view.facelets.impl.TemplateContextImpl) VariableMapperWrapper(org.apache.myfaces.view.facelets.el.VariableMapperWrapper)

Example 2 with TemplateContextImpl

use of org.apache.myfaces.view.facelets.impl.TemplateContextImpl in project myfaces by apache.

the class UserTagHandler method apply.

/**
 * Iterate over all TagAttributes and set them on the FaceletContext's VariableMapper, then include the target
 * Facelet. Finally, replace the old VariableMapper.
 *
 * @see TagAttribute#getValueExpression(FaceletContext, Class)
 * @see jakarta.el.VariableMapper
 * @see jakarta.faces.view.facelets.FaceletHandler#apply(jakarta.faces.view.facelets.FaceletContext,
 *        jakarta.faces.component.UIComponent)
 */
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, ELException {
    AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
    // eval include
    try {
        String[] names = null;
        ValueExpression[] values = null;
        if (this._vars.length > 0) {
            names = new String[_vars.length];
            values = new ValueExpression[_vars.length];
            for (int i = 0; i < _vars.length; i++) {
                names[i] = _vars[i].getLocalName();
                values[i] = _vars[i].getValueExpression(ctx, Object.class);
            }
        }
        actx.pushTemplateContext(new TemplateContextImpl());
        actx.pushClient(this);
        FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
        String uniqueId = fcc.startComponentUniqueIdSection();
        try {
            if (this._vars.length > 0) {
                if (ELExpressionCacheMode.alwaysRecompile.equals(actx.getELExpressionCacheMode())) {
                    FaceletState faceletState = ComponentSupport.getFaceletState(ctx, parent, true);
                    for (int i = 0; i < this._vars.length; i++) {
                        faceletState.putBinding(uniqueId, names[i], values[i]);
                        ValueExpression ve = new FaceletStateValueExpression(uniqueId, names[i]);
                        actx.getTemplateContext().setParameter(names[i], ve);
                    }
                } else {
                    for (int i = 0; i < this._vars.length; i++) {
                        ((AbstractFaceletContext) ctx).getTemplateContext().setParameter(names[i], values[i]);
                    }
                }
            }
            // The only mode that can support EL caching in this condition is alwaysRedirect.
            if (!ELExpressionCacheMode.alwaysRecompile.equals(actx.getELExpressionCacheMode())) {
                actx.getTemplateContext().setAllowCacheELExpressions(false);
            }
            ctx.includeFacelet(parent, this._location);
        } finally {
            fcc.endComponentUniqueIdSection();
        }
    } catch (FileNotFoundException e) {
        throw new TagException(this.tag, e.getMessage());
    } finally {
        // make sure we undo our changes
        actx.popClient(this);
        actx.popTemplateContext();
    }
}
Also used : FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) FileNotFoundException(java.io.FileNotFoundException) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext) FaceletStateValueExpression(org.apache.myfaces.view.facelets.el.FaceletStateValueExpression) TagException(jakarta.faces.view.facelets.TagException) ValueExpression(jakarta.el.ValueExpression) FaceletStateValueExpression(org.apache.myfaces.view.facelets.el.FaceletStateValueExpression) TemplateContextImpl(org.apache.myfaces.view.facelets.impl.TemplateContextImpl) FaceletState(org.apache.myfaces.view.facelets.tag.faces.FaceletState)

Aggregations

ValueExpression (jakarta.el.ValueExpression)2 AbstractFaceletContext (org.apache.myfaces.view.facelets.AbstractFaceletContext)2 FaceletCompositionContext (org.apache.myfaces.view.facelets.FaceletCompositionContext)2 TemplateContextImpl (org.apache.myfaces.view.facelets.impl.TemplateContextImpl)2 VariableMapper (jakarta.el.VariableMapper)1 TagException (jakarta.faces.view.facelets.TagException)1 FileNotFoundException (java.io.FileNotFoundException)1 URL (java.net.URL)1 FaceletStateValueExpression (org.apache.myfaces.view.facelets.el.FaceletStateValueExpression)1 VariableMapperWrapper (org.apache.myfaces.view.facelets.el.VariableMapperWrapper)1 FaceletState (org.apache.myfaces.view.facelets.tag.faces.FaceletState)1