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 */
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/**
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 */
33public 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){
00            System.out.println("Validated:regex must be set");
01            return false;
02        }
03        try{
04            //matcher = new RegexpMatcherFactory().newRegexpMatcher();
05            //matcher.setPattern(getRegex());
06        }
07        catch(Exception e){
08            System.out.println("Validated:regex must compile");
09            
10        }
11        return true;
12    }
13}
14