Search in sources :

Example 1 with ApplicationImpl

use of org.apache.myfaces.config.impl.element.ApplicationImpl in project myfaces by apache.

the class AnnotationConfigurator method createFacesConfig.

/**
 * TODO: Implement strategy pattern over this method.
 *
 * @param map
 * @return
 */
protected FacesConfigImpl createFacesConfig(Map<Class<? extends Annotation>, Set<Class<?>>> map) {
    FacesConfigImpl facesConfig = new FacesConfigImpl();
    Set<Class<?>> classes = map.get(FacesComponent.class);
    if (classes != null && !classes.isEmpty()) {
        for (Class<?> clazz : classes) {
            FacesComponent comp = (FacesComponent) clazz.getAnnotation(FacesComponent.class);
            if (comp != null) {
                if (log.isLoggable(Level.FINEST)) {
                    log.finest("addComponent(" + comp.value() + ',' + clazz.getName() + ')');
                }
                String value = comp.value();
                if (StringUtils.isEmpty(value)) {
                    String simpleName = clazz.getSimpleName();
                    value = Character.toLowerCase(simpleName.charAt(0)) + simpleName.substring(1);
                }
                facesConfig.addComponent(new ComponentImpl(value, clazz.getName()));
                if (comp.createTag()) {
                    String tagName = comp.tagName();
                    if (StringUtils.isEmpty(tagName)) {
                        tagName = clazz.getSimpleName();
                        tagName = Character.toLowerCase(tagName.charAt(0)) + tagName.substring(1);
                    }
                    facesConfig.addComponentTagDeclaration(value, new ComponentTagDeclarationImpl(value, comp.namespace(), tagName));
                }
            }
        }
    }
    classes = map.get(FacesConverter.class);
    if (classes != null && !classes.isEmpty()) {
        for (Class<?> clazz : classes) {
            FacesConverter conv = (FacesConverter) clazz.getAnnotation(FacesConverter.class);
            if (conv != null) {
                if (log.isLoggable(Level.FINEST)) {
                    log.finest("addConverter(" + conv.value() + ',' + clazz.getName() + ')');
                }
                // If there is a previous entry on Application Configuration Resources,
                // the entry there takes precedence
                boolean hasForClass = !Object.class.equals(conv.forClass());
                boolean hasValue = conv.value().length() > 0;
                if (hasForClass || hasValue) {
                    ConverterImpl converter = new ConverterImpl();
                    if (hasForClass) {
                        converter.setForClass(conv.forClass().getName());
                    }
                    if (hasValue) {
                        converter.setConverterId(conv.value());
                    }
                    converter.setConverterClass(clazz.getName());
                    facesConfig.addConverter(converter);
                } else {
                    throw new FacesException("@FacesConverter must have value, forClass or both. Check annotation " + "@FacesConverter on class: " + clazz.getName());
                }
            }
        }
    }
    classes = map.get(FacesValidator.class);
    if (classes != null && !classes.isEmpty()) {
        for (Class<?> clazz : classes) {
            FacesValidator val = (FacesValidator) clazz.getAnnotation(FacesValidator.class);
            if (val != null) {
                if (log.isLoggable(Level.FINEST)) {
                    log.finest("addValidator(" + val.value() + ',' + clazz.getName() + ')');
                }
                String value = val.value();
                if (value == null || value.isEmpty()) {
                    String simpleName = clazz.getSimpleName();
                    value = Character.toLowerCase(simpleName.charAt(0)) + simpleName.substring(1);
                }
                facesConfig.addValidator(value, clazz.getName());
                if (val.isDefault()) {
                    ApplicationImpl app = null;
                    if (facesConfig.getApplications().isEmpty()) {
                        app = new ApplicationImpl();
                    } else {
                        app = (ApplicationImpl) facesConfig.getApplications().get(0);
                    }
                    app.addDefaultValidatorId(value);
                }
            }
        }
    }
    classes = map.get(FacesRenderer.class);
    if (classes != null && !classes.isEmpty()) {
        for (Class<?> clazz : classes) {
            FacesRenderer rend = (FacesRenderer) clazz.getAnnotation(FacesRenderer.class);
            if (rend != null) {
                String renderKitId = rend.renderKitId();
                if (renderKitId == null) {
                    renderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT;
                }
                if (log.isLoggable(Level.FINEST)) {
                    log.finest("addRenderer(" + renderKitId + ", " + rend.componentFamily() + ", " + rend.rendererType() + ", " + clazz.getName() + ')');
                }
                org.apache.myfaces.config.impl.element.RenderKitImpl renderKit = (org.apache.myfaces.config.impl.element.RenderKitImpl) facesConfig.getRenderKit(renderKitId);
                if (renderKit == null) {
                    renderKit = new org.apache.myfaces.config.impl.element.RenderKitImpl();
                    renderKit.setId(renderKitId);
                    facesConfig.addRenderKit(renderKit);
                }
                org.apache.myfaces.config.impl.element.RendererImpl renderer = new org.apache.myfaces.config.impl.element.RendererImpl();
                renderer.setComponentFamily(rend.componentFamily());
                renderer.setRendererClass(clazz.getName());
                renderer.setRendererType(rend.rendererType());
                renderKit.addRenderer(renderer);
            }
        }
    }
    classes = map.get(NamedEvent.class);
    if (classes != null && !classes.isEmpty()) {
        handleNamedEvent(facesConfig, classes);
    }
    classes = map.get(FacesBehavior.class);
    if (classes != null && !classes.isEmpty()) {
        handleFacesBehavior(facesConfig, classes);
    }
    classes = map.get(FacesBehaviorRenderer.class);
    if (classes != null && !classes.isEmpty()) {
        handleFacesBehaviorRenderer(facesConfig, classes);
    }
    classes = map.get(FaceletsResourceResolver.class);
    if (classes != null && !classes.isEmpty()) {
        handleFaceletsResourceResolver(facesConfig, classes);
    }
    return facesConfig;
}
Also used : FacesBehaviorRenderer(jakarta.faces.render.FacesBehaviorRenderer) ApplicationImpl(org.apache.myfaces.config.impl.element.ApplicationImpl) ConverterImpl(org.apache.myfaces.config.impl.element.ConverterImpl) ComponentImpl(org.apache.myfaces.config.impl.element.ComponentImpl) ComponentTagDeclarationImpl(org.apache.myfaces.config.impl.element.ComponentTagDeclarationImpl) FaceletsResourceResolver(jakarta.faces.view.facelets.FaceletsResourceResolver) FacesConfigImpl(org.apache.myfaces.config.impl.element.FacesConfigImpl) FacesValidator(jakarta.faces.validator.FacesValidator) FacesRenderer(jakarta.faces.render.FacesRenderer) FacesConverter(jakarta.faces.convert.FacesConverter) NamedEvent(jakarta.faces.event.NamedEvent) FacesException(jakarta.faces.FacesException) FacesComponent(jakarta.faces.component.FacesComponent) FacesBehavior(jakarta.faces.component.behavior.FacesBehavior)

