Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / edition / AddFacsAttributeMacro.groovy @ 499

History | View | Annotate | Download (2.9 kB)

1 321 mdecorde
package org.txm.macro.edition
2 321 mdecorde
3 321 mdecorde
import org.kohsuke.args4j.*
4 321 mdecorde
import groovy.transform.Field
5 499 mdecorde
import org.txm.rcp.swt.widget.parameters.*
6 321 mdecorde
7 321 mdecorde
try { println "ARGS=$args";} catch(Exception e) {args = [:]}
8 321 mdecorde
9 321 mdecorde
// BEGINNING OF PARAMETERS
10 321 mdecorde
@Field @Option(name="sourceDirectory", usage="Directory containig XML-TXM or XML SRC files", widget="Folder", required=true, def="src")
11 321 mdecorde
def sourceDirectory
12 321 mdecorde
13 321 mdecorde
@Field @Option(name="imageDirectory", usage="directory containing the ordered images files in subdirectories", widget="Folder", required=true, def="img")
14 321 mdecorde
def imageDirectory
15 321 mdecorde
16 321 mdecorde
@Field @Option(name="outputDirectory", usage="Output directory", widget="Folder", required=true, def="out")
17 321 mdecorde
def outputDirectory
18 321 mdecorde
19 321 mdecorde
@Field @Option(name="element", usage="The element to upgrade", widget="String", required=true, def="pb")
20 321 mdecorde
def element
21 321 mdecorde
22 321 mdecorde
@Field @Option(name="attribute", usage="The attribute to add", widget="String", required=true, def="facs")
23 321 mdecorde
def attribute
24 321 mdecorde
25 321 mdecorde
@Field @Option(name="prefix", usage="The image path prefix, if empty then the absolute file path is used", widget="String", required=true, def="../img/")
26 321 mdecorde
def prefix
27 321 mdecorde
28 321 mdecorde
// Open the parameters input dialog box
29 321 mdecorde
if (args.size() == 0) {
30 321 mdecorde
        if (!ParametersDialog.open(this)) return;
31 321 mdecorde
} else {
32 321 mdecorde
        monitor = args["monitor"];
33 321 mdecorde
        sourceDirectory = args["sourceDirectory"]
34 321 mdecorde
        imageDirectory = args["imageDirectory"]
35 321 mdecorde
        outputDirectory = args["outputDirectory"]
36 321 mdecorde
        element = args["element"]
37 321 mdecorde
        attribute = args["attribute"]
38 321 mdecorde
        prefix = args["prefix"]
39 321 mdecorde
}
40 321 mdecorde
// END OF PARAMETERS
41 321 mdecorde
42 321 mdecorde
println "Parameters: "
43 321 mdecorde
println " sourceDirectory: $sourceDirectory"
44 321 mdecorde
println " imageDirectory: $imageDirectory"
45 321 mdecorde
println " outputDirectory: $outputDirectory"
46 321 mdecorde
println " element: $element"
47 321 mdecorde
println " attribute: $attribute"
48 321 mdecorde
49 321 mdecorde
assert(sourceDirectory.exists())
50 321 mdecorde
assert(imageDirectory.exists())
51 321 mdecorde
52 321 mdecorde
outputDirectory.deleteDir()
53 321 mdecorde
outputDirectory.mkdir()
54 321 mdecorde
55 321 mdecorde
if (!outputDirectory.exists()) {
56 321 mdecorde
        println "Error: failed to create $outputDirectory"
57 321 mdecorde
        return;
58 321 mdecorde
}
59 321 mdecorde
60 321 mdecorde
def srcFiles = []
61 321 mdecorde
sourceDirectory.eachFile() { file ->
62 321 mdecorde
        if (!file.isDirectory() && !file.isHidden() && file.getName().endsWith(".xml") && file.getName() != "import.xml") srcFiles << file
63 321 mdecorde
}
64 321 mdecorde
println "srcFiles=$srcFiles"
65 321 mdecorde
66 321 mdecorde
for (def subdir : imageDirectory.listFiles()) {
67 321 mdecorde
        if (!subdir.isDirectory()) continue;
68 321 mdecorde
69 321 mdecorde
        String name = subdir.getName();
70 321 mdecorde
        File srcFile = new File(sourceDirectory, name+".xml")
71 321 mdecorde
        if (!srcFile.exists()) { println "Warning: missing source file: $srcFile"; continue }
72 321 mdecorde
73 321 mdecorde
        def imgFiles = subdir.listFiles().sort()
74 321 mdecorde
        if (imgFiles.size() == 0) { println "Warning: no image in $subdir"; continue }
75 321 mdecorde
76 321 mdecorde
        def imgPaths = []
77 321 mdecorde
        for (def img : imgFiles) {
78 321 mdecorde
                if (img.isFile() && !img.isHidden()) {
79 321 mdecorde
                        if (prefix != null && prefix.length() > 0)
80 321 mdecorde
                                imgPaths << prefix+name+"/"+img.getName()
81 321 mdecorde
                        else
82 321 mdecorde
                                imgPaths << img.getAbsolutePath()
83 321 mdecorde
                }
84 321 mdecorde
        }
85 321 mdecorde
86 321 mdecorde
        println "Processing '$name' directory with "+imgPaths.size()+" images."
87 321 mdecorde
88 321 mdecorde
        AddAttributeValuesInXML builder = new AddAttributeValuesInXML(srcFile, element, attribute, imgPaths);
89 321 mdecorde
        builder.process(new File(outputDirectory, srcFile.getName()));
90 321 mdecorde
}