Search in sources :

Example 1 with TagException

use of jakarta.faces.view.facelets.TagException in project myfaces by apache.

the class CompositeComponentResourceTagHandler method createComponent.

@Override
public UIComponent createComponent(FaceletContext ctx) {
    FacesContext facesContext = ctx.getFacesContext();
    UIComponent component = facesContext.getApplication().createComponent(facesContext, _resource);
    if (getBinding() != null) {
        ValueExpression bindingVE = getBinding().getValueExpression(ctx, Object.class);
        component.setValueExpression("binding", bindingVE);
        if (!bindingVE.isReadOnly(facesContext.getELContext())) {
            if (PhaseId.RESTORE_VIEW.equals(facesContext.getCurrentPhaseId())) {
                bindingVE.setValue(ctx, component);
            }
            ComponentSupport.getViewRoot(ctx, component).getAttributes().put(NavigationHandlerImpl.CALL_PRE_DISPOSE_VIEW, Boolean.TRUE);
            component.subscribeToEvent(PreDisposeViewEvent.class, new ClearBindingValueExpressionListener());
        }
    }
    // have a viewId.
    if (!facesContext.isProjectStage(ProjectStage.Production)) {
        BeanInfo beanInfo = (BeanInfo) component.getAttributes().get(UIComponent.BEANINFO_KEY);
        for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
            ValueExpression ve = (ValueExpression) propertyDescriptor.getValue("required");
            if (ve != null) {
                Object value = ve.getValue(facesContext.getELContext());
                Boolean required;
                if (value instanceof Boolean) {
                    required = (Boolean) value;
                } else {
                    required = Boolean.valueOf(value.toString());
                }
                if (required != null && required) {
                    Object attrValue = this.tag.getAttributes().get(propertyDescriptor.getName());
                    if (attrValue == null) {
                        throw new TagException(this.tag, "Attribute '" + propertyDescriptor.getName() + "' is required");
                    }
                }
            }
        }
    }
    return component;
}
Also used : FacesContext(jakarta.faces.context.FacesContext) PropertyDescriptor(java.beans.PropertyDescriptor) TagException(jakarta.faces.view.facelets.TagException) ValueExpression(jakarta.el.ValueExpression) BeanInfo(java.beans.BeanInfo) UIComponent(jakarta.faces.component.UIComponent) ClearBindingValueExpressionListener(org.apache.myfaces.view.facelets.tag.faces.ClearBindingValueExpressionListener)

Example 2 with TagException

use of jakarta.faces.view.facelets.TagException in project myfaces by apache.

the class CompositeComponentResourceTagHandler method applyNextHandlerIfNotApplied.

