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

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