1 | /*
|
---|
2 | * Copyright 2005 Paul Hinds
|
---|
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.tp23.antinstaller.input;
|
---|
17 |
|
---|
18 |
|
---|
19 | import java.util.regex.Matcher;
|
---|
20 | import java.util.regex.Pattern;
|
---|
21 |
|
---|
22 | import org.apache.tools.ant.BuildException;
|
---|
23 | import org.tp23.antinstaller.InstallerContext;
|
---|
24 | import org.tp23.antinstaller.ValidationException;
|
---|
25 |
|
---|
26 |
|
---|
27 | /**
|
---|
28 | *
|
---|
29 | * <p>Free text input type with validation using regular expressions</p>
|
---|
30 | * @author Paul Hinds
|
---|
31 | * @version $Id: ValidatedTextInput.java,v 1.2 2006/12/21 00:03:09 teknopaul Exp $
|
---|
32 | */
|
---|
33 | public class ValidatedTextInput
|
---|
34 | extends InputField{
|
---|
35 |
|
---|
36 | private String regex;
|
---|
37 | private Pattern pattern;
|
---|
38 | //private RegexpMatcher matcher; // use ant version in an attempt to support jdk1.3
|
---|
39 |
|
---|
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 | //matcher = new RegexpMatcherFactory().newRegexpMatcher();
|
---|
53 | //matcher.setPattern(regex);
|
---|
54 | pattern = Pattern.compile(regex);
|
---|
55 | }
|
---|
56 | catch (BuildException ex) {
|
---|
57 | throw new InputException("Invalid regex in Validated text input");
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Called to validate the user input
|
---|
63 | */
|
---|
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(toTest);
|
---|
71 | boolean matches = matcher.matches();
|
---|
72 |
|
---|
73 | return matches;
|
---|
74 | }
|
---|
75 | catch (Throwable e) {
|
---|
76 | cxt.log(e);
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Used by checkConfig to validate the configuration file.
|
---|
83 | * Not used at runtime.
|
---|
84 | * @return boolean
|
---|
85 | */
|
---|
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){
|
---|
100 | System.out.println("Validated:regex must be set");
|
---|
101 | return false;
|
---|
102 | }
|
---|
103 | try{
|
---|
104 | //matcher = new RegexpMatcherFactory().newRegexpMatcher();
|
---|
105 | //matcher.setPattern(getRegex());
|
---|
106 | }
|
---|
107 | catch(Exception e){
|
---|
108 | System.out.println("Validated:regex must compile");
|
---|
109 |
|
---|
110 | }
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 | }
|
---|