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;
}
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;
}
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);
}
}
}
}
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());
}
}
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;
}
Aggregations