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.

FOFileHandler.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // Imported SAX classes
  19. import org.xml.sax.InputSource;
  20. import org.xml.sax.XMLReader;
  21. import org.xml.sax.SAXException;
  22. import org.xml.sax.SAXNotSupportedException;
  23. // java
  24. import javax.xml.parsers.SAXParserFactory;
  25. import javax.xml.parsers.ParserConfigurationException;
  26. import java.io.File;
  27. import java.net.URL;
  28. /**
  29. * Manages input if it is an XSL-FO file.
  30. */
  31. public class FOFileHandler extends InputHandler {
  32. private File fofile = null;
  33. private URL foURL = null;
  34. /**
  35. * Create a FOFileHandler for a file.
  36. * @param fofile the file to read the FO document.
  37. */
  38. public FOFileHandler(File fofile) {
  39. this.fofile = fofile;
  40. }
  41. /**
  42. * Create a FOFileHandler for an URL.
  43. * @param url the URL to read the FO document.
  44. */
  45. public FOFileHandler(URL url) {
  46. this.foURL = url;
  47. }
  48. /**
  49. * @see org.apache.fop.apps.InputHandler#getInputSource()
  50. */
  51. public InputSource getInputSource () {
  52. if (fofile != null) {
  53. return super.fileInputSource(fofile);
  54. }
  55. return super.urlInputSource(foURL);
  56. }
  57. /**
  58. * @see org.apache.fop.apps.InputHandler#getParser()
  59. */
  60. public XMLReader getParser() throws FOPException {
  61. return createParser();
  62. }
  63. /**
  64. * Creates <code>XMLReader</code> object using default
  65. * <code>SAXParserFactory</code>
  66. * @return the created <code>XMLReader</code>
  67. * @throws FOPException if the parser couldn't be created or configured for proper operation.
  68. */
  69. protected static XMLReader createParser() throws FOPException {
  70. try {
  71. SAXParserFactory factory = SAXParserFactory.newInstance();
  72. factory.setNamespaceAware(true);
  73. factory.setFeature(
  74. "http://xml.org/sax/features/namespace-prefixes", true);
  75. return factory.newSAXParser().getXMLReader();
  76. } catch (SAXNotSupportedException se) {
  77. throw new FOPException("Error: You need a parser which allows the"
  78. + " http://xml.org/sax/features/namespace-prefixes"
  79. + " feature to be set to true to support namespaces", se);
  80. } catch (SAXException se) {
  81. throw new FOPException("Couldn't create XMLReader", se);
  82. } catch (ParserConfigurationException pce) {
  83. throw new FOPException("Couldn't create XMLReader", pce);
  84. }
  85. }
  86. /**
  87. * Returns the fully qualified classname of the standard XML parser for FOP
  88. * to use.
  89. * @return the XML parser classname
  90. */
  91. public static final String getParserClassName() {
  92. try {
  93. return createParser().getClass().getName();
  94. } catch (FOPException e) {
  95. return null;
  96. }
  97. }
  98. }