Skip to content

Commit 9ac1de1

Browse files
author
Joao Goncalves
committed
FOP-3165 Fix table structure creation when using PDFUA
1 parent 38fbe94 commit 9ac1de1

File tree

3 files changed

+93
-23
lines changed

3 files changed

+93
-23
lines changed

fop-core/src/main/java/org/apache/fop/render/pdf/PDFStructureTreeBuilder.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,7 @@ public void endNode(String name) {
465465
}
466466

467467
private boolean isPDFA1Safe(String name) {
468-
return !((pdfFactory.getDocument().getProfile().getPDFAMode().isPart1()
469-
|| pdfFactory.getDocument().getProfile().getPDFUAMode().isEnabled())
468+
return !((pdfFactory.getDocument().getProfile().getPDFAMode().isPart1())
470469
&& (name.equals("table-body")
471470
|| name.equals("table-header")
472471
|| name.equals("table-footer")));

fop-core/src/test/java/org/apache/fop/accessibility/fo/FO2StructureTreeConverterTestCase.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,8 @@
2020
package org.apache.fop.accessibility.fo;
2121

2222
import java.io.ByteArrayInputStream;
23-
import java.io.ByteArrayOutputStream;
24-
import java.io.File;
2523
import java.io.IOException;
2624
import java.io.InputStream;
27-
import java.io.OutputStream;
2825
import java.io.StringWriter;
2926
import java.nio.charset.StandardCharsets;
3027

@@ -50,7 +47,6 @@
5047
import org.xml.sax.SAXException;
5148
import org.xml.sax.helpers.AttributesImpl;
5249

53-
import static org.junit.Assert.assertNull;
5450
import static org.junit.Assert.assertTrue;
5551

5652
import org.apache.commons.io.IOUtils;
@@ -59,14 +55,11 @@
5955
import org.apache.fop.accessibility.StructureTreeEventHandler;
6056
import org.apache.fop.apps.FOPException;
6157
import org.apache.fop.apps.FOUserAgent;
62-
import org.apache.fop.apps.FopFactory;
6358
import org.apache.fop.fo.FODocumentParser;
6459
import org.apache.fop.fo.FODocumentParser.FOEventHandlerFactory;
6560
import org.apache.fop.fo.FOEventHandler;
6661
import org.apache.fop.fo.LoadingException;
6762
import org.apache.fop.fotreetest.DummyFOEventHandler;
68-
import org.apache.fop.render.intermediate.IFContext;
69-
import org.apache.fop.render.pdf.PDFDocumentHandler;
7063

7164
public class FO2StructureTreeConverterTestCase {
7265

@@ -137,20 +130,6 @@ private static InputStream getResource(String name) {
137130
return FO2StructureTreeConverterTestCase.class.getResourceAsStream(name);
138131
}
139132

140-
@Test
141-
public void testPDFA() throws Exception {
142-
FOUserAgent userAgent = FopFactory.newInstance(new File(".").toURI()).newFOUserAgent();
143-
userAgent.getRendererOptions().put("pdf-a-mode", "PDF/A-1b");
144-
userAgent.setAccessibility(true);
145-
PDFDocumentHandler d = new PDFDocumentHandler(new IFContext(userAgent));
146-
OutputStream writer = new ByteArrayOutputStream();
147-
StreamResult result = new StreamResult(writer);
148-
d.setResult(result);
149-
d.getStructureTreeEventHandler();
150-
d.startDocument();
151-
assertNull(d.getStructureTreeEventHandler().startNode("table-body", null, null));
152-
}
153-
154133
@Test
155134
public void testRemoveBlocks() throws Exception {
156135
keepEmptyTags = false;

fop-core/src/test/java/org/apache/fop/render/pdf/PDFStructureTreeBuilderTestCase.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020
package org.apache.fop.render.pdf;
2121

2222
import java.io.ByteArrayOutputStream;
23+
import java.io.File;
2324
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;
2431

2532
import org.junit.Before;
2633
import org.junit.Test;
@@ -30,16 +37,23 @@
3037
import static org.junit.Assert.assertEquals;
3138
import static org.junit.Assert.assertNotNull;
3239
import static org.junit.Assert.assertNull;
40+
import static org.mockito.Mockito.mock;
3341

42+
import org.apache.fop.accessibility.StructureTreeElement;
43+
import org.apache.fop.apps.FOUserAgent;
44+
import org.apache.fop.apps.FopFactory;
3445
import org.apache.fop.pdf.PDFDocument;
3546
import org.apache.fop.pdf.PDFFactory;
3647
import org.apache.fop.pdf.PDFResources;
3748
import org.apache.fop.pdf.PDFStructElem;
3849
import org.apache.fop.pdf.StandardStructureTypes;
50+
import org.apache.fop.render.intermediate.IFContext;
3951

4052
public class PDFStructureTreeBuilderTestCase {
4153
private PDFFactory pdfFactory;
4254

55+
private static final List<String> TABLE_TAGS = Arrays.asList("table-header", "table-footer", "table-body");
56+
4357
@Before
4458
public void setUp() {
4559
PDFDocument doc = new PDFDocument("");
@@ -82,4 +96,82 @@ public void testExternalDocumentBuilder() {
8296
assertEquals("Elem must be of type figure", StandardStructureTypes.Illustration.FIGURE,
8397
elem.getStructureType());
8498
}
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+
}
85177
}

0 commit comments

Comments
 (0)