1. /* $Id: ParserFeatureSetterFactory.java,v 1.7 2004/05/10 06:52:50 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;
  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.digester.parser.GenericParser;
  24. import org.apache.commons.digester.parser.XercesParser;
  25. import org.xml.sax.SAXException;
  26. import org.xml.sax.SAXNotRecognizedException;
  27. import org.xml.sax.SAXNotSupportedException;
  28. /**
  29. * Creates a <code>SAXParser</code> based on the underlying parser.
  30. * Allows logical properties depending on logical parser versions
  31. * to be set.
  32. *
  33. * @since 1.6
  34. */
  35. public class ParserFeatureSetterFactory{
  36. /**
  37. * <code>true</code> is Xerces is used.
  38. */
  39. private static boolean isXercesUsed;
  40. static {
  41. try{
  42. // Use reflection to avoid a build dependency with Xerces.
  43. Class versionClass =
  44. Class.forName("org.apache.xerces.impl.Version");
  45. isXercesUsed = true;
  46. } catch (Exception ex){
  47. isXercesUsed = false;
  48. }
  49. }
  50. /**
  51. * Create a new <code>SAXParser</code>
  52. * @param properties (logical) properties to be set on parser
  53. * @return a <code>SAXParser</code> configured based on the underlying
  54. * parser implementation.
  55. */
  56. public static SAXParser newSAXParser(Properties properties)
  57. throws ParserConfigurationException,
  58. SAXException,
  59. SAXNotRecognizedException,
  60. SAXNotSupportedException {
  61. if (isXercesUsed){
  62. return XercesParser.newSAXParser(properties);
  63. } else {
  64. return GenericParser.newSAXParser(properties);
  65. }
  66. }
  67. }