Search in sources :

Example 1 with TextHandler

use of jakarta.faces.view.facelets.TextHandler 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 2 with TextHandler

use of jakarta.faces.view.facelets.TextHandler in project mojarra by eclipse-ee4j.

the class SetHandler method apply.

@Override
public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
    StringBuilder bodyValue = new StringBuilder();
    Iterator iter = TagHandlerImpl.findNextByType(nextHandler, TextHandler.class);
    while (iter.hasNext()) {
        TextHandler text = (TextHandler) iter.next();
        bodyValue.append(text.getText(ctx));
    }
    // true if either a value in body or value attr
    boolean valSet = bodyValue.length() > 0 || value != null && value.getValue().length() > 0;
    // Apply precedence algorithm for attributes. The JstlCoreTLV doesn't
    // seem to enforce much in the way of this, so I edburns needs to check
    // with an authority on the matter, probabyl Kin-Man Chung
    ValueExpression veObj;
    ValueExpression lhs;
    String expr;
    if (value != null) {
        veObj = value.getValueExpression(ctx, Object.class);
    } else {
        veObj = ctx.getExpressionFactory().createValueExpression(ctx.getFacesContext().getELContext(), bodyValue.toString(), Object.class);
    }
    // Otherwise, if var is set, ignore the other attributes
    if (var != null) {
        String scopeStr, varStr = var.getValue(ctx);
        // If scope is set, check for validity
        if (null != scope) {
            if (0 == scope.getValue().length()) {
                throw new TagException(tag, "zero length scope attribute set");
            }
            if (scope.isLiteral()) {
                scopeStr = scope.getValue();
            } else {
                scopeStr = scope.getValue(ctx);
            }
            if (scopeStr.equals("page")) {
                throw new TagException(tag, "page scope does not exist in Faces, consider using view scope instead.");
            }
            if (scopeStr.equals("request") || scopeStr.equals("session") || scopeStr.equals("application") || scopeStr.equals("view")) {
                scopeStr = scopeStr + "Scope";
            }
            // otherwise, assume it's a valid scope. With custom scopes,
            // it may be.
            // Conjure up an expression
            expr = "#{" + scopeStr + "." + varStr + "}";
            lhs = ctx.getExpressionFactory().createValueExpression(ctx, expr, Object.class);
            lhs.setValue(ctx, veObj.getValue(ctx));
        } else {
            ctx.getVariableMapper().setVariable(varStr, veObj);
        }
    } else {
        // Otherwise, target, property and value must be set
        if (null == target || null == target.getValue() || target.getValue().length() <= 0 || null == property || null == property.getValue() || property.getValue().length() <= 0 || !valSet) {
            throw new TagException(tag, "when using this tag either one of var and value, or (target, property, value) must be set.");
        }
        // Ensure that target is an expression
        if (target.isLiteral()) {
            throw new TagException(tag, "value of target attribute must be an expression");
        }
        // Get the value of property
        String propertyStr = null;
        if (property.isLiteral()) {
            propertyStr = property.getValue();
        } else {
            propertyStr = property.getValue(ctx);
        }
        ValueExpression targetVe = target.getValueExpression(ctx, Object.class);
        Object targetValue = targetVe.getValue(ctx);
        ctx.getFacesContext().getELContext().getELResolver().setValue(ctx, targetValue, propertyStr, veObj.getValue(ctx));
    }
}
Also used : TagException(jakarta.faces.view.facelets.TagException) ValueExpression(jakarta.el.ValueExpression) Iterator(java.util.Iterator) TextHandler(jakarta.faces.view.facelets.TextHandler)

Aggregations

ValueExpression (jakarta.el.ValueExpression)2 TagException (jakarta.faces.view.facelets.TagException)2 TextHandler (jakarta.faces.view.facelets.TextHandler)2 ComponentHandler (jakarta.faces.view.facelets.ComponentHandler)1 FaceletHandler (jakarta.faces.view.facelets.FaceletHandler)1 BeanDescriptor (java.beans.BeanDescriptor)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 ComponentContainerHandler (org.apache.myfaces.view.facelets.tag.ComponentContainerHandler)1