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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.apps;
  18. // SAX
  19. import org.xml.sax.InputSource;
  20. import org.xml.sax.XMLReader;
  21. // Java
  22. import java.net.URL;
  23. import java.io.File;
  24. /**
  25. * Abstract super class for input handlers.
  26. * Should be used to abstract the various possibilities on how input
  27. * can be provided to FOP (but actually isn't).
  28. */
  29. public abstract class InputHandler {
  30. /**
  31. * Get the input source associated with this input handler.
  32. * @return the input source
  33. */
  34. public abstract InputSource getInputSource();
  35. /**
  36. * Get the SAX parser associated with this input handler.
  37. * @return the SAX parser
  38. * @throws FOPException in case of an error determining the SAX parser
  39. */
  40. public abstract XMLReader getParser() throws FOPException;
  41. /**
  42. * Creates an InputSource from a URL.
  43. * @param url URL to use
  44. * @return the newly created InputSource
  45. */
  46. public static InputSource urlInputSource(URL url) {
  47. return new InputSource(url.toString());
  48. }
  49. /**
  50. * Creates an <code>InputSource</code> from a <code>File</code>
  51. * @param file the <code>File</code>
  52. * @return the <code>InputSource</code> created
  53. */
  54. public static InputSource fileInputSource(File file) {
  55. /* this code adapted from James Clark's in XT */
  56. String path = file.getAbsolutePath();
  57. String fSep = System.getProperty("file.separator");
  58. if (fSep != null && fSep.length() == 1) {
  59. path = path.replace(fSep.charAt(0), '/');
  60. }
  61. if (path.length() > 0 && path.charAt(0) != '/') {
  62. path = '/' + path;
  63. }
  64. try {
  65. return new InputSource(new URL("file", null, path).toString());
  66. } catch (java.net.MalformedURLException e) {
  67. throw new Error("unexpected MalformedURLException");
  68. }
  69. }
  70. }