1 package com.inigoserrano.isvalidator.check;
2
3 import org.apache.regexp.RE;
4 import org.apache.regexp.RESyntaxException;
5
6 import com.inigoserrano.isvalidator.checkExceptions.CheckException;
7 import com.inigoserrano.isvalidator.checkExceptions.RegularExpresionException;
8
9 /***
10 * This class is used to verify if one data match one regular expresion.
11 * It need the package jakarta-regexp to work properly.
12 * This Check is designed to apply regular expresion to data. It can be
13 * used for what you want (that can be expressed
14 * with a regular expresion)
15 * @license@
16 * @version @version@
17 * @author @author@
18 **/
19 public class RegularExpresionCheck extends SimpleCheck {
20 /***
21 * To store the regular expression to match
22 */
23 private String regularExpresion;
24 /***
25 * an instance of the RE
26 */
27 private RE expresion = null;
28
29 /***
30 * Constructor. This constructor is used by the classes that
31 * extends this.
32 * It must put the regular expresion to work properly
33 **/
34 protected RegularExpresionCheck() {
35 super();
36 }
37
38 /***
39 * Constructor
40 * @param regularExpresion the RE used to verify the data
41 * @throws RESyntaxException if the expression is not valid
42 **/
43 public RegularExpresionCheck(String regularExpresion)
44 throws RESyntaxException {
45 super();
46 this.regularExpresion = regularExpresion;
47 this.expresion = new RE(regularExpresion);
48 }
49
50 /***
51 * Constructor
52 * @param regularExpresion RE used to verify the data
53 * @param throwException if we want that if this check dosenīt match
54 * throw or not an Exception, This overrides the containerīs value for
55 * this Check
56 * @throws RESyntaxException if the expression is not valid
57 **/
58 public RegularExpresionCheck(
59 String regularExpresion,
60 boolean throwException)
61 throws RESyntaxException {
62 super();
63 this.regularExpresion = regularExpresion;
64 this.expresion = new RE(regularExpresion);
65 this.throwException = throwException;
66 this.explicityThrowException = true;
67 }
68
69 /***
70 * This method verify that all the needes checks are executed,
71 * if some Checks isnīt in the container then this method add it to
72 * the container, and executes it.
73 * @throws CheckException if the data dosnīt match and it is indicated to
74 * throw an Exception
75 * @throws InternalCheckException if an internal error occurs
76 **/
77 protected void allChecksPresent()
78 throws CheckException, InternalCheckException {
79 if (!container.needExecutedCheck(NotBlankCheck.class.getName())) {
80 NotBlankCheck notBlank = new NotBlankCheck(throwException);
81 container.addCheck(notBlank);
82 notBlank.check();
83 }
84 }
85
86 /***
87 * This method verify that the data match with the Check
88 * @return true if it match or false f it dosenīt
89 * @throws CheckException if the data dosnīt match and it is indicated to
90 * throw an Exception
91 * @throws InternalCheckException if an internal error occurs
92 **/
93 public boolean check() throws CheckException, InternalCheckException {
94 hasBeenChecked = true;
95 this.allChecksPresent();
96 if (valueToCheck == null) {
97 match = false;
98 } else {
99 match = expresion.match(valueToCheck);
100 }
101 if (throwException & !match) {
102 throw new RegularExpresionException(
103 this.getIdentifier() + "Exception");
104 }
105 return match;
106 }
107
108 /***
109 * This method return the identification of the Check
110 * @return the name of the Check
111 **/
112 public String getIdentifier() {
113 return "RegularExpresionCheck";
114 }
115 /***
116 * Set the regular expression
117 * @param regularExpresion the regular expression
118 */
119 protected void setRegularExpresion(String regularExpresion) {
120 this.regularExpresion = regularExpresion;
121 }
122
123 /***
124 * gets the regular expresion
125 * @return the regular expression
126 */
127 protected String getRegularExpresion() {
128 return regularExpresion;
129 }
130 /***
131 * Sets the expression
132 * @param expresion the expression
133 */
134 protected void setExpresion(RE expresion) {
135 this.expresion = expresion;
136 }
137 /***
138 * gets the expresion
139 * @return the expresion
140 */
141 protected RE getExpresion() {
142 return expresion;
143 }
144 }
This page was automatically generated by Maven