/projects/wct-1.5.2/WCTCore/src-app/org/webcurator/ui/common/validation/AbstractBaseValidator.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 87 lines · 26 code · 10 blank · 51 comment · 0 complexity · b66598e67a817fbb8085096f816b2f91 MD5 · raw file

  1. /*
  2. * Copyright 2006 The National Library of New Zealand
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.webcurator.ui.common.validation;
  17. import java.util.regex.Pattern;
  18. import org.springframework.context.support.DefaultMessageSourceResolvable;
  19. import org.springframework.validation.Validator;
  20. import org.webcurator.ui.common.Constants;
  21. /**
  22. * Abstract base class to be extended by all WCT Validators.
  23. * This base class provides some useful methods for building object arrays
  24. * for the i8n messages.
  25. * @author nwaight
  26. */
  27. public abstract class AbstractBaseValidator implements Validator {
  28. /** Regular Expression used to validate an email address. */
  29. public static final String EMAIL_REGEX = "^[a-zA-Z0-9]+([_\\.-][a-zA-Z0-9]+)*@([a-zA-Z0-9]+([\\.-][a-zA-Z0-9]+)*)+\\.[a-zA-Z]{2,}$";
  30. /** Default COnstructor. */
  31. public AbstractBaseValidator() {
  32. super();
  33. }
  34. /**
  35. * Retrurn the Object array containing the specified label.
  36. * @param aLabel the label to add to the array
  37. * @return the object array
  38. */
  39. protected Object[] getObjectArrayForLabel(String aLabel) {
  40. return new Object[] {new DefaultMessageSourceResolvable(new String[] {Constants.GBL_CMD_DATA + "." + aLabel})};
  41. }
  42. /**
  43. * Return an Object array containing the specified label and int value.
  44. * @param aLabel the label to add to the array
  45. * @param aInt the int value to add to the array
  46. * @return the Object array
  47. */
  48. protected Object[] getObjectArrayForLabelAndInt(String aLabel, int aInt) {
  49. return new Object[] {new DefaultMessageSourceResolvable(new String[] {Constants.GBL_CMD_DATA + "." + aLabel}), Integer.toString(aInt)};
  50. }
  51. /**
  52. * Return an Object array containing the specified label and String value.
  53. * @param aLabel the label to add to the array
  54. * @param aValue the String value to add to the array
  55. * @return the Object array
  56. */
  57. protected Object[] getObjectArrayForLabelAndValue(String aLabel, String aValue) {
  58. return new Object[] {new DefaultMessageSourceResolvable(new String[] {Constants.GBL_CMD_DATA + "." + aLabel}), aValue};
  59. }
  60. /**
  61. * Return an Object array containing two specific labels.
  62. * @param aLabel1 the first label
  63. * @param aLabel2 the second label
  64. * @return the Object array
  65. */
  66. protected Object[] getObjectArrayForTwoLabels(String aLabel1, String aLabel2) {
  67. return new Object[] {new DefaultMessageSourceResolvable(new String[] {Constants.GBL_CMD_DATA + "." + aLabel1}), new DefaultMessageSourceResolvable(new String[] {Constants.GBL_CMD_DATA + "." + aLabel2})};
  68. }
  69. /**
  70. * Validate Email address is valid. Will only validate a single email address at a time
  71. * @param email an email address to validate as a String
  72. * @return true if the email is valid
  73. */
  74. public boolean validateEmail(String email) {
  75. return Pattern.matches(EMAIL_REGEX, email);
  76. }
  77. }