01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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.core.datadictionary.validation.charlevel;
17:
18: import java.util.regex.Pattern;
19:
20: import org.apache.commons.lang.StringUtils;
21: import org.kuali.core.datadictionary.exporter.ExportMap;
22: import org.kuali.core.datadictionary.validation.CharacterLevelValidationPattern;
23:
24: /**
25: * Pattern for matching any character in the given list (String)
26: *
27: *
28: */
29: public class CharsetValidationPattern extends
30: CharacterLevelValidationPattern {
31: private String validChars;
32:
33: /**
34: * @return String containing all valid chars for this charset
35: */
36: public String getValidChars() {
37: return validChars;
38: }
39:
40: /**
41: * @param validChars for this charset
42: */
43: public void setValidChars(String validChars) {
44: if (StringUtils.isEmpty(validChars)) {
45: throw new IllegalArgumentException(
46: "invalid (empty) validChars");
47: }
48:
49: this .validChars = validChars;
50: }
51:
52: /**
53: * Escapes every special character I could think of, to limit potential misuse of this pattern.
54: *
55: * @see org.kuali.core.datadictionary.validation.ValidationPattern#getRegexString()
56: */
57: protected String getRegexString() {
58: if (StringUtils.isEmpty(validChars)) {
59: throw new IllegalStateException("validChars is empty");
60: }
61:
62: // filter out and escape chars which would confuse the pattern-matcher
63: Pattern filteringChars = Pattern
64: .compile("([\\-\\[\\]\\{\\}\\$\\.\\^\\(\\)\\*\\&\\|])");
65: String filteredChars = filteringChars.matcher(validChars)
66: .replaceAll("\\\\$1");
67:
68: StringBuffer regexString = new StringBuffer("[");
69: regexString.append(filteredChars);
70: if (filteredChars.endsWith("\\")) {
71: regexString.append("\\");
72: }
73: regexString.append("]");
74:
75: return regexString.toString();
76: }
77:
78: /**
79: * @see org.kuali.core.datadictionary.validation.CharacterLevelValidationPattern#extendExportMap(org.kuali.bo.datadictionary.exporter.ExportMap)
80: */
81: public void extendExportMap(ExportMap exportMap) {
82: exportMap.set("type", "charset");
83:
84: exportMap.set("validChars", getValidChars());
85: }
86:
87: /**
88: * @see org.kuali.core.datadictionary.validation.ValidationPattern#getPatternXml()
89: */
90: public String getPatternXml() {
91: StringBuffer xml = new StringBuffer("<charSet ");
92: xml.append(validChars);
93: xml.append("/>");
94:
95: return xml.toString();
96: }
97: }
|