Search in sources :

Example 1 with Facelet

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

the class CreateDynamicCompositeComponentListener method processEvent.

@Override
public void processEvent(ComponentSystemEvent event) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    FaceletViewDeclarationLanguage vdl = (FaceletViewDeclarationLanguage) facesContext.getApplication().getViewHandler().getViewDeclarationLanguage(facesContext, facesContext.getViewRoot().getViewId());
    Facelet componentFacelet;
    FaceletFactory faceletFactory = vdl.getFaceletFactory();
    FaceletFactory.setInstance(faceletFactory);
    try {
        componentFacelet = faceletFactory.compileComponentFacelet(taglibURI, tagName, attributes);
    } finally {
        FaceletFactory.setInstance(null);
    }
    UIComponent component = event.getComponent();
    // The execution of this listener activated another call to PostAddToViewEvent, because
    // ComponentTagHandlerDelegate removes and add the component again. This is necessary because
    // the inner components also require the propagation of PostAddToViewEvent to refresh themselves.
    // but this check avoids the duplicate call to the facelet, even if the duplicate call does not
    // have any side effect (counts as a refresh).
    Integer step = (Integer) component.getAttributes().get(CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW);
    if (step != null && step == 0) {
        component.getAttributes().put(CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW, 1);
    } else {
        return;
    }
    try {
        facesContext.getAttributes().put(FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD, Boolean.TRUE);
        // Detect the relationship between parent and child, to ensure the component is properly created
        // and refreshed. In facelets this is usually done by core.FacetHandler, but since it is a
        // dynamic component, we need to do it here before apply the handler
        UIComponent parent = component.getParent();
        String facetName = null;
        if (parent.getFacetCount() > 0 && !parent.getChildren().contains(component)) {
            facetName = ComponentSupport.findFacetNameByComponentInstance(parent, component);
        }
        try {
            if (facetName != null) {
                parent.getAttributes().put(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY, facetName);
            }
            // The trick here is restore MARK_CREATED, just to allow ComponentTagHandlerDelegate to
            // find the component. Then we reset it to exclude it from facelets refresh algorithm.
            String markId = (String) component.getAttributes().get(FaceletViewDeclarationLanguage.GEN_MARK_ID);
            if (markId == null) {
                ((AbstractFacelet) componentFacelet).applyDynamicComponentHandler(facesContext, component, baseKey);
            } else {
                try {
                    component.getAttributes().put(ComponentSupport.MARK_CREATED, markId);
                    ((AbstractFacelet) componentFacelet).applyDynamicComponentHandler(facesContext, component.getParent(), baseKey);
                } finally {
                    component.getAttributes().put(ComponentSupport.MARK_CREATED, null);
                }
            }
            if (FaceletViewDeclarationLanguageBase.isDynamicComponentNeedsRefresh(facesContext)) {
                FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(facesContext);
                if (fcc == null) {
                    FaceletViewDeclarationLanguageBase.activateDynamicComponentRefreshTransientBuild(facesContext);
                    FaceletViewDeclarationLanguageBase.resetDynamicComponentNeedsRefreshFlag(facesContext);
                    component.subscribeToEvent(DynamicComponentRefreshTransientBuildEvent.class, new RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
                    component.getAttributes().put(DynamicComponentRefreshTransientBuildEvent.DYN_COMP_REFRESH_FLAG, Boolean.TRUE);
                } else {
                    component.subscribeToEvent(FaceletDynamicComponentRefreshTransientBuildEvent.class, new RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
                }
            }
        } finally {
            if (facetName != null) {
                parent.getAttributes().remove(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY);
            }
        }
    } catch (IOException e) {
        throw new LocationAwareFacesException(e, component);
    } finally {
        facesContext.getAttributes().remove(FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD);
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) AbstractFacelet(org.apache.myfaces.view.facelets.AbstractFacelet) Facelet(jakarta.faces.view.facelets.Facelet) FaceletFactory(org.apache.myfaces.view.facelets.FaceletFactory) AbstractFacelet(org.apache.myfaces.view.facelets.AbstractFacelet) UIComponent(jakarta.faces.component.UIComponent) FaceletViewDeclarationLanguage(org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage) IOException(java.io.IOException) RefreshDynamicComponentListener(org.apache.myfaces.view.facelets.compiler.RefreshDynamicComponentListener) LocationAwareFacesException(org.apache.myfaces.view.facelets.LocationAwareFacesException)

Example 2 with Facelet

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

the class FaceletViewDeclarationLanguage method getComponentMetadata.

/**
 * retargetMethodExpressions(FacesContext, UIComponent) has some clues about the behavior of this method
 *
 * {@inheritDoc}
 */
@Override
public BeanInfo getComponentMetadata(FacesContext context, Resource componentResource) {
    BeanInfo beanInfo = null;
    Assert.notNull(context, "context");
    try {
        Facelet compositeComponentFacelet;
        FaceletFactory.setInstance(faceletFactory);
        try {
            compositeComponentFacelet = faceletFactory.getCompositeComponentMetadataFacelet(componentResource.getURL());
        } finally {
            FaceletFactory.setInstance(null);
        }
        // context.getAttributes().put(BUILDING_COMPOSITE_COMPONENT_METADATA, Boolean.TRUE);
        // Create a temporal tree where all components will be put, but we are only
        // interested in metadata.
        UINamingContainer compositeComponentBase = (UINamingContainer) context.getApplication().createComponent(context, UINamingContainer.COMPONENT_TYPE, null);
        // Fill the component resource key, because this information should be available
        // on metadata to recognize which is the component used as composite component base.
        // Since this method is called from Application.createComponent(FacesContext,Resource),
        // and in that specific method this key is updated, this is the best option we
        // have for recognize it (also this key is used by UIComponent.isCompositeComponent)
        compositeComponentBase.getAttributes().put(Resource.COMPONENT_RESOURCE_KEY, componentResource);
        // According to UserTagHandler, in this point we need to wrap the facelet
        // VariableMapper, so local changes are applied on "page context", but
        // data is retrieved from full context
        FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
        VariableMapper orig = faceletContext.getVariableMapper();
        try {
            faceletContext.setVariableMapper(new VariableMapperWrapper(orig));
            compositeComponentBase.pushComponentToEL(context, compositeComponentBase);
            compositeComponentFacelet.apply(context, compositeComponentBase);
            compositeComponentBase.popComponentFromEL(context);
        } finally {
            faceletContext.setVariableMapper(orig);
        }
        beanInfo = (BeanInfo) compositeComponentBase.getAttributes().get(UIComponent.BEANINFO_KEY);
    } catch (IOException e) {
        throw new FacesException(e);
    }
    return beanInfo;
}
Also used : Facelet(jakarta.faces.view.facelets.Facelet) VariableMapper(jakarta.el.VariableMapper) BeanInfo(java.beans.BeanInfo) VariableMapperWrapper(org.apache.myfaces.view.facelets.el.VariableMapperWrapper) FaceletContext(jakarta.faces.view.facelets.FaceletContext) UINamingContainer(jakarta.faces.component.UINamingContainer) IOException(java.io.IOException) FacesException(jakarta.faces.FacesException)

Example 3 with Facelet

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

the class DoctypeTestCase method testCompilerXhtml.

@Test
public void testCompilerXhtml() throws Exception {
    FaceletFactory ff = vdl.getFaceletFactory();
    Facelet f = ff.getFacelet(this.getClass().getResource("/org/apache/myfaces/view/facelets/compiler/doctype/xhtml.xhtml"));
    Assert.assertNotNull(((AbstractFacelet) f).getDoctype());
    Assert.assertEquals("html", ((AbstractFacelet) f).getDoctype().getRootElement());
    Assert.assertEquals("-//W3C//DTD XHTML 1.0 Transitional//EN", ((AbstractFacelet) f).getDoctype().getPublic());
    Assert.assertEquals("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", ((AbstractFacelet) f).getDoctype().getSystem());
}
Also used : AbstractFacelet(org.apache.myfaces.view.facelets.AbstractFacelet) Facelet(jakarta.faces.view.facelets.Facelet) FaceletFactory(org.apache.myfaces.view.facelets.FaceletFactory) AbstractFacelet(org.apache.myfaces.view.facelets.AbstractFacelet) Test(org.junit.Test)

Example 4 with Facelet

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

the class DefaultFaceletFactory method getFacelet.

/*
     * (non-Javadoc)
     * 
     * @see org.apache.myfaces.view.facelets.FaceletFactory#getFacelet(java.lang.String)
     */
@Override
public Facelet getFacelet(FacesContext facesContext, String uri) throws IOException, FaceletException, FacesException, ELException {
    Boolean isManagedFacelet = _managedFacelet.get(uri);
    if (isManagedFacelet == null || isManagedFacelet) {
        Facelet facelet = null;
        if (ExternalSpecifications.isCDIAvailable(facesContext.getExternalContext())) {
            BeanManager bm = CDIUtils.getBeanManager(facesContext);
            facelet = CDIUtils.get(bm, Facelet.class, true, View.Literal.of(uri));
        }
        _managedFacelet.put(uri, facelet != null);
        if (facelet != null) {
            return facelet;
        }
    }
    URL url = (URL) _relativeLocations.get(uri);
    if (url == null) {
        url = resolveURL(facesContext, getBaseUrl(), uri);
        if (url != null) {
            ViewResource viewResource = (ViewResource) facesContext.getAttributes().get(FaceletFactory.LAST_RESOURCE_RESOLVED);
            if (viewResource != null) {
            // If a view resource has been used to resolve a resource, the cache is in
            // the ResourceHandler implementation. No need to cache in _relativeLocations.
            } else {
                Map<String, URL> newLoc = new HashMap<>(_relativeLocations);
                newLoc.put(uri, url);
                _relativeLocations = newLoc;
            }
        } else {
            throw new IOException('\'' + uri + "' not found.");
        }
    }
    return this.getFacelet(url);
}
Also used : Facelet(jakarta.faces.view.facelets.Facelet) HashMap(java.util.HashMap) ViewResource(jakarta.faces.application.ViewResource) IOException(java.io.IOException) BeanManager(jakarta.enterprise.inject.spi.BeanManager) URL(java.net.URL)

Example 5 with Facelet

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

the class FaceletViewHandlingStrategy method createComponentMetadata.

private BeanInfo createComponentMetadata(FacesContext context, Resource ccResource) {
    // PENDING this implementation is terribly wasteful.
    // Must find a better way.
    FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FACELET_CONTEXT_KEY);
    DefaultFaceletFactory factory = (DefaultFaceletFactory) RequestStateManager.get(context, FACELET_FACTORY);
    VariableMapper orig = faceletContext.getVariableMapper();
    // Create tmp and facetComponent
    UIComponent tmp = context.getApplication().createComponent("jakarta.faces.NamingContainer");
    UIPanel facetComponent = (UIPanel) context.getApplication().createComponent("jakarta.faces.Panel");
    // PENDING I think this can be skipped because we don't render
    // this component instance.
    facetComponent.setRendererType("jakarta.faces.Group");
    // PENDING This could possibly be skipped too. However, I think
    // this is important because other tag handlers, within
    // <cc:interface> expect it will be there.
    tmp.getFacets().put(COMPOSITE_FACET_NAME, facetComponent);
    // We have to put the resource in here just so the classes that eventually
    // get called by facelets have access to it.
    tmp.getAttributes().put(COMPONENT_RESOURCE_KEY, ccResource);
    Facelet facelet;
    try {
        facelet = factory.getFacelet(context, ccResource.getURL());
        VariableMapper wrapper = new VariableMapperWrapper(orig) {

            @Override
            public ValueExpression resolveVariable(String variable) {
                // PENDING is this needed?
                return super.resolveVariable(variable);
            }
        };
        faceletContext.setVariableMapper(wrapper);
        context.getAttributes().put(IS_BUILDING_METADATA, TRUE);
        // Because mojarra currently requires a <cc:interface>
        // element within the compcomp markup, we can rely on the
        // fact that its tag handler, InterfaceHandler.apply(), is
        // called. In this method, we first imbue facetComponent
        // with any config information present on the <cc:interface>
        // element.
        // Then we do the normal facelet thing:
        // this.nextHandler.apply(). This causes any child tag
        // handlers of the <cc:interface> to be called. The
        // compcomp spec says each such tag handler is responsible
        // for adding to the compcomp metadata, referenced from the
        // facetComponent parent.
        facelet.apply(context, facetComponent);
    // When facelet.apply() returns (and therefore
    // InterfaceHandler.apply() returns), the compcomp metadata
    // pointed to by facetComponent is fully populated.
    } catch (Exception e) {
        if (e instanceof FacesException) {
            throw (FacesException) e;
        } else {
            throw new FacesException(e);
        }
    } finally {
        context.getAttributes().remove(IS_BUILDING_METADATA);
        faceletContext.setVariableMapper(orig);
    }
    // not yet implemented.
    return (CompositeComponentBeanInfo) tmp.getAttributes().get(BEANINFO_KEY);
}
Also used : DefaultFaceletFactory(com.sun.faces.facelets.impl.DefaultFaceletFactory) Facelet(jakarta.faces.view.facelets.Facelet) CompositeComponentBeanInfo(com.sun.faces.facelets.tag.composite.CompositeComponentBeanInfo) VariableMapper(jakarta.el.VariableMapper) UIComponent(jakarta.faces.component.UIComponent) UIPanel(jakarta.faces.component.UIPanel) VariableMapperWrapper(com.sun.faces.facelets.el.VariableMapperWrapper) FaceletContext(jakarta.faces.view.facelets.FaceletContext) IOException(java.io.IOException) FacesException(jakarta.faces.FacesException) FileNotFoundException(java.io.FileNotFoundException) FacesException(jakarta.faces.FacesException)

Aggregations

Facelet (jakarta.faces.view.facelets.Facelet)11 IOException (java.io.IOException)7 UIComponent (jakarta.faces.component.UIComponent)5 FacesException (jakarta.faces.FacesException)4 AbstractFacelet (org.apache.myfaces.view.facelets.AbstractFacelet)4 FaceletFactory (org.apache.myfaces.view.facelets.FaceletFactory)4 VariableMapper (jakarta.el.VariableMapper)2 Doctype (jakarta.faces.component.Doctype)2 UIPanel (jakarta.faces.component.UIPanel)2 FacesContext (jakarta.faces.context.FacesContext)2 FaceletContext (jakarta.faces.view.facelets.FaceletContext)2 URL (java.net.URL)2 FaceletViewDeclarationLanguage (org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage)2 RefreshDynamicComponentListener (org.apache.myfaces.view.facelets.compiler.RefreshDynamicComponentListener)2 Test (org.junit.Test)2 StateContext (com.sun.faces.context.StateContext)1 StateContext.getStateContext (com.sun.faces.context.StateContext.getStateContext)1 VariableMapperWrapper (com.sun.faces.facelets.el.VariableMapperWrapper)1 DefaultFaceletFactory (com.sun.faces.facelets.impl.DefaultFaceletFactory)1 XMLFrontMatterSaver (com.sun.faces.facelets.impl.XMLFrontMatterSaver)1