Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

URIResolutionTestCase.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.FileNotFoundException;
  21. import java.io.OutputStream;
  22. import javax.xml.transform.Result;
  23. import javax.xml.transform.Source;
  24. import javax.xml.transform.Transformer;
  25. import javax.xml.transform.TransformerException;
  26. import javax.xml.transform.URIResolver;
  27. import javax.xml.transform.dom.DOMResult;
  28. import javax.xml.transform.dom.DOMSource;
  29. import javax.xml.transform.sax.SAXResult;
  30. import javax.xml.transform.sax.SAXTransformerFactory;
  31. import javax.xml.transform.sax.TransformerHandler;
  32. import javax.xml.transform.stream.StreamResult;
  33. import javax.xml.transform.stream.StreamSource;
  34. import org.w3c.dom.Document;
  35. import org.apache.commons.io.IOUtils;
  36. import org.apache.commons.io.output.ByteArrayOutputStream;
  37. import org.apache.xpath.XPathAPI;
  38. import org.apache.xpath.objects.XObject;
  39. import org.apache.fop.apps.FOPException;
  40. import org.apache.fop.apps.FOUserAgent;
  41. import org.apache.fop.apps.Fop;
  42. import org.apache.fop.apps.FopFactory;
  43. import org.apache.fop.apps.MimeConstants;
  44. import org.apache.fop.render.xml.XMLRenderer;
  45. /**
  46. * Tests URI resolution facilities.
  47. */
  48. public class URIResolutionTestCase extends AbstractFOPTestCase {
  49. // configure fopFactory as desired
  50. private FopFactory fopFactory = FopFactory.newInstance();
  51. private SAXTransformerFactory tfactory
  52. = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
  53. private File backupDir = new File(getBaseDir(), "build/test-results");
  54. /** @see junit.framework.TestCase#TestCase(String) */
  55. public URIResolutionTestCase(String name) {
  56. super(name);
  57. }
  58. /**
  59. * Test custom URI resolution with a hand-written URIResolver.
  60. * @throws Exception if anything fails
  61. */
  62. public void testFO1a() throws Exception {
  63. innerTestFO1(false);
  64. }
  65. /**
  66. * Test custom URI resolution with a hand-written URIResolver.
  67. * @throws Exception if anything fails
  68. */
  69. public void testFO1b() throws Exception {
  70. innerTestFO1(true);
  71. }
  72. private void innerTestFO1(boolean withStream) throws Exception {
  73. FOUserAgent ua = fopFactory.newFOUserAgent();
  74. File foFile = new File(getBaseDir(), "test/xml/uri-resolution1.fo");
  75. MyURIResolver resolver = new MyURIResolver(withStream);
  76. ua.setURIResolver(resolver);
  77. ua.setBaseURL(foFile.getParentFile().toURL().toString());
  78. Document doc = createAreaTree(foFile, ua);
  79. //Check how many times the resolver was consulted
  80. assertEquals("Expected resolver to do 1 successful URI resolution",
  81. 1, resolver.successCount);
  82. assertEquals("Expected resolver to do 0 failed URI resolution",
  83. 0, resolver.failureCount);
  84. //Additional XPath checking on the area tree
  85. assertEquals("viewport for external-graphic is missing",
  86. "true", evalXPath(doc, "boolean(//flow/block[1]/lineArea/viewport)"));
  87. assertEquals("46080", evalXPath(doc, "//flow/block[1]/lineArea/viewport/@ipd"));
  88. assertEquals("46080", evalXPath(doc, "//flow/block[1]/lineArea/viewport/@bpd"));
  89. }
  90. /**
  91. * Test custom URI resolution with a hand-written URIResolver.
  92. * @throws Exception if anything fails
  93. */
  94. public void DISABLEDtestFO2() throws Exception {
  95. //TODO This will only work when we can do URI resolution inside Batik!
  96. File foFile = new File(getBaseDir(), "test/xml/uri-resolution2.fo");
  97. FOUserAgent ua = fopFactory.newFOUserAgent();
  98. MyURIResolver resolver = new MyURIResolver(false);
  99. ua.setURIResolver(resolver);
  100. ua.setBaseURL(foFile.getParentFile().toURL().toString());
  101. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  102. Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, ua, 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.setContentHandler(athandler);
  132. ua.setRendererOverride(atrenderer);
  133. Fop fop = fopFactory.newFop(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. }