Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / xml / ExecXSLMacro.groovy @ 479

History | View | Annotate | Download (2 kB)

1
package org.txm.macro.xml;
2
// STANDARD DECLARATIONS
3

    
4
import org.kohsuke.args4j.*
5
import groovy.transform.Field
6
import org.txm.rcpapplication.swt.widget.parameters.*
7
import org.txm.importer.ApplyXsl2;
8

    
9
// BEGINNING OF PARAMETERS
10
@Field @Option(name="XSLFile", usage="an example file", widget="File", required=true, def="file.xsl")
11
def XSLFile = new File(System.getProperty("user.home"),"TXM/xsl/identity.xsl")
12

    
13
@Field @Option(name="intputDirectory", usage="an example folder", widget="Folder", required=true, def="in")
14
def intputDirectory = new File(System.getProperty("user.home"),"xml/TESTS2/xml")
15

    
16
@Field @Option(name="outputDirectory", usage="an example folder", widget="Folder", required=true, def="out")
17
def outputDirectory = new File("/tmp/xsltest")
18

    
19
//@Field @Option(name="parameters", usage="an example folder", widget="Text", required=false, def="")
20
def parameters = ""
21

    
22
@Field @Option(name="debug", usage="Show debug messages, value = true|false", widget="String", required=true, def="false")
23
def debug
24

    
25
if (!ParametersDialog.open(this)) return;
26
// END OF PARAMETERS
27

    
28
debug = "true" == debug
29

    
30
outputDirectory.mkdir()
31

    
32
parameters = parameters.trim()
33
if (parameters.length() == 0) {
34
        parameters = null
35
} else {
36
        def split = parameters.split("\n")
37
        parameters = []
38
        for (def str : split) {
39
                def split2 = str.split("=", 2)
40
                if (split2.size() == 2) {
41
                        parameters << split2
42
                }
43
        }
44
}
45

    
46
println "Use XSL $XSLFile with parameters $parameters"
47
println "Processed directory: $intputDirectory"
48

    
49
def files = [] 
50
ApplyXsl2 a = new ApplyXsl2(XSLFile.getAbsolutePath());
51
intputDirectory.eachFileMatch(~/.+\.(xml|XML)/) { XMLFile ->
52
        String name = XMLFile.getName()
53
        try {
54
                
55
                def outFile = new File(outputDirectory, name)
56
                if (parameters != null) {
57
                        for(def param : parameters) {
58
                                a.setParam(param[0], param[1])
59
                        }
60
                }
61
                a.process(XMLFile, outFile);
62
                a.resetParams()
63
                files << XMLFile
64
        } catch (Exception e) {
65
                println "Warning: XSL transformation of '$name' failed with error=$e with "
66
                if (debug) e.printStackTrace(); 
67
        }
68
}