Search in sources :

Example 1 with VariableMapperWrapper

use of org.apache.myfaces.view.facelets.el.VariableMapperWrapper in project myfaces by apache.

the class FaceletViewDeclarationLanguage method getComponentMetadata.

/**
 * retargetMethodExpressions(FacesContext, UIComponent) has some clues about the behavior of this method
 *
 * {@inheritDoc}
 */
@Override
public BeanInfo getComponentMetadata(FacesContext context, Resource componentResource) {
    BeanInfo beanInfo = null;
    Assert.notNull(context, "context");
    try {
        Facelet compositeComponentFacelet;
        FaceletFactory.setInstance(faceletFactory);
        try {
            compositeComponentFacelet = faceletFactory.getCompositeComponentMetadataFacelet(componentResource.getURL());
        } finally {
            FaceletFactory.setInstance(null);
        }
        // context.getAttributes().put(BUILDING_COMPOSITE_COMPONENT_METADATA, Boolean.TRUE);
        // Create a temporal tree where all components will be put, but we are only
        // interested in metadata.
        UINamingContainer compositeComponentBase = (UINamingContainer) context.getApplication().createComponent(context, UINamingContainer.COMPONENT_TYPE, null);
        // Fill the component resource key, because this information should be available
        // on metadata to recognize which is the component used as composite component base.
        // Since this method is called from Application.createComponent(FacesContext,Resource),
        // and in that specific method this key is updated, this is the best option we
        // have for recognize it (also this key is used by UIComponent.isCompositeComponent)
        compositeComponentBase.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);
        // According to UserTagHandler, in this point we need to wrap the facelet
        // VariableMapper, so local changes are applied on "page context", but
        // data is retrieved from full context
        FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
        VariableMapper orig = faceletContext.getVariableMapper();
        try {
            faceletContext.setVariableMapper(new VariableMapperWrapper(orig));
            compositeComponentBase.pushComponentToEL(context, compositeComponentBase);
            compositeComponentFacelet.apply(context, compositeComponentBase);
            compositeComponentBase.popComponentFromEL(context);
        } finally {
            faceletContext.setVariableMapper(orig);
        }
        beanInfo = (BeanInfo) compositeComponentBase.getAttributes().get(UIComponent.BEANINFO_KEY);
    } catch (IOException e) {
        throw new FacesException(e);
    }
    return beanInfo;
}
Also used : Facelet(jakarta.faces.view.facelets.Facelet) VariableMapper(jakarta.el.VariableMapper) BeanInfo(java.beans.BeanInfo) VariableMapperWrapper(org.apache.myfaces.view.facelets.el.VariableMapperWrapper) FaceletContext(jakarta.faces.view.facelets.FaceletContext) UINamingContainer(jakarta.faces.component.UINamingContainer) IOException(java.io.IOException) FacesException(jakarta.faces.FacesException)

Example 2 with VariableMapperWrapper

use of org.apache.myfaces.view.facelets.el.VariableMapperWrapper 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 3 with VariableMapperWrapper

use of org.apache.myfaces.view.facelets.el.VariableMapperWrapper in project myfaces by apache.

the class LegacyCompositionHandler 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 {
    if (_template != null) {
        VariableMapper orig = ctx.getVariableMapper();
        AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
        actx.extendClient(this);
        if (_params != null) {
            VariableMapper vm = new VariableMapperWrapper(orig);
            ctx.setVariableMapper(vm);
            for (int i = 0; i < _params.length; i++) {
                _params[i].apply(ctx, parent);
            }
        }
        try {
            ctx.includeFacelet(parent, _template.getValue(ctx));
        } finally {
            actx.popExtendedClient(this);
            ctx.setVariableMapper(orig);
        }
    } else {
        this.nextHandler.apply(ctx, parent);
    }
}
Also used : VariableMapper(jakarta.el.VariableMapper) VariableMapperWrapper(org.apache.myfaces.view.facelets.el.VariableMapperWrapper) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext)

Example 4 with VariableMapperWrapper

use of org.apache.myfaces.view.facelets.el.VariableMapperWrapper in project myfaces by apache.

