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