Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / macro / txt / TXT2XMLMacro.groovy @ 187

History | View | Annotate | Download (1.6 kB)

1
package org.txm.macro.txt
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="inputDirectory", usage="TXT directory", widget="Folder", required=false, def="txt")
12
File inputDirectory;
13

    
14
@Field @Option(name="encoding", usage="File encoding", widget="String", required=false, def="UTF-8")
15
String encoding;
16

    
17
@Field @Option(name="rootTag", usage="rootTag", widget="String", required=false, def="doc")
18
String rootTag;
19

    
20
if (!ParametersDialog.open(this)) return;
21
rootTag = rootTag.trim()
22
encoding = encoding.trim()
23

    
24
File outputDirectory = new File(inputDirectory, "out")
25
outputDirectory.mkdir()
26
println "processing: "+inputDirectory
27
for (File inputfile : inputDirectory.listFiles()) {
28
        if (inputfile.isDirectory() || inputfile.isHidden() || !inputfile.getName().endsWith(".txt")) continue // ignore
29
        println " file: "+inputfile
30
        
31
        String name = inputfile.getName()
32
        int idx = name.indexOf(".")
33
        if (idx > 0) name = name.substring(0, idx)
34
        File outputfile = new File(outputDirectory, name+".xml")
35
        
36
        XMLOutputFactory factory = XMLOutputFactory.newInstance()
37
        FileOutputStream output = new FileOutputStream(outputfile)
38
        XMLStreamWriter writer = factory.createXMLStreamWriter(output)
39

    
40
        writer.writeStartDocument("UTF-8","1.0")
41
        writer.writeCharacters("\n") // manage XML reserved characters
42
        writer.writeStartElement(rootTag)
43
        writer.writeCharacters(inputfile.getText(encoding)) // manage XML reserved characters
44
        writer.writeEndElement() // rootTag
45
        writer.close()
46
        output.close()
47
}