📄 validatorresources.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.validator;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.URL;
import java.util.Collections;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import org.apache.commons.collections.FastHashMap;
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.Rule;
import org.apache.commons.digester.xmlrules.DigesterLoader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;
/**
* <p>
* General purpose class for storing <code>FormSet</code> objects based
* on their associated <code>Locale</code>. Instances of this class are usually
* configured through a validation.xml file that is parsed in a constructor.
* </p>
*
* <p><strong>Note</strong> - Classes that extend this class
* must be Serializable so that instances may be used in distributable
* application server environments.</p>
*
* <p>
* The use of FastHashMap is deprecated and will be replaced in a future
* release.
* </p>
*
* @version $Revision: 478473 $ $Date: 2006-11-23 05:42:30 +0000 (Thu, 23 Nov 2006) $
*/
public class ValidatorResources implements Serializable {
/** Name of the digester validator rules file */
private static final String VALIDATOR_RULES = "digester-rules.xml";
/**
* The set of public identifiers, and corresponding resource names, for
* the versions of the configuration file DTDs that we know about. There
* <strong>MUST</strong> be an even number of Strings in this list!
*/
private static final String REGISTRATIONS[] = {
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN",
"/org/apache/commons/validator/resources/validator_1_0.dtd",
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0.1//EN",
"/org/apache/commons/validator/resources/validator_1_0_1.dtd",
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN",
"/org/apache/commons/validator/resources/validator_1_1.dtd",
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN",
"/org/apache/commons/validator/resources/validator_1_1_3.dtd",
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.2.0//EN",
"/org/apache/commons/validator/resources/validator_1_2_0.dtd",
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN",
"/org/apache/commons/validator/resources/validator_1_3_0.dtd"
};
private transient Log log = LogFactory.getLog(ValidatorResources.class);
/**
* <code>Map</code> of <code>FormSet</code>s stored under
* a <code>Locale</code> key.
* @deprecated Subclasses should use getFormSets() instead.
*/
protected FastHashMap hFormSets = new FastHashMap();
/**
* <code>Map</code> of global constant values with
* the name of the constant as the key.
* @deprecated Subclasses should use getConstants() instead.
*/
protected FastHashMap hConstants = new FastHashMap();
/**
* <code>Map</code> of <code>ValidatorAction</code>s with
* the name of the <code>ValidatorAction</code> as the key.
* @deprecated Subclasses should use getActions() instead.
*/
protected FastHashMap hActions = new FastHashMap();
/**
* The default locale on our server.
*/
protected static Locale defaultLocale = Locale.getDefault();
/**
* Create an empty ValidatorResources object.
*/
public ValidatorResources() {
super();
}
/**
* This is the default <code>FormSet</code> (without locale). (We probably don't need
* the defaultLocale anymore.)
*/
protected FormSet defaultFormSet;
/**
* Create a ValidatorResources object from an InputStream.
*
* @param in InputStream to a validation.xml configuration file. It's the client's
* responsibility to close this stream.
* @throws IOException
* @throws SAXException if the validation XML files are not valid or well
* formed.
* @throws IOException if an I/O error occurs processing the XML files
* @since Validator 1.1
*/
public ValidatorResources(InputStream in) throws IOException, SAXException {
this(new InputStream[]{in});
}
/**
* Create a ValidatorResources object from an InputStream.
*
* @param streams An array of InputStreams to several validation.xml
* configuration files that will be read in order and merged into this object.
* It's the client's responsibility to close these streams.
* @throws IOException
* @throws SAXException if the validation XML files are not valid or well
* formed.
* @throws IOException if an I/O error occurs processing the XML files
* @since Validator 1.1
*/
public ValidatorResources(InputStream[] streams)
throws IOException, SAXException {
super();
Digester digester = initDigester();
for (int i = 0; i < streams.length; i++) {
digester.push(this);
digester.parse(streams[i]);
}
this.process();
}
/**
* Create a ValidatorResources object from an uri
*
* @param uri The location of a validation.xml configuration file.
* @throws IOException
* @throws SAXException if the validation XML files are not valid or well
* formed.
* @throws IOException if an I/O error occurs processing the XML files
* @since Validator 1.2
*/
public ValidatorResources(String uri) throws IOException, SAXException {
this(new String[]{uri});
}
/**
* Create a ValidatorResources object from several uris
*
* @param uris An array of uris to several validation.xml
* configuration files that will be read in order and merged into this object.
* @throws IOException
* @throws SAXException if the validation XML files are not valid or well
* formed.
* @throws IOException if an I/O error occurs processing the XML files
* @since Validator 1.2
*/
public ValidatorResources(String[] uris)
throws IOException, SAXException {
super();
Digester digester = initDigester();
for (int i = 0; i < uris.length; i++) {
digester.push(this);
digester.parse(uris[i]);
}
this.process();
}
/**
* Create a ValidatorResources object from a URL.
*
* @param url The URL for the validation.xml
* configuration file that will be read into this object.
* @throws IOException
* @throws SAXException if the validation XML file are not valid or well
* formed.
* @throws IOException if an I/O error occurs processing the XML files
* @since Validator 1.3.1
*/
public ValidatorResources(URL url)
throws IOException, SAXException {
this(new URL[]{url});
}
/**
* Create a ValidatorResources object from several URL.
*
* @param urls An array of URL to several validation.xml
* configuration files that will be read in order and merged into this object.
* @throws IOException
* @throws SAXException if the validation XML files are not valid or well
* formed.
* @throws IOException if an I/O error occurs processing the XML files
* @since Validator 1.3.1
*/
public ValidatorResources(URL[] urls)
throws IOException, SAXException {
super();
Digester digester = initDigester();
for (int i = 0; i < urls.length; i++) {
digester.push(this);
InputStream stream = null;
try {
stream = urls[i].openStream();
org.xml.sax.InputSource source =
new org.xml.sax.InputSource(urls[i].toExternalForm());
source.setByteStream(stream);
digester.parse(source);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// ignore problem closing
}
}
}
}
this.process();
}
/**
* Initialize the digester.
*/
private Digester initDigester() {
URL rulesUrl = this.getClass().getResource(VALIDATOR_RULES);
if (rulesUrl == null) {
// Fix for Issue# VALIDATOR-195
rulesUrl = ValidatorResources.class.getResource(VALIDATOR_RULES);
}
if (getLog().isDebugEnabled()) {
getLog().debug("Loading rules from '" + rulesUrl + "'");
}
Digester digester = DigesterLoader.createDigester(rulesUrl);
digester.setNamespaceAware(true);
digester.setValidating(true);
digester.setUseContextClassLoader(true);
// Add rules for arg0-arg3 elements
addOldArgRules(digester);
// register DTDs
for (int i = 0; i < REGISTRATIONS.length; i += 2) {
URL url = this.getClass().getResource(REGISTRATIONS[i + 1]);
if (url != null) {
digester.register(REGISTRATIONS[i], url.toString());
}
}
return digester;
}
private static final String ARGS_PATTERN
= "form-validation/formset/form/field/arg";
/**
* Create a <code>Rule</code> to handle <code>arg0-arg3</code>
* elements. This will allow validation.xml files that use the
* versions of the DTD prior to Validator 1.2.0 to continue
* working.
*/
private void addOldArgRules(Digester digester) {
// Create a new rule to process args elements
Rule rule = new Rule() {
public void begin(String namespace, String name,
Attributes attributes) throws Exception {
// Create the Arg
Arg arg = new Arg();
arg.setKey(attributes.getValue("key"));
arg.setName(attributes.getValue("name"));
if ("false".equalsIgnoreCase(attributes.getValue("resource"))) {
arg.setResource(false);
}
try {
arg.setPosition(Integer.parseInt(name.substring(3)));
} catch (Exception ex) {
getLog().error("Error parsing Arg position: "
+ name + " " + arg + " " + ex);
}
// Add the arg to the parent field
((Field)getDigester().peek(0)).addArg(arg);
}
};
// Add the rule for each of the arg elements
digester.addRule(ARGS_PATTERN + "0", rule);
digester.addRule(ARGS_PATTERN + "1", rule);
digester.addRule(ARGS_PATTERN + "2", rule);
digester.addRule(ARGS_PATTERN + "3", rule);
}
/**
* Add a <code>FormSet</code> to this <code>ValidatorResources</code>
* object. It will be associated with the <code>Locale</code> of the
* <code>FormSet</code>.
* @param fs The form set to add.
* @since Validator 1.1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -