Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ConfigurationReader.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * Copyright 1999-2003 The Apache Software Foundation.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. *
  20. */
  21. package org.apache.fop.configuration;
  22. import org.xml.sax.XMLReader;
  23. import org.xml.sax.SAXException;
  24. import java.io.IOException;
  25. import org.xml.sax.InputSource;
  26. // fop
  27. import org.apache.fop.apps.Driver;
  28. import org.apache.fop.apps.FOPException;
  29. /**
  30. * entry class for reading configuration from file and creating a configuration
  31. * class. Typical use looks like: <br>
  32. *
  33. * <code>ConfigurationReader reader = new ConfigurationReader ("config.xml","standard");
  34. * try {
  35. * reader.start();
  36. * } catch (org.apache.fop.apps.FOPException error) {
  37. * reader.dumpError(error);
  38. * }
  39. * </code>
  40. * Once the configuration has been setup, the information can be accessed with
  41. * the methods of StandardConfiguration.
  42. */
  43. public class ConfigurationReader {
  44. /**
  45. * show a full dump on error
  46. */
  47. private static boolean errorDump = false;
  48. /**
  49. * inputsource for configuration file
  50. */
  51. private InputSource filename;
  52. /**
  53. * creates a configuration reader
  54. * @param filename the file which contains the configuration information
  55. */
  56. public ConfigurationReader(InputSource filename) {
  57. this.filename = filename;
  58. }
  59. /**
  60. * intantiates parser and starts parsing of config file
  61. */
  62. public void start() throws FOPException {
  63. XMLReader parser = createParser();
  64. // setting the parser features
  65. try {
  66. parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
  67. false);
  68. } catch (SAXException e) {
  69. throw new FOPException("You need a parser which supports SAX version 2",
  70. e);
  71. }
  72. ConfigurationParser configurationParser = new ConfigurationParser();
  73. parser.setContentHandler(configurationParser);
  74. try {
  75. parser.parse(filename);
  76. } catch (SAXException e) {
  77. if (e.getException() instanceof FOPException) {
  78. throw (FOPException)e.getException();
  79. } else {
  80. throw new FOPException(e);
  81. }
  82. } catch (IOException e) {
  83. throw new FOPException(e);
  84. }
  85. }
  86. /**
  87. * creates a SAX parser, using the value of org.xml.sax.parser
  88. * defaulting to org.apache.xerces.parsers.SAXParser
  89. *
  90. * @return the created SAX parser
  91. */
  92. public static XMLReader createParser() throws FOPException {
  93. String parserClassName = Driver.getParserClassName();
  94. if (errorDump) {
  95. Configuration.logger.config(
  96. "configuration reader using SAX parser "
  97. + parserClassName);
  98. }
  99. try {
  100. return (XMLReader)Class.forName(parserClassName).newInstance();
  101. } catch (ClassNotFoundException e) {
  102. throw new FOPException("Could not find " + parserClassName, e);
  103. } catch (InstantiationException e) {
  104. throw new FOPException("Could not instantiate "
  105. + parserClassName, e);
  106. } catch (IllegalAccessException e) {
  107. throw new FOPException("Could not access " + parserClassName, e);
  108. } catch (ClassCastException e) {
  109. throw new FOPException(parserClassName + " is not a SAX driver",
  110. e);
  111. }
  112. }
  113. /**
  114. * Dumps an error
  115. */
  116. public void dumpError(Exception e) {
  117. if (errorDump) {
  118. if (e instanceof SAXException) {
  119. e.printStackTrace();
  120. if (((SAXException)e).getException() != null) {
  121. ((SAXException)e).getException().printStackTrace();
  122. }
  123. } else {
  124. e.printStackTrace();
  125. }
  126. }
  127. }
  128. /**
  129. * long or short error messages
  130. *
  131. */
  132. public void setDumpError(boolean dumpError) {
  133. errorDump = dumpError;
  134. }
  135. }