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.

IFProcessorImpl.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.threading;
  19. import java.io.File;
  20. import java.io.OutputStream;
  21. import java.net.MalformedURLException;
  22. import java.net.URL;
  23. import javax.xml.transform.Result;
  24. import javax.xml.transform.Source;
  25. import javax.xml.transform.Templates;
  26. import javax.xml.transform.Transformer;
  27. import javax.xml.transform.TransformerException;
  28. import javax.xml.transform.TransformerFactory;
  29. import javax.xml.transform.sax.SAXResult;
  30. import javax.xml.transform.stream.StreamResult;
  31. import org.xml.sax.ContentHandler;
  32. import org.apache.commons.io.FilenameUtils;
  33. import org.apache.commons.logging.Log;
  34. import org.apache.commons.logging.LogFactory;
  35. import org.apache.fop.activity.Initializable;
  36. import org.apache.fop.apps.FOPException;
  37. import org.apache.fop.apps.FOUserAgent;
  38. import org.apache.fop.apps.FopFactory;
  39. import org.apache.fop.apps.MimeConstants;
  40. import org.apache.fop.configuration.Configurable;
  41. import org.apache.fop.configuration.Configuration;
  42. import org.apache.fop.configuration.ConfigurationException;
  43. import org.apache.fop.render.intermediate.IFDocumentHandler;
  44. import org.apache.fop.render.intermediate.IFException;
  45. import org.apache.fop.render.intermediate.IFParser;
  46. import org.apache.fop.render.intermediate.IFUtil;
  47. /**
  48. * Implementation of the {@link Processor} interface that renders IF XML to a final output format.
  49. */
  50. public class IFProcessorImpl
  51. implements Processor, Configurable, Initializable {
  52. private static final Log LOGGER = LogFactory.getLog(IFProcessorImpl.class);
  53. private FopFactory fopFactory;
  54. private TransformerFactory factory = TransformerFactory.newInstance();
  55. private String userconfig;
  56. private String mime;
  57. private String fileExtension;
  58. /** {@inheritDoc} */
  59. public void configure(Configuration configuration) throws ConfigurationException {
  60. this.userconfig = configuration.getChild("userconfig").getValue(null);
  61. this.mime = configuration.getChild("mime").getValue(MimeConstants.MIME_PDF);
  62. this.fileExtension = configuration.getChild("extension").getValue(".pdf");
  63. }
  64. /** {@inheritDoc} */
  65. public void initialize() throws Exception {
  66. if (this.userconfig != null) {
  67. LOGGER.debug("Setting user config: " + userconfig);
  68. fopFactory = FopFactory.newInstance(new File(this.userconfig));
  69. } else {
  70. fopFactory = FopFactory.newInstance(new File(".").toURI());
  71. }
  72. }
  73. /** {@inheritDoc}
  74. * @throws IFException */
  75. public void process(Source src, Templates templates, OutputStream out)
  76. throws org.apache.fop.apps.FOPException, java.io.IOException, IFException {
  77. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  78. try {
  79. URL url = new URL(src.getSystemId());
  80. String filename = FilenameUtils.getName(url.getPath());
  81. foUserAgent.getEventBroadcaster().addEventListener(
  82. new AvalonAdapter(LOGGER, filename));
  83. } catch (MalformedURLException mfue) {
  84. throw new RuntimeException(mfue);
  85. }
  86. //Setup target handler
  87. IFDocumentHandler targetHandler = fopFactory.getRendererFactory().createDocumentHandler(
  88. foUserAgent, mime);
  89. //Setup fonts
  90. IFUtil.setupFonts(targetHandler);
  91. targetHandler.setResult(new StreamResult(out));
  92. try {
  93. Transformer transformer;
  94. if (templates == null) {
  95. transformer = factory.newTransformer();
  96. } else {
  97. transformer = templates.newTransformer();
  98. }
  99. IFParser parser = new IFParser();
  100. ContentHandler contentHandler = parser.getContentHandler(targetHandler, foUserAgent);
  101. Result res = new SAXResult(contentHandler);
  102. transformer.transform(src, res);
  103. } catch (TransformerException e) {
  104. throw new FOPException(e);
  105. }
  106. }
  107. /** {@inheritDoc} */
  108. public String getTargetFileExtension() {
  109. return this.fileExtension;
  110. }
  111. }