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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 org.xml.sax.InputSource;
  13. // java
  14. import java.io.IOException;
  15. import javax.xml.parsers.*;
  16. // fop
  17. import org.apache.fop.apps.Driver;
  18. import org.apache.fop.messaging.MessageHandler;
  19. import org.apache.fop.apps.FOPException;
  20. import org.apache.fop.configuration.Configuration;
  21. /**
  22. * Entry class for reading configuration from file and creating a configuration
  23. * class. Example of typical use:
  24. *
  25. * <pre>ConfigurationReader reader = new ConfigurationReader ("config.xml","standard");
  26. * try {
  27. * reader.start();
  28. * }
  29. * catch (org.apache.fop.apps.FOPException error) {
  30. * reader.dumpError(error);
  31. * }</pre>
  32. *
  33. * Once the configuration has been setup, the information can be accessed with
  34. * the methods of StandardConfiguration.
  35. *
  36. * @version $Revision$
  37. */
  38. public class ConfigurationReader {
  39. /**
  40. * show a full dump on error
  41. */
  42. private static boolean errorDump = false;
  43. /**
  44. * inputsource for configuration file
  45. */
  46. private InputSource filename;
  47. /**
  48. * creates a configuration reader
  49. * @param filename the file which contains the configuration information
  50. */
  51. public ConfigurationReader(InputSource filename) {
  52. this.filename = filename;
  53. }
  54. /**
  55. * intantiates parser and starts parsing of config file
  56. */
  57. public void start() throws FOPException {
  58. XMLReader parser = createParser();
  59. ConfigurationParser configurationParser = new ConfigurationParser();
  60. parser.setContentHandler(configurationParser);
  61. try {
  62. parser.parse(filename);
  63. } catch (SAXException e) {
  64. if (e.getException() instanceof FOPException) {
  65. throw (FOPException)e.getException();
  66. } else {
  67. throw new FOPException(e);
  68. }
  69. } catch (IOException e) {
  70. throw new FOPException(e);
  71. }
  72. }
  73. /**
  74. * creates a SAX parser
  75. *
  76. * @return the created SAX parser
  77. */
  78. public static XMLReader createParser() throws FOPException {
  79. try {
  80. SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance();
  81. spf.setNamespaceAware(true);
  82. XMLReader xmlReader = spf.newSAXParser().getXMLReader();
  83. MessageHandler.logln("Using " + xmlReader.getClass().getName() + " as SAX2 Parser");
  84. return xmlReader;
  85. } catch (javax.xml.parsers.ParserConfigurationException e) {
  86. throw new FOPException(e);
  87. } catch (org.xml.sax.SAXException e) {
  88. throw new FOPException( e);
  89. }
  90. }
  91. /**
  92. * Dumps an error
  93. */
  94. public void dumpError(Exception e) {
  95. if (errorDump) {
  96. if (e instanceof SAXException) {
  97. e.printStackTrace();
  98. if (((SAXException)e).getException() != null) {
  99. ((SAXException)e).getException().printStackTrace();
  100. }
  101. } else {
  102. e.printStackTrace();
  103. }
  104. }
  105. }
  106. /**
  107. * long or short error messages
  108. *
  109. */
  110. public void setDumpError(boolean dumpError) {
  111. errorDump = dumpError;
  112. }
  113. }