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.

BasePDFTest.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.render.pdf;
  19. import java.io.File;
  20. import javax.xml.transform.Source;
  21. import javax.xml.transform.Transformer;
  22. import javax.xml.transform.TransformerException;
  23. import javax.xml.transform.TransformerFactory;
  24. import javax.xml.transform.sax.SAXResult;
  25. import javax.xml.transform.stream.StreamSource;
  26. import org.apache.commons.io.FileUtils;
  27. import org.apache.commons.io.output.ByteArrayOutputStream;
  28. import org.apache.fop.AbstractFOPTest;
  29. import org.apache.fop.apps.FOUserAgent;
  30. import org.apache.fop.apps.Fop;
  31. import org.apache.fop.apps.FopFactory;
  32. import org.apache.fop.apps.MimeConstants;
  33. import org.xml.sax.SAXException;
  34. /**
  35. * Base class for automated tests that create PDF files
  36. */
  37. public class BasePDFTest extends AbstractFOPTest {
  38. /** the FopFactory */
  39. protected final FopFactory fopFactory = FopFactory.newInstance();
  40. /** the JAXP TransformerFactory */
  41. protected final TransformerFactory tFactory = TransformerFactory.newInstance();
  42. /**
  43. * Main constructor
  44. */
  45. protected BasePDFTest() {
  46. init();
  47. }
  48. /**
  49. * initalizes the test
  50. */
  51. protected void init() {
  52. final File uc = getUserConfigFile();
  53. try {
  54. fopFactory.setUserConfig(uc);
  55. } catch (Exception e) {
  56. throw new RuntimeException("fopFactory.setUserConfig ("
  57. + uc.getAbsolutePath() + ") failed: " + e.getMessage());
  58. }
  59. }
  60. /**
  61. * Convert a test FO file to PDF
  62. * @param foFile the FO file
  63. * @param ua the preconfigured user agent
  64. * @param dumpPdfFile if true, dumps the generated PDF file to a file name (foFile).pdf
  65. * @return the generated PDF data
  66. * @throws Exception if the conversion fails
  67. */
  68. protected byte[] convertFO(File foFile, FOUserAgent ua, boolean dumpPdfFile)
  69. throws Exception {
  70. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  71. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, ua, baout);
  72. Transformer transformer = tFactory.newTransformer();
  73. Source src = new StreamSource(foFile);
  74. SAXResult res = new SAXResult(fop.getDefaultHandler());
  75. try {
  76. transformer.transform(src, res);
  77. final byte[] result = baout.toByteArray();
  78. if (dumpPdfFile) {
  79. final File outFile = new File(foFile.getParentFile(), foFile.getName() + ".pdf");
  80. FileUtils.writeByteArrayToFile(outFile, result);
  81. }
  82. return result;
  83. } catch (TransformerException e) {
  84. throw extractOriginalException(e);
  85. }
  86. }
  87. private static Exception extractOriginalException(Exception e) {
  88. if (e.getCause() != null) {
  89. return extractOriginalException((Exception)e.getCause());
  90. } else if (e instanceof SAXException) {
  91. SAXException se = (SAXException)e;
  92. if (se.getException() != null) {
  93. return extractOriginalException(se.getException());
  94. }
  95. }
  96. return e;
  97. }
  98. /**
  99. * get FOP config File
  100. * @return user config file to be used for testing
  101. */
  102. protected File getUserConfigFile() {
  103. return new File("test/test.xconf");
  104. }
  105. }