|
20 | 20 | package org.apache.fop.accessibility.fo; |
21 | 21 |
|
22 | 22 | import java.io.ByteArrayInputStream; |
| 23 | +import java.io.ByteArrayOutputStream; |
| 24 | +import java.io.File; |
23 | 25 | import java.io.IOException; |
24 | 26 | import java.io.InputStream; |
25 | 27 | import java.io.StringWriter; |
26 | 28 | import java.nio.charset.StandardCharsets; |
| 29 | +import java.util.List; |
27 | 30 |
|
28 | 31 | import javax.xml.transform.Result; |
29 | 32 | import javax.xml.transform.Source; |
|
34 | 37 | import javax.xml.transform.TransformerFactoryConfigurationError; |
35 | 38 | import javax.xml.transform.dom.DOMResult; |
36 | 39 | import javax.xml.transform.dom.DOMSource; |
| 40 | +import javax.xml.transform.sax.SAXResult; |
37 | 41 | import javax.xml.transform.sax.SAXTransformerFactory; |
38 | 42 | import javax.xml.transform.sax.TransformerHandler; |
39 | 43 | import javax.xml.transform.stream.StreamResult; |
|
46 | 50 | import org.w3c.dom.Document; |
47 | 51 | import org.xml.sax.SAXException; |
48 | 52 | import org.xml.sax.helpers.AttributesImpl; |
49 | | - |
| 53 | +import static org.junit.Assert.assertEquals; |
50 | 54 | import static org.junit.Assert.assertTrue; |
51 | 55 |
|
52 | 56 | import org.apache.commons.io.IOUtils; |
| 57 | +import org.apache.pdfbox.Loader; |
| 58 | +import org.apache.pdfbox.pdmodel.PDDocument; |
| 59 | +import org.apache.pdfbox.pdmodel.documentinterchange.markedcontent.PDMarkedContent; |
| 60 | +import org.apache.pdfbox.text.PDFMarkedContentExtractor; |
53 | 61 |
|
54 | 62 | import org.apache.fop.accessibility.StructureTree2SAXEventAdapter; |
55 | 63 | import org.apache.fop.accessibility.StructureTreeEventHandler; |
56 | 64 | import org.apache.fop.apps.FOPException; |
57 | 65 | import org.apache.fop.apps.FOUserAgent; |
| 66 | +import org.apache.fop.apps.Fop; |
| 67 | +import org.apache.fop.apps.FopFactory; |
| 68 | +import org.apache.fop.apps.MimeConstants; |
58 | 69 | import org.apache.fop.fo.FODocumentParser; |
59 | 70 | import org.apache.fop.fo.FODocumentParser.FOEventHandlerFactory; |
60 | 71 | import org.apache.fop.fo.FOEventHandler; |
@@ -242,6 +253,83 @@ public void testSVGArtifact() throws Exception { |
242 | 253 | + "</structure-tree-sequence>"); |
243 | 254 | } |
244 | 255 |
|
| 256 | + @Test |
| 257 | + public void testMultipleStaticContentArtifact() throws FOPException, |
| 258 | + TransformerException, IOException { |
| 259 | + FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI()); |
| 260 | + FOUserAgent foUserAgent = fopFactory.newFOUserAgent(); |
| 261 | + foUserAgent.setAccessibility(true); |
| 262 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 263 | + Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out); |
| 264 | + TransformerFactory factory = TransformerFactory.newInstance(); |
| 265 | + Transformer transformer = factory.newTransformer(); |
| 266 | + |
| 267 | + String fo = "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\" xmlns:svg=\"http://www.w3.org/2000/svg\">" |
| 268 | + + " <fo:layout-master-set>\n" |
| 269 | + + " <fo:simple-page-master page-width=\"8.5in\" page-height=\"11in\" master-name=\"Page\">\n" |
| 270 | + + " <fo:region-body region-name=\"Body\"/>\n" |
| 271 | + + " <fo:region-before region-name=\"xsl-region-before\" extent=\"0.5in\"/>\n" |
| 272 | + + " </fo:simple-page-master>\n" |
| 273 | + + " </fo:layout-master-set>\n" |
| 274 | + + " <fo:page-sequence master-reference=\"Page\">\n" |
| 275 | + + " <fo:static-content flow-name=\"xsl-region-before\" role=\"artifact\">\n" |
| 276 | + + " <fo:block>\n" |
| 277 | + + " <fo:retrieve-marker retrieve-class-name=\"headline-current\"/>\n" |
| 278 | + + " </fo:block>\n" |
| 279 | + + " </fo:static-content>\n" |
| 280 | + + " <fo:flow flow-name=\"Body\">\n" |
| 281 | + + " <fo:block>\n" |
| 282 | + + " <fo:marker marker-class-name=\"headline-current\">1</fo:marker>\n" |
| 283 | + + " <fo:block>A</fo:block>\n" |
| 284 | + + " </fo:block>\n" |
| 285 | + + " </fo:flow>\n" |
| 286 | + + " </fo:page-sequence>\n" |
| 287 | + + " <fo:page-sequence master-reference=\"Page\">\n" |
| 288 | + + " <fo:static-content flow-name=\"xsl-region-before\" role=\"artifact\">\n" |
| 289 | + + " <fo:block>\n" |
| 290 | + + " <fo:retrieve-marker retrieve-class-name=\"headline-current\"/>\n" |
| 291 | + + " </fo:block>\n" |
| 292 | + + " </fo:static-content>\n" |
| 293 | + + " <fo:flow flow-name=\"Body\">\n" |
| 294 | + + " <fo:block>\n" |
| 295 | + + " <fo:marker marker-class-name=\"headline-current\">2</fo:marker>\n" |
| 296 | + + " <fo:block>B</fo:block>\n" |
| 297 | + + " </fo:block>\n" |
| 298 | + + " </fo:flow>\n" |
| 299 | + + " </fo:page-sequence>\n" |
| 300 | + + "</fo:root>"; |
| 301 | + Source src = new StreamSource(new ByteArrayInputStream(fo.getBytes())); |
| 302 | + Result res = new SAXResult(fop.getDefaultHandler()); |
| 303 | + |
| 304 | + try { |
| 305 | + transformer.transform(src, res); |
| 306 | + } finally { |
| 307 | + out.close(); |
| 308 | + } |
| 309 | + |
| 310 | + try (PDDocument pdfDocument = Loader.loadPDF(out.toByteArray())) { |
| 311 | + PDFMarkedContentExtractor extractor = new PDFMarkedContentExtractor(); |
| 312 | + assertEquals(2, pdfDocument.getPages().getCount()); |
| 313 | + extractor.processPage(pdfDocument.getPages().get(0)); |
| 314 | + extractor.processPage(pdfDocument.getPages().get(1)); |
| 315 | + |
| 316 | + List<PDMarkedContent> markedContents = extractor.getMarkedContents(); |
| 317 | + assertEquals(4, markedContents.size()); |
| 318 | + |
| 319 | + assertEquals("Artifact", markedContents.get(0).getTag()); |
| 320 | + assertEquals("1", markedContents.get(0).getContents().get(0).toString()); |
| 321 | + |
| 322 | + assertEquals("P", markedContents.get(1).getTag()); |
| 323 | + assertEquals("A", markedContents.get(1).getContents().get(0).toString()); |
| 324 | + |
| 325 | + assertEquals("Artifact", markedContents.get(2).getTag()); |
| 326 | + assertEquals("2", markedContents.get(2).getContents().get(0).toString()); |
| 327 | + |
| 328 | + assertEquals("P", markedContents.get(3).getTag()); |
| 329 | + assertEquals("B", markedContents.get(3).getContents().get(0).toString()); |
| 330 | + } |
| 331 | + } |
| 332 | + |
245 | 333 | private void compare(final String fo, String tree) throws Exception { |
246 | 334 | foLoader = new FOLoader("") { |
247 | 335 | public InputStream getFoInputStream() { |
|
0 commit comments