Search in sources :

Example 1 with TagAttributeException

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

the class AjaxHandler method applyAttachedObject.

/**
 * This method should create an AjaxBehavior object and attach it to the
 * parent component.
 *
 * Also, it should check if the parent can apply the selected AjaxBehavior
 * to the selected component through ClientBehaviorHolder.getEventNames() or
 * ClientBehaviorHolder.getDefaultEventName()
 */
@Override
public void applyAttachedObject(FacesContext context, UIComponent parent) {
    // Retrieve the current FaceletContext from FacesContext object
    FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
    // cast to a ClientBehaviorHolder
    ClientBehaviorHolder cvh = (ClientBehaviorHolder) parent;
    String eventName = null;
    if (_event != null) {
        if (_event.isLiteral()) {
            eventName = _event.getValue();
        } else {
            eventName = (String) _event.getValueExpression(faceletContext, String.class).getValue(faceletContext);
        }
    }
    if (eventName == null) {
        eventName = cvh.getDefaultEventName();
        if (eventName == null) {
            if (_wrapMode) {
                // for this attached object.
                return;
            } else {
                throw new TagAttributeException(_event, "eventName could not be defined for f:ajax tag with no wrap mode.");
            }
        }
    } else if (!cvh.getEventNames().contains(eventName)) {
        if (_wrapMode) {
            // component could not be the target for this attached object.
            return;
        } else {
            throw new TagAttributeException(_event, "event it is not a valid eventName defined for this component");
        }
    }
    Map<String, List<ClientBehavior>> clientBehaviors = cvh.getClientBehaviors();
    List<ClientBehavior> clientBehaviorList = clientBehaviors.get(eventName);
    if (clientBehaviorList != null && !clientBehaviorList.isEmpty()) {
        for (ClientBehavior cb : clientBehaviorList) {
            if (cb instanceof AjaxBehavior) {
                // the outer one.
                return;
            }
        }
    }
    AjaxBehavior ajaxBehavior = createBehavior(context);
    if (_disabled != null) {
        if (_disabled.isLiteral()) {
            ajaxBehavior.setDisabled(_disabled.getBoolean(faceletContext));
        } else {
            ajaxBehavior.setValueExpression("disabled", _disabled.getValueExpression(faceletContext, Boolean.class));
        }
    }
    if (_execute != null) {
        ajaxBehavior.setValueExpression("execute", _execute.getValueExpression(faceletContext, Object.class));
    }
    if (_immediate != null) {
        if (_immediate.isLiteral()) {
            ajaxBehavior.setImmediate(_immediate.getBoolean(faceletContext));
        } else {
            ajaxBehavior.setValueExpression("immediate", _immediate.getValueExpression(faceletContext, Boolean.class));
        }
    }
    if (_listener != null) {
        MethodExpression expr = _listener.getMethodExpression(faceletContext, Void.TYPE, AJAX_BEHAVIOR_LISTENER_SIG);
        AjaxBehaviorListener abl = new AjaxBehaviorListenerImpl(expr);
        ajaxBehavior.addAjaxBehaviorListener(abl);
    }
    if (_onerror != null) {
        if (_onerror.isLiteral()) {
            ajaxBehavior.setOnerror(_onerror.getValue(faceletContext));
        } else {
            ajaxBehavior.setValueExpression("onerror", _onerror.getValueExpression(faceletContext, String.class));
        }
    }
    if (_onevent != null) {
        if (_onevent.isLiteral()) {
            ajaxBehavior.setOnevent(_onevent.getValue(faceletContext));
        } else {
            ajaxBehavior.setValueExpression("onevent", _onevent.getValueExpression(faceletContext, String.class));
        }
    }
    if (_render != null) {
        ajaxBehavior.setValueExpression("render", _render.getValueExpression(faceletContext, Object.class));
    }
    if (_delay != null) {
        if (_delay.isLiteral()) {
            ajaxBehavior.setDelay(_delay.getValue(faceletContext));
        } else {
            ajaxBehavior.setValueExpression("delay", _delay.getValueExpression(faceletContext, String.class));
        }
    }
    if (_resetValues != null) {
        if (_resetValues.isLiteral()) {
            ajaxBehavior.setResetValues(_resetValues.getBoolean(faceletContext));
        } else {
            ajaxBehavior.setValueExpression("resetValues", _resetValues.getValueExpression(faceletContext, Boolean.class));
        }
    }
    // map @this in a composite to @composite
    if (parent instanceof ClientBehaviorRedirectEventComponentWrapper) {
        UIComponent composite = ((ClientBehaviorRedirectEventComponentWrapper) parent).getComposite();
        if (composite != null) {
            Collection<String> execute = ajaxBehavior.getExecute();
            if (execute != null && execute.contains("@this")) {
                Collection<String> newExecute = new ArrayList<>(execute);
                newExecute.remove("@this");
                newExecute.add("@composite");
                ajaxBehavior.setExecute(newExecute);
            }
            Collection<String> render = ajaxBehavior.getRender();
            if (render != null && render.contains("@this")) {
                Collection<String> newRender = new ArrayList<>(render);
                newRender.remove("@this");
                newRender.add("@composite");
                ajaxBehavior.setRender(newRender);
            }
        }
    }
    cvh.addClientBehavior(eventName, ajaxBehavior);
}
Also used : TagAttributeException(jakarta.faces.view.facelets.TagAttributeException) AjaxBehaviorListener(jakarta.faces.event.AjaxBehaviorListener) UIComponent(jakarta.faces.component.UIComponent) ArrayList(java.util.ArrayList) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) MethodExpression(jakarta.el.MethodExpression) AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior) ClientBehaviorRedirectEventComponentWrapper(org.apache.myfaces.view.facelets.tag.composite.ClientBehaviorRedirectEventComponentWrapper) FaceletContext(jakarta.faces.view.facelets.FaceletContext) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext) ArrayList(java.util.ArrayList) List(java.util.List) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior)

