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.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.apps;
  8. // SAX
  9. import org.xml.sax.InputSource;
  10. import org.xml.sax.XMLReader;
  11. // Java
  12. import java.net.URL;
  13. import java.io.File;
  14. // FOP
  15. import org.apache.fop.configuration.Configuration;
  16. abstract public class InputHandler {
  17. abstract public InputSource getInputSource();
  18. abstract public XMLReader getParser() throws FOPException;
  19. static public InputSource urlInputSource(URL url) {
  20. return new InputSource(url.toString());
  21. }
  22. /**
  23. * create an InputSource from a File
  24. *
  25. * @param file the File
  26. * @return the InputSource created
  27. */
  28. static public InputSource fileInputSource(File file) {
  29. /* this code adapted from James Clark's in XT */
  30. String path = file.getAbsolutePath();
  31. String fSep = System.getProperty("file.separator");
  32. if (fSep != null && fSep.length() == 1)
  33. path = path.replace(fSep.charAt(0), '/');
  34. if (path.length() > 0 && path.charAt(0) != '/')
  35. path = '/' + path;
  36. try {
  37. return new InputSource(new URL("file", null, path).toString());
  38. } catch (java.net.MalformedURLException e) {
  39. throw new Error("unexpected MalformedURLException");
  40. }
  41. }
  42. /**
  43. * creates a SAX parser, using the value of org.xml.sax.parser
  44. * defaulting to org.apache.xerces.parsers.SAXParser
  45. *
  46. * @return the created SAX parser
  47. */
  48. protected static XMLReader createParser() throws FOPException {
  49. String parserClassName = Driver.getParserClassName();
  50. //log.debug("using SAX parser " + parserClassName);
  51. try {
  52. return (XMLReader)Class.forName(parserClassName).newInstance();
  53. } catch (ClassNotFoundException e) {
  54. throw new FOPException(e);
  55. } catch (InstantiationException e) {
  56. throw new FOPException("Could not instantiate "
  57. + parserClassName, e);
  58. } catch (IllegalAccessException e) {
  59. throw new FOPException("Could not access " + parserClassName, e);
  60. } catch (ClassCastException e) {
  61. throw new FOPException(parserClassName + " is not a SAX driver",
  62. e);
  63. }
  64. }
  65. }