|
| 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 | +} |
0 commit comments