~ubuntu-branches/ubuntu/trusty/libstruts1.2-java/trusty-proposed

« back to all changes in this revision

Viewing changes to src/share/org/apache/struts/validator/ValidatorPlugIn.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2004-11-19 15:35:25 UTC
  • Revision ID: james.westby@ubuntu.com-20041119153525-mdu08a76z4zo67xt
Tags: upstream-1.2.4
ImportĀ upstreamĀ versionĀ 1.2.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 1.27 $
 
4
 * $Date: 2004/08/31 18:39:04 $
 
5
 *
 
6
 * Copyright 2000-2004 The Apache Software Foundation.
 
7
 * 
 
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
 
11
 * 
 
12
 *      http://www.apache.org/licenses/LICENSE-2.0
 
13
 * 
 
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.
 
19
 */
 
20
 
 
21
package org.apache.struts.validator;
 
22
 
 
23
import org.xml.sax.SAXException;
 
24
 
 
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;
 
34
 
 
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;
 
41
 
 
42
/**
 
43
 * Loads <code>ValidatorResources</code> based on configuration in the
 
44
 * struts-config.xml file.
 
45
 *
 
46
 * @version $Revision: 1.27 $ $Date: 2004/08/31 18:39:04 $
 
47
 * @since Struts 1.1
 
48
 */
 
