Search in sources :

Example 1 with Location

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");
            }
        }
    }
}
Also used : PostAddToViewEvent(jakarta.faces.event.PostAddToViewEvent) FacesContext(jakarta.faces.context.FacesContext) UIComponent(jakarta.faces.component.UIComponent) FacesException(jakarta.faces.FacesException) Location(jakarta.faces.view.Location) PreRenderViewEvent(jakarta.faces.event.PreRenderViewEvent)

Example 2 with Location

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();
}
Also used : TagAttributeImpl(org.apache.myfaces.view.facelets.tag.TagAttributeImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) ValueExpression(jakarta.el.ValueExpression) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Location(jakarta.faces.view.Location) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 3 with Location

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;
}
Also used : UIComponent(jakarta.faces.component.UIComponent) Location(jakarta.faces.view.Location)

Example 4 with Location

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;
}
Also used : UIComponent(jakarta.faces.component.UIComponent) Location(jakarta.faces.view.Location)

Example 5 with Location

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

Aggregations

Location (jakarta.faces.view.Location)25 TagAttributeImpl (org.apache.myfaces.view.facelets.tag.TagAttributeImpl)11 Test (org.junit.Test)10 UIComponent (jakarta.faces.component.UIComponent)9 Tag (jakarta.faces.view.facelets.Tag)9 TagAttribute (jakarta.faces.view.facelets.TagAttribute)9 TagAttributesImpl (org.apache.myfaces.view.facelets.tag.TagAttributesImpl)9 TagDecorator (jakarta.faces.view.facelets.TagDecorator)8 ValueExpression (jakarta.el.ValueExpression)6 ELException (jakarta.el.ELException)4 FacesContext (jakarta.faces.context.FacesContext)4 ServletException (jakarta.servlet.ServletException)4 IOException (java.io.IOException)4 PrintWriter (java.io.PrintWriter)4 ExpressionFactory (jakarta.el.ExpressionFactory)2 MethodExpression (jakarta.el.MethodExpression)2 PostAddToViewEvent (jakarta.faces.event.PostAddToViewEvent)2 TagAttributeException (jakarta.faces.view.facelets.TagAttributeException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2