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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 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;
  18. import java.io.File;
  19. import java.io.FileNotFoundException;
  20. import java.io.OutputStream;
  21. import javax.xml.transform.Result;
  22. import javax.xml.transform.Source;
  23. import javax.xml.transform.Transformer;
  24. import javax.xml.transform.TransformerException;
  25. import javax.xml.transform.URIResolver;
  26. import javax.xml.transform.dom.DOMResult;
  27. import javax.xml.transform.dom.DOMSource;
  28. import javax.xml.transform.sax.SAXResult;
  29. import javax.xml.transform.sax.SAXTransformerFactory;
  30. import javax.xml.transform.sax.TransformerHandler;
  31. import javax.xml.transform.stream.StreamResult;
  32. import javax.xml.transform.stream.StreamSource;
  33. import org.apache.commons.io.IOUtils;
  34. import org.apache.commons.io.output.ByteArrayOutputStream;
  35. import org.apache.fop.apps.FOPException;
  36. import org.apache.fop.apps.FOUserAgent;
  37. import org.apache.fop.apps.Fop;
  38. import org.apache.fop.apps.MimeConstants;
  39. import org.apache.fop.image.ImageFactory;
  40. import org.apache.fop.render.xml.XMLRenderer;
  41. import org.apache.xpath.XPathAPI;
  42. import org.apache.xpath.objects.XObject;
  43. import org.w3c.dom.Document;
  44. /**
  45. * Tests URI resolution facilities.
  46. */
  47. public class URIResolutionTestCase extends AbstractFOPTestCase {
  48. private SAXTransformerFactory tfactory
  49. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  50. private File backupDir = new File(getBaseDir(), "build/test-results");
  51. /** @see junit.framework.TestCase#TestCase(String) */
  52. public URIResolutionTestCase(String name) {
  53. super(name);
  54. }
  55. /**
  56. * Test custom URI resolution with a hand-written URIResolver.
  57. * @throws Exception if anything fails
  58. */
  59. public void testFO1a() throws Exception {
  60. innerTestFO1(false);
  61. }
  62. /**
  63. * Test custom URI resolution with a hand-written URIResolver.
  64. * @throws Exception if anything fails
  65. */
  66. public void testFO1b() throws Exception {
  67. innerTestFO1(true);
  68. }
  69. private void innerTestFO1(boolean withStream) throws Exception {
  70. //Reset the image caches to force URI resolution!
  71. ImageFactory.getInstance().clearCaches();
  72. File foFile = new File(getBaseDir(), "test/xml/uri-resolution1.fo");
  73. FOUserAgent ua = new FOUserAgent();
  74. MyURIResolver resolver = new MyURIResolver(withStream);
  75. ua.setURIResolver(resolver);
  76. ua.setBaseURL(foFile.getParentFile().toURL().toString());
  77. Document doc = createAreaTree(foFile, ua);
  78. //Check how many times the resolver was consulted
  79. assertEquals("Expected resolver to do 1 successful URI resolution",
  80. 1, resolver.successCount);
  81. assertEquals("Expected resolver to do 0 failed URI resolution",
  82. 0, resolver.failureCount);
  83. //Additional XPath checking on the area tree
  84. assertEquals("viewport for external-graphic is missing",
  85. "true", evalXPath(doc, "boolean(//flow/block[1]/lineArea/viewport)"));
  86. assertEquals("46080", evalXPath(doc, "//flow/block[1]/lineArea/viewport/@ipd"));
  87. assertEquals("46080", evalXPath(doc, "//flow/block[1]/lineArea/viewport/@bpd"));
  88. }
  89. /**
  90. * Test custom URI resolution with a hand-written URIResolver.
  91. * @throws Exception if anything fails
  92. */
  93. public void DISABLEDtestFO2() throws Exception {
  94. //TODO This will only work when we can do URI resolution inside Batik!
  95. File foFile = new File(getBaseDir(), "test/xml/uri-resolution2.fo");
  96. FOUserAgent ua = new FOUserAgent();
  97. MyURIResolver resolver = new MyURIResolver(false);
  98. ua.setURIResolver(resolver);
  99. ua.setBaseURL(foFile.getParentFile().toURL().toString());
  100. Fop fop = new Fop(MimeConstants.MIME_PDF, ua);
  101. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  102. fop.setOutputStream(baout);
  103. Transformer transformer = tfactory.newTransformer(); //Identity transf.
  104. Source src = new StreamSource(foFile);
  105. Result res = new SAXResult(fop.getDefaultHandler());
  106. transformer.transform(src, res);
  107. OutputStream out = new java.io.FileOutputStream(
  108. new File(backupDir, foFile.getName() + ".pdf"));
  109. try {
  110. baout.writeTo(out);
  111. } finally {
  112. IOUtils.closeQuietly(out);
  113. }
  114. //Check how many times the resolver was consulted
  115. assertEquals("Expected resolver to do 1 successful URI resolution",
  116. 1, resolver.successCount);
  117. assertEquals("Expected resolver to do 0 failed URI resolutions",
  118. 0, resolver.failureCount);
  119. //Test using PDF as the area tree doesn't invoke Batik so we could check
  120. //if the resolver is actually passed to Batik by FOP
  121. assertTrue("Generated PDF has zero length", baout.size() > 0);
  122. }
  123. private Document createAreaTree(File fo, FOUserAgent ua)
  124. throws TransformerException, FOPException {
  125. DOMResult domres = new DOMResult();
  126. //Setup Transformer to convert the area tree to a DOM
  127. TransformerHandler athandler = tfactory.newTransformerHandler();
  128. athandler.setResult(domres);
  129. XMLRenderer atrenderer = new XMLRenderer();
  130. atrenderer.setUserAgent(ua);
  131. atrenderer.setTransformerHandler(athandler);
  132. ua.setRendererOverride(atrenderer);
  133. Fop fop = new Fop(MimeConstants.MIME_FOP_AREA_TREE, ua);
  134. Transformer transformer = tfactory.newTransformer(); //Identity transf.
  135. Source src = new StreamSource(fo);
  136. Result res = new SAXResult(fop.getDefaultHandler());
  137. transformer.transform(src, res);
  138. Document doc = (Document)domres.getNode();
  139. saveAreaTreeXML(doc, new File(backupDir, fo.getName() + ".at.xml"));
  140. return doc;
  141. }
  142. private String evalXPath(Document doc, String xpath) {
  143. XObject res;
  144. try {
  145. res = XPathAPI.eval(doc, xpath);
  146. } catch (TransformerException e) {
  147. throw new RuntimeException("XPath evaluation failed: " + e.getMessage());
  148. }
  149. return res.str();
  150. }
  151. /**
  152. * Save the area tree XML for later inspection.
  153. * @param doc area tree as a DOM document
  154. * @param target target file
  155. * @throws TransformerException if a problem occurs during serialization
  156. */
  157. protected void saveAreaTreeXML(Document doc, File target) throws TransformerException {
  158. Transformer transformer = tfactory.newTransformer();
  159. Source src = new DOMSource(doc);
  160. Result res = new StreamResult(target);
  161. transformer.transform(src, res);
  162. }
  163. private class MyURIResolver implements URIResolver {
  164. private static final String PREFIX = "funky:";
  165. private boolean withStream;
  166. private int successCount = 0;
  167. private int failureCount = 0;
  168. public MyURIResolver(boolean withStream) {
  169. this.withStream = withStream;
  170. }
  171. /**
  172. * @see javax.xml.transform.URIResolver#resolve(java.lang.String, java.lang.String)
  173. */
  174. public Source resolve(String href, String base) throws TransformerException {
  175. if (href.startsWith(PREFIX)) {
  176. String name = href.substring(PREFIX.length());
  177. if ("myimage123".equals(name)) {
  178. File image = new File(getBaseDir(), "test/resources/images/bgimg300dpi.jpg");
  179. Source src;
  180. if (withStream) {
  181. try {
  182. src = new StreamSource(new java.io.FileInputStream(image));
  183. } catch (FileNotFoundException e) {
  184. throw new TransformerException(e.getMessage(), e);
  185. }
  186. } else {
  187. src = new StreamSource(image);
  188. }
  189. successCount++;
  190. return src;
  191. } else {
  192. failureCount++;
  193. throw new TransformerException("funky image not found");
  194. }
  195. } else {
  196. failureCount++;
  197. return null;
  198. }
  199. }
  200. }
  201. }