@SuppressWarnings("unchecked")
protected void applyNextHandlerIfNotApplied(FaceletContext ctx, UIComponent c) throws IOException {
    // Apply all facelets not applied yet.
    CompositeComponentBeanInfo beanInfo = (CompositeComponentBeanInfo) c.getAttributes().get(UIComponent.BEANINFO_KEY);
    BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
    boolean insertChildrenUsed = (beanDescriptor.getValue(InsertChildrenHandler.INSERT_CHILDREN_USED) != null);
    List<String> insertFacetList = (List<String>) beanDescriptor.getValue(InsertFacetHandler.INSERT_FACET_USED);
    if (nextHandler instanceof jakarta.faces.view.facelets.CompositeFaceletHandler) {
        for (FaceletHandler handler : ((jakarta.faces.view.facelets.CompositeFaceletHandler) nextHandler).getHandlers()) {
            if (handler instanceof jakarta.faces.view.facelets.FacetHandler) {
                if (insertFacetList == null || !insertFacetList.contains(((jakarta.faces.view.facelets.FacetHandler) handler).getFacetName(ctx))) {
                    handler.apply(ctx, c);
                }
            } else if (handler instanceof InsertFacetHandler) {
                if (insertFacetList == null || !insertFacetList.contains(((InsertFacetHandler) handler).getFacetName(ctx))) {
                    handler.apply(ctx, c);
                }
            } else if (insertChildrenUsed) {
                if (!(handler instanceof jakarta.faces.view.facelets.ComponentHandler || handler instanceof ComponentContainerHandler || handler instanceof TextHandler)) {
                    handler.apply(ctx, c);
                }
            } else {
                handler.apply(ctx, c);
            }
        }
    } else {
        if (nextHandler instanceof jakarta.faces.view.facelets.FacetHandler) {
            if (insertFacetList == null || !insertFacetList.contains(((jakarta.faces.view.facelets.FacetHandler) nextHandler).getFacetName(ctx))) {
                nextHandler.apply(ctx, c);
            }
        } else if (nextHandler instanceof InsertFacetHandler) {
            if (insertFacetList == null || !insertFacetList.contains(((InsertFacetHandler) nextHandler).getFacetName(ctx))) {
                nextHandler.apply(ctx, c);
            }
        } else if (insertChildrenUsed) {
            if (!(nextHandler instanceof jakarta.faces.view.facelets.ComponentHandler || nextHandler instanceof ComponentContainerHandler || nextHandler instanceof TextHandler)) {
                nextHandler.apply(ctx, c);
            }
        } else {
            nextHandler.apply(ctx, c);
        }
    }
    // Check for required facets
    Map<String, PropertyDescriptor> facetPropertyDescriptorMap = (Map<String, PropertyDescriptor>) beanDescriptor.getValue(UIComponent.FACETS_KEY);
    if (facetPropertyDescriptorMap != null) {
        List<String> facetsRequiredNotFound = null;
        for (Map.Entry<String, PropertyDescriptor> entry : facetPropertyDescriptorMap.entrySet()) {
            ValueExpression requiredExpr = (ValueExpression) entry.getValue().getValue("required");
            if (requiredExpr != null) {
                Boolean required = (Boolean) requiredExpr.getValue(ctx.getFacesContext().getELContext());
                if (Boolean.TRUE.equals(required)) {
                    initFacetHandlersMap(ctx);
                    if (!_facetHandlersMap.containsKey(entry.getKey())) {
                        if (facetsRequiredNotFound == null) {
                            facetsRequiredNotFound = new ArrayList(facetPropertyDescriptorMap.size());
                        }
                        facetsRequiredNotFound.add(entry.getKey());
                    }
                }
            }
        }
        if (facetsRequiredNotFound != null && !facetsRequiredNotFound.isEmpty()) {
            throw new TagException(getTag(), "The following facets are required by the component: " + facetsRequiredNotFound);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) FaceletHandler(jakarta.faces.view.facelets.FaceletHandler) ComponentContainerHandler(org.apache.myfaces.view.facelets.tag.ComponentContainerHandler) TextHandler(jakarta.faces.view.facelets.TextHandler) List(java.util.List) ArrayList(java.util.ArrayList) PropertyDescriptor(java.beans.PropertyDescriptor) ComponentHandler(jakarta.faces.view.facelets.ComponentHandler) BeanDescriptor(java.beans.BeanDescriptor) TagException(jakarta.faces.view.facelets.TagException) ValueExpression(jakarta.el.ValueExpression) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with TagException

use of jakarta.faces.view.facelets.TagException in project myfaces by apache.

the class CompositeMetaRulesetImpl method _getBaseMetadataTarget.

private MetadataTarget _getBaseMetadataTarget() {
    Map<String, MetadataTarget> metadata = getMetaData();
    String key = _type.getName();
    MetadataTarget meta = metadata.get(key);
    if (meta == null) {
        try {
            if (PropertyDescriptorUtils.isUseLambdaMetafactory(FacesContext.getCurrentInstance().getExternalContext())) {
                meta = new LambdaMetadataTargetImpl(_type);
            } else {
                meta = new MetadataTargetImpl(_type);
            }
        } catch (IntrospectionException e) {
            throw new TagException(_tag, "Error Creating TargetMetadata", e);
        }
        metadata.put(key, meta);
    }
    return meta;
}
Also used : LambdaMetadataTargetImpl(org.apache.myfaces.view.facelets.tag.LambdaMetadataTargetImpl) MetadataTarget(jakarta.faces.view.facelets.MetadataTarget) TagException(jakarta.faces.view.facelets.TagException) IntrospectionException(java.beans.IntrospectionException) MetadataTargetImpl(org.apache.myfaces.view.facelets.tag.MetadataTargetImpl) LambdaMetadataTargetImpl(org.apache.myfaces.view.facelets.tag.LambdaMetadataTargetImpl)

Example 4 with TagException

use of jakarta.faces.view.facelets.TagException in project myfaces by apache.

the class ComponentTagHandlerDelegate method apply.

/**
 * Method handles UIComponent tree creation in accordance with the JSF 1.2 spec.
 * <ol>
 * <li>First determines this UIComponent's id by calling {@link #getId(FaceletContext) getId(FaceletContext)}.</li>
 * <li>Search the parent for an existing UIComponent of the id we just grabbed</li>
 * <li>If found, {@link FaceletCompositionContext#markForDeletion(UIComponent) mark} its children for deletion.</li>
 * <li>If <i>not</i> found, call {@link #createComponent(FaceletContext) createComponent}.
 * <ol>
 * <li>Only here do we apply TagHandler#setAttributes(FaceletCompositionContext, Object)</li>
 * <li>Set the UIComponent's id</li>
 * <li>Set the RendererType of this instance</li>
 * </ol>
 * </li>
 * <li>Now apply the nextHandler, passing the UIComponent we've created/found.</li>
 * <li>Now add the UIComponent to the passed parent</li>
 * <li>Lastly, if the UIComponent already existed (found),
 * then #finalizeForDeletion(FaceletCompositionContext, UIComponent)
 * for deletion.</li>
 * </ol>
 *
 * See jakarta.faces.view.facelets.FaceletHandler#apply(jakarta.faces.view.facelets.FaceletContext,
 * jakarta.faces.component.UIComponent)
 *
 * @throws TagException
 *             if the UIComponent parent is null
 */
@SuppressWarnings("unchecked")
@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
    // make sure our parent is not null
    if (parent == null) {
        throw new TagException(_delegate.getTag(), "Parent UIComponent was null");
    }
    FacesContext facesContext = ctx.getFacesContext();
    // possible facet scoped
    String facetName = this.getFacetName(ctx, parent);
    // our id
    String id = ctx.generateUniqueId(_delegate.getTagId());
    // Cast to use UniqueIdVendor stuff
    FaceletCompositionContext mctx = (FaceletCompositionContext) FaceletCompositionContext.getCurrentInstance(ctx);
    // grab our component
    UIComponent c = null;
    // Used to preserve the original parent. Note when the view is being refreshed, the real parent could be
    // another component.
    UIComponent oldParent = parent;
    if (mctx.isRefreshingSection()) {
        if (_relocatableResourceHandler != null) {
            c = _relocatableResourceHandler.findChildByTagId(ctx, parent, id);
        } else {
            if (facetName != null) {
                c = ComponentSupport.findChildInFacetByTagId(parent, id, facetName);
            } else {
                c = ComponentSupport.findChildInChildrenByTagId(parent, id);
            }
        }
    }
    boolean componentFound = false;
    if (c != null) {
        componentFound = true;
        // preserve the same context
        if (_delegate.getBinding() != null && c.getAttributes().containsKey(FaceletDynamicComponentRefreshTransientBuildEvent.DYNAMIC_COMPONENT_BINDING_NEEDS_REFRESH)) {
            VisitContext visitContext = (VisitContext) mctx.getVisitContextFactory().getVisitContext(facesContext, null, MyFacesVisitHints.SET_SKIP_ITERATION);
            c.visitTree(visitContext, PublishFaceletDynamicComponentRefreshTransientBuildCallback.INSTANCE);
        }
        mctx.incrementUniqueComponentId();
        // mark all children for cleaning
        if (log.isLoggable(Level.FINE)) {
            log.fine(_delegate.getTag() + " Component[" + id + "] Found, marking children for cleanup");
        }
        // The call for mctx.markForDeletion(c) is always necessary, because
        // component resource relocation occur as an effect of PostAddToViewEvent,
        // so at this point it is unknown if the component was relocated or not.
        mctx.markForDeletion(c);
        if (_relocatableResourceHandler != null) {
            mctx.markRelocatableResourceForDeletion(c);
        }
    } else {
        c = this.createComponent(ctx);
        if (log.isLoggable(Level.FINE)) {
            log.fine(_delegate.getTag() + " Component[" + id + "] Created: " + c.getClass().getName());
        }
        _delegate.setAttributes(ctx, c);
        // mark it owned by a facelet instance
        c.getAttributes().put(ComponentSupport.MARK_CREATED, id);
        if (facesContext.isProjectStage(ProjectStage.Development)) {
            c.getAttributes().put(UIComponent.VIEW_LOCATION_KEY, _delegate.getTag().getLocation());
        }
        // assign our unique id
        if (this._id != null) {
            mctx.incrementUniqueComponentId();
            c.setId(this._id.getValue(ctx));
        } else {
            String componentId = mctx.generateUniqueComponentId();
            UniqueIdVendor uniqueIdVendor = mctx.getUniqueIdVendorFromStack();
            if (uniqueIdVendor == null) {
                uniqueIdVendor = facesContext.getViewRoot();
                if (uniqueIdVendor == null) {
                    // facesContext.getViewRoot() returns null here if we are in
                    // phase restore view, so we have to try to get the view root
                    // via the method in ComponentSupport and our parent
                    uniqueIdVendor = ComponentSupport.getViewRoot(ctx, parent);
                }
            }
            if (uniqueIdVendor != null) {
                // UIViewRoot implements UniqueIdVendor, so there is no need to cast to UIViewRoot
                // and call createUniqueId()
                String uid = uniqueIdVendor.createUniqueId(facesContext, componentId);
                c.setId(uid);
            }
        }
        if (this._rendererType != null) {
            c.setRendererType(this._rendererType);
        }
        // hook method
        _delegate.onComponentCreated(ctx, c, parent);
        if (_relocatableResourceHandler != null && _relocatableResourceHandler instanceof ComponentRelocatableResourceHandler) {
            UIComponent parentCompositeComponent = mctx.getCompositeComponentFromStack();
            if (parentCompositeComponent != null) {
                c.getAttributes().put(CompositeComponentELUtils.LOCATION_KEY, parentCompositeComponent.getAttributes().get(CompositeComponentELUtils.LOCATION_KEY));
            }
        }
        if (mctx.isRefreshingTransientBuild() && _relocatableResourceHandler != null) {
            mctx.markRelocatableResourceForDeletion(c);
        }
    }
    c.pushComponentToEL(facesContext, c);
    if (c instanceof UniqueIdVendor) {
        mctx.pushUniqueIdVendorToStack((UniqueIdVendor) c);
    }
    if (mctx.isDynamicComponentTopLevel()) {
        mctx.setDynamicComponentTopLevel(false);
        _delegate.applyNextHandler(ctx, c);
        mctx.setDynamicComponentTopLevel(true);
    } else {
        // first allow c to get populated
        _delegate.applyNextHandler(ctx, c);
    }
    boolean oldProcessingEvents = facesContext.isProcessingEvents();
    // finish cleaning up orphaned children
    if (componentFound && !mctx.isDynamicComponentTopLevel()) {
        mctx.finalizeForDeletion(c);
        if (mctx.isRefreshingSection()) {
            facesContext.setProcessingEvents(false);
            if (_relocatableResourceHandler != null && parent != null && !parent.equals(c.getParent())) {
                // Replace parent with the relocated parent.
                parent = c.getParent();
                // Since we changed the parent, the facetName becomes invalid, because it points
                // to the component before relocation. We need to find the right facetName (if any) so we can
                // refresh the component properly.
                UIComponent c1 = ComponentSupport.findChildInChildrenByTagId(parent, id);
                if (c1 == null) {
                    facetName = ComponentSupport.findChildInFacetsByTagId(parent, id);
                } else {
                    facetName = null;
                }
            }
            ComponentSupport.setCachedFacesContext(c, facesContext);
        }
        if (facetName == null) {
            parent.getChildren().remove(c);
        } else {
            ComponentSupport.removeFacet(ctx, parent, c, facetName);
        }
        if (mctx.isRefreshingSection()) {
            ComponentSupport.setCachedFacesContext(c, null);
            facesContext.setProcessingEvents(oldProcessingEvents);
        }
    }
    if (!componentFound) {
        if (c instanceof ClientBehaviorHolder && !UIComponent.isCompositeComponent(c)) {
            Iterator<AjaxHandler> it = ((AbstractFaceletContext) ctx).getAjaxHandlers();
            if (it != null) {
                while (it.hasNext()) {
                    it.next().applyAttachedObject(facesContext, c);
                }
            }
        }
        if (c instanceof EditableValueHolder) {
            // add default validators here, because this feature
            // is only available in facelets (see MYFACES-2362 for details)
            addEnclosingAndDefaultValidators(ctx, mctx, facesContext, (EditableValueHolder) c);
        }
    }
    _delegate.onComponentPopulated(ctx, c, oldParent);
    if (!mctx.isDynamicComponentTopLevel() || !componentFound) {
        if (componentFound && mctx.isRefreshingSection()) {
            facesContext.setProcessingEvents(false);
            ComponentSupport.setCachedFacesContext(c, facesContext);
        }
        if (facetName == null) {
            parent.getChildren().add(c);
        } else {
            ComponentSupport.addFacet(ctx, parent, c, facetName);
        }
        if (componentFound && mctx.isRefreshingSection()) {
            ComponentSupport.setCachedFacesContext(c, null);
            facesContext.setProcessingEvents(oldProcessingEvents);
        }
    }
    if (c instanceof UniqueIdVendor) {
        mctx.popUniqueIdVendorToStack();
    }
    c.popComponentFromEL(facesContext);
    if (mctx.isMarkInitialState()) {
        // Call it only if we are using partial state saving
        c.markInitialState();
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) VisitContext(jakarta.faces.component.visit.VisitContext) UIComponent(jakarta.faces.component.UIComponent) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) UniqueIdVendor(jakarta.faces.component.UniqueIdVendor) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext) AjaxHandler(org.apache.myfaces.view.facelets.tag.faces.core.AjaxHandler) TagException(jakarta.faces.view.facelets.TagException) EditableValueHolder(jakarta.faces.component.EditableValueHolder)

