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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. import org.xml.sax.SAXException;
  12. // Java
  13. import javax.xml.parsers.SAXParserFactory;
  14. import javax.xml.parsers.ParserConfigurationException;
  15. import java.net.URL;
  16. import java.io.File;
  17. /*
  18. * Abstract super class for input handlers.
  19. * Should be used to abstract the various possibilities on how input
  20. * can be provided to FOP (but actually isn't).
  21. */
  22. public abstract class InputHandler {
  23. public abstract InputSource getInputSource();
  24. public abstract XMLReader getParser() throws FOPException;
  25. public static InputSource urlInputSource(URL url) {
  26. return new InputSource(url.toString());
  27. }
  28. /**
  29. * Creates an <code>InputSource</code> from a <code>File</code>
  30. * @param file the <code>File</code>
  31. * @return the <code>InputSource</code> created
  32. */
  33. public static InputSource fileInputSource(File file) {
  34. /* this code adapted from James Clark's in XT */
  35. String path = file.getAbsolutePath();
  36. String fSep = System.getProperty("file.separator");
  37. if (fSep != null && fSep.length() == 1) {
  38. path = path.replace(fSep.charAt(0), '/');
  39. }
  40. if (path.length() > 0 && path.charAt(0) != '/') {
  41. path = '/' + path;
  42. }
  43. try {
  44. return new InputSource(new URL("file", null, path).toString());
  45. } catch (java.net.MalformedURLException e) {
  46. throw new Error("unexpected MalformedURLException");
  47. }
  48. }
  49. /**
  50. * Creates <code>XMLReader</code> object using default
  51. * <code>SAXParserFactory</code>
  52. * @return the created <code>XMLReader</code>
  53. * @throws FOPException if the parser couldn't be created or configured for proper operation.
  54. */
  55. protected static XMLReader createParser() throws FOPException {
  56. try {
  57. SAXParserFactory factory = SAXParserFactory.newInstance();
  58. factory.setNamespaceAware(true);
  59. return factory.newSAXParser().getXMLReader();
  60. } catch (SAXException se) {
  61. throw new FOPException("Coudn't create XMLReader", se);
  62. } catch (ParserConfigurationException pce) {
  63. throw new FOPException("Coudn't create XMLReader", pce);
  64. }
  65. }
  66. }