49
public class ValidatorPlugIn implements PlugIn {
 
50
 
 
51
    /**
 
52
     * Commons Logging instance.
 
53
     */
 
54
    private static Log log = LogFactory.getLog(ValidatorPlugIn.class);
 
55
 
 
56
    /**
 
57
     * The module configuration for our owning module.
 
58
     */
 
59
    private ModuleConfig config = null;
 
60
 
 
61
    /**
 
62
     * The {@link ActionServlet} owning this application.
 
63
     */
 
64
    private ActionServlet servlet = null;
 
65
 
 
66
    /**
 
67
     * Delimitter for Validator resources.
 
68
     */
 
69
    private final static String RESOURCE_DELIM = ",";
 
70
 
 
71
    /**
 
72
     * Application scope key that <code>ValidatorResources</code> is stored under.
 
73
     */
 
74
    public final static String VALIDATOR_KEY =
 
75
            "org.apache.commons.validator.VALIDATOR_RESOURCES";
 
76
 
 
77
    /**
 
78
     * Application scope key that <code>StopOnError</code> is stored under.
 
79
     *
 
80
     * @since Struts 1.2
 
81
     */
 
82
    public final static String STOP_ON_ERROR_KEY =
 
83
            "org.apache.struts.validator.STOP_ON_ERROR";
 
84
 
 
85
    /**
 
86
     * The set of Form instances that have been created and initialized,
 
87
     * keyed by the struts form name.
 
88
     */
 
89
    protected ValidatorResources resources = null;
 
90
 
 
91
 
 
92
    // ------------------------------------------------------------- Properties
 
93
 
 
94
    /**
 
95
     * A comma delimitted list of Validator resource.
 
96
     */
 
97
    private String pathnames = null;
 
98
 
 
99
    /**
 
100
     * Gets a comma delimitted list of Validator resources.
 
101
     *
 
102
     * @return comma delimited list of Validator resource path names
 
103
     */
 
104
    public String getPathnames() {
 
105
        return pathnames;
 
106
    }
 
107
 
 
108
    /**
 
109
     * Sets a comma delimitted list of Validator resources.
 
110
     *
 
111
     * @param pathnames delimited list of Validator resource path names
 
112
     */
 
113
    public void setPathnames(String pathnames) {
 
114
        this.pathnames = pathnames;
 
115
    }
 
116
 
 
117
    /**
 
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.
 
121
     */
 
122
    private boolean stopOnFirstError = true;
 
123
 
 
124
    /**
 
125
     * Gets the value for stopOnFirstError.
 
126
     *
 
127
     * @return A boolean indicating whether JavaScript validation should stop
 
128
     *         when it finds the first error (Struts 1.1 behaviour) or continue
 
129
     *         validation.
 
130
     * @since Struts 1.2
 
131
     */
 
132
    public boolean isStopOnFirstError() {
 
133
        return this.stopOnFirstError;
 
134
    }
 
135
 
 
136
    /**
 
137
     * Sets the value for stopOnFirstError.
 
138
     *
 
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.
 
142
     * @since Struts 1.2
 
143
     */
 
144
    public void setStopOnFirstError(boolean stopOnFirstError) {
 
145
        this.stopOnFirstError = stopOnFirstError;
 
146
    }
 
147
 
 
148
    /**
 
149
     * Initialize and load our resources.
 
150
     *
 
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
 
154
     */
 
155
    public void init(ActionServlet servlet, ModuleConfig config)
 
156
            throws ServletException {
 
157
 
 
158
        // Remember our associated configuration and servlet
 
159
        this.config = config;
 
160
        this.servlet = servlet;
 
161
 
 
162
        // Load our database from persistent storage
 
163
        try {
 
164
            this.initResources();
 
165
 
 
166
            servlet.getServletContext().setAttribute(VALIDATOR_KEY + config.getPrefix(),
 
167
                                                     resources);
 
168
 
 
169
            servlet.getServletContext().setAttribute(STOP_ON_ERROR_KEY + '.' + config.getPrefix(),
 
170
                                                     (this.stopOnFirstError ? Boolean.TRUE : Boolean.FALSE));
 
171
 
 
172
        } catch (Exception e) {
 
173
            log.error(e.getMessage(), e);
 
174
            throw new UnavailableException("Cannot load a validator resource from '" + pathnames + "'");
 
175
        }
 
176
 
 
177
    }
 
178
 
 
179
    /**
 
180
     * Gracefully shut down, releasing any resources
 
181
     * that were allocated at initialization.
 
182
     */
 
183
    public void destroy() {
 
184
 
 
185
        if (log.isDebugEnabled()) {
 
186
            log.debug("Destroying ValidatorPlugin");
 
187
        }
 
188
 
 
189
        servlet = null;
 
190
        config = null;
 
191
 
 
192
        destroyResources();
 
193
    }
 
194
 
 
195
    /**
 
196
     * Initialize the validator resources for this module.
 
197
     *
 
198
     * @throws IOException      if an input/output error is encountered
 
199
     * @throws ServletException if we cannot initialize these resources
 
200
     */
 
201
    protected void initResources() throws IOException, ServletException {
 
202
 
 
203
        if (pathnames == null || pathnames.length() <= 0) {
 
204
            return;
 
205
        }
 
206
        StringTokenizer st = new StringTokenizer(pathnames, RESOURCE_DELIM);
 
207
 
 
208
        List streamList = new ArrayList();
 
209
        try {
 
210
            while (st.hasMoreTokens()) {
 
211
                String validatorRules = st.nextToken().trim();
 
212
                if (log.isInfoEnabled()) {
 
213
                    log.info("Loading validation rules file from '" + validatorRules + "'");
 
214
                }
 
215
 
 
216
                InputStream input = servlet.getServletContext().getResourceAsStream(validatorRules);
 
217
 
 
218
                if (input != null) {
 
219
                    BufferedInputStream bis = new BufferedInputStream(input);
 
220
                    streamList.add(bis);
 
221
                } else {
 
222
                    throw new ServletException("Skipping validation rules file from '"
 
223
                              + validatorRules + "'.  No stream could be opened.");
 
224
                }
 
225
            }
 
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;
 
231
            }
 
232
 
 
233
            this.resources = new ValidatorResources(streamArray);
 
234
        } catch (SAXException sex) {
 
235
            log.error("Skipping all validation",sex);
 
236
            throw new ServletException(sex);
 
237
        } finally {
 
238
            Iterator streamIterator = streamList.iterator();
 
239
            while (streamIterator.hasNext()) {
 
240
                InputStream is = (InputStream) streamIterator.next();
 
241
                is.close();
 
242
            }
 
243
        }
 
244
 
 
245
    }
 
246
 
 
247
    /**
 
248
     * Destroy <code>ValidatorResources</code>.
 
249
     */
 
250
    protected void destroyResources() {
 
251
        resources = null;
 
252
    }
 
253
 
 
254
}