Search in sources :

Example 1 with FaceletHandler

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

the class CompositeComponentResourceTagHandler method applyNextHandlerIfNotApplied.

@SuppressWarnings("unchecked")
protected void applyNextHandlerIfNotApplied(FaceletContext ctx, UIComponent c) throws IOException {
    // Apply all facelets not applied yet.
    CompositeComponentBeanInfo beanInfo = (CompositeComponentBeanInfo) c.getAttributes().get(UIComponent.BEANINFO_KEY);
    BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor();
    boolean insertChildrenUsed = (beanDescriptor.getValue(InsertChildrenHandler.INSERT_CHILDREN_USED) != null);
    List<String> insertFacetList = (List<String>) beanDescriptor.getValue(InsertFacetHandler.INSERT_FACET_USED);
    if (nextHandler instanceof jakarta.faces.view.facelets.CompositeFaceletHandler) {
        for (FaceletHandler handler : ((jakarta.faces.view.facelets.CompositeFaceletHandler) nextHandler).getHandlers()) {
            if (handler instanceof jakarta.faces.view.facelets.FacetHandler) {
                if (insertFacetList == null || !insertFacetList.contains(((jakarta.faces.view.facelets.FacetHandler) handler).getFacetName(ctx))) {
                    handler.apply(ctx, c);
                }
            } else if (handler instanceof InsertFacetHandler) {
                if (insertFacetList == null || !insertFacetList.contains(((InsertFacetHandler) handler).getFacetName(ctx))) {
                    handler.apply(ctx, c);
                }
            } else if (insertChildrenUsed) {
                if (!(handler instanceof jakarta.faces.view.facelets.ComponentHandler || handler instanceof ComponentContainerHandler || handler instanceof TextHandler)) {
                    handler.apply(ctx, c);
                }
            } else {
                handler.apply(ctx, c);
            }
        }
    } else {
        if (nextHandler instanceof jakarta.faces.view.facelets.FacetHandler) {
            if (insertFacetList == null || !insertFacetList.contains(((jakarta.faces.view.facelets.FacetHandler) nextHandler).getFacetName(ctx))) {
                nextHandler.apply(ctx, c);
            }
        } else if (nextHandler instanceof InsertFacetHandler) {
            if (insertFacetList == null || !insertFacetList.contains(((InsertFacetHandler) nextHandler).getFacetName(ctx))) {
                nextHandler.apply(ctx, c);
            }
        } else if (insertChildrenUsed) {
            if (!(nextHandler instanceof jakarta.faces.view.facelets.ComponentHandler || nextHandler instanceof ComponentContainerHandler || nextHandler instanceof TextHandler)) {
                nextHandler.apply(ctx, c);
            }
        } else {
            nextHandler.apply(ctx, c);
        }
    }
    // Check for required facets
    Map<String, PropertyDescriptor> facetPropertyDescriptorMap = (Map<String, PropertyDescriptor>) beanDescriptor.getValue(UIComponent.FACETS_KEY);
    if (facetPropertyDescriptorMap != null) {
        List<String> facetsRequiredNotFound = null;
        for (Map.Entry<String, PropertyDescriptor> entry : facetPropertyDescriptorMap.entrySet()) {
            ValueExpression requiredExpr = (ValueExpression) entry.getValue().getValue("required");
            if (requiredExpr != null) {
                Boolean required = (Boolean) requiredExpr.getValue(ctx.getFacesContext().getELContext());
                if (Boolean.TRUE.equals(required)) {
                    initFacetHandlersMap(ctx);
                    if (!_facetHandlersMap.containsKey(entry.getKey())) {
                        if (facetsRequiredNotFound == null) {
                            facetsRequiredNotFound = new ArrayList(facetPropertyDescriptorMap.size());
                        }
                        facetsRequiredNotFound.add(entry.getKey());
                    }
                }
            }
        }
        if (facetsRequiredNotFound != null && !facetsRequiredNotFound.isEmpty()) {
            throw new TagException(getTag(), "The following facets are required by the component: " + facetsRequiredNotFound);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) FaceletHandler(jakarta.faces.view.facelets.FaceletHandler) ComponentContainerHandler(org.apache.myfaces.view.facelets.tag.ComponentContainerHandler) TextHandler(jakarta.faces.view.facelets.TextHandler) List(java.util.List) ArrayList(java.util.ArrayList) PropertyDescriptor(java.beans.PropertyDescriptor) ComponentHandler(jakarta.faces.view.facelets.ComponentHandler) BeanDescriptor(java.beans.BeanDescriptor) TagException(jakarta.faces.view.facelets.TagException) ValueExpression(jakarta.el.ValueExpression) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with FaceletHandler

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

the class SAXCompiler method doCompile.

protected FaceletHandler doCompile(CompilationManager mngr, CompilationHandler handler, URL src, String alias) throws IOException {
    String encoding = getEncoding();
    try (InputStream is = new BufferedInputStream(src.openStream(), 1024)) {
        writeXmlDecl(is, encoding, mngr);
        SAXParser parser = createSAXParser(handler);
        parser.parse(is, handler);
    } catch (SAXException e) {
        throw new FaceletException("Error Parsing " + alias + ": " + e.getMessage(), e.getCause());
    } catch (ParserConfigurationException e) {
        throw new FaceletException("Error Configuring Parser " + alias + ": " + e.getMessage(), e.getCause());
    } catch (FaceletException e) {
        throw e;
    }
    FaceletHandler result = new EncodingHandler(mngr.createFaceletHandler(), encoding, mngr.getCompilationMessageHolder());
    mngr.setCompilationMessageHolder(null);
    return result;
}
Also used : FaceletException(jakarta.faces.view.facelets.FaceletException) FaceletHandler(jakarta.faces.view.facelets.FaceletHandler) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 3 with FaceletHandler

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

the class CompositeComponentResourceTagHandler method apply.

@Override
public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException, FaceletException, ELException {
    if (_dynamicCompositeComponent) {
        AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
        FaceletCompositionContext fcc = actx.getFaceletCompositionContext();
        UIComponent innerCompositeComponent = fcc.getCompositeComponentFromStack();
        // In a programatical addition, the code that process the composite component only takes effect
        // when the composite component is added to the view.
        Integer step = (Integer) innerCompositeComponent.getAttributes().get(CREATE_CC_ON_POST_ADD_TO_VIEW);
        if (step != null && step == 1) {
            if (name != null) {
                // 1. Initialize map used to retrieve facets
                if (innerCompositeComponent.getFacetCount() == 0) {
                    checkFacetRequired(ctx, name);
                    return true;
                }
                UIComponent facet = innerCompositeComponent.getFacet(name);
                if (facet != null) {
                    // Insert facet
                    innerCompositeComponent.getFacets().remove(name);
                    parent.getFacets().put(name, facet);
                    return true;
                } else {
                    checkFacetRequired(ctx, name);
                    return true;
                }
            } else {
                if (innerCompositeComponent.getChildCount() > 0) {
                    String facetName = (String) parent.getAttributes().get(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY);
                    // Insert children
                    List<UIComponent> children = new ArrayList<>(innerCompositeComponent.getChildCount());
                    while (innerCompositeComponent.getChildCount() > 0) {
                        children.add(innerCompositeComponent.getChildren().remove(0));
                    }
                    while (children.size() > 0) {
                        UIComponent child = children.remove(0);
                        child.getAttributes().put(InsertChildrenHandler.INSERT_CHILDREN_USED, Boolean.TRUE);
                        if (facetName != null) {
                            ComponentSupport.addFacet(ctx, parent, child, facetName);
                        } else {
                            parent.getChildren().add(child);
                        }
                    }
                }
                return true;
            }
        } else if (step != null && step > 1) {
            // is no relative order (it is always on the same spot).
            if (name == null) {
                String facetName = (String) parent.getAttributes().get(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY);
                // refresh case, remember the inserted children does not have any
                // associated tag handler, so in this case we just need to remove and add them in the same order
                // we found them
                List<UIComponent> children = null;
                if (facetName == null) {
                    children = new ArrayList<>(parent.getChildCount());
                    int i = 0;
                    while (parent.getChildCount() - i > 0) {
                        UIComponent child = parent.getChildren().get(i);
                        if (Boolean.TRUE.equals(child.getAttributes().get(InsertChildrenHandler.INSERT_CHILDREN_USED))) {
                            children.add(parent.getChildren().remove(i));
                        } else {
                            i++;
                        }
                    }
                } else {
                    children = new ArrayList<UIComponent>();
                    UIComponent child = parent.getFacet(facetName);
                    if (Boolean.TRUE.equals(child.getAttributes().get(InsertChildrenHandler.INSERT_CHILDREN_USED))) {
                        parent.getFacets().remove(facetName);
                        children.add(child);
                    } else {
                        UIComponent parentToApply = child;
                        int i = 0;
                        while (parentToApply.getChildCount() - i > 0) {
                            child = parentToApply.getChildren().get(i);
                            if (Boolean.TRUE.equals(child.getAttributes().get(InsertChildrenHandler.INSERT_CHILDREN_USED))) {
                                children.add(parentToApply.getChildren().remove(i));
                            } else {
                                i++;
                            }
                        }
                    }
                }
                while (children.size() > 0) {
                    UIComponent child = children.remove(0);
                    if (facetName != null) {
                        ComponentSupport.addFacet(ctx, parent, child, facetName);
                    } else {
                        parent.getChildren().add(child);
                    }
                }
            }
        }
        return true;
    }
    if (name != null) {
        // 1. Initialize map used to retrieve facets
        if (_facetHandlers == null || _facetHandlers.isEmpty()) {
            checkFacetRequired(ctx, name);
            return true;
        }
        initFacetHandlersMap(ctx);
        FaceletHandler handler = _facetHandlersMap.get(name);
        if (handler != null) {
            AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
            // Pop the current composite component on stack, so #{cc} references
            // can be resolved correctly, because they are relative to the page
            // that define it.
            FaceletCompositionContext fcc = actx.getFaceletCompositionContext();
            UIComponent innerCompositeComponent = fcc.getCompositeComponentFromStack();
            fcc.popCompositeComponentToStack();
            // Pop the template context, so ui:xx tags and nested composite component
            // cases could work correctly
            TemplateContext itc = actx.popTemplateContext();
            try {
                handler.apply(ctx, parent);
            } finally {
                actx.pushTemplateContext(itc);
                fcc.pushCompositeComponentToStack(innerCompositeComponent);
            }
            return true;
        } else {
            checkFacetRequired(ctx, name);
            return true;
        }
    } else {
        AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
        // Pop the current composite component on stack, so #{cc} references
        // can be resolved correctly, because they are relative to the page
        // that define it.
        FaceletCompositionContext fcc = actx.getFaceletCompositionContext();
        UIComponent innerCompositeComponent = fcc.getCompositeComponentFromStack();
        fcc.popCompositeComponentToStack();
        // Pop the template context, so ui:xx tags and nested composite component
        // cases could work correctly
        TemplateContext itc = actx.popTemplateContext();
        try {
            for (int i = 0; i < _componentHandlers.size(); i++) {
                _componentHandlers.get(i).apply(ctx, parent);
            }
        } finally {
            actx.pushTemplateContext(itc);
            fcc.pushCompositeComponentToStack(innerCompositeComponent);
        }
        return true;
    }
}
Also used : FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) FaceletHandler(jakarta.faces.view.facelets.FaceletHandler) UIComponent(jakarta.faces.component.UIComponent) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) TemplateContext(org.apache.myfaces.view.facelets.TemplateContext) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext)

