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);
}
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;
}
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() };
}
}
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();
}
}
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);
}
}
Aggregations