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 java.util.regex.Matcher;
019: import java.util.regex.Pattern;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.tp23.antinstaller.InstallerContext;
023: import org.tp23.antinstaller.ValidationException;
024:
025: /**
026: *
027: * <p>Free text input type with validation using regular expressions</p>
028: * @author Paul Hinds
029: * @version $Id: ValidatedTextInput.java,v 1.2 2006/12/21 00:03:09 teknopaul Exp $
030: */
031: public class ValidatedTextInput extends InputField {
032:
033: private String regex;
034: private Pattern pattern;
035:
036: //private RegexpMatcher matcher; // use ant version in an attempt to support jdk1.3
037:
038: public ValidatedTextInput() {
039: }
040:
041: public void setValue(String dir) {
042: setInputResult(dir);
043: }
044:
045: public String getRegex() {
046: return regex;
047: }
048:
049: public void setRegex(String regex) {
050: this .regex = regex;
051: try {
052: //matcher = new RegexpMatcherFactory().newRegexpMatcher();
053: //matcher.setPattern(regex);
054: pattern = Pattern.compile(regex);
055: } catch (BuildException ex) {
056: throw new InputException(
057: "Invalid regex in Validated text input");
058: }
059: }
060:
061: /**
062: * Called to validate the user input
063: */
064: public boolean validate(InstallerContext cxt)
065: throws ValidationException {
066: try {
067: if (getInputResult() == null)
068: return false;
069: String toTest = getInputResult();
070:
071: Matcher matcher = pattern.matcher(toTest);
072: //boolean matches = matcher.matches(toTest);
073: boolean matches = matcher.matches();
074:
075: return matches;
076: } catch (Throwable e) {
077: cxt.log(e);
078: return false;
079: }
080: }
081:
082: /**
083: * Used by checkConfig to validate the configuration file.
084: * Not used at runtime.
085: * @return boolean
086: */
087: public boolean validateObject() {
088: if (getDisplayText() == null) {
089: System.out.println("Validated:displayText must be set");
090: return false;
091: }
092: if (getProperty() == null) {
093: System.out.println("Validated:property must be set");
094: return false;
095: }
096: if (getDefaultValue() == null) {
097: System.out.println("Validated:defaultValue must be set");
098: return false;
099: }
100: if (getRegex() == null) {
101: System.out.println("Validated:regex must be set");
102: return false;
103: }
104: try {
105: //matcher = new RegexpMatcherFactory().newRegexpMatcher();
106: //matcher.setPattern(getRegex());
107: } catch (Exception e) {
108: System.out.println("Validated:regex must compile");
109:
110: }
111: return true;
112: }
113: }
|