Example 5 with TagException

use of jakarta.faces.view.facelets.TagException in project myfaces by apache.

the class RenderFacetHandler method onComponentPopulated.

@Override
public void onComponentPopulated(FaceletContext ctx, UIComponent c, UIComponent parent) {
    if (!((AbstractFaceletContext) ctx).isBuildingCompositeComponentMetadata()) {
        UIComponent parentCompositeComponent = FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack();
        String facetName = _name.getValue(ctx);
        if (_required != null && _required.getBoolean(ctx) && parentCompositeComponent.getFacet(facetName) == null) {
            throw new TagException(this.tag, "Cannot find facet with name '" + facetName + "' in composite component");
        }
        c.getAttributes().put(UIComponent.FACETS_KEY, facetName);
    }
}
Also used : TagException(jakarta.faces.view.facelets.TagException) UIComponent(jakarta.faces.component.UIComponent)

Aggregations

TagException (jakarta.faces.view.facelets.TagException)45 ValueExpression (jakarta.el.ValueExpression)24 UIComponent (jakarta.faces.component.UIComponent)8 BeanDescriptor (java.beans.BeanDescriptor)8 IntrospectionException (java.beans.IntrospectionException)8 PropertyDescriptor (java.beans.PropertyDescriptor)8 AbstractFaceletContext (org.apache.myfaces.view.facelets.AbstractFaceletContext)8 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)7 FaceletCompositionContext (org.apache.myfaces.view.facelets.FaceletCompositionContext)7 FaceletContext (jakarta.faces.view.facelets.FaceletContext)6 Map (java.util.Map)6 EditableValueHolder (jakarta.faces.component.EditableValueHolder)5 Tag (jakarta.faces.view.facelets.Tag)5 BeanInfo (java.beans.BeanInfo)5 HashMap (java.util.HashMap)5 List (java.util.List)5 ValueHolder (jakarta.faces.component.ValueHolder)4 FacesContext (jakarta.faces.context.FacesContext)4 ArrayList (java.util.ArrayList)4 ELException (jakarta.el.ELException)3