Example 2 with ApplicationImpl

use of org.apache.myfaces.config.impl.element.ApplicationImpl in project myfaces by apache.

the class FacesConfigUnmarshallerImpl method processApplication.

protected ApplicationImpl processApplication(Node node) {
    ApplicationImpl obj = new ApplicationImpl();
    onChild("action-listener", node, (n) -> {
        obj.addActionListener(getTextContent(n));
    });
    onChild("message-bundle", node, (n) -> {
        obj.addMessageBundle(getTextContent(n));
    });
    onChild("navigation-handler", node, (n) -> {
        obj.addNavigationHandler(getTextContent(n));
    });
    onChild("view-handler", node, (n) -> {
        obj.addViewHandler(getTextContent(n));
    });
    onChild("state-manager", node, (n) -> {
        obj.addStateManager(getTextContent(n));
    });
    onChild("property-resolver", node, (n) -> {
        obj.addPropertyResolver(getTextContent(n));
    });
    onChild("variable-resolver", node, (n) -> {
        obj.addVariableResolver(getTextContent(n));
    });
    onChild("el-resolver", node, (n) -> {
        obj.addElResolver(getTextContent(n));
    });
    onChild("resource-handler", node, (n) -> {
        obj.addResourceHandler(getTextContent(n));
    });
    onChild("default-render-kit-id", node, (n) -> {
        obj.addDefaultRenderkitId(getTextContent(n));
    });
    onChild("search-expression-handler", node, (n) -> {
        obj.addSearchExpressionHandler(getTextContent(n));
    });
    onChild("search-keyword-resolver", node, (n) -> {
        obj.addSearchKeywordResolver(getTextContent(n));
    });
    onChild("default-validators", node, (n) -> {
        obj.setDefaultValidatorsPresent();
        onChild("validator-id", n, (cn) -> {
            obj.addDefaultValidatorId(getTextContent(cn));
        });
    });
    onChild("locale-config", node, (n) -> {
        LocaleConfigImpl lc = new LocaleConfigImpl();
        onChild("default-locale", n, (cn) -> {
            lc.setDefaultLocale(getTextContent(cn));
        });
        onChild("supported-locale", n, (cn) -> {
            lc.addSupportedLocale(getTextContent(cn));
        });
        obj.addLocaleConfig(lc);
    });
    onChild("resource-bundle", node, (n) -> {
        ResourceBundleImpl rb = new ResourceBundleImpl();
        onChild("base-name", n, (cn) -> {
            rb.setBaseName(getTextContent(cn));
        });
        onChild("var", n, (cn) -> {
            rb.setVar(getTextContent(cn));
        });
        onChild("display-name", n, (cn) -> {
            rb.setDisplayName(getTextContent(cn));
        });
        obj.addResourceBundle(rb);
    });
    onChild("system-event-listener", node, (n) -> {
        SystemEventListenerImpl sel = new SystemEventListenerImpl();
        onChild("system-event-listener-class", n, (cn) -> {
            sel.setSystemEventListenerClass(getTextContent(cn));
        });
        onChild("system-event-class", n, (cn) -> {
            sel.setSystemEventClass(getTextContent(cn));
        });
        onChild("source-class", n, (cn) -> {
            sel.setSourceClass(getTextContent(cn));
        });
        obj.addSystemEventListener(sel);
    });
    onChild("resource-library-contracts", node, (n) -> {
        onChild("contract-mapping", n, (cn) -> {
            ContractMappingImpl cm = new ContractMappingImpl();
            onChild("url-pattern", cn, (ccn) -> {
                cm.addUrlPattern(getTextContent(ccn));
            });
            onChild("contracts", cn, (ccn) -> {
                cm.addContract(getTextContent(ccn));
            });
            obj.addResourceLibraryContractMapping(cm);
        });
    });
    return obj;
}
Also used : ResourceBundleImpl(org.apache.myfaces.config.impl.element.ResourceBundleImpl) SystemEventListenerImpl(org.apache.myfaces.config.impl.element.SystemEventListenerImpl) ApplicationImpl(org.apache.myfaces.config.impl.element.ApplicationImpl) ContractMappingImpl(org.apache.myfaces.config.impl.element.ContractMappingImpl) LocaleConfigImpl(org.apache.myfaces.config.impl.element.LocaleConfigImpl)

