|
| 1 | +package org.verapdf.tools; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import org.apache.commons.cli.Option; |
| 11 | +import org.apache.commons.cli.Options; |
| 12 | +import org.apache.pdfbox.cos.COSArray; |
| 13 | +import org.apache.pdfbox.cos.COSBase; |
| 14 | +import org.apache.pdfbox.cos.COSDictionary; |
| 15 | +import org.apache.pdfbox.pdmodel.PDDocument; |
| 16 | +import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureElement; |
| 17 | +import org.apache.pdfbox.pdmodel.documentinterchange.logicalstructure.PDStructureTreeRoot; |
| 18 | +import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline; |
| 19 | +import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem; |
| 20 | +import org.verapdf.pdfa.flavours.PDFAFlavour; |
| 21 | + |
| 22 | +public enum OptionFeature { |
| 23 | + UA2_PRESET("ua2_preset", "ua2", "Default preset for pdf-ua2 migration", false, false, FeatureTag.PRESET) { |
| 24 | + @Override |
| 25 | + public void preset(TestMigration testMigration, String argument) { |
| 26 | + Features features = testMigration.getFeatures(); |
| 27 | + features.isPreset = true; |
| 28 | + |
| 29 | + features.modifyFeature(TARGET_FLAVOUR, "ua2"); |
| 30 | + features.modifyFeature(FIX_DEPRECATED, ""); |
| 31 | + features.modifyFeature(FIX_METADATA, ""); |
| 32 | + features.modifyFeature(SET_VERSION, "2.0"); |
| 33 | + features.modifyFeature(REMOVE_OPENACTION, ""); |
| 34 | + features.modifyFeature(ADD_NAMESPACE, "http://iso.org/pdf2/ssn"); |
| 35 | + } |
| 36 | + }, |
| 37 | + INPUT_FOLDER("parent_input_folder", "pif", "<arg> - path to parent input folder", true, false, |
| 38 | + FeatureTag.PRE_PROCESS) { |
| 39 | + @Override |
| 40 | + public void preFeature(TestMigration testMigration, String argument) { |
| 41 | + if (argument.endsWith("/")) { |
| 42 | + testMigration.inputParentFolderPath = argument; |
| 43 | + } else { |
| 44 | + testMigration.inputParentFolderPath = argument + "/"; |
| 45 | + } |
| 46 | + } |
| 47 | + }, |
| 48 | + TARGET_FOLDER("parent_target_folder", "ptf", "<arg> - path to parent target folder", true, false, |
| 49 | + FeatureTag.PRE_PROCESS) { |
| 50 | + @Override |
| 51 | + public void preFeature(TestMigration testMigration, String argument) { |
| 52 | + if (argument.endsWith("/")) { |
| 53 | + testMigration.targetParentFolderPath = argument; |
| 54 | + } else { |
| 55 | + testMigration.targetParentFolderPath = argument + "/"; |
| 56 | + } |
| 57 | + } |
| 58 | + }, |
| 59 | + CSV_FILE("csv_file", "csv", |
| 60 | + "<arg> - .csv file with <INPUT_FOLDER>;<INPUT_VERSION>;<OUTPUT_FOLDER>;<OUTPUT_VERSION>;<OUTLINE_HEADER> format", |
| 61 | + true, false, FeatureTag.PRE_PROCESS) { |
| 62 | + @Override |
| 63 | + public void preFeature(TestMigration testMigration, String argument) { |
| 64 | + testMigration.setCSVFile(argument); |
| 65 | + } |
| 66 | + }, |
| 67 | + TARGET_FLAVOUR("flavour", "f", "Sets target flavour to <arg>", true, false, FeatureTag.PRE_PROCESS) { |
| 68 | + @Override |
| 69 | + public void preFeature(TestMigration testMigration, String argument) { |
| 70 | + testMigration.targetFlavour = PDFAFlavour.byFlavourId(argument); |
| 71 | + } |
| 72 | + }, |
| 73 | + SET_OUTLINEHEADER("outline_header", "oh", "Replaces first outline with <arg>", true, false, FeatureTag.PROCESS) { |
| 74 | + @Override |
| 75 | + public void feature(PDDocument document, String argument) throws Exception { |
| 76 | + if (argument == null || argument.length() == 0) { |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + ArrayList<String> oldOutlines = new ArrayList<String>(); |
| 81 | + Map<String, List<String>> childs = new HashMap<String, List<String>>(); |
| 82 | + PDOutlineItem currentOutlineItem = document.getDocumentCatalog().getDocumentOutline().getFirstChild(); |
| 83 | + |
| 84 | + currentOutlineItem = currentOutlineItem.getNextSibling(); |
| 85 | + while (currentOutlineItem != null) { |
| 86 | + oldOutlines.add(currentOutlineItem.getTitle()); |
| 87 | + PDOutlineItem childOutline = currentOutlineItem.getFirstChild(); |
| 88 | + |
| 89 | + List<String> subChilds = new ArrayList<String>(); |
| 90 | + |
| 91 | + while (childOutline != null) { |
| 92 | + subChilds.add(childOutline.getTitle()); |
| 93 | + childOutline = childOutline.getNextSibling(); |
| 94 | + } |
| 95 | + |
| 96 | + childs.put(currentOutlineItem.getTitle(), subChilds); |
| 97 | + currentOutlineItem = currentOutlineItem.getNextSibling(); |
| 98 | + } |
| 99 | + |
| 100 | + PDDocumentOutline outlines = new PDDocumentOutline(); |
| 101 | + currentOutlineItem = new PDOutlineItem(); |
| 102 | + currentOutlineItem.setTitle(argument); |
| 103 | + |
| 104 | + outlines.addFirst(currentOutlineItem); |
| 105 | + |
| 106 | + for (String line : oldOutlines) { |
| 107 | + PDOutlineItem newOutline = new PDOutlineItem(); |
| 108 | + newOutline.setTitle(line); |
| 109 | + |
| 110 | + if (childs.get(line).size() > 0) { |
| 111 | + for (String subChild : childs.get(line)) { |
| 112 | + PDOutlineItem subChildOutline = new PDOutlineItem(); |
| 113 | + subChildOutline.setTitle(subChild); |
| 114 | + |
| 115 | + newOutline.addFirst(subChildOutline); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + currentOutlineItem.insertSiblingAfter(newOutline); |
| 120 | + currentOutlineItem = newOutline; |
| 121 | + } |
| 122 | + |
| 123 | + document.getDocumentCatalog().setDocumentOutline(outlines); |
| 124 | + } |
| 125 | + }, |
| 126 | + SET_LANGUAGE("language", "l", "Set pdf language to <arg>", true, false, FeatureTag.PROCESS) { |
| 127 | + @Override |
| 128 | + public void feature(PDDocument document, String argument) throws Exception { |
| 129 | + if (argument == null) { |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + document.getDocumentCatalog().setLanguage(argument); |
| 134 | + } |
| 135 | + }, |
| 136 | + SET_VERSION("version", "v", "Set pdf version to <arg>", true, false, FeatureTag.PROCESS) { |
| 137 | + @Override |
| 138 | + public void feature(PDDocument document, String argument) throws Exception { |
| 139 | + if (argument == null) { |
| 140 | + return; |
| 141 | + } |
| 142 | + |
| 143 | + try { |
| 144 | + Float version = Float.parseFloat(argument); |
| 145 | + |
| 146 | + document.getDocument().setVersion(version); |
| 147 | + } catch (NumberFormatException exception) { |
| 148 | + return; |
| 149 | + } |
| 150 | + } |
| 151 | + }, |
| 152 | + REMOVE_OPENACTION("remove_openaction", "ro", "Removes open action from document catalog", false, false, |
| 153 | + FeatureTag.PROCESS) { |
| 154 | + @Override |
| 155 | + public void feature(PDDocument document, String argument) throws Exception { |
| 156 | + document.getDocumentCatalog().setOpenAction(null); |
| 157 | + } |
| 158 | + }, |
| 159 | + ADD_NAMESPACE("namespace", "ns", "Set <arg> namespace for document", true, false, FeatureTag.PROCESS) { |
| 160 | + @Override |
| 161 | + public void feature(PDDocument document, String argument) throws Exception { |
| 162 | + if (argument == null || argument.length() == 0) { |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + PDStructureTreeRoot root = document.getDocumentCatalog().getStructureTreeRoot(); |
| 167 | + |
| 168 | + if (root != null) { |
| 169 | + COSArray namespaces = new COSArray(); |
| 170 | + COSDictionary NS = new COSDictionary(); |
| 171 | + |
| 172 | + NS.setName("Type", "Namespace"); |
| 173 | + NS.setString("NS", "http://iso.org/pdf2/ssn"); |
| 174 | + |
| 175 | + namespaces.add(NS); |
| 176 | + root.getCOSObject().setItem("Namespaces", namespaces); |
| 177 | + |
| 178 | + COSBase base = root.getK(); |
| 179 | + if (base instanceof COSDictionary) { |
| 180 | + COSDictionary dictionary = (COSDictionary) base; |
| 181 | + |
| 182 | + dictionary.setItem("NS", NS); |
| 183 | + |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + root.getKids().forEach((child) -> { |
| 188 | + if (child instanceof PDStructureElement) { |
| 189 | + COSDictionary dictionary = ((PDStructureElement) child).getCOSObject(); |
| 190 | + |
| 191 | + dictionary.setItem("NS", NS); |
| 192 | + } |
| 193 | + }); |
| 194 | + } |
| 195 | + } |
| 196 | + }, |
| 197 | + FIX_DEPRECATED("fix_deprecated", "fd", "Fixes deprecated files", false, false, FeatureTag.PRE_PROCESS, FeatureTag.POST_PROCESS) { |
| 198 | + @Override |
| 199 | + public void preFeature(TestMigration testMigration, String argument) { |
| 200 | + testMigration.getTempDir().toFile().mkdirs(); |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public void postFeature(TestMigration testMigration, String targetFilePath, String argument) throws Exception { |
| 205 | + String[] args = { targetFilePath }; |
| 206 | + |
| 207 | + DeprecatedFinderCli.main(args); |
| 208 | + |
| 209 | + File originalFile = new File(targetFilePath); |
| 210 | + File fixedFile = testMigration.getTempDir().resolve("fix_" + originalFile.getName()).toFile(); |
| 211 | + |
| 212 | + if (!fixedFile.exists()) { |
| 213 | + return; |
| 214 | + } |
| 215 | + |
| 216 | + originalFile.delete(); |
| 217 | + fixedFile.renameTo(new File(targetFilePath)); |
| 218 | + } |
| 219 | + }, |
| 220 | + FIX_METADATA("fix_metadata", "fm", "Fixes metadata", false, false, FeatureTag.PRE_PROCESS, FeatureTag.POST_PROCESS) { |
| 221 | + @Override |
| 222 | + public void preFeature(TestMigration testMigration, String argument) { |
| 223 | + testMigration.getTempDir().toFile().mkdirs(); |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public void postFeature(TestMigration testMigration, String targetFilePath, String argument) throws Exception { |
| 228 | + File originalFile = new File(targetFilePath); |
| 229 | + File temp = testMigration.getTempDir().resolve(originalFile.getName()).toFile(); |
| 230 | + originalFile.renameTo(temp); |
| 231 | + |
| 232 | + String[] args = { temp.getAbsolutePath(), targetFilePath, testMigration.targetFlavour.toString() }; |
| 233 | + FixMetadataTool.main(args); |
| 234 | + |
| 235 | + temp.delete(); |
| 236 | + } |
| 237 | + }; |
| 238 | + |
| 239 | + private final String option; |
| 240 | + private final String short_form; |
| 241 | + private final String description; |
| 242 | + private final Boolean hasArgs; |
| 243 | + private final Boolean required; |
| 244 | + private final Integer featureTags; |
| 245 | + |
| 246 | + public void preset(TestMigration testMigration, String argument) { |
| 247 | + |
| 248 | + } |
| 249 | + |
| 250 | + public void preFeature(TestMigration testMigration, String argument) { |
| 251 | + |
| 252 | + } |
| 253 | + |
| 254 | + public void feature(PDDocument document, String argument) throws Exception { |
| 255 | + |
| 256 | + } |
| 257 | + |
| 258 | + public void postFeature(TestMigration testMigration, String targetFilePath, String argument) throws Exception { |
| 259 | + |
| 260 | + } |
| 261 | + |
| 262 | + private OptionFeature(String option, String short_form, String description, Boolean hasArgs, Boolean required, FeatureTag... tags) { |
| 263 | + this.option = option; |
| 264 | + this.short_form = short_form; |
| 265 | + this.description = description; |
| 266 | + this.hasArgs = hasArgs; |
| 267 | + this.required = required; |
| 268 | + |
| 269 | + this.featureTags = FeatureTag.getInteger(tags); |
| 270 | + } |
| 271 | + |
| 272 | + public Boolean isRequired() { |
| 273 | + return required; |
| 274 | + } |
| 275 | + |
| 276 | + public String getShortOption() { |
| 277 | + return short_form; |
| 278 | + } |
| 279 | + |
| 280 | + public Boolean hasTag(FeatureTag tag) { |
| 281 | + return tag.checkTag(featureTags); |
| 282 | + } |
| 283 | + |
| 284 | + public static OptionFeature fromOption(String shortFormOption) { |
| 285 | + return features.getOrDefault(shortFormOption, null); |
| 286 | + } |
| 287 | + |
| 288 | + public static List<OptionFeature> getOptionsByTag(FeatureTag tag) { |
| 289 | + return featuresByTag.getOrDefault(tag, Arrays.asList()); |
| 290 | + } |
| 291 | + |
| 292 | + private static Map<String, OptionFeature> features = new HashMap<String, OptionFeature>(); |
| 293 | + private static Map<FeatureTag, List<OptionFeature>> featuresByTag = new HashMap<FeatureTag, List<OptionFeature>>(); |
| 294 | + static { |
| 295 | + for (FeatureTag tag : FeatureTag.values()) { |
| 296 | + featuresByTag.put(tag, new ArrayList<OptionFeature>()); |
| 297 | + } |
| 298 | + |
| 299 | + for (OptionFeature optionFeature : OptionFeature.values()) { |
| 300 | + features.put(optionFeature.short_form, optionFeature); |
| 301 | + |
| 302 | + for (FeatureTag tag : FeatureTag.fromInteger(optionFeature.featureTags)) { |
| 303 | + featuresByTag.get(tag).add(optionFeature); |
| 304 | + } |
| 305 | + } |
| 306 | + } |
| 307 | + |
| 308 | + public static Options generateOptions() { |
| 309 | + Options options = new Options(); |
| 310 | + |
| 311 | + for (OptionFeature optionFeature : OptionFeature.values()) { |
| 312 | + Option option = new Option(optionFeature.short_form, optionFeature.option, optionFeature.hasArgs, |
| 313 | + optionFeature.description); |
| 314 | + option.setRequired(optionFeature.required); |
| 315 | + |
| 316 | + options.addOption(option); |
| 317 | + } |
| 318 | + |
| 319 | + return options; |
| 320 | + } |
| 321 | +} |
0 commit comments