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.

FOProcessorImpl.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.URI;
  23. import java.net.URISyntaxException;
  24. import java.net.URL;
  25. import javax.xml.transform.Result;
  26. import javax.xml.transform.Source;
  27. import javax.xml.transform.Templates;
  28. import javax.xml.transform.Transformer;
  29. import javax.xml.transform.TransformerException;
  30. import javax.xml.transform.TransformerFactory;
  31. import javax.xml.transform.sax.SAXResult;
  32. import org.xml.sax.SAXException;
  33. import org.apache.commons.io.FilenameUtils;
  34. import org.apache.commons.logging.Log;
  35. import org.apache.commons.logging.LogFactory;
  36. import org.apache.fop.activity.Initializable;
  37. import org.apache.fop.apps.FOPException;
  38. import org.apache.fop.apps.FOUserAgent;
  39. import org.apache.fop.apps.Fop;
  40. import org.apache.fop.apps.FopFactory;
  41. import org.apache.fop.apps.MimeConstants;
  42. import org.apache.fop.configuration.Configurable;
  43. import org.apache.fop.configuration.Configuration;
  44. import org.apache.fop.configuration.ConfigurationException;
  45. /**
  46. * Default implementation of the {@link Processor} interface using FOP.
  47. */
  48. public class FOProcessorImpl
  49. implements Processor, Configurable, Initializable {
  50. private static final Log LOG = LogFactory.getLog(FOProcessorImpl.class);
  51. private FopFactory fopFactory;
  52. private TransformerFactory factory = TransformerFactory.newInstance();
  53. private URI userconfig;
  54. private String mime;
  55. private String fileExtension;
  56. /** {@inheritDoc} */
  57. public void configure(Configuration configuration) throws ConfigurationException {
  58. try {
  59. this.userconfig = new URI(configuration.getChild("userconfig").getValue(null));
  60. this.mime = configuration.getChild("mime").getValue(MimeConstants.MIME_PDF);
  61. this.fileExtension = configuration.getChild("extension").getValue(".pdf");
  62. } catch (URISyntaxException use) {
  63. throw new RuntimeException(use);
  64. }
  65. }
  66. public void initialize() throws Exception {
  67. if (this.userconfig != null) {
  68. LOG.debug("Setting user config: " + userconfig);
  69. fopFactory = FopFactory.newInstance(new File(userconfig));
  70. } else {
  71. fopFactory = FopFactory.newInstance(new File(".").toURI());
  72. }
  73. }
  74. /** {@inheritDoc}
  75. * @throws URISyntaxException
  76. * @throws SAXException */
  77. public void process(Source src, Templates templates, OutputStream out)
  78. throws java.io.IOException, URISyntaxException, SAXException {
  79. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  80. try {
  81. URL url = new URL(src.getSystemId());
  82. String filename = FilenameUtils.getName(url.getPath());
  83. foUserAgent.getEventBroadcaster().addEventListener(
  84. new AvalonAdapter(LOG, filename));
  85. } catch (MalformedURLException mfue) {
  86. throw new RuntimeException(mfue);
  87. }
  88. Fop fop = fopFactory.newFop(this.mime, foUserAgent, out);
  89. try {
  90. Transformer transformer;
  91. if (templates == null) {
  92. transformer = factory.newTransformer();
  93. } else {
  94. transformer = templates.newTransformer();
  95. }
  96. Result res = new SAXResult(fop.getDefaultHandler());
  97. transformer.transform(src, res);
  98. } catch (TransformerException e) {
  99. throw new FOPException(e);
  100. }
  101. }
  102. /** {@inheritDoc} */
  103. public String getTargetFileExtension() {
  104. return this.fileExtension;
  105. }
  106. }