Example 4 with FaceletHandler

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

the class CompositeComponentResourceTagHandler method initFacetHandlersMap.

private void initFacetHandlersMap(FaceletContext ctx) {
    if (_facetHandlersMap == null) {
        Map<String, FaceletHandler> map = new HashMap<>(_facetHandlers.size());
        for (FaceletHandler handler : _facetHandlers) {
            if (handler instanceof jakarta.faces.view.facelets.FacetHandler) {
                map.put(((jakarta.faces.view.facelets.FacetHandler) handler).getFacetName(ctx), handler);
            } else if (handler instanceof InsertFacetHandler) {
                map.put(((InsertFacetHandler) handler).getFacetName(ctx), handler);
            }
        }
        _facetHandlersMap = map;
    }
}
Also used : FaceletHandler(jakarta.faces.view.facelets.FaceletHandler) HashMap(java.util.HashMap)

Example 5 with FaceletHandler

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

the class SAXCompiler method doCompileComponent.

@Override
protected CompilerResult doCompileComponent(String taglibURI, String tagName, Map<String, Object> attributes) {
    String alias = tagName;
    CompilationManager mngr = new CompilationManager(alias, this, getDefaultFaceletsProcessingInstructions());
    // The prefix is only a logical name.
    String prefix = "oamf";
    mngr.pushNamespace(prefix, taglibURI);
    boolean tagContainParams = (("include".equals(tagName) || "decorate".equals(tagName) || "composition".equals(tagName)) && (UILibrary.NAMESPACE.equals(taglibURI) || UILibrary.JCP_NAMESPACE.equals(taglibURI) || UILibrary.SUN_NAMESPACE.equals(taglibURI)));
    Location location = new Location(alias, 0, 0);
    int len = attributes.size();
    if (tagContainParams && attributes.containsKey("params")) {
        len = len - 1;
    }
    TagAttribute[] ta = new TagAttribute[len];
    int i = 0;
    Map<String, Object> paramsMap = null;
    for (Map.Entry<String, Object> entry : attributes.entrySet()) {
        String stringValue = null;
        if (tagContainParams && "params".equals(entry.getKey())) {
            paramsMap = (Map<String, Object>) entry.getValue();
        } else {
            if (entry.getValue() instanceof ValueExpression) {
                stringValue = ((ValueExpression) entry.getValue()).getExpressionString();
            } else if (entry.getValue() instanceof MethodExpression) {
                stringValue = ((MethodExpression) entry.getValue()).getExpressionString();
            } else if (entry.getValue() != null) {
                stringValue = entry.getValue().toString();
            }
            ta[i] = new TagAttributeImpl(location, "", entry.getKey(), entry.getKey(), stringValue);
            i++;
        }
    }
    mngr.pushTag(new Tag(location, taglibURI, tagName, "oamf:" + tagName, new TagAttributesImpl(ta)));
    if (tagContainParams && paramsMap != null) {
        for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
            TagAttribute[] tap = new TagAttribute[2];
            String stringValue = null;
            if (entry.getValue() instanceof ValueExpression) {
                stringValue = ((ValueExpression) entry.getValue()).getExpressionString();
            } else if (entry.getValue() instanceof MethodExpression) {
                stringValue = ((MethodExpression) entry.getValue()).getExpressionString();
            } else if (entry.getValue() != null) {
                stringValue = entry.getValue().toString();
            }
            tap[0] = new TagAttributeImpl(location, "", "name", "name", entry.getKey());
            tap[1] = new TagAttributeImpl(location, "", "value", "value", stringValue);
            mngr.pushTag(new Tag(location, UILibrary.NAMESPACE, "param", "oamf:param", new TagAttributesImpl(tap)));
            mngr.popTag();
        }
    }
    mngr.popTag();
    mngr.popNamespace(prefix);
    FaceletHandler handler = new DynamicComponentFacelet((NamespaceHandler) mngr.createFaceletHandler());
    return new CompilerResult(handler, mngr.getDoctype());
}
Also used : MethodExpression(jakarta.el.MethodExpression) FaceletHandler(jakarta.faces.view.facelets.FaceletHandler) TagAttributeImpl(org.apache.myfaces.view.facelets.tag.TagAttributeImpl) ValueExpression(jakarta.el.ValueExpression) TagAttribute(jakarta.faces.view.facelets.TagAttribute) Tag(jakarta.faces.view.facelets.Tag) Map(java.util.Map) Location(jakarta.faces.view.Location) TagAttributesImpl(org.apache.myfaces.view.facelets.tag.TagAttributesImpl)

Aggregations

FaceletHandler (jakarta.faces.view.facelets.FaceletHandler)5 ValueExpression (jakarta.el.ValueExpression)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 MethodExpression (jakarta.el.MethodExpression)1 UIComponent (jakarta.faces.component.UIComponent)1 Location (jakarta.faces.view.Location)1 ComponentHandler (jakarta.faces.view.facelets.ComponentHandler)1 FaceletException (jakarta.faces.view.facelets.FaceletException)1 Tag (jakarta.faces.view.facelets.Tag)1 TagAttribute (jakarta.faces.view.facelets.TagAttribute)1 TagException (jakarta.faces.view.facelets.TagException)1 TextHandler (jakarta.faces.view.facelets.TextHandler)1 BeanDescriptor (java.beans.BeanDescriptor)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1