Search in sources :

Example 1 with Validator

use of com.opensymphony.xwork2.validator.Validator in project struts by apache.

the class AnnotationActionValidatorManager method getValidators.

public List<Validator> getValidators(Class clazz, String context, String method) {
    final String validatorKey = buildValidatorKey(clazz, context);
    final List<ValidatorConfig> cfgs;
    if (validatorCache.containsKey(validatorKey)) {
        if (reloadingConfigs) {
            validatorCache.put(validatorKey, buildValidatorConfigs(clazz, context, true, null));
        }
    } else {
        validatorCache.put(validatorKey, buildValidatorConfigs(clazz, context, false, null));
    }
    // get the set of validator configs
    cfgs = new ArrayList<ValidatorConfig>(validatorCache.get(validatorKey));
    ValueStack stack = ActionContext.getContext().getValueStack();
    // create clean instances of the validators for the caller's use
    ArrayList<Validator> validators = new ArrayList<>(cfgs.size());
    for (ValidatorConfig cfg : cfgs) {
        if (method == null || method.equals(cfg.getParams().get("methodName"))) {
            Validator validator = validatorFactory.getValidator(new ValidatorConfig.Builder(cfg).removeParam("methodName").build());
            validator.setValidatorType(cfg.getType());
            validator.setValueStack(stack);
            validators.add(validator);
        }
    }
    return validators;
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 2 with Validator

use of com.opensymphony.xwork2.validator.Validator in project struts by apache.

the class DefaultActionValidatorManager method getValidators.

public synchronized List<Validator> getValidators(Class clazz, String context, String method) {
    final String validatorKey = buildValidatorKey(clazz, context);
    if (validatorCache.containsKey(validatorKey)) {
        if (reloadingConfigs) {
            validatorCache.put(validatorKey, buildValidatorConfigs(clazz, context, true, null));
        }
    } else {
        validatorCache.put(validatorKey, buildValidatorConfigs(clazz, context, false, null));
    }
    ValueStack stack = ActionContext.getContext().getValueStack();
    // get the set of validator configs
    List<ValidatorConfig> cfgs = validatorCache.get(validatorKey);
    // create clean instances of the validators for the caller's use
    ArrayList<Validator> validators = new ArrayList<>(cfgs.size());
    for (ValidatorConfig cfg : cfgs) {
        if (method == null || method.equals(cfg.getParams().get("methodName"))) {
            Validator validator = validatorFactory.getValidator(cfg);
            validator.setValidatorType(cfg.getType());
            validator.setValueStack(stack);
            validators.add(validator);
        }
    }
    return validators;
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 3 with Validator

use of com.opensymphony.xwork2.validator.Validator in project struts by apache.

the class DefaultValidatorFileParser method parseValidatorDefinitions.

public void parseValidatorDefinitions(Map<String, String> validators, InputStream is, String resourceName) {
    InputSource in = new InputSource(is);
    in.setSystemId(resourceName);
    Map<String, String> dtdMappings = new HashMap<>();
    dtdMappings.put("-//Apache Struts//XWork Validator Config 1.0//EN", "xwork-validator-config-1.0.dtd");
    dtdMappings.put("-//Apache Struts//XWork Validator Definition 1.0//EN", "xwork-validator-definition-1.0.dtd");
    Document doc = DomHelper.parse(in, dtdMappings);
    if (doc != null) {
        NodeList nodes = doc.getElementsByTagName("validator");
        for (int i = 0; i < nodes.getLength(); i++) {
            Element validatorElement = (Element) nodes.item(i);
            String name = validatorElement.getAttribute("name");
            String className = validatorElement.getAttribute("class");
            try {
                // catch any problems here
                objectFactory.buildValidator(className, new HashMap<String, Object>(), ActionContext.getContext().getContextMap());
                validators.put(name, className);
            } catch (Exception e) {
                throw new ConfigurationException("Unable to load validator class " + className, e, validatorElement);
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Example 4 with Validator

use of com.opensymphony.xwork2.validator.Validator in project struts by apache.

the class DefaultValidatorFileParser method addValidatorConfigs.

private void addValidatorConfigs(ValidatorFactory factory, NodeList validatorNodes, Map<String, Object> extraParams, List<ValidatorConfig> validatorCfgs) {
    for (int j = 0; j < validatorNodes.getLength(); j++) {
        Element validatorElement = (Element) validatorNodes.item(j);
        String validatorType = validatorElement.getAttribute("type");
        Map<String, Object> params = new HashMap<String, Object>(extraParams);
        params.putAll(XmlHelper.getParams(validatorElement));
        // ensure that the type is valid...
        try {
            factory.lookupRegisteredValidatorType(validatorType);
        } catch (IllegalArgumentException ex) {
            throw new ConfigurationException("Invalid validation type: " + validatorType, validatorElement);
        }
        ValidatorConfig.Builder vCfg = new ValidatorConfig.Builder(validatorType).addParams(params).location(DomHelper.getLocationObject(validatorElement)).shortCircuit(Boolean.valueOf(validatorElement.getAttribute("short-circuit")));
        NodeList messageNodes = validatorElement.getElementsByTagName("message");
        Element messageElement = (Element) messageNodes.item(0);
        final Node defaultMessageNode = messageElement.getFirstChild();
        String defaultMessage = (defaultMessageNode == null) ? "" : defaultMessageNode.getNodeValue();
        vCfg.defaultMessage(defaultMessage);
        Map<String, String> messageParams = XmlHelper.getParams(messageElement);
        String key = messageElement.getAttribute("key");
        if ((key != null) && (key.trim().length() > 0)) {
            vCfg.messageKey(key);
            if (messageParams.containsKey("defaultMessage")) {
                vCfg.defaultMessage(messageParams.get("defaultMessage"));
            }
            // Sort the message param. those with keys as '1', '2', '3' etc. (numeric values)
            // are i18n message parameter, others are excluded.
            TreeMap<Integer, String> sortedMessageParameters = new TreeMap<Integer, String>();
            for (Map.Entry<String, String> messageParamEntry : messageParams.entrySet()) {
                try {
                    int _order = Integer.parseInt(messageParamEntry.getKey());
                    sortedMessageParameters.put(_order, messageParamEntry.getValue());
                } catch (NumberFormatException e) {
                // ignore if its not numeric.
                }
            }
            vCfg.messageParams(sortedMessageParameters.values().toArray(new String[sortedMessageParameters.values().size()]));
        } else {
            if (messageParams != null && (messageParams.size() > 0)) {
                // let's warn the user.
                if (LOG.isWarnEnabled()) {
                    LOG.warn("validator of type [" + validatorType + "] have i18n message parameters defined but no i18n message key, it's parameters will be ignored");
                }
            }
        }
        validatorCfgs.add(vCfg.build());
    }
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Example 5 with Validator

use of com.opensymphony.xwork2.validator.Validator in project struts by apache.

the class Form method getValidators.

public List getValidators(String name) {
    Class actionClass = (Class) getParameters().get("actionClass");
    if (actionClass == null) {
        return Collections.EMPTY_LIST;
    }
    String formActionValue = findString(action);
    ActionMapping mapping = actionMapper.getMappingFromActionName(formActionValue);
    if (mapping == null) {
        mapping = actionMapper.getMappingFromActionName((String) getParameters().get("actionName"));
    }
    if (mapping == null) {
        return Collections.EMPTY_LIST;
    }
    String actionName = mapping.getName();
    String methodName = null;
    if (isValidateAnnotatedMethodOnly(actionName)) {
        methodName = mapping.getMethod();
    }
    List<Validator> actionValidators = actionValidatorManager.getValidators(actionClass, actionName, methodName);
    List<Validator> validators = new ArrayList<>();
    findFieldValidators(name, actionClass, actionName, actionValidators, validators, "");
    return validators;
}
Also used : ActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping) ArrayList(java.util.ArrayList) VisitorFieldValidator(com.opensymphony.xwork2.validator.validators.VisitorFieldValidator)

Aggregations

ValueStack (com.opensymphony.xwork2.util.ValueStack)19 DummyValidatorContext (com.opensymphony.xwork2.validator.DummyValidatorContext)18 ValidatorContext (com.opensymphony.xwork2.validator.ValidatorContext)18 URLValidator (com.opensymphony.xwork2.validator.validators.URLValidator)16 RegexFieldValidator (com.opensymphony.xwork2.validator.validators.RegexFieldValidator)12 EmailValidator (com.opensymphony.xwork2.validator.validators.EmailValidator)9 ValueStackFactory (com.opensymphony.xwork2.util.ValueStackFactory)7 RequiredStringValidator (com.opensymphony.xwork2.validator.validators.RequiredStringValidator)6 HashMap (java.util.HashMap)6 ActionSupport (com.opensymphony.xwork2.ActionSupport)5 Validator (com.opensymphony.xwork2.validator.Validator)5 ExpressionValidator (com.opensymphony.xwork2.validator.validators.ExpressionValidator)5 VisitorFieldValidator (com.opensymphony.xwork2.validator.validators.VisitorFieldValidator)5 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)4 RequiredFieldValidator (com.opensymphony.xwork2.validator.validators.RequiredFieldValidator)4 StringLengthFieldValidator (com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator)4 TextProviderFactory (com.opensymphony.xwork2.TextProviderFactory)3 ConversionErrorFieldValidator (com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator)3 DateRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator)3 DoubleRangeFieldValidator (com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator)3