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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2004-2006 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.threading;
  18. import java.io.InputStream;
  19. import java.io.OutputStream;
  20. import javax.xml.transform.Result;
  21. import javax.xml.transform.Source;
  22. import javax.xml.transform.Templates;
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerException;
  25. import javax.xml.transform.TransformerFactory;
  26. import javax.xml.transform.stream.StreamSource;
  27. import javax.xml.transform.sax.SAXResult;
  28. import org.apache.avalon.framework.logger.AbstractLogEnabled;
  29. import org.apache.avalon.framework.configuration.Configurable;
  30. import org.apache.avalon.framework.configuration.Configuration;
  31. import org.apache.avalon.framework.configuration.ConfigurationException;
  32. import org.apache.fop.apps.FOUserAgent;
  33. import org.apache.fop.apps.Fop;
  34. import org.apache.fop.apps.FOPException;
  35. import org.apache.fop.apps.FopFactory;
  36. import org.apache.fop.apps.MimeConstants;
  37. import org.apache.avalon.framework.activity.Initializable;
  38. public class FOProcessorImpl extends AbstractLogEnabled
  39. implements FOProcessor, Configurable, Initializable {
  40. private FopFactory fopFactory = FopFactory.newInstance();
  41. private TransformerFactory factory = TransformerFactory.newInstance();
  42. private String baseDir;
  43. private String fontBaseDir;
  44. private String userconfig;
  45. private boolean strokeSVGText;
  46. public void configure(Configuration configuration) throws ConfigurationException {
  47. this.baseDir = configuration.getChild("basedir").getValue(null);
  48. this.fontBaseDir = configuration.getChild("fontbasedir").getValue(null);
  49. this.userconfig = configuration.getChild("userconfig").getValue(null);
  50. this.strokeSVGText = configuration.getChild("strokesvgtext").getValueAsBoolean(true);
  51. }
  52. public void initialize() throws Exception {
  53. /*
  54. org.apache.fop.messaging.MessageHandler.setScreenLogger(getLogger());
  55. if (userconfig != null) {
  56. getLogger().info("Using user config: "+userconfig);
  57. InputStream in = org.apache.fop.tools.URLBuilder.buildURL(userconfig).openStream();
  58. try {
  59. new org.apache.fop.apps.Options(in);
  60. } finally {
  61. in.close();
  62. }
  63. }
  64. if (this.baseDir != null) {
  65. getLogger().info("Setting base dir: "+baseDir);
  66. org.apache.fop.configuration.Configuration.put("baseDir", this.baseDir);
  67. }
  68. if (this.fontBaseDir != null) {
  69. getLogger().info("Setting font base dir: "+fontBaseDir);
  70. org.apache.fop.configuration.Configuration.put("fontBaseDir", this.fontBaseDir);
  71. }
  72. String value = (this.strokeSVGText?"true":"false");
  73. org.apache.fop.configuration.Configuration.put("strokeSVGText", value);
  74. */
  75. }
  76. public void process(InputStream in, Templates templates, OutputStream out)
  77. throws org.apache.fop.apps.FOPException, java.io.IOException {
  78. FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  79. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
  80. try {
  81. Transformer transformer;
  82. if (templates == null) {
  83. transformer = factory.newTransformer();
  84. } else {
  85. transformer = templates.newTransformer();
  86. }
  87. Source src = new StreamSource(in);
  88. Result res = new SAXResult(fop.getDefaultHandler());
  89. transformer.transform(src, res);
  90. } catch (TransformerException e) {
  91. throw new FOPException(e);
  92. }
  93. }
  94. }