001: /*
002: * Copyright 2005-2006 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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.opensource.org/licenses/ecl1.php
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.kuali.core.datadictionary.validation;
017:
018: import java.util.regex.Pattern;
019:
020: import org.kuali.core.datadictionary.exporter.ExportMap;
021:
022: /**
023: * Abstraction of the regular expressions used to validate attribute values.
024: *
025: *
026: */
027: abstract public class CharacterLevelValidationPattern extends
028: ValidationPattern {
029: private int maxLength = -1;
030: private int exactLength = -1;
031:
032: /**
033: * Sets maxLength parameter for the associated regex.
034: *
035: * @param maxLength
036: */
037: public void setMaxLength(int maxLength) {
038: if (this .exactLength != -1) {
039: throw new IllegalStateException(
040: "illegal attempt to set maxLength after mutually-exclusive exactLength has been set");
041: }
042:
043: this .maxLength = maxLength;
044: }
045:
046: /**
047: * @return current maxLength, or -1 if none has been set
048: */
049: public int getMaxLength() {
050: return maxLength;
051: }
052:
053: /**
054: * Sets exactLength parameter for the associated regex.
055: *
056: * @param exactLength
057: */
058: public void setExactLength(int exactLength) {
059: if (this .maxLength != -1) {
060: throw new IllegalStateException(
061: "illegal attempt to set exactLength after mutually-exclusive maxLength has been set");
062: }
063:
064: this .exactLength = exactLength;
065: }
066:
067: /**
068: * @return current exactLength, or -1 if none has been set
069: */
070: public int getExactLength() {
071: return exactLength;
072: }
073:
074: /**
075: * @return regular expression Pattern generated using the individual ValidationPattern subclass
076: */
077: final public Pattern getRegexPattern() {
078: String regexString = getRegexString();
079:
080: StringBuffer completeRegex = new StringBuffer("^");
081: completeRegex.append(getRegexString());
082:
083: if (maxLength != -1) {
084: completeRegex.append("{0," + maxLength + "}");
085: } else if (exactLength != -1) {
086: completeRegex.append("{" + exactLength + "}");
087: } else {
088: completeRegex.append("*");
089: }
090:
091: completeRegex.append("$");
092:
093: Pattern pattern = Pattern.compile(completeRegex.toString());
094: return pattern;
095: }
096:
097: /**
098: * @see org.kuali.core.datadictionary.validation.ValidationPattern#buildExportMap(java.lang.String)
099: */
100: final public ExportMap buildExportMap(String exportKey) {
101: ExportMap exportMap = new ExportMap(exportKey);
102:
103: if (getMaxLength() != -1) {
104: exportMap
105: .set("maxLength", Integer.toString(getMaxLength()));
106: } else if (getExactLength() != -1) {
107: exportMap.set("exactLength", Integer
108: .toString(getExactLength()));
109: }
110:
111: extendExportMap(exportMap);
112:
113: return exportMap;
114: }
115:
116: /**
117: * Extends the given (parent class) exportMap as needed to represent subclass instances
118: *
119: * @param exportMap
120: */
121: abstract public void extendExportMap(ExportMap exportMap);
122: }
|