Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macroproto / XMLText2MetadataCSVMacro.groovy @ 499

History | View | Annotate | Download (2.3 kB)

1
package org.txm.macroproto
2

    
3
// STANDARD DECLARATIONS
4

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

    
9
import org.txm.importer.*
10

    
11
// BEGINNING OF PARAMETERS
12

    
13
// Declare each parameter here
14
// (available widget types: Query, File, Folder, String, Text, Boolean, Integer, Float and Date)
15

    
16
@Field @Option(name="xpathFile", usage="'.properties' file", widget="File", required=true, def="metadata.properties")
17
def xpathFile
18

    
19
@Field @Option(name="srcDirectory", usage="XML source directory", widget="Folder", required=false, def="srcDirectory")
20
def srcDirectory
21

    
22
@Field @Option(name="metadataFile", usage="'.properties' file", widget="File", required=false, def="metadata.csv")
23
def metadataFile
24

    
25
@Field @Option(name="columnSeparator", usage="", widget="String", required=false, def=",")
26
def columnSeparator
27

    
28
@Field @Option(name="txtSeparator", usage="", widget="String", required=false, def="\"")
29
def txtSeparator
30

    
31
// Open the parameters input dialog box
32
if (!ParametersDialog.open(this)) return;
33

    
34
// END OF PARAMETERS
35

    
36
if (!srcDirectory.exists()) { println "$srcDirectory does not exist"; return}
37
if (!xpathFile.exists()) { println "$xpathFile does not exist"; return}
38

    
39
// get Xpath queries
40
def xpaths = [:]
41
for (String line : xpathFile.readLines("UTF-8")) {
42
        def split = line.split("=", 2);
43
        if (split.size() == 2) {
44
                xpaths[split[0]] = split[1]
45
        }
46
}
47
println "XPATH queries: $xpaths"
48

    
49
if (metadataFile == null)
50
        metadataFile = new File(srcDirectory, "metadata.csv")
51

    
52
def writer = metadataFile.newWriter("UTF-8")
53
println "Creating file: $metadataFile"
54
writer.print "id"
55
for (String p : xpaths.keySet()) {writer.print columnSeparator+p}
56
writer.println ""
57

    
58
def files = srcDirectory.listFiles()
59
if (files == null || files.size() == 0) {
60
        println "No files in $srcDirectory"
61
        return;
62
}
63
files.sort()
64

    
65
for (File xmlFile : files) {
66
        String name = xmlFile.getName();
67
        if (!name.endsWith(".xml")) continue;
68
        
69
        println "Processing file $xmlFile..."
70
        name = name.substring(0, name.indexOf(".xml"))
71
        
72
        writer.print "$name"
73
        
74
        XPathResult xpathProcessor = new XPathResult(xmlFile)
75
        for (String p : xpaths.keySet()) {
76
                def value = xpathProcessor.getXpathResponse(xpaths[p]);
77
                if (value == null) value = "N/A"
78
                writer.print columnSeparator+txtSeparator+value.replaceAll(txtSeparator,txtSeparator+txtSeparator)+txtSeparator
79
        }
80
        writer.println ""
81
}
82

    
83
writer.close()