use of jakarta.faces.view.Location in project myfaces by apache.
the class HtmlScriptRenderer method processEvent.
@Override
public void processEvent(ComponentSystemEvent event) {
if (event instanceof PostAddToViewEvent) {
UIComponent component = event.getComponent();
String target = (String) component.getAttributes().get(ComponentAttrs.TARGET_ATTR);
if (target != null) {
FacesContext facesContext = FacesContext.getCurrentInstance();
Location location = (Location) component.getAttributes().get(CompositeComponentELUtils.LOCATION_KEY);
if (location != null) {
UIComponent ccParent = CompositeComponentELUtils.getCompositeComponentBasedOnLocation(facesContext, location);
if (ccParent != null) {
component.getAttributes().put(CompositeComponentELUtils.CC_FIND_COMPONENT_EXPRESSION, ComponentSupport.getFindComponentExpression(facesContext, ccParent));
}
}
facesContext.getViewRoot().addComponentResource(facesContext, component, target);
}
}
if (event instanceof PreRenderViewEvent) {
UIComponent component = event.getComponent();
String target = (String) component.getAttributes().get(ComponentAttrs.TARGET_ATTR);
if (target != null) {
FacesContext facesContext = FacesContext.getCurrentInstance();
UIComponent uiTarget = facesContext.getViewRoot().getFacet(target);
if (uiTarget == null) {
throw new FacesException("Target for component not found");
}
}
}
}
use of jakarta.faces.view.Location in project myfaces by apache.
the class SerializableELExpressionsTestCase method testSerializeLocationValueExpressionUELWrapper.
@Test
public void testSerializeLocationValueExpressionUELWrapper() throws Exception {
ValueExpression ve = facesContext.getApplication().getExpressionFactory().createValueExpression(facesContext.getELContext(), "#{cc.attrs.value}", Object.class);
TagAttributeImpl tai = new TagAttributeImpl(new Location("path", 299, 12), null, "value", "value", "#{cc.attrs.value}");
TagValueExpression tve = new TagValueExpression(tai, ve);
LocationValueExpression lve = new LocationValueExpression(new Location("path2", 334, 22), tve);
ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(lve);
oos.flush();
baos.flush();
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
LocationValueExpression lve2 = (LocationValueExpression) ois.readObject();
Assert.assertEquals(lve.getExpressionString(), lve2.getExpressionString());
Assert.assertEquals(lve.getLocation().getPath(), lve2.getLocation().getPath());
Assert.assertEquals(lve.getLocation().getLine(), lve2.getLocation().getLine());
Assert.assertEquals(lve.getLocation().getColumn(), lve2.getLocation().getColumn());
oos.close();
ois.close();
}
use of jakarta.faces.view.Location in project myfaces by apache.
the class CompositeComponentELUtils method getCompositeComponentBasedOnLocation.
/**
* Try to find a composite component on the composite component stack
* and using UIComponent.getCurrentCompositeComponent() based on the
* location of the facelet page that generated the composite component.
* @param facesContext
* @param location
* @return
*/
public static UIComponent getCompositeComponentBasedOnLocation(final FacesContext facesContext, final Location location) {
// 1 Use getCurrentComponent and getCurrentCompositeComponent to look on the component stack
UIComponent currentCompositeComponent = UIComponent.getCurrentCompositeComponent(facesContext);
// 1.1 Use getCurrentCompositeComponent first!
if (currentCompositeComponent != null) {
Location componentLocation = (Location) currentCompositeComponent.getAttributes().get(LOCATION_KEY);
if (componentLocation != null && componentLocation.getPath().equals(location.getPath())) {
return currentCompositeComponent;
}
}
UIComponent currentComponent = UIComponent.getCurrentComponent(facesContext);
if (currentComponent == null) {
// Cannot found any component, because we don't have any reference!
return null;
}
// 2. Look on the stack using a recursive algorithm.
UIComponent matchingCompositeComponent = lookForCompositeComponentOnStack(facesContext, location, currentComponent);
if (matchingCompositeComponent != null) {
return matchingCompositeComponent;
}
// to see if the composite component can be found.
if (currentCompositeComponent != null) {
currentComponent = currentCompositeComponent;
} else {
// Try to find the composite component looking directly the parent
// ancestor of the current component
// currentComponent = UIComponent.getCurrentComponent(facesContext);
boolean found = false;
while (currentComponent != null && !found) {
String findComponentExpr = (String) currentComponent.getAttributes().get(CC_FIND_COMPONENT_EXPRESSION);
if (findComponentExpr != null) {
UIComponent foundComponent = facesContext.getViewRoot().findComponent(findComponentExpr);
if (foundComponent != null) {
Location foundComponentLocation = (Location) currentComponent.getAttributes().get(LOCATION_KEY);
if (foundComponentLocation != null && foundComponentLocation.getPath().equals(location.getPath())) {
return foundComponent;
} else {
while (foundComponent != null) {
Location componentLocation = (Location) foundComponent.getAttributes().get(LOCATION_KEY);
if (componentLocation != null && componentLocation.getPath().equals(location.getPath())) {
return foundComponent;
}
// get the composite component's parent
foundComponent = UIComponent.getCompositeComponentParent(foundComponent);
}
}
}
}
if (UIComponent.isCompositeComponent(currentComponent)) {
found = true;
} else {
currentComponent = currentComponent.getParent();
}
}
}
// Use UIComponent.getCompositeComponentParent() to traverse here.
while (currentComponent != null) {
Location componentLocation = (Location) currentComponent.getAttributes().get(LOCATION_KEY);
if (componentLocation != null && componentLocation.getPath().equals(location.getPath())) {
return currentComponent;
}
// get the composite component's parent
currentComponent = UIComponent.getCompositeComponentParent(currentComponent);
}
// not found
return null;
}
use of jakarta.faces.view.Location in project myfaces by apache.
the class CompositeComponentELUtils method getCompositeComponentBasedOnLocation.
public static UIComponent getCompositeComponentBasedOnLocation(final FacesContext facesContext, UIComponent baseComponent, final Location location) {
UIComponent currentComponent = baseComponent;
while (currentComponent != null) {
Location componentLocation = (Location) currentComponent.getAttributes().get(LOCATION_KEY);
if (componentLocation != null && componentLocation.getPath().equals(location.getPath())) {
return currentComponent;
}
// get the composite component's parent
currentComponent = UIComponent.getCompositeComponentParent(currentComponent);
}
return null;
}
use of jakarta.faces.view.Location 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();
}
}
Aggregations