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.

FO2StructureTreeConverterTestCase.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.accessibility.fo;
  19. import java.io.ByteArrayInputStream;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.OutputStream;
  25. import java.io.StringWriter;
  26. import javax.xml.transform.OutputKeys;
  27. import javax.xml.transform.Result;
  28. import javax.xml.transform.Source;
  29. import javax.xml.transform.Transformer;
  30. import javax.xml.transform.TransformerConfigurationException;
  31. import javax.xml.transform.TransformerException;
  32. import javax.xml.transform.TransformerFactory;
  33. import javax.xml.transform.TransformerFactoryConfigurationError;
  34. import javax.xml.transform.dom.DOMResult;
  35. import javax.xml.transform.dom.DOMSource;
  36. import javax.xml.transform.sax.SAXTransformerFactory;
  37. import javax.xml.transform.sax.TransformerHandler;
  38. import javax.xml.transform.stream.StreamResult;
  39. import javax.xml.transform.stream.StreamSource;
  40. import org.custommonkey.xmlunit.Diff;
  41. import org.junit.Test;
  42. import org.w3c.dom.Document;
  43. import org.xml.sax.SAXException;
  44. import org.xml.sax.helpers.AttributesImpl;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertNull;
  47. import static org.junit.Assert.assertTrue;
  48. import org.apache.fop.accessibility.StructureTree2SAXEventAdapter;
  49. import org.apache.fop.accessibility.StructureTreeEventHandler;
  50. import org.apache.fop.apps.FOPException;
  51. import org.apache.fop.apps.FOUserAgent;
  52. import org.apache.fop.apps.FopFactory;
  53. import org.apache.fop.fo.FODocumentParser;
  54. import org.apache.fop.fo.FODocumentParser.FOEventHandlerFactory;
  55. import org.apache.fop.fo.FOEventHandler;
  56. import org.apache.fop.fo.LoadingException;
  57. import org.apache.fop.fotreetest.DummyFOEventHandler;
  58. import org.apache.fop.render.intermediate.IFContext;
  59. import org.apache.fop.render.pdf.PDFDocumentHandler;
  60. public class FO2StructureTreeConverterTestCase {
  61. private static class FOLoader {
  62. private final String resourceName;
  63. FOLoader(String resourceName) {
  64. this.resourceName = resourceName;
  65. }
  66. public InputStream getFoInputStream() {
  67. return getResource(resourceName);
  68. }
  69. }
  70. private static final String STRUCTURE_TREE_SEQUENCE_NAME = "structure-tree-sequence";
  71. private FOLoader foLoader;
  72. private boolean keepEmptyTags = true;
  73. @Test
  74. public void testCompleteDocument() throws Exception {
  75. testConverter("/org/apache/fop/fo/complete_document.fo");
  76. }
  77. @Test
  78. public void testAbbreviationProperty() throws Exception {
  79. testConverter("abb.fo");
  80. }
  81. @Test
  82. public void testTableFooters() throws Exception {
  83. testConverter("table-footers.fo");
  84. }
  85. @Test
  86. public void testArtifact() throws Exception {
  87. testConverter("artifact.fo");
  88. }
  89. @Test
  90. public void testSideRegions() throws Exception {
  91. testConverter("/org/apache/fop/fo/pagination/side-regions.fo");
  92. }
  93. @Test
  94. public void headerTableCellMustPropagateScope() throws Exception {
  95. testConverter("table-header_scope.fo");
  96. }
  97. @Test
  98. public void testLanguage() throws Exception {
  99. testConverter("language.fo");
  100. }
  101. private static InputStream getResource(String name) {
  102. return FO2StructureTreeConverterTestCase.class.getResourceAsStream(name);
  103. }
  104. @Test
  105. public void testPDFA() throws Exception {
  106. FOUserAgent userAgent = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
  107. userAgent.getRendererOptions().put("pdf-a-mode", "PDF/A-1b");
  108. userAgent.setAccessibility(true);
  109. PDFDocumentHandler d = new PDFDocumentHandler(new IFContext(userAgent));
  110. OutputStream writer = new ByteArrayOutputStream();
  111. StreamResult result = new StreamResult(writer);
  112. d.setResult(result);
  113. d.getStructureTreeEventHandler();
  114. d.startDocument();
  115. assertNull(d.getStructureTreeEventHandler().startNode("table-body", null, null));
  116. }
  117. @Test
  118. public void testRemoveBlocks() throws Exception {
  119. keepEmptyTags = false;
  120. compare("<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n"
  121. + " <fo:layout-master-set>\n"
  122. + " <fo:simple-page-master master-name=\"simple\">\n"
  123. + " <fo:region-body />\n"
  124. + " </fo:simple-page-master>\n"
  125. + " </fo:layout-master-set>\n"
  126. + " <fo:page-sequence master-reference=\"simple\">\n"
  127. + " <fo:flow flow-name=\"xsl-region-body\">\n"
  128. + " <fo:block/>"
  129. + " <fo:block><fo:block/></fo:block>\n"
  130. + " <fo:block>a</fo:block>\n"
  131. + " <fo:block><fo:leader/></fo:block>\n"
  132. + " <fo:block>a<fo:leader/></fo:block>\n"
  133. + " </fo:flow>\n"
  134. + " </fo:page-sequence>\n"
  135. + "</fo:root>\n",
  136. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  137. + "<structure-tree-sequence>\n"
  138. + "<structure-tree xmlns=\"http://xmlgraphics.apache.org/fop/intermediate\" "
  139. + "xmlns:foi=\"http://xmlgraphics.apache.org/fop/internal\" "
  140. + "xmlns:fox=\"http://xmlgraphics.apache.org/fop/extensions\">\n"
  141. + "<fo:flow xmlns:fo=\"http://www.w3.org/1999/XSL/Format\" flow-name=\"xsl-region-body\">\n"
  142. + "<fo:block>\n"
  143. + "<marked-content/>\n"
  144. + "</fo:block>\n"
  145. + "<fo:block>\n"
  146. + "<marked-content/>\n"
  147. + "</fo:block>\n"
  148. + "</fo:flow>\n"
  149. + "</structure-tree>\n"
  150. + "</structure-tree-sequence>\n");
  151. }
  152. private void compare(final String fo, String tree) throws Exception {
  153. foLoader = new FOLoader("") {
  154. public InputStream getFoInputStream() {
  155. return new ByteArrayInputStream(fo.getBytes());
  156. }
  157. };
  158. DOMResult actualStructureTree = buildActualStructureTree();
  159. Document doc = (Document) actualStructureTree.getNode();
  160. StringWriter sw = new StringWriter();
  161. TransformerFactory tf = TransformerFactory.newInstance();
  162. Transformer transformer = tf.newTransformer();
  163. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  164. transformer.transform(new DOMSource(doc), new StreamResult(sw));
  165. assertEquals(tree, sw.toString());
  166. }
  167. private void testConverter(String foResourceName) throws Exception {
  168. foLoader = new FOLoader(foResourceName);
  169. DOMResult expectedStructureTree = loadExpectedStructureTree();
  170. DOMResult actualStructureTree = buildActualStructureTree();
  171. final Diff diff = createDiff(expectedStructureTree, actualStructureTree);
  172. assertTrue(diff.toString(), diff.identical());
  173. }
  174. private DOMResult loadExpectedStructureTree() {
  175. DOMResult expectedStructureTree = new DOMResult();
  176. InputStream xslt = getResource("fo2StructureTree.xsl");
  177. runXSLT(xslt, foLoader.getFoInputStream(), expectedStructureTree);
  178. return expectedStructureTree;
  179. }
  180. private static void runXSLT(InputStream xslt, InputStream doc, Result result) {
  181. Source fo = new StreamSource(doc);
  182. try {
  183. Transformer transformer = TransformerFactory.newInstance()
  184. .newTransformer(new StreamSource(xslt));
  185. transformer.transform(fo, result);
  186. } catch (TransformerConfigurationException e) {
  187. throw new RuntimeException(e);
  188. } catch (TransformerException e) {
  189. throw new RuntimeException(e);
  190. } finally {
  191. closeStream(xslt);
  192. closeStream(doc);
  193. }
  194. }
  195. private static void closeStream(InputStream stream) {
  196. try {
  197. stream.close();
  198. } catch (IOException e) {
  199. throw new RuntimeException(e);
  200. }
  201. }
  202. private DOMResult buildActualStructureTree() throws Exception {
  203. DOMResult actualStructureTree = new DOMResult();
  204. createStructureTreeFromDocument(foLoader.getFoInputStream(), actualStructureTree);
  205. return actualStructureTree;
  206. }
  207. private void createStructureTreeFromDocument(InputStream foInputStream,
  208. Result result) throws Exception {
  209. TransformerHandler tHandler = createTransformerHandler(result);
  210. startStructureTreeSequence(tHandler);
  211. StructureTreeEventHandler structureTreeEventHandler
  212. = StructureTree2SAXEventAdapter.newInstance(tHandler);
  213. FODocumentParser documentParser = createDocumentParser(structureTreeEventHandler);
  214. FOUserAgent userAgent = createFOUserAgent(documentParser);
  215. parseDocument(foInputStream, documentParser, userAgent);
  216. endStructureTreeSequence(tHandler);
  217. }
  218. private static TransformerHandler createTransformerHandler(Result domResult)
  219. throws TransformerConfigurationException, TransformerFactoryConfigurationError {
  220. SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
  221. TransformerHandler transformerHandler = factory.newTransformerHandler();
  222. transformerHandler.setResult(domResult);
  223. return transformerHandler;
  224. }
  225. private static void startStructureTreeSequence(TransformerHandler tHandler) throws SAXException {
  226. tHandler.startDocument();
  227. tHandler.startElement("", STRUCTURE_TREE_SEQUENCE_NAME, STRUCTURE_TREE_SEQUENCE_NAME,
  228. new AttributesImpl());
  229. }
  230. private static FODocumentParser createDocumentParser(
  231. final StructureTreeEventHandler structureTreeEventHandler) {
  232. return FODocumentParser.newInstance(new FOEventHandlerFactory() {
  233. public FOEventHandler newFOEventHandler(FOUserAgent foUserAgent) {
  234. return new FO2StructureTreeConverter(structureTreeEventHandler,
  235. new DummyFOEventHandler(foUserAgent));
  236. }
  237. });
  238. }
  239. private FOUserAgent createFOUserAgent(FODocumentParser documentParser) {
  240. FOUserAgent userAgent = documentParser.createFOUserAgent();
  241. userAgent.setAccessibility(true);
  242. userAgent.setKeepEmptyTags(keepEmptyTags);
  243. return userAgent;
  244. }
  245. private static void parseDocument(InputStream foInputStream, FODocumentParser documentParser,
  246. FOUserAgent userAgent) throws FOPException, LoadingException {
  247. try {
  248. documentParser.parse(foInputStream, userAgent);
  249. } finally {
  250. closeStream(foInputStream);
  251. }
  252. }
  253. private static void endStructureTreeSequence(TransformerHandler tHandler) throws SAXException {
  254. tHandler.endElement("", STRUCTURE_TREE_SEQUENCE_NAME, STRUCTURE_TREE_SEQUENCE_NAME);
  255. tHandler.endDocument();
  256. }
  257. private static Diff createDiff(DOMResult expected, DOMResult actual) {
  258. Diff diff = new Diff(getDocument(expected), getDocument(actual));
  259. return diff;
  260. }
  261. private static Document getDocument(DOMResult result) {
  262. return (Document) result.getNode();
  263. }
  264. }