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 3.9KB

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