org.mycore.frontend.editor
Class MCRInputValidator

java.lang.Object
  extended by org.mycore.frontend.editor.MCRInputValidator

public class MCRInputValidator
extends Object

This class provides input validation methods for editor data.

Author:
Frank Lützenkirchen

Method Summary
 boolean compare(String valueA, String valueB, String operator, String type, String format)
          Compares two input fields using a comparison operator.
static MCRInputValidator instance()
           
static void main(String[] args)
           
 boolean validateExternally(String clazz, String method, Element elem)
          Calls a "public static boolean" method in the given class and validates an XML element
 boolean validateExternally(String clazz, String method, String value)
          Calls a "public static boolean" method in the given class and validates the value externally using the given method in that class.
 boolean validateExternally(String clazz, String method, String value1, String value2)
          Calls a "public static boolean" method in the given class and validates the two values externally using the given method in that class.
 boolean validateLength(String input, String smin, String smax)
          Checks an input string for minimum and/or maximum length.
 boolean validateMinMaxType(String input, String type, String min, String max, String format)
          Checks input for correct data type and minimum/maximum value.
 boolean validateRegularExpression(String input, String regexp)
          Checks the input string against a regular expression.
 boolean validateRequired(String input)
          Checks if input is null or empty or just contains whitespace.
 boolean validateXSLCondition(Element input, String condition)
           
 boolean validateXSLCondition(String input, String condition)
          Checks the input string against an XSL condition.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

instance

public static MCRInputValidator instance()

validateXSLCondition

public boolean validateXSLCondition(String input,
                                    String condition)
Checks the input string against an XSL condition. The syntax of the condition string is same as it would be usable in a xsl:if condition. The input string can be referenced by "." or "text()" in the condition, for example a condition could be "starts-with(.,'http://')". If input string is null, false is returned.

Parameters:
input - the string that should be validated
condition - the XSL condition as it would be used in xsl:when or xsl:if
Returns:
false if input is null, otherwise the result of the test is returned
Throws:
MCRConfigurationException - if XSL condition has syntax errors

validateXSLCondition

public boolean validateXSLCondition(Element input,
                                    String condition)

validateRegularExpression

public boolean validateRegularExpression(String input,
                                         String regexp)
Checks the input string against a regular expression.

Parameters:
input - the string that should be validated
regexp - the regular expression using the syntax of the java.util.regex.Pattern class
Returns:
false if input is null, otherwise the result of the test is returned
See Also:
Pattern.compile(java.lang.String)

validateLength

public boolean validateLength(String input,
                              String smin,
                              String smax)
Checks an input string for minimum and/or maximum length. The minimum and maximum length must be given as a string that contains the actual int number, both arguments are optional if one of the limits should not be checked.

Parameters:
input - the input string thats length should be checked
smin - minimum length as a string, or null if min lenght should not be checked
smax - maximum length as a string, or null if max length should not be checked
Returns:
true, if the string matches the given min and max lengths

validateRequired

public boolean validateRequired(String input)
Checks if input is null or empty or just contains whitespace.

Parameters:
input - the string to be checked
Returns:
false if input is null or empty or just blanks

validateMinMaxType

public boolean validateMinMaxType(String input,
                                  String type,
                                  String min,
                                  String max,
                                  String format)
Checks input for correct data type and minimum/maximum value. Possible data types are string, integer, decimal or datetime. The min and max arguments are optional and must be expressed as strings. The min and max value are used inclusive in the allowed range of values. If no check for min or max value should be performed, null can be given for that argument. For datetime input, the format of the string must be given as defined in SimpleDateFormat. For decimal input, the format argument should contain a two-character, lowercase language code as defined by ISO 639. This code determines the locale that is used to parse decimal values. If null is given, the default locale will be used. Ffor other data types null should be used as the format argument. Usage examples:

Parameters:
input - the input string to check
type - one of "string", "integer", "decimal" or "datetime"
min - the minimum value as a string, or null if min should not be tested
max - the maximum value as a string, or null if max should not be tested
format - for datetime input, a java.text.SimpleDateFormat pattern; for decimal input, a ISO-639 language code
Returns:
true if input matches the given data type, min, max value and date time format
See Also:
SimpleDateFormat, Locale, NumberFormat.getInstance(java.util.Locale)

compare

public boolean compare(String valueA,
                       String valueB,
                       String operator,
                       String type,
                       String format)
Compares two input fields using a comparison operator.

Parameters:
valueA - the first input string to check
valueB - the second input string to check
type - one of "string", "integer", "decimal" or "datetime"
operator - One of =, <, >, <=, >=, !=
format - for datetime input, a java.text.SimpleDateFormat pattern; for decimal input, a ISO-639 language code
Returns:
true if the compare result is true OR one of the input fields is empty OR one of the input fields is in wrong format.

validateExternally

public boolean validateExternally(String clazz,
                                  String method,
                                  String value)
Calls a "public static boolean" method in the given class and validates the value externally using the given method in that class.

Parameters:
clazz - the name of the class that contains the validation method
method - the name of the public static boolean method that should be called
value - the value to validate
Returns:
true, if the value validates

validateExternally

public boolean validateExternally(String clazz,
                                  String method,
                                  String value1,
                                  String value2)
Calls a "public static boolean" method in the given class and validates the two values externally using the given method in that class.

Parameters:
clazz - the name of the class that contains the validation method
method - the name of the public static boolean method that should be called
value1 - the first value to validate
value2 - the second value to validate
Returns:
true, if the two values validate

validateExternally

public boolean validateExternally(String clazz,
                                  String method,
                                  Element elem)
Calls a "public static boolean" method in the given class and validates an XML element

Parameters:
clazz - the name of the class that contains the validation method
method - the name of the public static boolean method that should be called
elem - the XML element to validate
Returns:
true, if the XML element validates

main

public static void main(String[] args)