Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / macro / csv / CSV2XMLMacro.groovy @ 3025

History | View | Annotate | Download (3.3 kB)

1 321 mdecorde
package org.txm.macro.csv
2 321 mdecorde
3 321 mdecorde
import org.kohsuke.args4j.*
4 321 mdecorde
import groovy.transform.Field
5 321 mdecorde
import java.nio.charset.Charset
6 499 mdecorde
import org.txm.rcp.swt.widget.parameters.*
7 321 mdecorde
import org.txm.utils.*
8 321 mdecorde
import javax.xml.stream.*
9 321 mdecorde
import java.net.URL
10 321 mdecorde
11 321 mdecorde
@Field @Option(name="inputFile", usage="CSV File", widget="File", required=false, def="file.csv")
12 321 mdecorde
File inputFile;
13 321 mdecorde
14 321 mdecorde
@Field @Option(name="columnSeparator", usage="column columnSeparator", widget="String", required=false, def="\t")
15 321 mdecorde
def columnSeparator;
16 321 mdecorde
17 321 mdecorde
@Field @Option(name="characterEncoding", usage="File characterEncoding", widget="String", required=false, def="UTF-8")
18 321 mdecorde
def characterEncoding;
19 321 mdecorde
20 321 mdecorde
@Field @Option(name="rootTag", usage="Root tag name", widget="String", required=false, def="root")
21 321 mdecorde
def rootTag;
22 321 mdecorde
23 321 mdecorde
@Field @Option(name="textTag", usage="Text tag name", widget="String", required=false, def="unit")
24 321 mdecorde
def textTag;
25 321 mdecorde
26 321 mdecorde
@Field @Option(name="metadataColumnList", usage="metadataColumnList column list separated with comma", widget="String", required=false, def="meta1,meta2")
27 321 mdecorde
def metadataColumnList;
28 321 mdecorde
29 321 mdecorde
@Field @Option(name="textColumnList", usage="textColumnList column list separated with comma", widget="String", required=false, def="textColumnList1,textColumnList2")
30 321 mdecorde
def textColumnList;
31 321 mdecorde
32 321 mdecorde
if (!ParametersDialog.open(this)) return;
33 321 mdecorde
34 321 mdecorde
metadataColumnList = metadataColumnList.split(",")
35 321 mdecorde
textColumnList = textColumnList.split(",")
36 321 mdecorde
textTag = textTag.trim()
37 321 mdecorde
rootTag = rootTag.trim()
38 321 mdecorde
39 321 mdecorde
40 321 mdecorde
41 321 mdecorde
CsvReader reader = new CsvReader(inputFile.getAbsolutePath(), columnSeparator.charAt(0), Charset.forName(characterEncoding));
42 321 mdecorde
if (!reader.readHeaders()) {
43 321 mdecorde
        println "Error: no header"
44 321 mdecorde
        return
45 321 mdecorde
}
46 321 mdecorde
def headers = Arrays.asList(reader.getHeaders())
47 321 mdecorde
48 321 mdecorde
println "root tag: $rootTag"
49 321 mdecorde
println "textColumnList tag: $textTag"
50 321 mdecorde
println "columns: $headers"
51 321 mdecorde
println "metadataColumnList columns: $metadataColumnList"
52 321 mdecorde
println "textColumnList columns: $textColumnList"
53 321 mdecorde
54 321 mdecorde
def ok = true
55 321 mdecorde
for (String m : metadataColumnList) {
56 321 mdecorde
        m = m.trim()
57 321 mdecorde
        if (!headers.contains(m)) {
58 321 mdecorde
                println "Error: missing metadataColumnList column: $m"
59 321 mdecorde
                ok = false
60 321 mdecorde
        }
61 321 mdecorde
}
62 321 mdecorde
for (String t : textColumnList) {
63 321 mdecorde
        t = t.trim()
64 321 mdecorde
        if (!headers.contains(t)) {
65 321 mdecorde
                println "Error: missing textColumnList column: $t"
66 321 mdecorde
        }
67 321 mdecorde
}
68 321 mdecorde
69 321 mdecorde
if (!ok) { return; }
70 321 mdecorde
71 321 mdecorde
String name = inputFile.getName()
72 479 mdecorde
int idx = name.lastIndexOf(".")
73 321 mdecorde
if (idx > 0) name = name.substring(0, idx)
74 321 mdecorde
File outputfile = new File(inputFile.getParentFile(), name+".xml")
75 321 mdecorde
XMLOutputFactory factory = XMLOutputFactory.newInstance()
76 321 mdecorde
FileOutputStream output = new FileOutputStream(outputfile)
77 321 mdecorde
XMLStreamWriter writer = factory.createXMLStreamWriter(output, "UTF-8")
78 321 mdecorde
79 321 mdecorde
writer.writeStartDocument("UTF-8","1.0")
80 321 mdecorde
writer.writeCharacters("\n") // simple XML formating
81 321 mdecorde
writer.writeStartElement(rootTag)
82 321 mdecorde
83 321 mdecorde
int pb_n = 1;
84 321 mdecorde
while (reader.readRecord()) {
85 321 mdecorde
86 321 mdecorde
        writer.writeCharacters("\n") // simple XML formating
87 321 mdecorde
88 321 mdecorde
        writer.writeEmptyElement("pb") // <pb/>
89 321 mdecorde
        writer.writeAttribute("n", ""+pb_n++)
90 321 mdecorde
91 321 mdecorde
        writer.writeCharacters("\n") // simple XML formating
92 321 mdecorde
93 321 mdecorde
        writer.writeStartElement(textTag)
94 321 mdecorde
95 321 mdecorde
        for (String m : metadataColumnList) {
96 321 mdecorde
                m = m.trim()
97 321 mdecorde
                writer.writeAttribute(m, reader.get(m))
98 321 mdecorde
        }
99 321 mdecorde
100 321 mdecorde
        for (String t : textColumnList) {
101 321 mdecorde
                t = t.trim()
102 321 mdecorde
                writer.writeStartElement(t)
103 321 mdecorde
                writer.writeCharacters(reader.get(t)) // get textColumnList content
104 321 mdecorde
                writer.writeEndElement() // t
105 321 mdecorde
        }
106 321 mdecorde
107 321 mdecorde
        writer.writeEndElement() // textTag
108 321 mdecorde
}
109 321 mdecorde
110 321 mdecorde
writer.writeEndElement() // rootTag
111 321 mdecorde
writer.close()
112 321 mdecorde
output.close()
113 321 mdecorde
reader.close()