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.

InputHandler.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  3. * For details on use and redistribution please refer to the
  4. * LICENSE file included with these sources."
  5. */
  6. package org.apache.fop.apps;
  7. // SAX
  8. import org.xml.sax.InputSource;
  9. import org.xml.sax.XMLReader;
  10. // Java
  11. import java.net.URL;
  12. import java.io.File;
  13. // FOP
  14. import org.apache.fop.messaging.MessageHandler;
  15. import org.apache.fop.configuration.Configuration;
  16. abstract public class InputHandler {
  17. abstract public InputSource getInputSource();
  18. abstract public XMLReader getParser();
  19. /**
  20. * create an InputSource from a File
  21. *
  22. * @param file the File
  23. * @return the InputSource created
  24. */
  25. static public InputSource fileInputSource(File file) {
  26. /* this code adapted from James Clark's in XT */
  27. String path = file.getAbsolutePath();
  28. String fSep = System.getProperty("file.separator");
  29. if (fSep != null && fSep.length() == 1)
  30. path = path.replace(fSep.charAt(0), '/');
  31. if (path.length() > 0 && path.charAt(0) != '/')
  32. path = '/' + path;
  33. try {
  34. return new InputSource(new URL("file", null, path).toString());
  35. } catch (java.net.MalformedURLException e) {
  36. throw new Error("unexpected MalformedURLException");
  37. }
  38. }
  39. /**
  40. * creates a SAX parser, using the value of org.xml.sax.parser
  41. * defaulting to org.apache.xerces.parsers.SAXParser
  42. *
  43. * @return the created SAX parser
  44. */
  45. static XMLReader createParser() {
  46. boolean debugMode = Configuration.getBooleanValue("debugMode").booleanValue();
  47. String parserClassName = System.getProperty("org.xml.sax.parser");
  48. if (parserClassName == null) {
  49. parserClassName = "org.apache.xerces.parsers.SAXParser";
  50. }
  51. MessageHandler.logln("using SAX parser " + parserClassName);
  52. try {
  53. return (XMLReader) Class.forName(
  54. parserClassName).newInstance();
  55. } catch (ClassNotFoundException e) {
  56. MessageHandler.errorln("Could not find " + parserClassName);
  57. if (debugMode) {
  58. e.printStackTrace();
  59. }
  60. }
  61. catch (InstantiationException e) {
  62. MessageHandler.errorln("Could not instantiate " +
  63. parserClassName);
  64. if (debugMode) {
  65. e.printStackTrace();
  66. }
  67. }
  68. catch (IllegalAccessException e) {
  69. MessageHandler.errorln("Could not access " + parserClassName);
  70. if (debugMode) {
  71. e.printStackTrace();
  72. }
  73. }
  74. catch (ClassCastException e) {
  75. MessageHandler.errorln(parserClassName + " is not a SAX driver");
  76. if (debugMode) {
  77. e.printStackTrace();
  78. }
  79. }
  80. return null;
  81. }
  82. }