|
20 | 20 | package org.apache.fop.render.pdf; |
21 | 21 |
|
22 | 22 | import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.File; |
23 | 24 | import java.io.IOException; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | + |
| 30 | +import javax.xml.transform.stream.StreamResult; |
24 | 31 |
|
25 | 32 | import org.junit.Before; |
26 | 33 | import org.junit.Test; |
|
30 | 37 | import static org.junit.Assert.assertEquals; |
31 | 38 | import static org.junit.Assert.assertNotNull; |
32 | 39 | import static org.junit.Assert.assertNull; |
| 40 | +import static org.mockito.Mockito.mock; |
33 | 41 |
|
| 42 | +import org.apache.fop.accessibility.StructureTreeElement; |
| 43 | +import org.apache.fop.apps.FOUserAgent; |
| 44 | +import org.apache.fop.apps.FopFactory; |
34 | 45 | import org.apache.fop.pdf.PDFDocument; |
35 | 46 | import org.apache.fop.pdf.PDFFactory; |
36 | 47 | import org.apache.fop.pdf.PDFResources; |
37 | 48 | import org.apache.fop.pdf.PDFStructElem; |
38 | 49 | import org.apache.fop.pdf.StandardStructureTypes; |
| 50 | +import org.apache.fop.render.intermediate.IFContext; |
39 | 51 |
|
40 | 52 | public class PDFStructureTreeBuilderTestCase { |
41 | 53 | private PDFFactory pdfFactory; |
42 | 54 |
|
| 55 | + private static final List<String> TABLE_TAGS = Arrays.asList("table-header", "table-footer", "table-body"); |
| 56 | + |
43 | 57 | @Before |
44 | 58 | public void setUp() { |
45 | 59 | PDFDocument doc = new PDFDocument(""); |
@@ -82,4 +96,82 @@ public void testExternalDocumentBuilder() { |
82 | 96 | assertEquals("Elem must be of type figure", StandardStructureTypes.Illustration.FIGURE, |
83 | 97 | elem.getStructureType()); |
84 | 98 | } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testNonFilteredTag() throws Exception { |
| 102 | + Result result = defaultStartNode(new HashMap<>(), "table-row"); |
| 103 | + assertNotNull("Table row is not one of the filtered tags", result.structTree); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testPDFATableTags() throws Exception { |
| 108 | + List<String> invalidPDFAModes = Arrays.asList("PDF/A-1a", "PDF/A-1b"); |
| 109 | + |
| 110 | + Result result; |
| 111 | + for (String pdfMode : invalidPDFAModes) { |
| 112 | + Map<String, String> rendererOptionalMap = new HashMap<>(); |
| 113 | + rendererOptionalMap.put("pdf-a-mode", pdfMode); |
| 114 | + |
| 115 | + for (String tag : TABLE_TAGS) { |
| 116 | + result = defaultStartNode(rendererOptionalMap, tag); |
| 117 | + assertNull("These tags are not valid for PDF/A-1", result.structTree); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + List<String> validPDFAModes = Arrays.asList("PDF/A-2a", "PDF/A-2b", "PDF/A-2u", |
| 122 | + "PDF/A-3a", "PDF/A-3b", "PDF/A-3u"); |
| 123 | + |
| 124 | + for (String pdfMode : validPDFAModes) { |
| 125 | + Map<String, String> rendererOptionalMap = new HashMap<>(); |
| 126 | + rendererOptionalMap.put("pdf-a-mode", pdfMode); |
| 127 | + |
| 128 | + for (String tag : TABLE_TAGS) { |
| 129 | + result = defaultStartNode(rendererOptionalMap, tag); |
| 130 | + assertNotNull("These tags are valid on for PDF/A-2 and PDFA/-3", result.structTree); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testPDFUATableTags() throws Exception { |
| 137 | + Map<String, String> rendererOptionalMap = new HashMap<>(); |
| 138 | + rendererOptionalMap.put("pdf-ua-mode", "PDF/UA"); |
| 139 | + |
| 140 | + Result result; |
| 141 | + for (String tag : TABLE_TAGS) { |
| 142 | + result = defaultStartNode(rendererOptionalMap, tag); |
| 143 | + assertNotNull("Tags are valid for PDF/UA", result.structTree); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private static class Result { |
| 148 | + public StructureTreeElement structTree; |
| 149 | + public PDFDocument pdfDoc; |
| 150 | + |
| 151 | + public Result(StructureTreeElement structTree, PDFDocument pdfDoc) { |
| 152 | + this.structTree = structTree; |
| 153 | + this.pdfDoc = pdfDoc; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private Result defaultStartNode(Map<String, String> rendererOptionalMap, String nodeName) |
| 158 | + throws Exception { |
| 159 | + FOUserAgent userAgent = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent(); |
| 160 | + userAgent.getRendererOptions().putAll(rendererOptionalMap); |
| 161 | + userAgent.setAccessibility(true); |
| 162 | + |
| 163 | + PDFDocumentHandler documentHandler = new PDFDocumentHandler(new IFContext(userAgent)); |
| 164 | + documentHandler.setResult(new StreamResult(new ByteArrayOutputStream())); |
| 165 | + documentHandler.getStructureTreeEventHandler(); |
| 166 | + documentHandler.startDocument(); |
| 167 | + |
| 168 | + PDFStructElem divParent = new PDFStructElem(null, StandardStructureTypes.Grouping.DIV); |
| 169 | + PDFStructElem tableParent = PDFStructureTreeBuilder.createStructureElement("table", divParent, |
| 170 | + new AttributesImpl(), new PDFFactory(mock(PDFDocument.class)), null); |
| 171 | + |
| 172 | + StructureTreeElement structElem = documentHandler.getStructureTreeEventHandler() |
| 173 | + .startNode(nodeName, new AttributesImpl(), tableParent); |
| 174 | + |
| 175 | + return new Result(structElem, documentHandler.getPDFDocument()); |
| 176 | + } |
85 | 177 | } |
0 commit comments