use of jakarta.faces.validator.FacesValidator in project myfaces by apache.
the class ApplicationImpl method createValidator.
@Override
public final Validator createValidator(final String validatorId) throws FacesException {
Assert.notEmpty(validatorId, "validatorId");
Class<? extends Validator> validatorClass = getObjectFromClassMap(validatorId, _validatorClassMap);
if (validatorClass == null) {
String message = "Unknown validator id '" + validatorId + "'.";
log.severe(message);
throw new FacesException(message);
}
try {
if (!_cdiManagedValidatorMap.containsKey(validatorClass)) {
FacesValidator annotation = validatorClass.getAnnotation(FacesValidator.class);
if (annotation != null && annotation.managed()) {
_cdiManagedValidatorMap.put(validatorClass, true);
} else {
_cdiManagedValidatorMap.put(validatorClass, false);
}
}
boolean managed = _cdiManagedValidatorMap.get(validatorClass);
Validator validator = null;
if (managed) {
validator = new FacesValidatorCDIWrapper(validatorClass, validatorId);
_handleAttachedResourceDependencyAnnotations(getFacesContext(), ((FacesWrapper<Validator>) validator).getWrapped());
} else {
validator = createValidatorInstance(validatorClass);
_handleAttachedResourceDependencyAnnotations(getFacesContext(), validator);
}
return validator;
} catch (Exception e) {
log.log(Level.SEVERE, "Could not instantiate validator " + validatorClass, e);
throw new FacesException("Could not instantiate validator: " + validatorClass, e);
}
}
use of jakarta.faces.validator.FacesValidator in project mojarra by eclipse-ee4j.
the class CdiUtils method createValidator.
/**
* Create a validator using the FacesValidator value attribute.
*
* @param beanManager the bean manager.
* @param value the value attribute.
* @return the validator, or null if we could not match one.
*/
public static Validator<?> createValidator(BeanManager beanManager, String value) {
Annotation qualifier = FacesValidator.Literal.of(value, false, true);
// Try to find parameterized validator first
Validator<?> managedValidator = (Validator<?>) getBeanReferenceByType(beanManager, VALIDATOR_TYPE, qualifier);
if (managedValidator == null) {
// No parameterized validator, try raw validator
managedValidator = getBeanReference(beanManager, Validator.class, qualifier);
}
if (managedValidator == null) {
// Still nothing found, try default qualifier and value as bean name.
qualifier = FacesValidator.Literal.of("", false, true);
managedValidator = (Validator<?>) getBeanReferenceByType(beanManager, VALIDATOR_TYPE, value, qualifier);
}
if (managedValidator == null) {
// No parameterized validator, try raw validator
managedValidator = getBeanReference(beanManager, Validator.class, value, qualifier);
}
if (managedValidator != null) {
return new CdiValidator(value, managedValidator);
}
return null;
}
use of jakarta.faces.validator.FacesValidator 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;
}
use of jakarta.faces.validator.FacesValidator in project mojarra by eclipse-ee4j.
the class ValidatorConfigHandler method collect.
/**
* @see com.sun.faces.application.annotation.ConfigAnnotationHandler#collect(Class, java.lang.annotation.Annotation)
*/
@Override
public void collect(Class<?> target, Annotation annotation) {
if (validators == null) {
validators = new HashMap<>();
}
FacesValidator validatorAnnotation = (FacesValidator) annotation;
String value = ((FacesValidator) annotation).value();
if (null == value || 0 == value.length()) {
value = target.getSimpleName();
value = Character.toLowerCase(value.charAt(0)) + value.substring(1);
}
ValidatorInfo info = new ValidatorInfo(value, validatorAnnotation.isDefault());
validators.put(info, target.getName());
}
Aggregations