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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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;
  19. import static org.junit.Assert.assertTrue;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import javax.xml.transform.Result;
  23. import javax.xml.transform.Source;
  24. import javax.xml.transform.Transformer;
  25. import javax.xml.transform.TransformerConfigurationException;
  26. import javax.xml.transform.TransformerException;
  27. import javax.xml.transform.TransformerFactory;
  28. import javax.xml.transform.TransformerFactoryConfigurationError;
  29. import javax.xml.transform.dom.DOMResult;
  30. import javax.xml.transform.sax.SAXTransformerFactory;
  31. import javax.xml.transform.sax.TransformerHandler;
  32. import javax.xml.transform.stream.StreamSource;
  33. import org.custommonkey.xmlunit.Diff;
  34. import org.custommonkey.xmlunit.Difference;
  35. import org.custommonkey.xmlunit.DifferenceConstants;
  36. import org.custommonkey.xmlunit.DifferenceListener;
  37. import org.junit.Test;
  38. import org.w3c.dom.Document;
  39. import org.w3c.dom.Node;
  40. import org.xml.sax.SAXException;
  41. import org.xml.sax.helpers.AttributesImpl;
  42. import org.apache.fop.apps.FOPException;
  43. import org.apache.fop.apps.FOUserAgent;
  44. import org.apache.fop.fo.FODocumentParser;
  45. import org.apache.fop.fo.FODocumentParser.FOEventHandlerFactory;
  46. import org.apache.fop.fo.FOEventHandler;
  47. import org.apache.fop.fo.LoadingException;
  48. import org.apache.fop.fotreetest.DummyFOEventHandler;
  49. public class FO2StructureTreeConverterTestCase {
  50. private static class IgnoringPtrDifferenceListener implements DifferenceListener {
  51. public int differenceFound(Difference difference) {
  52. switch (difference.getId()) {
  53. case DifferenceConstants.ELEMENT_NUM_ATTRIBUTES_ID:
  54. return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
  55. case DifferenceConstants.ATTR_NAME_NOT_FOUND_ID:
  56. String additionalAttribute = difference.getTestNodeDetail().getValue();
  57. if (additionalAttribute.equals("ptr")) {
  58. return RETURN_IGNORE_DIFFERENCE_NODES_SIMILAR;
  59. } else {
  60. return RETURN_ACCEPT_DIFFERENCE;
  61. }
  62. default:
  63. return RETURN_ACCEPT_DIFFERENCE;
  64. }
  65. }
  66. public void skippedComparison(Node control, Node test) {
  67. throw new UnsupportedOperationException("Not implemented");
  68. }
  69. }
  70. private static final String STRUCTURE_TREE_SEQUENCE_NAME = "structure-tree-sequence";
  71. @Test
  72. public void testConverter() throws Exception {
  73. DOMResult expectedStructureTree = loadExpectedStructureTree();
  74. DOMResult actualStructureTree = buildActualStructureTree();
  75. final Diff diff = createDiff(expectedStructureTree, actualStructureTree);
  76. assertTrue(diff.toString(), diff.similar());
  77. }
  78. private static DOMResult loadExpectedStructureTree() {
  79. DOMResult expectedStructureTree = new DOMResult();
  80. runXSLT(getXsltInputStream(), getFoInputStream(), expectedStructureTree);
  81. return expectedStructureTree;
  82. }
  83. private static InputStream getXsltInputStream() {
  84. return FO2StructureTreeConverterTestCase.class.getResourceAsStream("foToIfStructureTree.xsl");
  85. }
  86. private static InputStream getFoInputStream() {
  87. return FO2StructureTreeConverterTestCase.class.getResourceAsStream(
  88. "/org/apache/fop/fo/complete_document.fo");
  89. }
  90. private static void runXSLT(InputStream xslt, InputStream doc, Result result) {
  91. Source fo = new StreamSource(doc);
  92. try {
  93. Transformer transformer = TransformerFactory.newInstance()
  94. .newTransformer(new StreamSource(xslt));
  95. transformer.transform(fo, result);
  96. } catch (TransformerConfigurationException e) {
  97. throw new RuntimeException(e);
  98. } catch (TransformerException e) {
  99. throw new RuntimeException(e);
  100. } finally {
  101. closeStream(xslt);
  102. closeStream(doc);
  103. }
  104. }
  105. private static void closeStream(InputStream stream) {
  106. try {
  107. stream.close();
  108. } catch (IOException e) {
  109. throw new RuntimeException(e);
  110. }
  111. }
  112. private static DOMResult buildActualStructureTree() throws Exception {
  113. DOMResult actualStructureTree = new DOMResult();
  114. createStructureTreeFromDocument(getFoInputStream(), actualStructureTree);
  115. return actualStructureTree;
  116. }
  117. private static void createStructureTreeFromDocument(InputStream foInputStream,
  118. DOMResult domResult) throws Exception {
  119. TransformerHandler tHandler = createTransformerHandler(domResult);
  120. startStructureTreeSequence(tHandler);
  121. StructureTreeEventHandler structureTreeEventHandler
  122. = StructureTree2SAXEventAdapter.newInstance(tHandler);
  123. FODocumentParser documentParser = createDocumentParser(structureTreeEventHandler);
  124. FOUserAgent userAgent = createFOUserAgent(documentParser);
  125. parseDocument(foInputStream, documentParser, userAgent);
  126. endStructureTreeSequence(tHandler);
  127. }
  128. private static TransformerHandler createTransformerHandler(DOMResult domResult)
  129. throws TransformerConfigurationException, TransformerFactoryConfigurationError {
  130. SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
  131. TransformerHandler transformerHandler = factory.newTransformerHandler();
  132. transformerHandler.setResult(domResult);
  133. return transformerHandler;
  134. }
  135. private static void startStructureTreeSequence(TransformerHandler tHandler) throws SAXException {
  136. tHandler.startDocument();
  137. tHandler.startElement("", STRUCTURE_TREE_SEQUENCE_NAME, STRUCTURE_TREE_SEQUENCE_NAME,
  138. new AttributesImpl());
  139. }
  140. private static FODocumentParser createDocumentParser(
  141. final StructureTreeEventHandler structureTreeEventHandler) {
  142. return FODocumentParser.newInstance(new FOEventHandlerFactory() {
  143. public FOEventHandler newFOEventHandler(FOUserAgent foUserAgent) {
  144. return new FO2StructureTreeConverter(structureTreeEventHandler,
  145. new DummyFOEventHandler(foUserAgent));
  146. }
  147. });
  148. }
  149. private static FOUserAgent createFOUserAgent(FODocumentParser documentParser) {
  150. FOUserAgent userAgent = documentParser.createFOUserAgent();
  151. userAgent.setAccessibility(true);
  152. return userAgent;
  153. }
  154. private static void parseDocument(InputStream foInputStream, FODocumentParser documentParser,
  155. FOUserAgent userAgent) throws FOPException, LoadingException {
  156. try {
  157. documentParser.parse(foInputStream, userAgent);
  158. } finally {
  159. closeStream(foInputStream);
  160. }
  161. }
  162. private static void endStructureTreeSequence(TransformerHandler tHandler) throws SAXException {
  163. tHandler.endElement("", STRUCTURE_TREE_SEQUENCE_NAME, STRUCTURE_TREE_SEQUENCE_NAME);
  164. tHandler.endDocument();
  165. }
  166. private static Diff createDiff(DOMResult expected, DOMResult actual) {
  167. Diff diff = new Diff(getDocument(expected), getDocument(actual));
  168. diff.overrideDifferenceListener(new IgnoringPtrDifferenceListener());
  169. return diff;
  170. }
  171. private static Document getDocument(DOMResult result) {
  172. return (Document) result.getNode();
  173. }
  174. }