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
package org.txm.macro.edition
2

    
3
import org.kohsuke.args4j.*
4
import groovy.transform.Field
5
import org.txm.rcp.swt.widget.parameters.*
6

    
7
try { println "ARGS=$args";} catch(Exception e) {args = [:]}
8

    
9
// BEGINNING OF PARAMETERS
10
@Field @Option(name="sourceDirectory", usage="Directory containig XML-TXM or XML SRC files", widget="Folder", required=true, def="src")
11
def sourceDirectory
12

    
13
@Field @Option(name="imageDirectory", usage="directory containing the ordered images files in subdirectories", widget="Folder", required=true, def="img")
14
def imageDirectory
15

    
16
@Field @Option(name="outputDirectory", usage="Output directory", widget="Folder", required=true, def="out")
17
def outputDirectory
18

    
19
@Field @Option(name="element", usage="The element to upgrade", widget="String", required=true, def="pb")
20
def element
21

    
22
@Field @Option(name="attribute", usage="The attribute to add", widget="String", required=true, def="facs")
23
def attribute
24

    
25
@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
def prefix
27

    
28
// Open the parameters input dialog box
29
if (args.size() == 0) {
30
        if (!ParametersDialog.open(this)) return;
31
} else {
32
        monitor = args["monitor"];
33
        sourceDirectory = args["sourceDirectory"]
34
        imageDirectory = args["imageDirectory"]
35
        outputDirectory = args["outputDirectory"]
36
        element = args["element"]
37
        attribute = args["attribute"]
38
        prefix = args["prefix"]
39
}
40
// END OF PARAMETERS
41

    
42
println "Parameters: "
43
println " sourceDirectory: $sourceDirectory"
44
println " imageDirectory: $imageDirectory"
45
println " outputDirectory: $outputDirectory"
46
println " element: $element"
47
println " attribute: $attribute"
48

    
49
assert(sourceDirectory.exists())
50
assert(imageDirectory.exists())
51

    
52
outputDirectory.deleteDir()
53
outputDirectory.mkdir()
54

    
55
if (!outputDirectory.exists()) {
56
        println "Error: failed to create $outputDirectory"
57
        return;
58
}
59

    
60
def srcFiles = []
61
sourceDirectory.eachFile() { file ->
62
        if (!file.isDirectory() && !file.isHidden() && file.getName().endsWith(".xml") && file.getName() != "import.xml") srcFiles << file
63
}
64
println "srcFiles=$srcFiles"
65

    
66
for (def subdir : imageDirectory.listFiles()) {
67
        if (!subdir.isDirectory()) continue;
68
        
69
        String name = subdir.getName();
70
        File srcFile = new File(sourceDirectory, name+".xml")
71
        if (!srcFile.exists()) { println "Warning: missing source file: $srcFile"; continue }
72
        
73
        def imgFiles = subdir.listFiles().sort()
74
        if (imgFiles.size() == 0) { println "Warning: no image in $subdir"; continue }
75
        
76
        def imgPaths = []
77
        for (def img : imgFiles) {
78
                if (img.isFile() && !img.isHidden()) {
79
                        if (prefix != null && prefix.length() > 0)
80
                                imgPaths << prefix+name+"/"+img.getName()
81
                        else
82
                                imgPaths << img.getAbsolutePath()
83
                }
84
        }
85
        
86
        println "Processing '$name' directory with "+imgPaths.size()+" images."
87
        
88
        AddAttributeValuesInXML builder = new AddAttributeValuesInXML(srcFile, element, attribute, imgPaths);
89
        builder.process(new File(outputDirectory, srcFile.getName()));
90
}