KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > validator > ValidatorFactoryImpl


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.form.validator;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import org.apache.hivemind.ApplicationRuntimeException;
24 import org.apache.hivemind.HiveMind;
25 import org.apache.hivemind.util.PropertyUtils;
26 import org.apache.tapestry.util.RegexpMatch;
27 import org.apache.tapestry.util.RegexpMatcher;
28
29 /**
30  * Implementation of the tapestry.form.validator.ValidatorFactory service, which builds and caches
31  * validators and lists of validators from a "magic" string specification.
32  *
33  * @author Howard Lewis Ship
34  * @since 4.0
35  */

36 public class ValidatorFactoryImpl implements ValidatorFactory
37 {
38     private static final String JavaDoc PATTERN = "^\\s*(\\w+)\\s*(=\\s*(((?!,|\\[).)*))?";
39
40     /**
41      * Cache of List (of Validator) keyed on specification.
42      */

43
44     private Map JavaDoc _masterCache = new HashMap JavaDoc();
45
46     private RegexpMatcher _matcher = new RegexpMatcher();
47
48     /**
49      * Injected map of validator names to ValidatorContribution.
50      */

51
52     private Map JavaDoc _validators;
53
54     public synchronized List JavaDoc constructValidatorList(String JavaDoc specification)
55     {
56         List JavaDoc result = (List JavaDoc) _masterCache.get(specification);
57
58         if (result == null)
59         {
60             result = buildValidatorList(specification);
61             _masterCache.put(specification, result);
62         }
63
64         return result;
65     }
66
67     private List JavaDoc buildValidatorList(String JavaDoc specification)
68     {
69         if (HiveMind.isBlank(specification))
70             return Collections.EMPTY_LIST;
71
72         List JavaDoc result = new ArrayList JavaDoc();
73         String JavaDoc chopped = specification;
74
75         while (true)
76         {
77             if (chopped.length() == 0)
78                 break;
79
80             if (!result.isEmpty())
81             {
82                 if (chopped.charAt(0) != ',')
83                     throw new ApplicationRuntimeException(ValidatorMessages
84                             .badSpecification(specification));
85
86                 chopped = chopped.substring(1);
87             }
88
89             RegexpMatch[] matches = _matcher.getMatches(PATTERN, chopped);
90
91             if (matches.length != 1)
92                 throw new ApplicationRuntimeException(ValidatorMessages
93                         .badSpecification(specification));
94
95             RegexpMatch match = matches[0];
96
97             String JavaDoc name = match.getGroup(1);
98             String JavaDoc value = match.getGroup(3);
99             String JavaDoc message = null;
100
101             int length = match.getMatchLength();
102
103             if (chopped.length() > length)
104             {
105                 char lastChar = chopped.charAt(length);
106                 if (lastChar == ',')
107                     length--;
108                 else if (lastChar == '[')
109                 {
110                     int messageClose = chopped.indexOf(']', length);
111                     message = chopped.substring(length + 1, messageClose);
112                     length = messageClose;
113                 }
114             }
115
116             Validator validator = buildValidator(name, value, message);
117
118             result.add(validator);
119
120             if (length >= chopped.length())
121                 break;
122             else
123                 chopped = chopped.substring(length + 1);
124
125         }
126
127         return Collections.unmodifiableList(result);
128     }
129
130     private Validator buildValidator(String JavaDoc name, String JavaDoc value, String JavaDoc message)
131     {
132         ValidatorContribution vc = (ValidatorContribution) _validators.get(name);
133
134         if (vc == null)
135             throw new ApplicationRuntimeException(ValidatorMessages.unknownValidator(name));
136
137         if (value == null && vc.isConfigurable())
138             throw new ApplicationRuntimeException(ValidatorMessages.needsConfiguration("name"));
139
140         if (value != null && !vc.isConfigurable())
141             throw new ApplicationRuntimeException(ValidatorMessages.notConfigurable(name, value));
142
143         try
144         {
145             Object JavaDoc result = vc.getValidatorClass().newInstance();
146
147             if (vc.isConfigurable())
148                 PropertyUtils.smartWrite(result, name, value);
149
150             if (message != null)
151                 PropertyUtils.write(result, "message", message);
152
153             return (Validator) result;
154         }
155         catch (Exception JavaDoc ex)
156         {
157             throw new ApplicationRuntimeException(ValidatorMessages.errorInitializingValidator(
158                     name,
159                     vc.getValidatorClass(),
160                     ex), ex);
161         }
162     }
163
164     public void setValidators(Map JavaDoc validators)
165     {
166         _validators = validators;
167     }
168 }
169
Popular Tags