Example 2 with TagAttributeException

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

the class EventHandler method getEventClass.

/**
 * Gets the event class defined by the tag (either in the "name" or "type" attribute).
 *
 * @param context the Facelet context
 * @return a Class containing the event class defined by the tag.
 */
@SuppressWarnings("unchecked")
private Class<? extends ComponentSystemEvent> getEventClass(FaceletContext context) {
    Class<?> eventClass = null;
    String value = null;
    if (type.isLiteral() && eventClassLiteral != null) {
        // if type is literal it does not change, avoid Reflection and use cached value
        return (Class<? extends ComponentSystemEvent>) eventClassLiteral;
    }
    if (type.isLiteral()) {
        value = type.getValue();
    } else {
        value = (String) type.getValueExpression(context, String.class).getValue(context.getFacesContext().getELContext());
    }
    Collection<Class<? extends ComponentSystemEvent>> events;
    // We can look up the event class by name in the NamedEventManager.
    events = RuntimeConfig.getCurrentInstance(context.getFacesContext().getExternalContext()).getNamedEventManager().getNamedEvent(value);
    if (events == null) {
        try {
            eventClass = ClassUtils.forName(value);
            if (type.isLiteral()) {
                eventClassLiteral = eventClass;
            }
        } catch (Throwable e) {
            throw new TagAttributeException(type, "Couldn't create event class", e);
        }
    } else if (events.size() > 1) {
        StringBuilder classNames = new StringBuilder("[");
        Iterator<Class<? extends ComponentSystemEvent>> eventIterator = events.iterator();
        while (eventIterator.hasNext()) {
            classNames.append(eventIterator.next().getName());
            if (eventIterator.hasNext()) {
                classNames.append(", ");
            } else {
                classNames.append(']');
            }
        }
        throw new FacesException("The event name '" + value + "' is mapped to more than one " + " event class: " + classNames.toString());
    } else {
        eventClass = events.iterator().next();
    }
    if (!ComponentSystemEvent.class.isAssignableFrom(eventClass)) {
        throw new TagAttributeException(type, "Event class " + eventClass.getName() + " is not of type jakarta.faces.event.ComponentSystemEvent");
    }
    return (Class<? extends ComponentSystemEvent>) eventClass;
}
Also used : TagAttributeException(jakarta.faces.view.facelets.TagAttributeException) ComponentSystemEvent(jakarta.faces.event.ComponentSystemEvent) Iterator(java.util.Iterator) FacesException(jakarta.faces.FacesException)

Example 3 with TagAttributeException

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

the class CompilationManager method determineQName.

private String[] determineQName(Tag tag) {
    TagAttribute attr = tag.getAttributes().get("jsfc");
    if (attr != null) {
        if (log.isLoggable(Level.FINE)) {
            log.fine(attr + " JSF Facelet Compile Directive Found");
        }
        String value = attr.getValue();
        String namespace;
        String localName;
        int c = value.indexOf(':');
        if (c == -1) {
            namespace = this.namespaceManager.getNamespace("");
            localName = value;
        } else {
            String prefix = value.substring(0, c);
            namespace = this.namespaceManager.getNamespace(prefix);
            if (namespace == null) {
                throw new TagAttributeException(tag, attr, "No Namespace matched for: " + prefix);
            }
            localName = value.substring(c + 1);
        }
        return new String[] { namespace, localName };
    } else {
        return new String[] { tag.getNamespace(), tag.getLocalName() };
    }
}
Also used : TagAttributeException(jakarta.faces.view.facelets.TagAttributeException) TagAttribute(jakarta.faces.view.facelets.TagAttribute)

