in core/cocoon-sitemap/cocoon-sitemap-impl/src/main/java/org/apache/cocoon/acting/AbstractValidatorAction.java [399:549]
private ValidatorActionHelper validateString(
String name,
Configuration constraints,
Configuration conf,
Map params,
Object param) {
String value = null;
String dflt = getDefault(conf, constraints);
boolean nullable = getNullable(conf, constraints);
if (getLogger().isDebugEnabled())
getLogger().debug("Validating string parameter " + name);
try {
value = getStringValue(param);
} catch (Exception e) {
// ClassCastException
return new ValidatorActionHelper(value, ValidatorActionResult.ERROR);
}
if (value == null) {
if (getLogger().isDebugEnabled())
getLogger().debug("String parameter " + name + " is null");
if (!nullable) {
return new ValidatorActionHelper(value, ValidatorActionResult.ISNULL);
} else {
return new ValidatorActionHelper(dflt);
}
}
if (constraints != null) {
String eq = constraints.getAttribute("equals-to", "");
eq = conf.getAttribute("equals-to", eq);
String eqp = constraints.getAttribute("equals-to-param", "");
eqp = conf.getAttribute("equals-to-param", eqp);
String regex = conf.getAttribute("matches-regex", "");
regex = constraints.getAttribute("matches-regex", regex);
String oneOf = conf.getAttribute("one-of", "");
oneOf = constraints.getAttribute("one-of", oneOf);
Long minlen = getAttributeAsLong(conf, "min-len", null);
minlen = getAttributeAsLong(constraints, "min-len", minlen);
Long maxlen = getAttributeAsLong(conf, "max-len", null);
maxlen = getAttributeAsLong(constraints, "max-len", maxlen);
// Validate whether param is equal to constant
if (!"".equals(eq)) {
if (getLogger().isDebugEnabled())
getLogger().debug("String parameter " + name + " should be equal to " + eq);
if (!value.equals(eq)) {
if (getLogger().isDebugEnabled())
getLogger().debug("and it is not");
return new ValidatorActionHelper(value, ValidatorActionResult.NOMATCH);
}
}
// Validate whether param is equal to another param
// FIXME: take default value of param being compared with into
// account?
if (!"".equals(eqp)) {
if (getLogger().isDebugEnabled())
getLogger().debug(
"String parameter " + name + " should be equal to " + params.get(eqp));
if (!value.equals(params.get(eqp))) {
if (getLogger().isDebugEnabled())
getLogger().debug("and it is not");
return new ValidatorActionHelper(value, ValidatorActionResult.NOMATCH);
}
}
// Validate whether param length is at least of minimum length
if (minlen != null) {
if (getLogger().isDebugEnabled())
getLogger().debug(
"String parameter "
+ name
+ " should be at least "
+ minlen
+ " characters long");
if (value.length() < minlen.longValue()) {
if (getLogger().isDebugEnabled())
getLogger().debug("and it is shorter (" + value.length() + ")");
return new ValidatorActionHelper(value, ValidatorActionResult.TOOSMALL);
}
}
// Validate whether param length is at most of maximum length
if (maxlen != null) {
if (getLogger().isDebugEnabled())
getLogger().debug(
"String parameter "
+ name
+ " should be at most "
+ maxlen
+ " characters long");
if (value.length() > maxlen.longValue()) {
if (getLogger().isDebugEnabled())
getLogger().debug("and it is longer (" + value.length() + ")");
return new ValidatorActionHelper(value, ValidatorActionResult.TOOLARGE);
}
}
// Validate wheter param matches regular expression
if (!"".equals(regex)) {
if (getLogger().isDebugEnabled())
getLogger().debug(
"String parameter " + name + " should match regexp \"" + regex + "\"");
try {
RE r = new RE(regex);
if (!r.match(value)) {
if (getLogger().isDebugEnabled())
getLogger().debug("and it does not match");
return new ValidatorActionHelper(value, ValidatorActionResult.NOMATCH);
}
} catch (RESyntaxException rese) {
if (getLogger().isDebugEnabled())
getLogger().error("String parameter " + name + " regex error ", rese);
return new ValidatorActionHelper(value, ValidatorActionResult.NOMATCH);
}
}
// Validates against a set of possibilities
if (!"".equals(oneOf)) {
if (getLogger().isDebugEnabled())
getLogger().debug(
"String parameter " + name + " should be one of \"" + oneOf + "\"");
if (!oneOf.startsWith("|"))
oneOf = "|" + oneOf;
if (!oneOf.endsWith("|"))
oneOf = oneOf + "|";
if (value.indexOf("|") != -1) {
if (getLogger().isDebugEnabled())
getLogger().debug(
"String parameter " + name + "contains \"|\" - can't validate that.");
return new ValidatorActionHelper(value, ValidatorActionResult.ERROR);
}
if (oneOf.indexOf("|" + value + "|") == -1) {
if (getLogger().isDebugEnabled())
getLogger().debug("and it is not");
return new ValidatorActionHelper(value, ValidatorActionResult.NOMATCH);
}
return new ValidatorActionHelper(value, ValidatorActionResult.OK);
}
}
return new ValidatorActionHelper(value);
}