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.

URIResolutionTestCase.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.net.URI;
  24. import javax.xml.transform.Result;
  25. import javax.xml.transform.Source;
  26. import javax.xml.transform.Transformer;
  27. import javax.xml.transform.TransformerException;
  28. import javax.xml.transform.dom.DOMResult;
  29. import javax.xml.transform.dom.DOMSource;
  30. import javax.xml.transform.sax.SAXResult;
  31. import javax.xml.transform.sax.SAXTransformerFactory;
  32. import javax.xml.transform.sax.TransformerHandler;
  33. import javax.xml.transform.stream.StreamResult;
  34. import javax.xml.transform.stream.StreamSource;
  35. import org.junit.BeforeClass;
  36. import org.junit.Test;
  37. import org.w3c.dom.Document;
  38. import static org.junit.Assert.assertEquals;
  39. import static org.junit.Assert.assertTrue;
  40. import org.apache.commons.io.IOUtils;
  41. import org.apache.commons.io.output.ByteArrayOutputStream;
  42. import org.apache.xpath.XPathAPI;
  43. import org.apache.xpath.objects.XObject;
  44. import org.apache.xmlgraphics.io.Resource;
  45. import org.apache.xmlgraphics.io.ResourceResolver;
  46. import org.apache.fop.apps.FOPException;
  47. import org.apache.fop.apps.FOUserAgent;
  48. import org.apache.fop.apps.Fop;
  49. import org.apache.fop.apps.FopFactory;
  50. import org.apache.fop.apps.FopFactoryBuilder;
  51. import org.apache.fop.apps.MimeConstants;
  52. import org.apache.fop.apps.io.ResourceResolverFactory;
  53. import org.apache.fop.render.xml.XMLRenderer;
  54. import static org.apache.fop.FOPTestUtils.getBaseDir;
  55. /**
  56. * Tests URI resolution facilities.
  57. */
  58. public class URIResolutionTestCase {
  59. private SAXTransformerFactory tfactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
  60. private static final File BACKUP_DIR = new File(getBaseDir(), "build/test-results");
  61. private static FopFactory fopFactory;
  62. @BeforeClass
  63. public static void makeDirs() {
  64. BACKUP_DIR.mkdirs();
  65. fopFactory = new FopFactoryBuilder(new File(".").getAbsoluteFile().toURI(),
  66. new CustomURIResolver()).build();
  67. }
  68. private static File getTestDir() {
  69. return new File(getBaseDir(), "test/xml/uri-testing/");
  70. }
  71. @Test
  72. public void innerTestFO1() throws Exception {
  73. File foFile = new File(getTestDir(), "custom-scheme/only-scheme-specific-part.fo");
  74. FOUserAgent ua = fopFactory.newFOUserAgent();
  75. Document doc = createAreaTree(foFile, ua);
  76. // XPath checking on the area tree
  77. assertEquals("viewport for external-graphic is missing",
  78. "true", evalXPath(doc, "boolean(//flow/block[1]/lineArea/viewport)"));
  79. assertEquals("46080", evalXPath(doc, "//flow/block[1]/lineArea/viewport/@ipd"));
  80. assertEquals("46080", evalXPath(doc, "//flow/block[1]/lineArea/viewport/@bpd"));
  81. }
  82. /**
  83. * Test custom URI resolution with a hand-written URIResolver.
  84. * @throws Exception if anything fails
  85. */
  86. @Test
  87. public void testFO2() throws Exception {
  88. File foFile = new File(getTestDir(), "custom-scheme/only-scheme-specific-part-svg.fo");
  89. FOUserAgent ua = fopFactory.newFOUserAgent();
  90. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  91. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, ua, baout);
  92. Transformer transformer = tfactory.newTransformer(); //Identity transf.
  93. Source src = new StreamSource(foFile);
  94. Result res = new SAXResult(fop.getDefaultHandler());
  95. transformer.transform(src, res);
  96. OutputStream out = new java.io.FileOutputStream(
  97. new File(BACKUP_DIR, foFile.getName() + ".pdf"));
  98. try {
  99. baout.writeTo(out);
  100. } finally {
  101. IOUtils.closeQuietly(out);
  102. }
  103. //Test using PDF as the area tree doesn't invoke Batik so we could check
  104. //if the resolver is actually passed to Batik by FOP
  105. assertTrue("Generated PDF has zero length", baout.size() > 0);
  106. }
  107. private Document createAreaTree(File fo, FOUserAgent ua)
  108. throws TransformerException, FOPException {
  109. DOMResult domres = new DOMResult();
  110. //Setup Transformer to convert the area tree to a DOM
  111. TransformerHandler athandler = tfactory.newTransformerHandler();
  112. athandler.setResult(domres);
  113. XMLRenderer atrenderer = new XMLRenderer(ua);
  114. atrenderer.setContentHandler(athandler);
  115. ua.setRendererOverride(atrenderer);
  116. Fop fop = fopFactory.newFop(ua);
  117. Transformer transformer = tfactory.newTransformer(); //Identity transf.
  118. Source src = new StreamSource(fo);
  119. Result res = new SAXResult(fop.getDefaultHandler());
  120. transformer.transform(src, res);
  121. Document doc = (Document) domres.getNode();
  122. saveAreaTreeXML(doc, new File(BACKUP_DIR, fo.getName() + ".at.xml"));
  123. return doc;
  124. }
  125. private String evalXPath(Document doc, String xpath) {
  126. XObject res;
  127. try {
  128. res = XPathAPI.eval(doc, xpath);
  129. } catch (TransformerException e) {
  130. throw new RuntimeException("XPath evaluation failed: " + e.getMessage());
  131. }
  132. return res.str();
  133. }
  134. /**
  135. * Save the area tree XML for later inspection.
  136. * @param doc area tree as a DOM document
  137. * @param target target file
  138. * @throws TransformerException if a problem occurs during serialization
  139. */
  140. protected void saveAreaTreeXML(Document doc, File target) throws TransformerException {
  141. Transformer transformer = tfactory.newTransformer();
  142. Source src = new DOMSource(doc);
  143. Result res = new StreamResult(target);
  144. transformer.transform(src, res);
  145. }
  146. private static final class CustomURIResolver implements ResourceResolver {
  147. private final ResourceResolver defaultImpl = ResourceResolverFactory.createDefaultResourceResolver();
  148. public Resource getResource(URI uri) throws IOException {
  149. if (uri.getScheme().equals("funky") && uri.getSchemeSpecificPart().equals("myimage123")) {
  150. return new Resource("", new FileInputStream("test/resources/images/bgimg300dpi.jpg"));
  151. }
  152. return defaultImpl.getResource(uri);
  153. }
  154. public OutputStream getOutputStream(URI uri) throws IOException {
  155. return defaultImpl.getOutputStream(uri);
  156. }
  157. }
  158. }