Aggregations

ApplicationImpl (org.apache.myfaces.config.impl.element.ApplicationImpl)2 FacesException (jakarta.faces.FacesException)1 FacesComponent (jakarta.faces.component.FacesComponent)1 FacesBehavior (jakarta.faces.component.behavior.FacesBehavior)1 FacesConverter (jakarta.faces.convert.FacesConverter)1 NamedEvent (jakarta.faces.event.NamedEvent)1 FacesBehaviorRenderer (jakarta.faces.render.FacesBehaviorRenderer)1 FacesRenderer (jakarta.faces.render.FacesRenderer)1 FacesValidator (jakarta.faces.validator.FacesValidator)1 FaceletsResourceResolver (jakarta.faces.view.facelets.FaceletsResourceResolver)1 ComponentImpl (org.apache.myfaces.config.impl.element.ComponentImpl)1 ComponentTagDeclarationImpl (org.apache.myfaces.config.impl.element.ComponentTagDeclarationImpl)1 ContractMappingImpl (org.apache.myfaces.config.impl.element.ContractMappingImpl)1 ConverterImpl (org.apache.myfaces.config.impl.element.ConverterImpl)1 FacesConfigImpl (org.apache.myfaces.config.impl.element.FacesConfigImpl)1 LocaleConfigImpl (org.apache.myfaces.config.impl.element.LocaleConfigImpl)1 ResourceBundleImpl (org.apache.myfaces.config.impl.element.ResourceBundleImpl)1 SystemEventListenerImpl (org.apache.myfaces.config.impl.element.SystemEventListenerImpl)1