Search in sources :

Example 1 with Validator

use of org.interlis2.validator.Validator in project struts by apache.

the class PlexusObjectFactory method buildValidator.

/* (non-Javadoc)
     * @see com.opensymphony.xwork2.ObjectFactory#buildValidator(java.lang.String, java.util.Map, java.util.Map)
     */
public Validator buildValidator(String className, Map params, Map extraContext) throws Exception {
    Map context = new HashMap();
    context.put(PLEXUS_COMPONENT_TYPE, Validator.class.getName());
    Validator validator = (Validator) buildBean(className, context);
    reflectionProvider.setProperties(params, validator);
    return validator;
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Validator(com.opensymphony.xwork2.validator.Validator)

Example 2 with Validator

use of org.interlis2.validator.Validator in project struts by apache.

the class SpringObjectFactoryTest method testFallsBackToDefaultObjectFactoryValidatorBuilding.

public void testFallsBackToDefaultObjectFactoryValidatorBuilding() throws Exception {
    Map<String, Object> extraContext = new HashMap<>();
    Validator validator = objectFactory.buildValidator(RequiredStringValidator.class.getName(), new HashMap<String, Object>(), extraContext);
    assertEquals(RequiredStringValidator.class, validator.getClass());
}
Also used : HashMap(java.util.HashMap) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) Validator(com.opensymphony.xwork2.validator.Validator) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) ExpressionValidator(com.opensymphony.xwork2.validator.validators.ExpressionValidator)

Example 3 with Validator

use of org.interlis2.validator.Validator in project gretl by sogis.

the class IliValidator method validate.

