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
19
20import org.tp23.antinstaller.InstallerContext;
21import org.tp23.antinstaller.ValidationException;
22
23
24/**
25 *
26 * <p>Free text input type with validation using a custon supplied class.</p>
27 * This enable very coplext validation, for example, a class could be written
28 * to validate a port number enterd by a user that tries to open a socket
29 * on the port and returns false to the validate method if the socket is in use. 
30 * @author Paul Hinds
31 * @version $Id: ExtValidatedTextInput.java,v 1.2 2007/01/09 22:41:41 teknopaul Exp $
32 */
33public class ExtValidatedTextInput
34    extends ValidatedTextInput{
35
36    private String validationClass;
37    private Validator validator;
38    private Throwable throwable ;
39
40    public ExtValidatedTextInput() {
41    }
42
43    public void setValue(String dir){
44        setInputResult(dir);
45    }
46    public String getValidationClass() {
47        return validationClass;
48    }
49    public void setValidationClass(String validationClass) {
50        this.validationClass = validationClass;
51        try {
52            validator = (Validator)Class.forName(validationClass).newInstance();
53        }
54        catch (Exception ex) {
55            throw new InputException("Invalid Class in ExtValidated text input");
56        }
57    }
58
59    /**
60     * Called to validate the user input
61     */
62    public boolean validate(InstallerContext ctx) throws ValidationException{
63        String result = getInputResult();
64        try{
65            validator.validate(result,ctx);
66            throwable = null;
67            return true;
68        }
69        catch(Throwable t){
70            throwable = t;
71            return false;
72        }
73        
74    }
75    /**
76     * @return Returns the validator.
77     */
78    public Validator getValidator() {
79        return validator;
80    }
81
82    /**
83     * Used by checkConfig to validate the configuration file.
84     * Not used at runtime.
85     * @return boolean
86     */
87    public boolean validateObject() {
88        if(getDisplayText() == null){
89            System.out.println("ExtValidated:displayText must be set");
90            return false;
91        }
92        if(getProperty() == null){
93            System.out.println("ExtValidated:property must be set");
94            return false;
95        }
96        if(getValidationClass() == null){
97            System.out.println("ExtValidated:validationClass must be set");
98            return false;
99        }
00        return true;
01    }
02    /**
03     * @return Returns the throwable.
04     */
05    public Throwable getThrowable() {
06        return throwable;
07    }
08}
09