View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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   */
16  package org.kuali.rice.kns.datadictionary.validation;
17  
18  import java.util.regex.Pattern;
19  
20  import org.kuali.rice.kns.datadictionary.exporter.ExportMap;
21  import org.kuali.rice.kns.util.KNSConstants;
22  
23  /**
24   * Abstraction of the regular expressions used to validate attribute values.
25   * 
26   * 
27   */
28  abstract public class CharacterLevelValidationPattern extends ValidationPattern {
29      protected Pattern regexPattern;
30  
31      protected int maxLength = -1;
32      protected int exactLength = -1;
33  
34      /**
35       * Sets maxLength parameter for the associated regex.
36       * 
37       * @param maxLength
38       */
39      public void setMaxLength(int maxLength) {
40          if (this.exactLength != -1) {
41              throw new IllegalStateException("illegal attempt to set maxLength after mutually-exclusive exactLength has been set");
42          }
43  
44          this.maxLength = maxLength;
45      }
46  
47      /**
48       * @return current maxLength, or -1 if none has been set
49       */
50      public int getMaxLength() {
51          return maxLength;
52      }
53  
54  
55      /**
56       * Sets exactLength parameter for the associated regex.
57       * 
58       * @param exactLength
59       */
60      public void setExactLength(int exactLength) {
61          if (this.maxLength != -1) {
62              throw new IllegalStateException("illegal attempt to set exactLength after mutually-exclusive maxLength has been set");
63          }
64  
65          this.exactLength = exactLength;
66      }
67  
68      /**
69       * @return current exactLength, or -1 if none has been set
70       */
71      public int getExactLength() {
72          return exactLength;
73      }
74  
75  
76      /**
77       * @return regular expression Pattern generated using the individual ValidationPattern subclass
78       */
79      final public Pattern getRegexPattern() {
80          if ( regexPattern == null ) {
81              String regexString = getRegexString();
82      
83              StringBuffer completeRegex = new StringBuffer("^");
84              completeRegex.append(getRegexString());
85      
86              if (maxLength != -1) {
87                  completeRegex.append("{0," + maxLength + "}");
88              }
89              else if (exactLength != -1) {
90                  completeRegex.append("{" + exactLength + "}");
91              }
92              else {
93                  completeRegex.append("*");
94              }
95      
96              completeRegex.append("$");
97      
98              regexPattern = Pattern.compile(completeRegex.toString());
99          }
100         return regexPattern;
101     }
102 
103 
104     /**
105      * @see org.kuali.rice.kns.datadictionary.validation.ValidationPattern#buildExportMap(java.lang.String)
106      */
107     final public ExportMap buildExportMap(String exportKey) {
108         ExportMap exportMap = new ExportMap(exportKey);
109 
110         if (getMaxLength() != -1) {
111             exportMap.set("maxLength", Integer.toString(getMaxLength()));
112         }
113         else if (getExactLength() != -1) {
114             exportMap.set("exactLength", Integer.toString(getExactLength()));
115         }
116 
117         extendExportMap(exportMap);
118 
119         return exportMap;
120     }
121 
122     /**
123      * Extends the given (parent class) exportMap as needed to represent subclass instances
124      * 
125      * @param exportMap
126      */
127     abstract public void extendExportMap(ExportMap exportMap);
128 
129 	@Override
130 	public String[] getValidationErrorMessageParameters(String attributeLabel) {
131 		if (getMaxLength() != -1) {
132 			return new String[] {attributeLabel, String.valueOf(getMaxLength())};
133 		}
134 		if (getExactLength() != -1) {
135 			return new String[] {attributeLabel, String.valueOf(getExactLength())};
136 		}
137 		return new String[] {attributeLabel};
138 	}
139 
140 	/**
141 	 * This overridden method ...
142 	 * 
143 	 * @see org.kuali.rice.kns.datadictionary.validation.ValidationPattern#getValidationErrorMessageKey()
144 	 */
145 	@Override
146 	public String getValidationErrorMessageKey() {
147 		StringBuilder buf = new StringBuilder();
148 		buf.append("error.format.").append(getClass().getName()).append(getValidationErrorMessageKeyOptions());
149 		if (getMaxLength() != -1) {
150 			buf.append(".maxLength");
151 		}
152 		if (getExactLength() != -1) {
153 			buf.append(".exactLength");
154 		}	
155 		return buf.toString();
156 	}
157 	
158 	protected String getValidationErrorMessageKeyOptions() {
159 		return KNSConstants.EMPTY_STRING;
160 	}
161 }