1 /*
2 * $Header: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/Attic/ValidatorResourcesInitializer.java,v 1.22 2004/02/21 17:10:29 rleland Exp $
3 * $Revision: 1.22 $
4 * $Date: 2004/02/21 17:10:29 $
5 *
6 * ====================================================================
7 * Copyright 2001-2004 The Apache Software Foundation
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22 package org.apache.commons.validator;
23
24 import java.io.BufferedInputStream;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.URL;
29
30 import org.apache.commons.digester.Digester;
31 import org.apache.commons.digester.xmlrules.DigesterLoader;
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import org.xml.sax.SAXException;
36
37 /***
38 * <p>Maps an xml file to <code>ValidatorResources</code>.</p>
39 *
40 * @deprecated ValidatorResources knows how to initialize itself now.
41 */
42 public class ValidatorResourcesInitializer {
43
44 /***
45 * Logger.
46 * @deprecated Subclasses should use their own logging instance.
47 */
48 protected static Log log = LogFactory.getLog(ValidatorResourcesInitializer.class);
49
50
51 /***
52 * The set of public identifiers, and corresponding resource names, for
53 * the versions of the configuration file DTDs that we know about. There
54 * <strong>MUST</strong> be an even number of Strings in this list!
55 */
56 protected static String registrations[] = {
57 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN",
58 "/org/apache/commons/validator/resources/validator_1_0.dtd",
59 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0.1//EN",
60 "/org/apache/commons/validator/resources/validator_1_0_1.dtd",
61 "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN",
62 "/org/apache/commons/validator/resources/validator_1_1.dtd"
63 };
64
65
66 /***
67 * Initializes a <code>ValidatorResources</code> based on a
68 * file path and automatically process the resources.
69 *
70 * @param fileName The file path for the xml resource.
71 */
72 public static ValidatorResources initialize(String fileName)
73 throws IOException {
74
75 return initialize(new BufferedInputStream(new FileInputStream(fileName)));
76 }
77
78 /***
79 * Initializes a <code>ValidatorResources</code> based on the
80 * <code>InputStream</code> and automatically process the resources.
81 *
82 * @param in <code>InputStream</code> for the xml resource.
83 */
84 public static ValidatorResources initialize(InputStream in) throws IOException {
85
86 ValidatorResources resources = new ValidatorResources();
87 initialize(resources, in);
88
89 return resources;
90 }
91
92
93 /***
94 * Initializes the <code>ValidatorResources</code> based on the <code>InputStream</code>
95 * and automatically process the resources.
96 *
97 * @param resources Resources to initialize.
98 * @param in <code>InputStream</code> for the xml resource.
99 */
100 public static void initialize(ValidatorResources resources, InputStream in)
101 throws IOException {
102
103 initialize(resources, in, true);
104 }
105
106 /***
107 * Initializes a <code>ValidatorResources</code> based on the <code>InputStream</code>
108 * and processes the resources based on the <code>boolean</code> passed in.
109 *
110 * @param resources Resources to initialize.
111 * @param in <code>InputStream</code> for the xml resource.
112 * @param process Whether or not to call process on
113 * <code>ValidatorResources</code>.
114 */
115 public static void initialize(
116 ValidatorResources resources,
117 InputStream in,
118 boolean process)
119 throws IOException {
120
121 URL rulesUrl = ValidatorResourcesInitializer.class.getResource("digester-rules.xml");
122 Digester digester = DigesterLoader.createDigester(rulesUrl);
123 digester.setNamespaceAware(true);
124 digester.setValidating(false);
125 digester.setUseContextClassLoader(true);
126
127 // register DTDs
128 for (int i = 0; i < registrations.length; i += 2) {
129 URL url =
130 ValidatorResourcesInitializer.class.getResource(
131 registrations[i + 1]);
132 if (url != null) {
133 digester.register(registrations[i], url.toString());
134 }
135 }
136
137 digester.push(resources);
138
139 try {
140 digester.parse(in);
141
142 } catch(SAXException e) {
143 log.error(e.getMessage(), e);
144
145 } finally {
146 if (in != null) {
147 in.close();
148 }
149 }
150
151 if (process) {
152 resources.process();
153 }
154
155 }
156
157 }
This page was automatically generated by Maven