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.

IFMimickingTestCase.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.intermediate;
  19. import java.io.File;
  20. import javax.xml.transform.ErrorListener;
  21. import javax.xml.transform.Transformer;
  22. import javax.xml.transform.TransformerException;
  23. import javax.xml.transform.TransformerFactory;
  24. import javax.xml.transform.dom.DOMResult;
  25. import javax.xml.transform.sax.SAXResult;
  26. import javax.xml.transform.stream.StreamSource;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import static org.junit.Assert.fail;
  30. import org.apache.fop.apps.FOPException;
  31. import org.apache.fop.apps.FOUserAgent;
  32. import org.apache.fop.apps.Fop;
  33. import org.apache.fop.apps.FopFactory;
  34. import org.apache.fop.apps.MimeConstants;
  35. import org.apache.fop.events.Event;
  36. import org.apache.fop.events.EventFormatter;
  37. import org.apache.fop.events.EventListener;
  38. import org.apache.fop.fonts.FontEventProducer;
  39. import org.apache.fop.render.intermediate.IFContext;
  40. import org.apache.fop.render.intermediate.IFDocumentHandler;
  41. import org.apache.fop.render.intermediate.IFException;
  42. import org.apache.fop.render.intermediate.IFSerializer;
  43. /**
  44. * This test checks the correct mimicking of a different output format.
  45. */
  46. public class IFMimickingTestCase {
  47. private FopFactory fopFactory;
  48. @Before
  49. public void setUp() throws Exception {
  50. File configFile = new File("test/test-no-xml-metrics.xconf");
  51. fopFactory = FopFactory.newInstance(configFile);
  52. }
  53. /**
  54. * Tests IF document handler mimicking with PDF output.
  55. * @throws Exception if an error occurs
  56. */
  57. @Test
  58. public void testMimickingPDF() throws Exception {
  59. doTestMimicking(MimeConstants.MIME_PDF);
  60. }
  61. /**
  62. * Tests IF document handler mimicking with PostScript output.
  63. * @throws Exception if an error occurs
  64. */
  65. @Test
  66. public void testMimickingPS() throws Exception {
  67. doTestMimicking(MimeConstants.MIME_POSTSCRIPT);
  68. }
  69. /**
  70. * Tests IF document handler mimicking with TIFF output.
  71. * @throws Exception if an error occurs
  72. */
  73. @Test
  74. public void testMimickingTIFF() throws Exception {
  75. doTestMimicking(MimeConstants.MIME_TIFF);
  76. }
  77. private void doTestMimicking(String mime) throws FOPException, IFException,
  78. TransformerException {
  79. //Set up XMLRenderer to render to a DOM
  80. DOMResult domResult = new DOMResult();
  81. FOUserAgent userAgent = fopFactory.newFOUserAgent();
  82. userAgent.getEventBroadcaster().addEventListener(new EventListener() {
  83. public void processEvent(Event event) {
  84. if (event.getEventGroupID().equals(FontEventProducer.class.getName())) {
  85. fail("There must be no font-related event! Got: "
  86. + EventFormatter.format(event));
  87. }
  88. }
  89. });
  90. //Create an instance of the target renderer so the XMLRenderer can use its font setup
  91. IFDocumentHandler targetHandler = userAgent.getRendererFactory().createDocumentHandler(
  92. userAgent, mime);
  93. //Setup painter
  94. IFSerializer serializer = new IFSerializer(new IFContext(userAgent));
  95. serializer.mimicDocumentHandler(targetHandler);
  96. serializer.setResult(domResult);
  97. userAgent.setDocumentHandlerOverride(serializer);
  98. Fop fop = fopFactory.newFop(userAgent);
  99. //minimal-pdf-a.fo uses the Gladiator font so is an ideal FO file for this test:
  100. StreamSource src = new StreamSource(new File("test/xml/pdf-a/minimal-pdf-a.fo"));
  101. TransformerFactory tFactory = TransformerFactory.newInstance();
  102. Transformer transformer = tFactory.newTransformer();
  103. setErrorListener(transformer);
  104. transformer.transform(src, new SAXResult(fop.getDefaultHandler()));
  105. }
  106. /**
  107. * Sets an error listener which doesn't swallow errors like Xalan's default one.
  108. * @param transformer the transformer to set the error listener on
  109. */
  110. protected void setErrorListener(Transformer transformer) {
  111. transformer.setErrorListener(new ErrorListener() {
  112. public void error(TransformerException exception) throws TransformerException {
  113. throw exception;
  114. }
  115. public void fatalError(TransformerException exception) throws TransformerException {
  116. throw exception;
  117. }
  118. public void warning(TransformerException exception) throws TransformerException {
  119. //ignore
  120. }
  121. });
  122. }
  123. }