@TaskAction
public void validate() {
    log = LogEnvironment.getLogger(IliValidator.class);
    if (dataFiles == null) {
        return;
    }
    FileCollection dataFilesCollection = null;
    if (dataFiles instanceof FileCollection) {
        dataFilesCollection = (FileCollection) dataFiles;
    } else {
        dataFilesCollection = getProject().files(dataFiles);
    }
    if (dataFilesCollection == null || dataFilesCollection.isEmpty()) {
        return;
    }
    List<String> files = new ArrayList<String>();
    for (java.io.File fileObj : dataFilesCollection) {
        String fileName = fileObj.getPath();
        files.add(fileName);
    }
    Settings settings = new Settings();
    initSettings(settings);
    List<String> userFunctionList = new ArrayList<String>();
    userFunctionList.add("ch.so.agi.ilivalidator.ext.IsHttpResourceIoxPlugin");
    userFunctionList.add("ch.so.agi.ilivalidator.ext.AreaIoxPlugin");
    userFunctionList.add("ch.so.agi.ilivalidator.ext.LengthIoxPlugin");
    userFunctionList.add("ch.so.agi.ilivalidator.ext.IsValidDocumentsCycleIoxPlugin");
    userFunctionList.add("ch.so.agi.ilivalidator.ext.RingSelfIntersectionIoxPlugin");
    userFunctionList.add("ch.so.agi.ilivalidator.ext.TooFewPointsPolylineIoxPlugin");
    // userFunctionList.add("ch.so.agi.ilivalidator.ext.IsHttpResourceFromOerebMultilingualUriIoxPlugin");
    Map<String, Class> userFunctions = new HashMap<String, Class>();
    try {
        for (String userFunction : userFunctionList) {
            Class clazz = Class.forName(userFunction);
            IoxPlugin plugin = (IoxPlugin) clazz.newInstance();
            userFunctions.put(((InterlisFunction) plugin).getQualifiedIliName(), clazz);
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        log.info("Class not found");
    } catch (InstantiationException e) {
        e.printStackTrace();
        log.error("cannot instantiate class", e);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        log.error("Class not accessible", e);
    }
    settings.setTransientObject(ch.interlis.iox_j.validator.Validator.CONFIG_CUSTOM_FUNCTIONS, userFunctions);
    validationOk = new Validator().validate(files.toArray(new String[files.size()]), settings);
    if (!validationOk && failOnError) {
        throw new TaskExecutionException(this, new Exception("validation failed"));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FileCollection(org.gradle.api.file.FileCollection) TaskExecutionException(org.gradle.api.tasks.TaskExecutionException) TaskExecutionException(org.gradle.api.tasks.TaskExecutionException) IoxPlugin(ch.interlis.iox_j.plugins.IoxPlugin) Settings(ch.ehi.basics.settings.Settings) Validator(org.interlis2.validator.Validator) TaskAction(org.gradle.api.tasks.TaskAction)

Example 4 with Validator

use of org.interlis2.validator.Validator in project struts by apache.

the class ShowValidatorAction method execute.

public String execute() throws Exception {
    loadValidators();
    Validator validator = getSelectedValidator();
    properties = new TreeSet<PropertyInfo>();
    try {
        Map<String, Object> context = reflectionContextFactory.createDefaultContext(validator);
        BeanInfo beanInfoFrom;
        try {
            beanInfoFrom = Introspector.getBeanInfo(validator.getClass(), Object.class);
        } catch (IntrospectionException e) {
            LOG.error("An error occurred", e);
            addActionError("An error occurred while introspecting a validator of type " + validator.getClass().getName());
            return ERROR;
        }
        PropertyDescriptor[] pds = beanInfoFrom.getPropertyDescriptors();
        for (PropertyDescriptor pd : pds) {
            String name = pd.getName();
            Object value = null;
            if (pd.getReadMethod() == null) {
                value = "No read method for property";
            } else {
                try {
                    value = reflectionProvider.getValue(name, context, validator);
                } catch (ReflectionException e) {
                    addActionError("Caught exception while getting property value for '" + name + "' on validator of type " + validator.getClass().getName());
                }
            }
            properties.add(new PropertyInfo(name, pd.getPropertyType(), value));
        }
    } catch (Exception e) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("Unable to retrieve properties.", e);
        }
        addActionError("Unable to retrieve properties: " + e.toString());
    }
    if (hasErrors()) {
        return ERROR;
    } else {
        return SUCCESS;
    }
}
Also used : ReflectionException(com.opensymphony.xwork2.util.reflection.ReflectionException) PropertyDescriptor(java.beans.PropertyDescriptor) BeanInfo(java.beans.BeanInfo) IntrospectionException(java.beans.IntrospectionException) Validator(com.opensymphony.xwork2.validator.Validator) IntrospectionException(java.beans.IntrospectionException) ReflectionException(com.opensymphony.xwork2.util.reflection.ReflectionException)

Example 5 with Validator

use of org.interlis2.validator.Validator in project struts by apache.

the class SpringObjectFactoryTest method testObtainValidatorBySpringName.

public void testObtainValidatorBySpringName() throws Exception {
    sac.registerPrototype("expression-validator", ExpressionValidator.class, new MutablePropertyValues());
    Map<String, Object> extraContext = new HashMap<>();
    Validator validator = objectFactory.buildValidator("expression-validator", new HashMap<String, Object>(), extraContext);
    assertEquals(ExpressionValidator.class, validator.getClass());
}
Also used : HashMap(java.util.HashMap) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Validator(com.opensymphony.xwork2.validator.Validator) RequiredStringValidator(com.opensymphony.xwork2.validator.validators.RequiredStringValidator) ExpressionValidator(com.opensymphony.xwork2.validator.validators.ExpressionValidator)

Aggregations

Validator (com.opensymphony.xwork2.validator.Validator)5 HashMap (java.util.HashMap)4 ExpressionValidator (com.opensymphony.xwork2.validator.validators.ExpressionValidator)2 RequiredStringValidator (com.opensymphony.xwork2.validator.validators.RequiredStringValidator)2 Settings (ch.ehi.basics.settings.Settings)1 IoxPlugin (ch.interlis.iox_j.plugins.IoxPlugin)1 ReflectionException (com.opensymphony.xwork2.util.reflection.ReflectionException)1 BeanInfo (java.beans.BeanInfo)1 IntrospectionException (java.beans.IntrospectionException)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 FileCollection (org.gradle.api.file.FileCollection)1 TaskAction (org.gradle.api.tasks.TaskAction)1 TaskExecutionException (org.gradle.api.tasks.TaskExecutionException)1 Validator (org.interlis2.validator.Validator)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1