1. /* $Id: XercesParser.java,v 1.7 2004/05/10 06:37:22 skitching Exp $
  2. *
  3. * Copyright 2004 The Apache Software Foundation.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.commons.digester.parser;
  18. import java.lang.reflect.Method;
  19. import java.util.Properties;
  20. import javax.xml.parsers.ParserConfigurationException;
  21. import javax.xml.parsers.SAXParser;
  22. import javax.xml.parsers.SAXParserFactory;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. import org.xml.sax.SAXException;
  26. import org.xml.sax.SAXNotRecognizedException;
  27. import org.xml.sax.SAXNotSupportedException;
  28. /**
  29. * Create a <code>SAXParser</code> based on the underlying Xerces version.
  30. * Currently, Xerces 2.3 and up doesn't implement schema validation the same way
  31. * 2.1 was. In other to support schema validation in a portable way between
  32. * parser, some features/properties need to be set.
  33. *
  34. * @since 1.6
  35. */
  36. public class XercesParser{
  37. /**
  38. * The Log to which all SAX event related logging calls will be made.
  39. */
  40. protected static Log log =
  41. LogFactory.getLog("org.apache.commons.digester.Digester.sax");
  42. /**
  43. * The JAXP 1.2 property required to set up the schema location.
  44. */
  45. private static final String JAXP_SCHEMA_SOURCE =
  46. "http://java.sun.com/xml/jaxp/properties/schemaSource";
  47. /**
  48. * The JAXP 1.2 property to set up the schemaLanguage used.
  49. */
  50. protected static String JAXP_SCHEMA_LANGUAGE =
  51. "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  52. /**
  53. * Xerces dynamic validation property
  54. */
  55. protected static String XERCES_DYNAMIC =
  56. "http://apache.org/xml/features/validation/dynamic";
  57. /**
  58. * Xerces schema validation property
  59. */
  60. protected static String XERCES_SCHEMA =
  61. "http://apache.org/xml/features/validation/schema";
  62. /**
  63. * A <code>float</code> representing the underlying Xerces version
  64. */
  65. protected static float version;
  66. /**
  67. * The current Xerces version.
  68. */
  69. protected static String versionNumber = null;
  70. /**
  71. * Return the current Xerces version.
  72. * @return the current Xerces version.
  73. */
  74. private static String getXercesVersion() {
  75. // If for some reason we can't get the version, set it to 1.0.
  76. String versionNumber = "1.0";
  77. try{
  78. // Use reflection to avoid a build dependency with Xerces.
  79. Class versionClass =
  80. Class.forName("org.apache.xerces.impl.Version");
  81. // Will return Xerces-J 2.x.0
  82. Method method =
  83. versionClass.getMethod("getVersion", null);
  84. String version = (String)method.invoke(null,null);
  85. versionNumber = version.substring( "Xerces-J".length() ,
  86. version.lastIndexOf(".") );
  87. } catch (Exception ex){
  88. // Do nothing.
  89. }
  90. return versionNumber;
  91. }
  92. /**
  93. * Create a <code>SAXParser</code> based on the underlying
  94. * <code>Xerces</code> version.
  95. * @param properties parser specific properties/features
  96. * @return an XML Schema/DTD enabled <code>SAXParser</code>
  97. */
  98. public static SAXParser newSAXParser(Properties properties)
  99. throws ParserConfigurationException,
  100. SAXException,
  101. SAXNotSupportedException {
  102. SAXParserFactory factory =
  103. (SAXParserFactory)properties.get("SAXParserFactory");
  104. if (versionNumber == null){
  105. versionNumber = getXercesVersion();
  106. version = new Float( versionNumber ).floatValue();
  107. }
  108. // Note: 2.2 is completely broken (with XML Schema).
  109. if (version > 2.1) {
  110. configureXerces(factory);
  111. return factory.newSAXParser();
  112. } else {
  113. SAXParser parser = factory.newSAXParser();
  114. configureOldXerces(parser,properties);
  115. return parser;
  116. }
  117. }
  118. /**
  119. * Configure schema validation as recommended by the JAXP 1.2 spec.
  120. * The <code>properties</code> object may contains information about
  121. * the schema local and language.
  122. * @param properties parser optional info
  123. */
  124. private static void configureOldXerces(SAXParser parser,
  125. Properties properties)
  126. throws ParserConfigurationException,
  127. SAXNotSupportedException {
  128. String schemaLocation = (String)properties.get("schemaLocation");
  129. String schemaLanguage = (String)properties.get("schemaLanguage");
  130. try{
  131. if (schemaLocation != null) {
  132. parser.setProperty(JAXP_SCHEMA_LANGUAGE, schemaLanguage);
  133. parser.setProperty(JAXP_SCHEMA_SOURCE, schemaLocation);
  134. }
  135. } catch (SAXNotRecognizedException e){
  136. log.info(parser.getClass().getName() + ": "
  137. + e.getMessage() + " not supported.");
  138. }
  139. }
  140. /**
  141. * Configure schema validation as recommended by the Xerces spec.
  142. * Both DTD and Schema validation will be enabled simultaneously.
  143. * @param factory SAXParserFactory to be configured
  144. */
  145. private static void configureXerces(SAXParserFactory factory)
  146. throws ParserConfigurationException,
  147. SAXNotRecognizedException,
  148. SAXNotSupportedException {
  149. factory.setFeature(XERCES_DYNAMIC, true);
  150. factory.setFeature(XERCES_SCHEMA, true);
  151. }
  152. }