1. /* $Id: GenericParser.java,v 1.6 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.util.Properties;
  19. import javax.xml.parsers.ParserConfigurationException;
  20. import javax.xml.parsers.SAXParser;
  21. import javax.xml.parsers.SAXParserFactory;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.xml.sax.SAXException;
  25. import org.xml.sax.SAXNotRecognizedException;
  26. import org.xml.sax.SAXNotSupportedException;
  27. /**
  28. * Create a <code>SAXParser</code> configured to support XML Schema and DTD.
  29. *
  30. * @since 1.6
  31. */
  32. public class GenericParser{
  33. /**
  34. * The Log to which all SAX event related logging calls will be made.
  35. */
  36. protected static Log log =
  37. LogFactory.getLog("org.apache.commons.digester.Digester.sax");
  38. /**
  39. * The JAXP 1.2 property required to set up the schema location.
  40. */
  41. private static final String JAXP_SCHEMA_SOURCE =
  42. "http://java.sun.com/xml/jaxp/properties/schemaSource";
  43. /**
  44. * The JAXP 1.2 property to set up the schemaLanguage used.
  45. */
  46. protected static String JAXP_SCHEMA_LANGUAGE =
  47. "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
  48. /**
  49. * Create a <code>SAXParser</code> configured to support XML Scheman and DTD
  50. * @param properties parser specific properties/features
  51. * @return an XML Schema/DTD enabled <code>SAXParser</code>
  52. */
  53. public static SAXParser newSAXParser(Properties properties)
  54. throws ParserConfigurationException,
  55. SAXException,
  56. SAXNotRecognizedException{
  57. SAXParserFactory factory =
  58. (SAXParserFactory)properties.get("SAXParserFactory");
  59. SAXParser parser = factory.newSAXParser();
  60. String schemaLocation = (String)properties.get("schemaLocation");
  61. String schemaLanguage = (String)properties.get("schemaLanguage");
  62. try{
  63. if (schemaLocation != null) {
  64. parser.setProperty(JAXP_SCHEMA_LANGUAGE, schemaLanguage);
  65. parser.setProperty(JAXP_SCHEMA_SOURCE, schemaLocation);
  66. }
  67. } catch (SAXNotRecognizedException e){
  68. log.info(parser.getClass().getName() + ": "
  69. + e.getMessage() + " not supported.");
  70. }
  71. return parser;
  72. }
  73. }