Statistics
| Revision:

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

History | View | Annotate | Download (2.4 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
import org.txm.metadatas.Metadatas
11

    
12
// BEGINNING OF PARAMETERS
13

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

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

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

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

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

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

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

    
35
// END OF PARAMETERS
36

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

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

    
50
if (metadataFile == null)
51
        metadataFile = Metadatas.findMetadataFile(srcDirectory)
52

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

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

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

    
84
writer.close()