Example 4 with TagAttributeException

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

the class TagAttributeImpl method getValueExpression.

/**
 * Create a ValueExpression, using this attribute's literal value and the passed expected type.
 *
 * See ExpressionFactory#createValueExpression(jakarta.el.ELContext, java.lang.String, java.lang.Class)
 * See ValueExpression
 * @param ctx
 *            FaceletContext to use
 * @param type
 *            expected return type
 * @return ValueExpression instance
 */
@Override
public ValueExpression getValueExpression(FaceletContext ctx, Class type) {
    AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
    // volatile reads are atomic, so take the tuple to later comparison.
    Object[] localCachedExpression = cachedExpression;
    if (actx.isAllowCacheELExpressions() && localCachedExpression != null && localCachedExpression.length == 2) {
        // If the expected type is the same return the cached one
        if (localCachedExpression[0] == null && type == null) {
            // If #{cc} recalculate the composite component level
            if ((this.capabilities & EL_CC) != 0) {
                UIComponent cc = actx.getFaceletCompositionContext().getCompositeComponentFromStack();
                if (cc != null) {
                    Location location = (Location) cc.getAttributes().get(CompositeComponentELUtils.LOCATION_KEY);
                    if (location != null) {
                        return ((LocationValueExpression) localCachedExpression[1]).apply(actx.getFaceletCompositionContext().getCompositeComponentLevel(), location);
                    }
                }
                return ((LocationValueExpression) localCachedExpression[1]).apply(actx.getFaceletCompositionContext().getCompositeComponentLevel());
            }
            return (ValueExpression) localCachedExpression[1];
        } else if (localCachedExpression[0] != null && localCachedExpression[0].equals(type)) {
            // If #{cc} recalculate the composite component level
            if ((this.capabilities & EL_CC) != 0) {
                UIComponent cc = actx.getFaceletCompositionContext().getCompositeComponentFromStack();
                if (cc != null) {
                    Location location = (Location) cc.getAttributes().get(CompositeComponentELUtils.LOCATION_KEY);
                    if (location != null) {
                        return ((LocationValueExpression) localCachedExpression[1]).apply(actx.getFaceletCompositionContext().getCompositeComponentLevel(), location);
                    }
                }
                return ((LocationValueExpression) localCachedExpression[1]).apply(actx.getFaceletCompositionContext().getCompositeComponentLevel());
            }
            return (ValueExpression) localCachedExpression[1];
        }
    }
    actx.beforeConstructELExpression();
    try {
        ExpressionFactory f = ctx.getExpressionFactory();
        ValueExpression valueExpression = f.createValueExpression(ctx, this.value, type);
        if (actx.getFaceletCompositionContext().isWrapTagExceptionsAsContextAware()) {
            valueExpression = new ContextAwareTagValueExpression(this, valueExpression);
        } else {
            valueExpression = new TagValueExpression(this, valueExpression);
        }
        // (see MYFACES-2561 for details)
        if ((this.capabilities & EL_CC) != 0) {
            // In MYFACES-4099 it was found that #{cc} could happen outside a composite component. In that
            // case, getLocation() will point to the template. To solve the problem, it is better to get
            // the location of the composite component from the stack directly, but only when the path
            // is different.
            Location currentLocation = getLocation();
            UIComponent cc = actx.getFaceletCompositionContext().getCompositeComponentFromStack();
            if (cc != null) {
                Location ccLocation = (Location) cc.getAttributes().get(CompositeComponentELUtils.LOCATION_KEY);
                if (ccLocation != null && !ccLocation.getPath().equals(currentLocation.getPath())) {
                    // #{cc} from a template called from inside a composite component, disable caching on
                    // this expression. The reason is we need to change the Location object used as
                    // reference as the one in the stack, and that depends on the template hierarchy.
                    // cacheable = false;
                    currentLocation = ccLocation;
                }
            }
            valueExpression = new LocationValueExpression(currentLocation, valueExpression, actx.getFaceletCompositionContext().getCompositeComponentLevel());
        } else if ((this.capabilities & EL_RESOURCE) != 0) {
            valueExpression = new ResourceLocationValueExpression(getLocation(), valueExpression);
        }
        if (actx.isAllowCacheELExpressions() && !actx.isAnyFaceletsVariableResolved()) {
            cachedExpression = new Object[] { type, valueExpression };
        }
        return valueExpression;
    } catch (Exception e) {
        throw new TagAttributeException(this, e);
    } finally {
        actx.afterConstructELExpression();
    }
}
Also used : TagAttributeException(jakarta.faces.view.facelets.TagAttributeException) ExpressionFactory(jakarta.el.ExpressionFactory) ContextAwareTagValueExpression(org.apache.myfaces.view.facelets.el.ContextAwareTagValueExpression) TagValueExpression(org.apache.myfaces.view.facelets.el.TagValueExpression) ResourceLocationValueExpression(org.apache.myfaces.view.facelets.el.ResourceLocationValueExpression) UIComponent(jakarta.faces.component.UIComponent) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext) TagAttributeException(jakarta.faces.view.facelets.TagAttributeException) ELException(jakarta.el.ELException) ResourceLocationValueExpression(org.apache.myfaces.view.facelets.el.ResourceLocationValueExpression) ContextAwareTagValueExpression(org.apache.myfaces.view.facelets.el.ContextAwareTagValueExpression) ValueExpression(jakarta.el.ValueExpression) LocationValueExpression(org.apache.myfaces.view.facelets.el.LocationValueExpression) TagValueExpression(org.apache.myfaces.view.facelets.el.TagValueExpression) ContextAwareTagValueExpression(org.apache.myfaces.view.facelets.el.ContextAwareTagValueExpression) Location(jakarta.faces.view.Location) ResourceLocationValueExpression(org.apache.myfaces.view.facelets.el.ResourceLocationValueExpression) LocationValueExpression(org.apache.myfaces.view.facelets.el.LocationValueExpression)

