1
2
3
4 package org.rcfaces.core.internal.validator.impl;
5
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.regex.Pattern;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.rcfaces.core.validator.IClientValidatorContext;
13 import org.rcfaces.core.validator.IClientValidatorTask;
14 import org.rcfaces.core.validator.IParameter;
15
16
17
18
19
20
21 public class AbstractClientValidatorTask implements IClientValidatorTask {
22 private static final String REVISION = "$Revision: 1.1 $";
23
24 private static final Log LOG = LogFactory
25 .getLog(AbstractClientValidatorTask.class);
26
27 private static final String PATTERN_ATTRIBUTE = "regexp.pattern";
28
29 private static final Pattern ESCAPE_REGEXP = Pattern.compile(
30 "([\\\\\\/\\.\\*\\+\\?\\|\\(\\)\\[\\]\\{\\}\\-\\^])",
31 Pattern.DOTALL);
32
33 private static final Map regExpressions = new HashMap(64);
34
35 protected String getParameter(IClientValidatorContext context,
36 String parameterName) {
37 IParameter parameters[] = context.getParameters();
38
39 for (int i = 0; i < parameters.length; i++) {
40 if (parameters[i].getName().equals(parameterName)) {
41 return parameters[i].getValue();
42 }
43 }
44
45 return null;
46 }
47
48 protected String getParameter(IClientValidatorContext context,
49 String parameterName, String defaultValue) {
50 String s = getParameter(context, parameterName);
51 if (s != null) {
52 return s;
53 }
54
55 return defaultValue;
56 }
57
58 protected int getIntParameter(IClientValidatorContext context,
59 String parameterName, int defaultValue) {
60 String s = getParameter(context, parameterName);
61 if (s == null) {
62 return defaultValue;
63 }
64
65 return Integer.parseInt(s);
66 }
67
68 protected boolean getBoolParameter(IClientValidatorContext context,
69 String parameterName, boolean defaultValue) {
70 String s = getParameter(context, parameterName);
71 if (s == null) {
72 return defaultValue;
73 }
74
75 return Boolean.parseBoolean(s);
76 }
77
78 protected String buildEscaped(String regEx) {
79 if (regEx == null || regEx.length() < 1) {
80 return "";
81 }
82
83 return ESCAPE_REGEXP.matcher(regEx).replaceAll("\\\\$1");
84 }
85
86 protected Pattern getPattern(String regEx) {
87 Pattern pattern;
88
89 synchronized (regExpressions) {
90 pattern = (Pattern) regExpressions.get(regEx);
91 if (pattern == null) {
92 pattern = Pattern.compile(regEx);
93 }
94 }
95
96 return pattern;
97
98 }
99 }