001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.input;
017:
018: import org.tp23.antinstaller.InstallerContext;
019: import org.tp23.antinstaller.ValidationException;
020:
021: /**
022: *
023: * <p>Free text input type with validation using a custon supplied class.</p>
024: * This enable very coplext validation, for example, a class could be written
025: * to validate a port number enterd by a user that tries to open a socket
026: * on the port and returns false to the validate method if the socket is in use.
027: *
028: * TODO push this up so that all input types can provide custom validation.
029: *
030: * @author Paul Hinds
031: * @version $Id: ExtValidatedTextInput.java,v 1.2 2007/01/09 22:41:41 teknopaul Exp $
032: */
033: public class ExtValidatedTextInput extends ValidatedTextInput {
034:
035: private String validationClass;
036: private Validator validator;
037: private Throwable throwable;
038:
039: public ExtValidatedTextInput() {
040: }
041:
042: public void setValue(String dir) {
043: setInputResult(dir);
044: }
045:
046: public String getValidationClass() {
047: return validationClass;
048: }
049:
050: public void setValidationClass(String validationClass) {
051: this .validationClass = validationClass;
052: try {
053: validator = (Validator) Class.forName(validationClass)
054: .newInstance();
055: } catch (Exception ex) {
056: throw new InputException(
057: "Invalid Class in ExtValidated text input");
058: }
059: }
060:
061: /**
062: * Called to validate the user input
063: */
064: public boolean validate(InstallerContext ctx)
065: throws ValidationException {
066: String result = getInputResult();
067: try {
068: validator.validate(result, ctx);
069: throwable = null;
070: return true;
071: } catch (Throwable t) {
072: throwable = t;
073: return false;
074: }
075:
076: }
077:
078: /**
079: * @return Returns the validator.
080: */
081: public Validator getValidator() {
082: return validator;
083: }
084:
085: /**
086: * Used by checkConfig to validate the configuration file.
087: * Not used at runtime.
088: * @return boolean
089: */
090: public boolean validateObject() {
091: if (getDisplayText() == null) {
092: System.out.println("ExtValidated:displayText must be set");
093: return false;
094: }
095: if (getProperty() == null) {
096: System.out.println("ExtValidated:property must be set");
097: return false;
098: }
099: if (getValidationClass() == null) {
100: System.out
101: .println("ExtValidated:validationClass must be set");
102: return false;
103: }
104: return true;
105: }
106:
107: /**
108: * @return Returns the throwable.
109: */
110: public Throwable getThrowable() {
111: return throwable;
112: }
113: }
|