1
16package org.tp23.antinstaller.input;
17
18
19import java.util.regex.Matcher;
20import java.util.regex.Pattern;
21
22import org.apache.tools.ant.BuildException;
23import org.tp23.antinstaller.InstallerContext;
24import org.tp23.antinstaller.ValidationException;
25
26
27
33public class ValidatedTextInput
34 extends InputField{
35
36 private String regex;
37 private Pattern pattern;
38
40 public ValidatedTextInput() {
41 }
42
43 public void setValue(String dir){
44 setInputResult(dir);
45 }
46 public String getRegex() {
47 return regex;
48 }
49 public void setRegex(String regex) {
50 this.regex = regex;
51 try {
52 pattern = Pattern.compile(regex);
55 }
56 catch (BuildException ex) {
57 throw new InputException("Invalid regex in Validated text input");
58 }
59 }
60
61
64 public boolean validate(InstallerContext cxt) throws ValidationException{
65 try {
66 if (getInputResult() == null)return false;
67 String toTest = getInputResult();
68
69 Matcher matcher = pattern.matcher(toTest);
70 boolean matches = matcher.matches();
72
73 return matches;
74 }
75 catch (Throwable e) {
76 cxt.log(e);
77 return false;
78 }
79 }
80
81
86 public boolean validateObject() {
87 if(getDisplayText()==null){
88 System.out.println("Validated:displayText must be set");
89 return false;
90 }
91 if(getProperty()==null){
92 System.out.println("Validated:property must be set");
93 return false;
94 }
95 if(getDefaultValue()==null){
96 System.out.println("Validated:defaultValue must be set");
97 return false;
98 }
99 if(getRegex()==null){
00 System.out.println("Validated:regex must be set");
01 return false;
02 }
03 try{
04 }
07 catch(Exception e){
08 System.out.println("Validated:regex must compile");
09
10 }
11 return true;
12 }
13}
14