001: /*
002: * Copyright 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.fieldlevel;
017:
018: import org.kuali.core.datadictionary.exporter.ExportMap;
019: import org.kuali.core.datadictionary.validation.FieldLevelValidationPattern;
020:
021: /**
022: * Validation pattern for matching fixed point numbers, optionally matching negative numbers
023: *
024: *
025: */
026: public class FixedPointValidationPattern extends
027: FieldLevelValidationPattern {
028: public static final String PATTERN_TYPE_PRECISION = "fixedPoint.precision";
029: public static final String PATTERN_TYPE_SCALE = "fixedPoint.scale";
030:
031: private boolean allowNegative;
032: private int precision;
033: private int scale;
034:
035: /**
036: * @return Returns the precision.
037: */
038: public int getPrecision() {
039: return precision;
040: }
041:
042: /**
043: * @param precision The precision to set.
044: */
045: public void setPrecision(int precision) {
046: this .precision = precision;
047: }
048:
049: /**
050: * @return Returns the scale.
051: */
052: public int getScale() {
053: return scale;
054: }
055:
056: /**
057: * @param scale The scale to set.
058: */
059: public void setScale(int scale) {
060: this .scale = scale;
061: }
062:
063: /**
064: * @return allowNegative
065: */
066: public boolean getAllowNegative() {
067: return allowNegative;
068: }
069:
070: /**
071: * @param allowNegative
072: */
073: public void setAllowNegative(boolean allowNegative) {
074: this .allowNegative = allowNegative;
075: }
076:
077: /**
078: * Adds special handling to account for optional allowNegative and dynamic precision, scale
079: *
080: * @see org.kuali.core.datadictionary.validation.ValidationPattern#getRegexString()
081: */
082: protected String getRegexString() {
083: StringBuffer regex = new StringBuffer();
084:
085: if (allowNegative) {
086: regex.append("-?");
087: }
088: // final patter will be: -?([0-9]{0,p-s}\.[0-9]{1,s}|[0-9]{1,p-s}) where p = precision, s=scale
089: regex.append("(");
090: regex.append("[0-9]{0," + (getPrecision() - getScale()) + "}");
091: regex.append("\\.");
092: regex.append("[0-9]{1," + getScale() + "}");
093: regex.append("|[0-9]{1," + (getPrecision() - getScale()) + "}");
094: regex.append(")");
095: return regex.toString();
096: }
097:
098: /**
099: * @see org.kuali.core.datadictionary.validation.FieldLevelValidationPattern#getPatternTypeName()
100: */
101: protected String getPatternTypeName() {
102: return "fixedPoint";
103: }
104:
105: /**
106: * @see org.kuali.core.datadictionary.validation.ValidationPattern#buildExportMap(java.lang.String)
107: */
108: public ExportMap buildExportMap(String exportKey) {
109: ExportMap exportMap = super .buildExportMap(exportKey);
110:
111: if (allowNegative) {
112: exportMap.set("allowNegative", "true");
113: }
114: exportMap.set("precision", Integer.toString(precision));
115: exportMap.set("scale", Integer.toString(scale));
116:
117: return exportMap;
118: }
119:
120: /**
121: * @see org.kuali.core.datadictionary.validation.ValidationPattern#getPatternXml()
122: */
123: public String getPatternXml() {
124: StringBuffer xml = new StringBuffer("<fixedPoint ");
125: if (allowNegative) {
126: xml.append("allowNegative=\"true\" ");
127: }
128: xml.append("precision=\"" + precision + "\" ");
129: xml.append("scale=\"" + scale + "\" ");
130: xml.append("/>");
131:
132: return xml.toString();
133: }
134: }
|