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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001-2002 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. public abstract class InputHandler {
  15. public abstract InputSource getInputSource();
  16. public abstract XMLReader getParser() throws FOPException;
  17. public static InputSource urlInputSource(URL url) {
  18. return new InputSource(url.toString());
  19. }
  20. /**
  21. * create an InputSource from a File
  22. *
  23. * @param file the File
  24. * @return the InputSource created
  25. */
  26. public static InputSource fileInputSource(File file) {
  27. /* this code adapted from James Clark's in XT */
  28. String path = file.getAbsolutePath();
  29. String fSep = System.getProperty("file.separator");
  30. if (fSep != null && fSep.length() == 1) {
  31. path = path.replace(fSep.charAt(0), '/');
  32. }
  33. if (path.length() > 0 && path.charAt(0) != '/') {
  34. path = '/' + path;
  35. }
  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. }