2
* $Header: /home/cvs/jakarta-struts/src/share/org/apache/struts/validator/ValidatorPlugIn.java,v 1.27 2004/08/31 18:39:04 jmitchell Exp $
4
* $Date: 2004/08/31 18:39:04 $
6
* Copyright 2000-2004 The Apache Software Foundation.
8
* Licensed under the Apache License, Version 2.0 (the "License");
9
* you may not use this file except in compliance with the License.
10
* You may obtain a copy of the License at
12
* http://www.apache.org/licenses/LICENSE-2.0
14
* Unless required by applicable law or agreed to in writing, software
15
* distributed under the License is distributed on an "AS IS" BASIS,
16
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
* See the License for the specific language governing permissions and
18
* limitations under the License.
21
package org.apache.struts.validator;
23
import org.xml.sax.SAXException;
25
import java.util.StringTokenizer;
26
import java.util.List;
27
import java.util.ArrayList;
28
import java.util.Iterator;
29
import java.io.BufferedInputStream;
30
import java.io.InputStream;
31
import java.io.IOException;
32
import javax.servlet.ServletException;
33
import javax.servlet.UnavailableException;
35
import org.apache.commons.logging.Log;
36
import org.apache.commons.logging.LogFactory;
37
import org.apache.commons.validator.ValidatorResources;
38
import org.apache.struts.action.ActionServlet;
39
import org.apache.struts.action.PlugIn;
40
import org.apache.struts.config.ModuleConfig;
43
* Loads <code>ValidatorResources</code> based on configuration in the
44
* struts-config.xml file.
46
* @version $Revision: 1.27 $ $Date: 2004/08/31 18:39:04 $
49
public class ValidatorPlugIn implements PlugIn {
52
* Commons Logging instance.
54
private static Log log = LogFactory.getLog(ValidatorPlugIn.class);
57
* The module configuration for our owning module.
59
private ModuleConfig config = null;
62
* The {@link ActionServlet} owning this application.
64
private ActionServlet servlet = null;
67
* Delimitter for Validator resources.
69
private final static String RESOURCE_DELIM = ",";
72
* Application scope key that <code>ValidatorResources</code> is stored under.
74
public final static String VALIDATOR_KEY =
75
"org.apache.commons.validator.VALIDATOR_RESOURCES";
78
* Application scope key that <code>StopOnError</code> is stored under.
82
public final static String STOP_ON_ERROR_KEY =
83
"org.apache.struts.validator.STOP_ON_ERROR";
86
* The set of Form instances that have been created and initialized,
87
* keyed by the struts form name.
89
protected ValidatorResources resources = null;
92
// ------------------------------------------------------------- Properties
95
* A comma delimitted list of Validator resource.
97
private String pathnames = null;
100
* Gets a comma delimitted list of Validator resources.
102
* @return comma delimited list of Validator resource path names
104
public String getPathnames() {
109
* Sets a comma delimitted list of Validator resources.
111
* @param pathnames delimited list of Validator resource path names
113
public void setPathnames(String pathnames) {
114
this.pathnames = pathnames;
118
* Informs the Validators if it has to stop validation when finding the
119
* first error or if it should continue. Default to <code>true</code> to
120
* keep Struts 1.1 backwards compatibility.
122
private boolean stopOnFirstError = true;
125
* Gets the value for stopOnFirstError.
127
* @return A boolean indicating whether JavaScript validation should stop
128
* when it finds the first error (Struts 1.1 behaviour) or continue
132
public boolean isStopOnFirstError() {
133
return this.stopOnFirstError;
137
* Sets the value for stopOnFirstError.
139
* @param stopOnFirstError A boolean indicating whether JavaScript
140
* validation should stop when it finds the first error
141
* (Struts 1.1 behaviour) or continue validation.
144
public void setStopOnFirstError(boolean stopOnFirstError) {
145
this.stopOnFirstError = stopOnFirstError;
149
* Initialize and load our resources.
151
* @param servlet The ActionServlet for our application
152
* @param config The ModuleConfig for our owning module
153
* @throws ServletException if we cannot configure ourselves correctly
155
public void init(ActionServlet servlet, ModuleConfig config)
156
throws ServletException {
158
// Remember our associated configuration and servlet
159
this.config = config;
160
this.servlet = servlet;
162
// Load our database from persistent storage
164
this.initResources();
166
servlet.getServletContext().setAttribute(VALIDATOR_KEY + config.getPrefix(),
169
servlet.getServletContext().setAttribute(STOP_ON_ERROR_KEY + '.' + config.getPrefix(),
170
(this.stopOnFirstError ? Boolean.TRUE : Boolean.FALSE));
172
} catch (Exception e) {
173
log.error(e.getMessage(), e);
174
throw new UnavailableException("Cannot load a validator resource from '" + pathnames + "'");
180
* Gracefully shut down, releasing any resources
181
* that were allocated at initialization.
183
public void destroy() {
185
if (log.isDebugEnabled()) {
186
log.debug("Destroying ValidatorPlugin");
196
* Initialize the validator resources for this module.
198
* @throws IOException if an input/output error is encountered
199
* @throws ServletException if we cannot initialize these resources
201
protected void initResources() throws IOException, ServletException {
203
if (pathnames == null || pathnames.length() <= 0) {
206
StringTokenizer st = new StringTokenizer(pathnames, RESOURCE_DELIM);
208
List streamList = new ArrayList();
210
while (st.hasMoreTokens()) {
211
String validatorRules = st.nextToken().trim();
212
if (log.isInfoEnabled()) {
213
log.info("Loading validation rules file from '" + validatorRules + "'");
216
InputStream input = servlet.getServletContext().getResourceAsStream(validatorRules);
219
BufferedInputStream bis = new BufferedInputStream(input);
222
throw new ServletException("Skipping validation rules file from '"
223
+ validatorRules + "'. No stream could be opened.");
226
int streamSize = streamList.size();
227
InputStream[] streamArray = new InputStream[streamSize];
228
for (int streamIndex = 0;streamIndex < streamSize;streamIndex++) {
229
InputStream is = (InputStream) streamList.get(streamIndex);
230
streamArray[streamIndex] = is;
233
this.resources = new ValidatorResources(streamArray);
234
} catch (SAXException sex) {
235
log.error("Skipping all validation",sex);
236
throw new ServletException(sex);
238
Iterator streamIterator = streamList.iterator();
239
while (streamIterator.hasNext()) {
240
InputStream is = (InputStream) streamIterator.next();
248
* Destroy <code>ValidatorResources</code>.
250
protected void destroyResources() {