the class LegacyDecorateHandler 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 {
    VariableMapper orig = ctx.getVariableMapper();
    AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
    actx.pushClient(this);
    if (_params != null) {
        VariableMapper vm = new VariableMapperWrapper(orig);
        ctx.setVariableMapper(vm);
        for (int i = 0; i < _params.length; i++) {
            _params[i].apply(ctx, parent);
        }
    }
    FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
    String path;
    boolean markInitialState = false;
    if (!_template.isLiteral()) {
        String uniqueId = actx.generateUniqueFaceletTagId(fcc.startComponentUniqueIdSection(), tagId);
        // path = getTemplateValue(actx, fcc, parent, uniqueId);
        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._template.getValue(ctx);
                if (StringUtils.isBlank(path)) {
                    return;
                }
                if (!path.equals(restoredPath)) {
                    markInitialState = true;
                }
            } else {
                path = restoredPath;
            }
        } else {
            // No state restored, calculate path
            path = this._template.getValue(ctx);
        }
        ComponentSupport.saveInitialTagState(ctx, fcc, parent, uniqueId, path);
    } else {
        path = _template.getValue(ctx);
    }
    try {
        try {
            boolean oldMarkInitialState = false;
            Boolean isBuildingInitialState = null;
            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 {
                ctx.includeFacelet(parent, path);
            } 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);
                }
            }
        } finally {
            ctx.setVariableMapper(orig);
            actx.popClient(this);
        }
    } finally {
        if (!_template.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) VariableMapperWrapper(org.apache.myfaces.view.facelets.el.VariableMapperWrapper) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext)

Example 5 with VariableMapperWrapper

use of org.apache.myfaces.view.facelets.el.VariableMapperWrapper in project myfaces by apache.

the class LegacyUserTagHandler 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 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 {
    VariableMapper orig = ctx.getVariableMapper();
    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.pushClient(this);
        // setup a variable map
        if (this._vars.length > 0) {
            VariableMapper varMapper = new VariableMapperWrapper(orig);
            for (int i = 0; i < this._vars.length; i++) {
                varMapper.setVariable(names[i], values[i]);
            }
            ctx.setVariableMapper(varMapper);
        }
        actx.getTemplateContext().setAllowCacheELExpressions(false);
        ctx.includeFacelet(parent, this._location);
    } catch (FileNotFoundException e) {
        throw new TagException(this.tag, e.getMessage());
    } finally {
        // make sure we undo our changes
        actx.popClient(this);
        ctx.setVariableMapper(orig);
    }
}
Also used : TagException(jakarta.faces.view.facelets.TagException) VariableMapper(jakarta.el.VariableMapper) ValueExpression(jakarta.el.ValueExpression) FileNotFoundException(java.io.FileNotFoundException) VariableMapperWrapper(org.apache.myfaces.view.facelets.el.VariableMapperWrapper) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext)

Aggregations

VariableMapper (jakarta.el.VariableMapper)7 VariableMapperWrapper (org.apache.myfaces.view.facelets.el.VariableMapperWrapper)7 AbstractFaceletContext (org.apache.myfaces.view.facelets.AbstractFaceletContext)6 FaceletCompositionContext (org.apache.myfaces.view.facelets.FaceletCompositionContext)4 ValueExpression (jakarta.el.ValueExpression)3 TagException (jakarta.faces.view.facelets.TagException)2 URL (java.net.URL)2 FacesException (jakarta.faces.FacesException)1 Resource (jakarta.faces.application.Resource)1 UINamingContainer (jakarta.faces.component.UINamingContainer)1 UIPanel (jakarta.faces.component.UIPanel)1 UniqueIdVendor (jakarta.faces.component.UniqueIdVendor)1 Facelet (jakarta.faces.view.facelets.Facelet)1 FaceletContext (jakarta.faces.view.facelets.FaceletContext)1 BeanInfo (java.beans.BeanInfo)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 TemplateContextImpl (org.apache.myfaces.view.facelets.impl.TemplateContextImpl)1 AjaxHandler (org.apache.myfaces.view.facelets.tag.faces.core.AjaxHandler)1