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. package org.apache.commons.validator;
  22. import java.io.BufferedInputStream;
  23. import java.io.FileInputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.net.URL;
  27. import org.apache.commons.digester.Digester;
  28. import org.apache.commons.digester.xmlrules.DigesterLoader;
  29. import org.apache.commons.logging.Log;
  30. import org.apache.commons.logging.LogFactory;
  31. import org.xml.sax.SAXException;
  32. /**
  33. * <p>Maps an xml file to <code>ValidatorResources</code>.</p>
  34. *
  35. * @deprecated ValidatorResources knows how to initialize itself now.
  36. */
  37. public class ValidatorResourcesInitializer {
  38. /**
  39. * Logger.
  40. * @deprecated Subclasses should use their own logging instance.
  41. */
  42. protected static Log log = LogFactory.getLog(ValidatorResourcesInitializer.class);
  43. /**
  44. * The set of public identifiers, and corresponding resource names, for
  45. * the versions of the configuration file DTDs that we know about. There
  46. * <strong>MUST</strong> be an even number of Strings in this list!
  47. */
  48. protected static String registrations[] = {
  49. "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN",
  50. "/org/apache/commons/validator/resources/validator_1_0.dtd",
  51. "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0.1//EN",
  52. "/org/apache/commons/validator/resources/validator_1_0_1.dtd",
  53. "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN",
  54. "/org/apache/commons/validator/resources/validator_1_1.dtd"
  55. };
  56. /**
  57. * Initializes a <code>ValidatorResources</code> based on a
  58. * file path and automatically process the resources.
  59. *
  60. * @param fileName The file path for the xml resource.
  61. */
  62. public static ValidatorResources initialize(String fileName)
  63. throws IOException {
  64. return initialize(new BufferedInputStream(new FileInputStream(fileName)));
  65. }
  66. /**
  67. * Initializes a <code>ValidatorResources</code> based on the
  68. * <code>InputStream</code> and automatically process the resources.
  69. *
  70. * @param in <code>InputStream</code> for the xml resource.
  71. */
  72. public static ValidatorResources initialize(InputStream in) throws IOException {
  73. ValidatorResources resources = new ValidatorResources();
  74. initialize(resources, in);
  75. return resources;
  76. }
  77. /**
  78. * Initializes the <code>ValidatorResources</code> based on the <code>InputStream</code>
  79. * and automatically process the resources.
  80. *
  81. * @param resources Resources to initialize.
  82. * @param in <code>InputStream</code> for the xml resource.
  83. */
  84. public static void initialize(ValidatorResources resources, InputStream in)
  85. throws IOException {
  86. initialize(resources, in, true);
  87. }
  88. /**
  89. * Initializes a <code>ValidatorResources</code> based on the <code>InputStream</code>
  90. * and processes the resources based on the <code>boolean</code> passed in.
  91. *
  92. * @param resources Resources to initialize.
  93. * @param in <code>InputStream</code> for the xml resource.
  94. * @param process Whether or not to call process on
  95. * <code>ValidatorResources</code>.
  96. */
  97. public static void initialize(
  98. ValidatorResources resources,
  99. InputStream in,
  100. boolean process)
  101. throws IOException {
  102. URL rulesUrl = ValidatorResourcesInitializer.class.getResource("digester-rules.xml");
  103. Digester digester = DigesterLoader.createDigester(rulesUrl);
  104. digester.setNamespaceAware(true);
  105. digester.setValidating(false);
  106. digester.setUseContextClassLoader(true);
  107. // register DTDs
  108. for (int i = 0; i < registrations.length; i += 2) {
  109. URL url =
  110. ValidatorResourcesInitializer.class.getResource(
  111. registrations[i + 1]);
  112. if (url != null) {
  113. digester.register(registrations[i], url.toString());
  114. }
  115. }
  116. digester.push(resources);
  117. try {
  118. digester.parse(in);
  119. } catch(SAXException e) {
  120. log.error(e.getMessage(), e);
  121. } finally {
  122. if (in != null) {
  123. in.close();
  124. }
  125. }
  126. if (process) {
  127. resources.process();
  128. }
  129. }
  130. }