You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigurationReader.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.configuration;
  8. // sax
  9. import org.xml.sax.helpers.DefaultHandler;
  10. import org.xml.sax.XMLReader;
  11. import org.xml.sax.SAXException;
  12. import java.io.IOException;
  13. import org.xml.sax.InputSource;
  14. // fop
  15. import org.apache.fop.apps.Driver;
  16. import org.apache.fop.apps.FOPException;
  17. import org.apache.fop.configuration.Configuration;
  18. /**
  19. * entry class for reading configuration from file and creating a configuration
  20. * class. typical use looks like that: <br>
  21. *
  22. * <code>ConfigurationReader reader = new ConfigurationReader ("config.xml","standard");
  23. * try {
  24. * reader.start();
  25. * } catch (org.apache.fop.apps.FOPException error) {
  26. * reader.dumpError(error);
  27. * }
  28. * </code>
  29. * Once the configuration has been setup, the information can be accessed with
  30. * the methods of StandardConfiguration.
  31. */
  32. public class ConfigurationReader {
  33. /**
  34. * show a full dump on error
  35. */
  36. private static boolean errorDump = false;
  37. /**
  38. * inputsource for configuration file
  39. */
  40. private InputSource filename;
  41. /**
  42. * creates a configuration reader
  43. * @param filename the file which contains the configuration information
  44. */
  45. public ConfigurationReader(InputSource filename) {
  46. this.filename = filename;
  47. }
  48. /**
  49. * intantiates parser and starts parsing of config file
  50. */
  51. public void start() throws FOPException {
  52. XMLReader parser = createParser();
  53. // setting the parser features
  54. try {
  55. parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
  56. false);
  57. } catch (SAXException e) {
  58. throw new FOPException("You need a parser which supports SAX version 2",
  59. e);
  60. }
  61. ConfigurationParser configurationParser = new ConfigurationParser();
  62. parser.setContentHandler(configurationParser);
  63. try {
  64. parser.parse(filename);
  65. } catch (SAXException e) {
  66. if (e.getException() instanceof FOPException) {
  67. throw (FOPException)e.getException();
  68. } else {
  69. throw new FOPException(e);
  70. }
  71. } catch (IOException e) {
  72. throw new FOPException(e);
  73. }
  74. }
  75. /**
  76. * creates a SAX parser, using the value of org.xml.sax.parser
  77. * defaulting to org.apache.xerces.parsers.SAXParser
  78. *
  79. * @return the created SAX parser
  80. */
  81. public static XMLReader createParser() throws FOPException {
  82. String parserClassName = Driver.getParserClassName();
  83. if (errorDump) {
  84. //log.debug("configuration reader using SAX parser "
  85. // + parserClassName);
  86. }
  87. try {
  88. return (XMLReader)Class.forName(parserClassName).newInstance();
  89. } catch (ClassNotFoundException e) {
  90. throw new FOPException("Could not find " + parserClassName, e);
  91. } catch (InstantiationException e) {
  92. throw new FOPException("Could not instantiate "
  93. + parserClassName, e);
  94. } catch (IllegalAccessException e) {
  95. throw new FOPException("Could not access " + parserClassName, e);
  96. } catch (ClassCastException e) {
  97. throw new FOPException(parserClassName + " is not a SAX driver",
  98. e);
  99. }
  100. }
  101. /**
  102. * Dumps an error
  103. */
  104. public void dumpError(Exception e) {
  105. if (errorDump) {
  106. if (e instanceof SAXException) {
  107. e.printStackTrace();
  108. if (((SAXException)e).getException() != null) {
  109. ((SAXException)e).getException().printStackTrace();
  110. }
  111. } else {
  112. e.printStackTrace();
  113. }
  114. }
  115. }
  116. /**
  117. * long or short error messages
  118. *
  119. */
  120. public void setDumpError(boolean dumpError) {
  121. errorDump = dumpError;
  122. }
  123. }