Statistics
| Revision:

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

History | View | Annotate | Download (3.3 kB)

1
package org.txm.macro.csv
2

    
3
import org.kohsuke.args4j.*
4
import groovy.transform.Field
5
import java.nio.charset.Charset
6
import org.txm.rcpapplication.swt.widget.parameters.*
7
import org.txm.utils.*
8
import javax.xml.stream.*
9
import java.net.URL
10

    
11
@Field @Option(name="inputFile", usage="CSV File", widget="File", required=false, def="file.csv")
12
File inputFile;
13

    
14
@Field @Option(name="columnSeparator", usage="column columnSeparator", widget="String", required=false, def="\t")
15
def columnSeparator;
16

    
17
@Field @Option(name="characterEncoding", usage="File characterEncoding", widget="String", required=false, def="UTF-8")
18
def characterEncoding;
19

    
20
@Field @Option(name="rootTag", usage="Root tag name", widget="String", required=false, def="root")
21
def rootTag;
22

    
23
@Field @Option(name="textTag", usage="Text tag name", widget="String", required=false, def="unit")
24
def textTag;
25

    
26
@Field @Option(name="metadataColumnList", usage="metadataColumnList column list separated with comma", widget="String", required=false, def="meta1,meta2")
27
def metadataColumnList;
28

    
29
@Field @Option(name="textColumnList", usage="textColumnList column list separated with comma", widget="String", required=false, def="textColumnList1,textColumnList2")
30
def textColumnList;
31

    
32
if (!ParametersDialog.open(this)) return;
33

    
34
metadataColumnList = metadataColumnList.split(",")
35
textColumnList = textColumnList.split(",")
36
textTag = textTag.trim()
37
rootTag = rootTag.trim()
38

    
39

    
40

    
41
CsvReader reader = new CsvReader(inputFile.getAbsolutePath(), columnSeparator.charAt(0), Charset.forName(characterEncoding));
42
if (!reader.readHeaders()) {
43
        println "Error: no header"
44
        return
45
}
46
def headers = Arrays.asList(reader.getHeaders()) 
47

    
48
println "root tag: $rootTag"
49
println "textColumnList tag: $textTag"
50
println "columns: $headers"
51
println "metadataColumnList columns: $metadataColumnList"
52
println "textColumnList columns: $textColumnList"
53

    
54
def ok = true
55
for (String m : metadataColumnList) {
56
        m = m.trim()
57
        if (!headers.contains(m)) {
58
                println "Error: missing metadataColumnList column: $m"
59
                ok = false
60
        }
61
}
62
for (String t : textColumnList) {
63
        t = t.trim()
64
        if (!headers.contains(t)) {
65
                println "Error: missing textColumnList column: $t"
66
        }
67
}
68

    
69
if (!ok) { return; }
70

    
71
String name = inputFile.getName()
72
int idx = name.lastIndexOf(".")
73
if (idx > 0) name = name.substring(0, idx)
74
File outputfile = new File(inputFile.getParentFile(), name+".xml")
75
XMLOutputFactory factory = XMLOutputFactory.newInstance()
76
FileOutputStream output = new FileOutputStream(outputfile)
77
XMLStreamWriter writer = factory.createXMLStreamWriter(output, "UTF-8")
78

    
79
writer.writeStartDocument("UTF-8","1.0")
80
writer.writeCharacters("\n") // simple XML formating
81
writer.writeStartElement(rootTag)
82

    
83
int pb_n = 1;
84
while (reader.readRecord()) {
85
        
86
        writer.writeCharacters("\n") // simple XML formating
87
        
88
        writer.writeEmptyElement("pb") // <pb/>
89
        writer.writeAttribute("n", ""+pb_n++)
90
        
91
        writer.writeCharacters("\n") // simple XML formating
92
        
93
        writer.writeStartElement(textTag)
94
        
95
        for (String m : metadataColumnList) {
96
                m = m.trim()
97
                writer.writeAttribute(m, reader.get(m))
98
        }
99
        
100
        for (String t : textColumnList) {
101
                t = t.trim()
102
                writer.writeStartElement(t)
103
                writer.writeCharacters(reader.get(t)) // get textColumnList content
104
                writer.writeEndElement() // t
105
        }
106
        
107
        writer.writeEndElement() // textTag
108
}
109

    
110
writer.writeEndElement() // rootTag
111
writer.close()
112
output.close()
113
reader.close()