Regular expression match and unmatch can be set as a validator. settings_categories_before_merge
authormentlicher@netbeans.org
Wed, 05 Sep 2001 10:27:59 +0000
changeset 15885d660a1c4ceb
parent 1587 03b9cc78a996
child 1589 19828f72dcb3
Regular expression match and unmatch can be set as a validator.
vcscore/src/org/netbeans/modules/vcscore/util/Bundle.properties
vcscore/src/org/netbeans/modules/vcscore/util/VariableInputValidator.java
     1.1 --- a/vcscore/src/org/netbeans/modules/vcscore/util/Bundle.properties	Wed Sep 05 10:27:15 2001 +0000
     1.2 +++ b/vcscore/src/org/netbeans/modules/vcscore/util/Bundle.properties	Wed Sep 05 10:27:59 2001 +0000
     1.3 @@ -34,6 +34,9 @@
     1.4  # {0} is the label of a text field in Variable Input Dialog
     1.5  VariableInputValidator.NotEmpty=The value of {0} should not be empty.
     1.6  VariableInputValidator.BadValidator=Unrecognized validator {0}.
     1.7 +VariableInputValidator.BadRegExp=Bad validating regular expression {0} for {1}: {2}
     1.8 +VariableInputValidator.RegExpNotMatched=The required regular expression {0} was not matched in the value of {1}
     1.9 +VariableInputValidator.RegExpNotUnmatched=The regular expression {0} should not be matched in the value of {1}
    1.10  
    1.11  # VariableInputDescriptor
    1.12  EXC_UnrecognizedItem=Unrecognized item at position {0}.
     2.1 --- a/vcscore/src/org/netbeans/modules/vcscore/util/VariableInputValidator.java	Wed Sep 05 10:27:15 2001 +0000
     2.2 +++ b/vcscore/src/org/netbeans/modules/vcscore/util/VariableInputValidator.java	Wed Sep 05 10:27:59 2001 +0000
     2.3 @@ -27,6 +27,8 @@
     2.4  
     2.5      public static final String VALIDATOR = "VALIDATOR_";
     2.6      public static final String VALIDATOR_NON_EMPTY = VALIDATOR + "NON_EMPTY";
     2.7 +    public static final String VALIDATOR_REGEXP_MATCH = VALIDATOR + "REGEXP_MATCH(";
     2.8 +    public static final String VALIDATOR_REGEXP_UNMATCH = VALIDATOR + "REGEXP_UNMATCH(";
     2.9  
    2.10      private boolean valid;
    2.11      private String message = null;
    2.12 @@ -40,6 +42,10 @@
    2.13          else {
    2.14              if (VALIDATOR_NON_EMPTY.equals(validator)) {
    2.15                  validateNonEmpty(component);
    2.16 +            } else if (validator.startsWith(VALIDATOR_REGEXP_MATCH)) {
    2.17 +                validateRegExpMatch(component, validator, true);
    2.18 +            } else if (validator.startsWith(VALIDATOR_REGEXP_UNMATCH)) {
    2.19 +                validateRegExpMatch(component, validator, false);
    2.20              } else {
    2.21                  valid = false;
    2.22                  message = g("VariableInputValidator.BadValidator", validator);
    2.23 @@ -57,6 +63,44 @@
    2.24              valid = true;
    2.25          }
    2.26      }
    2.27 +    
    2.28 +    private void validateRegExpMatch(VariableInputComponent component, String validator, boolean match) {
    2.29 +        int index = validator.lastIndexOf(')');
    2.30 +        if (index < 0) index = validator.length();
    2.31 +        String regExp;
    2.32 +        if (match) {
    2.33 +            regExp = validator.substring(VALIDATOR_REGEXP_MATCH.length(), index);
    2.34 +        } else {
    2.35 +            regExp = validator.substring(VALIDATOR_REGEXP_UNMATCH.length(), index);
    2.36 +        }
    2.37 +        valid = validateRegExpMatch(component, regExp);
    2.38 +        if (message != null) valid = false;
    2.39 +        else if (valid != match) {
    2.40 +            if (match) {
    2.41 +                message = g("VariableInputValidator.RegExpNotMatched", regExp, component.getLabel());
    2.42 +            } else {
    2.43 +                message = g("VariableInputValidator.RegExpNotUnmatched", regExp, component.getLabel());
    2.44 +            }
    2.45 +            variable = component.getVariable();
    2.46 +            valid = false;
    2.47 +        } else {
    2.48 +            valid = true;
    2.49 +        }
    2.50 +    }
    2.51 +    
    2.52 +    private boolean validateRegExpMatch(VariableInputComponent component, String regExpStr) {
    2.53 +        org.apache.regexp.RE regExp;
    2.54 +        try {
    2.55 +            regExp = new org.apache.regexp.RE(regExpStr);
    2.56 +        } catch (org.apache.regexp.RESyntaxException exc) {
    2.57 +            message = g("VariableInputValidator.BadRegExp", regExpStr, component.getLabel(), exc.getLocalizedMessage());
    2.58 +            variable = component.getVariable();
    2.59 +            return false;
    2.60 +        }
    2.61 +        String value = component.getValue();
    2.62 +        if (value == null) value = "";
    2.63 +        return regExp.match(value);
    2.64 +    }
    2.65  
    2.66      /** Tells whether the validation was successfull.
    2.67       */
    2.68 @@ -86,4 +130,18 @@
    2.69                  new Object[] { obj }
    2.70              );
    2.71      }
    2.72 +
    2.73 +    private static String g(String pattern, Object obj, Object obj2) {
    2.74 +        return MessageFormat.format(
    2.75 +                NbBundle.getBundle(VariableInputValidator.class).getString(pattern),
    2.76 +                new Object[] { obj, obj2 }
    2.77 +            );
    2.78 +    }
    2.79 +
    2.80 +    private static String g(String pattern, Object obj, Object obj2, Object obj3) {
    2.81 +        return MessageFormat.format(
    2.82 +                NbBundle.getBundle(VariableInputValidator.class).getString(pattern),
    2.83 +                new Object[] { obj, obj2, obj3 }
    2.84 +            );
    2.85 +    }
    2.86  }