Example 5 with TagAttributeException

use of jakarta.faces.view.facelets.TagAttributeException in project myfaces-tobago by apache.

the class ConverterHandler method apply.

@Override
public void apply(final FaceletContext faceletContext, final UIComponent parent) throws IOException, ELException {
    if (parent instanceof ValueHolder) {
        if (ComponentHandler.isNew(parent)) {
            final ValueHolder valueHolder = (ValueHolder) parent;
            Converter converter = null;
            ValueExpression valueExpression = null;
            if (binding != null) {
                valueExpression = binding.getValueExpression(faceletContext, Converter.class);
                converter = (Converter) valueExpression.getValue(faceletContext);
            }
            if (converter == null) {
                try {
                    converter = FacesContext.getCurrentInstance().getApplication().createConverter((String) converterId.getValueExpression(faceletContext, String.class).getValue(faceletContext));
                } catch (final Exception e) {
                    throw new TagAttributeException(tag, converterId, e.getCause());
                }
                if (valueExpression != null) {
                    valueExpression.setValue(faceletContext, converter);
                }
            }
            if (converter != null) {
                valueHolder.setConverter(converter);
            }
        // TODO else LOG.warn?
        }
    } else {
        throw new TagException(tag, "Parent is not of type ValueHolder, type is: " + parent);
    }
}
Also used : TagAttributeException(jakarta.faces.view.facelets.TagAttributeException) TagException(jakarta.faces.view.facelets.TagException) ValueExpression(jakarta.el.ValueExpression) Converter(jakarta.faces.convert.Converter) ValueHolder(jakarta.faces.component.ValueHolder) TagAttributeException(jakarta.faces.view.facelets.TagAttributeException) TagException(jakarta.faces.view.facelets.TagException) IOException(java.io.IOException) ELException(jakarta.el.ELException)

Aggregations

TagAttributeException (jakarta.faces.view.facelets.TagAttributeException)17 IOException (java.io.IOException)7 ValueExpression (jakarta.el.ValueExpression)6 ELException (jakarta.el.ELException)5 UIComponent (jakarta.faces.component.UIComponent)4 WebConfiguration (com.sun.faces.config.WebConfiguration)3 VariableMapperWrapper (com.sun.faces.facelets.el.VariableMapperWrapper)3 ExpressionFactory (jakarta.el.ExpressionFactory)3 MethodExpression (jakarta.el.MethodExpression)3 VariableMapper (jakarta.el.VariableMapper)3 FacesException (jakarta.faces.FacesException)3 UIViewRoot (jakarta.faces.component.UIViewRoot)3 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)3 FacesContext (jakarta.faces.context.FacesContext)3 FaceletContext (jakarta.faces.view.facelets.FaceletContext)3 TagException (jakarta.faces.view.facelets.TagException)3 AbstractFaceletContext (org.apache.myfaces.view.facelets.AbstractFaceletContext)3 FaceletContextImplBase (com.sun.faces.facelets.FaceletContextImplBase)2 ClientBehavior (jakarta.faces.component.behavior.ClientBehavior)2 Location (jakarta.faces.view.Location)2