Skip to content

Commit b97eaca

Browse files
committed
✨ Added Xci building classes
Currently only supports embedding fonts but can be built out later as required.
1 parent ceb8895 commit b97eaca

5 files changed

Lines changed: 293 additions & 0 deletions

File tree

fluentforms/core/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,15 @@
9494
<artifactId>equalsverifier</artifactId>
9595
<scope>test</scope>
9696
</dependency>
97+
<dependency>
98+
<groupId>org.xmlunit</groupId>
99+
<artifactId>xmlunit-core</artifactId>
100+
<scope>test</scope>
101+
</dependency>
102+
<dependency>
103+
<groupId>org.xmlunit</groupId>
104+
<artifactId>xmlunit-matchers</artifactId>
105+
<scope>test</scope>
106+
</dependency>
97107
</dependencies>
98108
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com._4point.aem.fluentforms.api;
2+
3+
public interface Xci {
4+
5+
public Document toDocument();
6+
public Document toDocument(DocumentFactory factory);
7+
8+
public interface XciBuilder {
9+
DestinationBuilder pdf();
10+
DestinationBuilder pcl();
11+
DestinationBuilder ps();
12+
Xci build();
13+
14+
public interface DestinationBuilder {
15+
DestinationBuilder embedFonts(Boolean embedFonts);
16+
XciBuilder buildDestination();
17+
}
18+
19+
}
20+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package com._4point.aem.fluentforms.impl;
2+
3+
import java.util.EnumMap;
4+
import java.util.Map;
5+
6+
import com._4point.aem.fluentforms.api.Document;
7+
import com._4point.aem.fluentforms.api.DocumentFactory;
8+
import com._4point.aem.fluentforms.api.Xci;
9+
10+
public class XciImpl implements Xci {
11+
private static final String XCI_FORMAT_STR = """
12+
<?xml version="1.0" encoding="UTF-8"?>
13+
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
14+
<config xmlns="http://www.xfa.org/schema/xci/1.0/"
15+
xmlns:xfa="http://www.xfa.org/schema/xci/1.0/">
16+
%s
17+
</config>
18+
</xdp:xdp>
19+
""";
20+
21+
private static final String XCI_DESTINATION_FORMAT_STR = """
22+
<destination>%s</destination> <!-- pdf|ps|pcl -->
23+
<%s> <!-- [0..n] -->
24+
<fontInfo>
25+
<embed>%d</embed> <!-- 0|1 -->
26+
</fontInfo>
27+
</%s>
28+
""";
29+
30+
private enum DestinationType {
31+
PDF("pdf"), PCL("pcl"), PS("ps");
32+
33+
private final String name;
34+
35+
DestinationType(String name) {
36+
this.name = name;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
}
43+
44+
private static record Destination(String name, boolean embedFonts) {
45+
public String toXml() {
46+
return destinationToXml(name, embedFonts);
47+
}
48+
49+
private static String destinationToXml(String destinationName, boolean embedFonts) {
50+
return XCI_DESTINATION_FORMAT_STR.formatted(destinationName, destinationName, embedFonts ? 1 : 0, destinationName);
51+
}
52+
};
53+
54+
private final Map<DestinationType, Destination> destinations;
55+
56+
private XciImpl(Map<DestinationType, Destination> destinations) {
57+
this.destinations = destinations;
58+
}
59+
60+
@Override
61+
public Document toDocument() {
62+
return toDocument(SimpleDocumentFactoryImpl.getFactory());
63+
}
64+
65+
@Override
66+
public Document toDocument(DocumentFactory factory) {
67+
// Implemented with simple String operations for now, eventually move to a proper XML builder.
68+
StringBuilder destSection = new StringBuilder();
69+
destinations.values().forEach(dest -> {
70+
destSection.append( "<present>" + dest.toXml() + "</present>");
71+
});
72+
var result = XCI_FORMAT_STR.formatted(destSection.toString());
73+
return factory.create(result.getBytes());
74+
}
75+
76+
public static class JavaxXciBuilder implements Xci.XciBuilder {
77+
private DestinationBuilderImpl pdfBuilder = null;
78+
private DestinationBuilderImpl pclBuilder = null;
79+
private DestinationBuilderImpl psBuilder = null;
80+
81+
@Override
82+
public DestinationBuilder pdf() {
83+
return this.pdfBuilder == null ? this.pdfBuilder = new DestinationBuilderImpl() : this.pdfBuilder;
84+
}
85+
86+
@Override
87+
public DestinationBuilder pcl() {
88+
return this.pclBuilder == null ? this.pclBuilder = new DestinationBuilderImpl() : this.pclBuilder;
89+
}
90+
91+
@Override
92+
public DestinationBuilder ps() {
93+
return this.psBuilder == null ? this.psBuilder = new DestinationBuilderImpl() : this.psBuilder;
94+
}
95+
96+
@Override
97+
public Xci build() {
98+
var destinations = new EnumMap<DestinationType, Destination>(DestinationType.class);
99+
if (pdfBuilder != null && pdfBuilder.embedFonts != null) {
100+
destinations.put(DestinationType.PDF, new Destination(DestinationType.PDF.getName(), pdfBuilder.embedFonts));
101+
}
102+
if (pclBuilder != null && pclBuilder.embedFonts != null) {
103+
destinations.put(DestinationType.PCL, new Destination(DestinationType.PCL.getName(), pclBuilder.embedFonts));
104+
}
105+
if (psBuilder != null && psBuilder.embedFonts != null) {
106+
destinations.put(DestinationType.PS, new Destination(DestinationType.PS.getName(), psBuilder.embedFonts));
107+
}
108+
return new XciImpl(destinations);
109+
}
110+
111+
private class DestinationBuilderImpl implements DestinationBuilder {
112+
113+
private Boolean embedFonts = null;
114+
115+
@Override
116+
public DestinationBuilder embedFonts(Boolean embedFonts) {
117+
this.embedFonts = embedFonts;
118+
return this;
119+
}
120+
121+
@Override
122+
public XciBuilder buildDestination() {
123+
return JavaxXciBuilder.this;
124+
}
125+
}
126+
}
127+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package com._4point.aem.fluentforms.impl;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.junit.jupiter.api.Assertions.*;
5+
6+
import java.io.IOException;
7+
8+
import org.junit.jupiter.api.Test;
9+
import org.xmlunit.matchers.CompareMatcher;
10+
11+
import com._4point.aem.fluentforms.api.Xci;
12+
13+
class XciImplTest {
14+
15+
private XciImpl.JavaxXciBuilder builder = new XciImpl.JavaxXciBuilder();
16+
17+
private static final String XCI_FORMAT_STR = """
18+
<?xml version="1.0" encoding="UTF-8"?>
19+
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
20+
<config xmlns="http://www.xfa.org/schema/xci/1.0/"
21+
xmlns:xfa="http://www.xfa.org/schema/xci/1.0/">
22+
%s
23+
</config>
24+
</xdp:xdp>
25+
""";
26+
27+
private static final String XCI_FORMAT_STR_NO_DESTINATIONS = """
28+
<?xml version="1.0" encoding="UTF-8"?>
29+
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
30+
<config xmlns="http://www.xfa.org/schema/xci/1.0/"
31+
xmlns:xfa="http://www.xfa.org/schema/xci/1.0/">
32+
<!-- No destinations defined -->
33+
</config>
34+
</xdp:xdp>
35+
""";
36+
37+
private static final String XCI_DESTINATION_FORMAT_STR = """
38+
<present> <!-- [0..n] -->
39+
<destination>%s</destination> <!-- pdf|ps|pcl -->
40+
<%s> <!-- [0..n] -->
41+
<fontInfo>
42+
<embed>%s</embed> <!-- 0|1 -->
43+
</fontInfo>
44+
</%s>
45+
</present>
46+
""";
47+
48+
private static void compare(Xci xci, String expectedXml) throws IOException {
49+
String xciDoc = new String(xci.toDocument().getInputStream().readAllBytes());
50+
assertThat(xciDoc, CompareMatcher.isIdenticalTo(expectedXml).ignoreWhitespace().ignoreComments());
51+
}
52+
53+
@Test
54+
void testToDocument_NoDestinations() throws Exception {
55+
Xci xci = builder.build();
56+
compare(xci, XCI_FORMAT_STR_NO_DESTINATIONS);
57+
}
58+
59+
@Test
60+
void testToDocument_Pdf() throws Exception {
61+
Xci xci = builder.pdf()
62+
.embedFonts(true)
63+
.buildDestination()
64+
.build();
65+
compare(xci, XCI_FORMAT_STR.formatted(pdfDestinationXml()));
66+
}
67+
68+
private static String pdfDestinationXml() {
69+
return XCI_DESTINATION_FORMAT_STR.formatted("pdf", "pdf", "1", "pdf");
70+
}
71+
72+
@Test
73+
void testToDocument_Pcl() throws Exception {
74+
Xci xci = builder.pcl()
75+
.embedFonts(true)
76+
.buildDestination()
77+
.build();
78+
compare(xci, XCI_FORMAT_STR.formatted(pclDestinationXml()));
79+
}
80+
81+
private static String pclDestinationXml() {
82+
return XCI_DESTINATION_FORMAT_STR.formatted("pcl", "pcl", "1", "pcl");
83+
}
84+
85+
@Test
86+
void testToDocument_Ps() throws Exception {
87+
Xci xci = builder.ps()
88+
.embedFonts(true)
89+
.buildDestination()
90+
.build();
91+
compare(xci, XCI_FORMAT_STR.formatted(psDestinationXml()));
92+
}
93+
94+
private static String psDestinationXml() {
95+
return XCI_DESTINATION_FORMAT_STR.formatted("ps", "ps", "1", "ps");
96+
}
97+
98+
// NOTE: To be honest, I'm not sure if the expected result is correct here, but since I don't have a valid XCI spec to refer to, I'm going with this.
99+
@Test
100+
void testToDocument_AllDestinations() throws Exception {
101+
Xci xci = builder.pdf()
102+
.embedFonts(true)
103+
.buildDestination()
104+
.pcl()
105+
.embedFonts(true)
106+
.buildDestination()
107+
.ps()
108+
.embedFonts(true)
109+
.buildDestination()
110+
.build();
111+
compare(xci, XCI_FORMAT_STR.formatted(pdfDestinationXml() + pclDestinationXml() + psDestinationXml()));
112+
}
113+
}

fluentforms/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<slf4j.version>2.0.17</slf4j.version>
9393
<hamcrest.version>3.0</hamcrest.version>
9494
<equalsverifier.version>3.17.5</equalsverifier.version>
95+
<xmlunit.version>2.10.2</xmlunit.version>
9596

9697
<!-- Project dependecies -->
9798
<aemfd.client.sdk.version>6.1.152</aemfd.client.sdk.version> <!-- fluentforms requires AEM 6.5 SP12 or later -->
@@ -568,6 +569,28 @@
568569
<version>${equalsverifier.version}</version>
569570
<scope>test</scope>
570571
</dependency>
572+
<dependency>
573+
<groupId>org.xmlunit</groupId>
574+
<artifactId>xmlunit-core</artifactId>
575+
<version>${xmlunit.version}</version>
576+
<scope>test</scope>
577+
</dependency>
578+
<dependency>
579+
<groupId>org.xmlunit</groupId>
580+
<artifactId>xmlunit-matchers</artifactId>
581+
<version>${xmlunit.version}</version>
582+
<scope>test</scope>
583+
<exclusions> <!-- Exclude older versions of hamcrest use the explicit one. -->
584+
<exclusion>
585+
<groupId>org.hamcrest</groupId>
586+
<artifactId>hamcrest-library</artifactId>
587+
</exclusion>
588+
<exclusion>
589+
<groupId>org.hamcrest</groupId>
590+
<artifactId>hamcrest-core</artifactId>
591+
</exclusion>
592+
</exclusions>
593+
</dependency>
571594
<!-- AEM Forms SDK -->
572595
<dependency>
573596
<groupId>com.adobe.aemfd</groupId>

0 commit comments

Comments
 (0)