Skip to content

Commit ebd103e

Browse files
FOP-3231: Add option to combine AFP page groups
1 parent 3739ddd commit ebd103e

File tree

6 files changed

+101
-15
lines changed

6 files changed

+101
-15
lines changed

fop-core/src/main/java/org/apache/fop/afp/DataStream.java

+9-7
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public void endDocument() throws IOException {
169169

170170
if (currentPageGroup != null) {
171171
// End the current page group if necessary
172-
endPageGroup();
172+
endPageGroup(true);
173173
}
174174

175175
// Write out document
@@ -626,23 +626,25 @@ public void startDocument() throws IOException {
626626

627627
/**
628628
* Start a new page group. When processing has finished on the current page
629-
* group the {@link #endPageGroup()}method must be invoked to mark the page
629+
* group the endPageGroup method must be invoked to mark the page
630630
* group ending.
631631
*
632632
* @throws IOException thrown if an I/O exception of some sort has occurred
633633
*/
634-
public void startPageGroup() throws IOException {
635-
endPageGroup();
636-
this.currentPageGroup = factory.createPageGroup();
634+
public void startPageGroup(boolean endPageGroup) throws IOException {
635+
endPageGroup(endPageGroup);
636+
if (currentPageGroup == null) {
637+
currentPageGroup = factory.createPageGroup();
638+
}
637639
}
638640

639641
/**
640642
* Helper method to mark the end of the page group.
641643
*
642644
* @throws IOException thrown if an I/O exception of some sort has occurred
643645
*/
644-
public void endPageGroup() throws IOException {
645-
if (currentPageGroup != null) {
646+
public void endPageGroup(boolean endPageGroup) throws IOException {
647+
if (currentPageGroup != null && endPageGroup) {
646648
currentPageGroup.endPageGroup();
647649
document.addPageGroup(currentPageGroup);
648650
currentPageGroup = null;

fop-core/src/main/java/org/apache/fop/render/afp/AFPDocumentHandler.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ public void endDocument() throws IFException {
216216
public void startPageSequence(String id) throws IFException {
217217
try {
218218
if (!"false".equals(getContext().getForeignAttribute(AFPElementMapping.PAGE_GROUP))) {
219-
dataStream.startPageGroup();
219+
boolean addToPreviousPageGroup =
220+
"true".equals(getContext().getForeignAttribute(AFPElementMapping.ADD_TO_PREVIOUS_PAGE_GROUP));
221+
dataStream.startPageGroup(!addToPreviousPageGroup);
220222
}
221223
} catch (IOException ioe) {
222224
throw new IFException("I/O error in startPageSequence()", ioe);
@@ -240,7 +242,7 @@ public void endPageSequence() throws IFException {
240242
}
241243

242244
//End page sequence
243-
dataStream.endPageGroup();
245+
dataStream.endPageGroup(false);
244246
} catch (IOException ioe) {
245247
throw new IFException("I/O error in endPageSequence()", ioe);
246248
}

fop-core/src/main/java/org/apache/fop/render/afp/extensions/AFPElementMapping.java

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class AFPElementMapping extends ElementMapping {
5959
public static final String NAMESPACE = "http://xmlgraphics.apache.org/fop/extensions/afp";
6060

6161
public static final QName PAGE_GROUP = new QName(NAMESPACE, null, "page-group");
62+
public static final QName ADD_TO_PREVIOUS_PAGE_GROUP = new QName(NAMESPACE, null, "add-to-previous-page-group");
6263

6364
/**
6465
* The usual namespace prefix used for AFP extensions

fop-core/src/test/java/org/apache/fop/afp/DataStreamTestCase.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void testCreateText() throws Exception {
8080
ds.createShading(10, 10, 300, 300, Color.white);
8181
ds.createIncludePageOverlay("testings", 10, 10);
8282
ds.startDocument();
83-
ds.startPageGroup();
83+
ds.startPageGroup(true);
8484
ds.createInvokeMediumMap("test");
8585
ds.createIncludePageSegment("test", 10, 10, 300, 300);
8686
ds.createTagLogicalElement("test", "test", 0);
@@ -118,10 +118,10 @@ public void testMediumMapBeforePageGroupOnDocument() throws Exception {
118118
ds = new DataStream(new Factory(), paintState, outStream);
119119
ds.startDocument();
120120
ds.createInvokeMediumMap("test");
121-
ds.startPageGroup();
121+
ds.startPageGroup(true);
122122
ds.startPage(1, 1, 0, 1, 1);
123123
ds.endPage();
124-
ds.endPageGroup();
124+
ds.endPageGroup(true);
125125
ds.endDocument();
126126
ByteArrayInputStream data = new ByteArrayInputStream(outStream.toByteArray());
127127
data.skip(21);
@@ -134,10 +134,10 @@ public void testMediumMapBeforePageGroupOnDocument() throws Exception {
134134
public void testMandatoryTripletIsAddedToAFP() throws Exception {
135135
ds = new DataStream(new Factory(), paintState, outStream);
136136
ds.startDocument();
137-
ds.startPageGroup();
137+
ds.startPageGroup(true);
138138
ds.startPage(1, 1, 0, 1, 1);
139139
ds.endPage();
140-
ds.endPageGroup();
140+
ds.endPageGroup(true);
141141
ds.endDocument();
142142
ByteArrayInputStream data = new ByteArrayInputStream(outStream.toByteArray());
143143
data.skip(17); //skipping the begin document data
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
package org.apache.fop.render.afp;
18+
19+
import java.awt.Dimension;
20+
import java.io.ByteArrayInputStream;
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.util.HashMap;
24+
import java.util.Map;
25+
26+
import javax.xml.transform.stream.StreamResult;
27+
28+
import org.junit.Assert;
29+
import org.junit.Test;
30+
31+
import org.apache.xmlgraphics.util.QName;
32+
33+
import org.apache.fop.apps.FOUserAgent;
34+
import org.apache.fop.render.afp.extensions.AFPElementMapping;
35+
import org.apache.fop.render.intermediate.IFContext;
36+
import org.apache.fop.render.intermediate.IFException;
37+
38+
public class AddToPreviousPageGroupTestCase extends AbstractAFPTest {
39+
@Test
40+
public void testAddToPreviousPageGroup() throws Exception {
41+
Assert.assertEquals("BEGIN DOCUMENT DOC00001 Triplets: 0x01,\n"
42+
+ "BEGIN PAGE_GROUP PGP00001\n"
43+
+ "BEGIN PAGE PGN00001\n"
44+
+ "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
45+
+ "DESCRIPTOR PAGE\n"
46+
+ "MIGRATION PRESENTATION_TEXT\n"
47+
+ "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
48+
+ "END PAGE PGN00001\n"
49+
+ "BEGIN PAGE PGN00002\n"
50+
+ "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00002\n"
51+
+ "DESCRIPTOR PAGE\n"
52+
+ "MIGRATION PRESENTATION_TEXT\n"
53+
+ "END ACTIVE_ENVIRONMENT_GROUP AEG00002\n"
54+
+ "END PAGE PGN00002\n"
55+
+ "END PAGE_GROUP PGP00001\n"
56+
+ "END DOCUMENT DOC00001\n", render());
57+
}
58+
59+
private String render() throws IFException, IOException {
60+
FOUserAgent ua = fopFactory.newFOUserAgent();
61+
AFPDocumentHandler documentHandler = new AFPDocumentHandler(new IFContext(ua));
62+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
63+
documentHandler.setResult(new StreamResult(outputStream));
64+
documentHandler.startDocument();
65+
documentHandler.startPageSequence("");
66+
documentHandler.startPage(0, "", "", new Dimension());
67+
documentHandler.endPage();
68+
documentHandler.endPageSequence();
69+
Map<QName, String> attributes = new HashMap<>();
70+
attributes.put(AFPElementMapping.ADD_TO_PREVIOUS_PAGE_GROUP, "true");
71+
documentHandler.getContext().setForeignAttributes(attributes);
72+
documentHandler.startPageSequence("");
73+
documentHandler.startPage(1, "", "", new Dimension());
74+
documentHandler.endPage();
75+
documentHandler.endPageSequence();
76+
documentHandler.endDocument();
77+
StringBuilder sb = new StringBuilder();
78+
new AFPParser(false).read(new ByteArrayInputStream(outputStream.toByteArray()), sb);
79+
return sb.toString();
80+
}
81+
}

fop-core/src/test/java/org/apache/fop/render/afp/PageOverlayTestCase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public class PageOverlayTestCase {
4242
public void testPageOverlay() throws Exception {
4343
Assert.assertEquals(getPageOverlay(), "BEGIN DOCUMENT DOC00001 Triplets: 0x01,\n"
4444
+ "BEGIN PAGE_GROUP PGP00001\n"
45-
+ "END PAGE_GROUP PGP00001\n"
4645
+ "BEGIN PAGE PGN00001\n"
4746
+ "BEGIN ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
4847
+ "MAP PAGE_OVERLAY Triplets: FULLY_QUALIFIED_NAME,RESOURCE_LOCAL_IDENTIFIER,\n"
@@ -51,6 +50,7 @@ public void testPageOverlay() throws Exception {
5150
+ "END ACTIVE_ENVIRONMENT_GROUP AEG00001\n"
5251
+ "INCLUDE PAGE_OVERLAY\n"
5352
+ "END PAGE PGN00001\n"
53+
+ "END PAGE_GROUP PGP00001\n"
5454
+ "END DOCUMENT DOC00001\n");
5555
}
5656

0 commit comments

Comments
 (0)