您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

URIResolutionTestCase.java 8.9KB

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