Statistics
| Revision:

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

History | View | Annotate | Download (2.4 kB)

1 321 mdecorde
package org.txm.macroproto
2 321 mdecorde
3 321 mdecorde
// STANDARD DECLARATIONS
4 321 mdecorde
5 321 mdecorde
import org.kohsuke.args4j.*
6 321 mdecorde
import groovy.transform.Field
7 499 mdecorde
import org.txm.rcp.swt.widget.parameters.*
8 321 mdecorde
9 321 mdecorde
import org.txm.importer.*
10 966 mdecorde
import org.txm.metadatas.Metadatas
11 321 mdecorde
12 321 mdecorde
// BEGINNING OF PARAMETERS
13 321 mdecorde
14 321 mdecorde
// Declare each parameter here
15 321 mdecorde
// (available widget types: Query, File, Folder, String, Text, Boolean, Integer, Float and Date)
16 321 mdecorde
17 321 mdecorde
@Field @Option(name="xpathFile", usage="'.properties' file", widget="File", required=true, def="metadata.properties")
18 321 mdecorde
def xpathFile
19 321 mdecorde
20 321 mdecorde
@Field @Option(name="srcDirectory", usage="XML source directory", widget="Folder", required=false, def="srcDirectory")
21 321 mdecorde
def srcDirectory
22 321 mdecorde
23 321 mdecorde
@Field @Option(name="metadataFile", usage="'.properties' file", widget="File", required=false, def="metadata.csv")
24 321 mdecorde
def metadataFile
25 321 mdecorde
26 321 mdecorde
@Field @Option(name="columnSeparator", usage="", widget="String", required=false, def=",")
27 321 mdecorde
def columnSeparator
28 321 mdecorde
29 321 mdecorde
@Field @Option(name="txtSeparator", usage="", widget="String", required=false, def="\"")
30 321 mdecorde
def txtSeparator
31 321 mdecorde
32 321 mdecorde
// Open the parameters input dialog box
33 321 mdecorde
if (!ParametersDialog.open(this)) return;
34 321 mdecorde
35 321 mdecorde
// END OF PARAMETERS
36 321 mdecorde
37 321 mdecorde
if (!srcDirectory.exists()) { println "$srcDirectory does not exist"; return}
38 321 mdecorde
if (!xpathFile.exists()) { println "$xpathFile does not exist"; return}
39 321 mdecorde
40 321 mdecorde
// get Xpath queries
41 321 mdecorde
def xpaths = [:]
42 321 mdecorde
for (String line : xpathFile.readLines("UTF-8")) {
43 321 mdecorde
        def split = line.split("=", 2);
44 321 mdecorde
        if (split.size() == 2) {
45 321 mdecorde
                xpaths[split[0]] = split[1]
46 321 mdecorde
        }
47 321 mdecorde
}
48 321 mdecorde
println "XPATH queries: $xpaths"
49 321 mdecorde
50 321 mdecorde
if (metadataFile == null)
51 966 mdecorde
        metadataFile = Metadatas.findMetadataFile(srcDirectory)
52 321 mdecorde
53 321 mdecorde
def writer = metadataFile.newWriter("UTF-8")
54 321 mdecorde
println "Creating file: $metadataFile"
55 321 mdecorde
writer.print "id"
56 321 mdecorde
for (String p : xpaths.keySet()) {writer.print columnSeparator+p}
57 321 mdecorde
writer.println ""
58 321 mdecorde
59 321 mdecorde
def files = srcDirectory.listFiles()
60 321 mdecorde
if (files == null || files.size() == 0) {
61 321 mdecorde
        println "No files in $srcDirectory"
62 321 mdecorde
        return;
63 321 mdecorde
}
64 321 mdecorde
files.sort()
65 321 mdecorde
66 321 mdecorde
for (File xmlFile : files) {
67 321 mdecorde
        String name = xmlFile.getName();
68 321 mdecorde
        if (!name.endsWith(".xml")) continue;
69 321 mdecorde
70 321 mdecorde
        println "Processing file $xmlFile..."
71 321 mdecorde
        name = name.substring(0, name.indexOf(".xml"))
72 321 mdecorde
73 321 mdecorde
        writer.print "$name"
74 321 mdecorde
75 321 mdecorde
        XPathResult xpathProcessor = new XPathResult(xmlFile)
76 321 mdecorde
        for (String p : xpaths.keySet()) {
77 321 mdecorde
                def value = xpathProcessor.getXpathResponse(xpaths[p]);
78 321 mdecorde
                if (value == null) value = "N/A"
79 321 mdecorde
                writer.print columnSeparator+txtSeparator+value.replaceAll(txtSeparator,txtSeparator+txtSeparator)+txtSeparator
80 321 mdecorde
        }
81 321 mdecorde
        writer.println ""
82 321 mdecorde
}
83 321 mdecorde
84 321 mdecorde
writer.close()