Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / scripts / swb / AddHeadTailInDirectory.groovy @ 1000

History | View | Annotate | Download (2 kB)

1
package org.txm.scripts.sw
2
// Copyright © - ENS de Lyon - http://textometrie.ens-lyon.fr
3
// @author sheiden
4
//
5
// This file is part of the TXM platform.
6
//
7
// The TXM platform is free software: you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation, either version 3 of the License, or
10
// (at your option) any later version.
11
//
12
// The TXM platform is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
19
//
20
// $LastChangedDate: 2012-02-01 17:58:45 +0100 (mer., 01 févr. 2012) $
21
// $LastChangedRevision: 2122 $
22
// $LastChangedBy: mdecorde $
23
//
24

    
25
import static groovy.io.FileType.*
26

    
27
// PARAMETERS
28

    
29
// dir
30
def dir = new File("/home/mdecorde/TEMP/atelier")
31

    
32
// inputEncoding
33
def inputEncoding = "UTF-8"
34

    
35
// outputEncoding
36
def outputEncoding = "UTF-8"
37

    
38
// head
39
def head = "<?xml version=\"1.0\" encoding=\"${outputEncoding}\"?>\n<discours>\n" // can be empty
40

    
41
// tail
42
def tail = "</discours>\n"; // can be empty
43

    
44
// fileExtension
45
def fileExtension = "txt"
46

    
47
//END OF PARAMETERS
48

    
49
// filePattern
50
def filePattern = ".*\\.$fileExtension\$"
51

    
52
// main body
53
dir.eachFileMatch(~/$filePattern/) { inputFile ->                                // for each file matching extension
54
    def outputTmp = File.createTempFile("AddHeadInDirectoryTemp", '.tmp', dir) // create temporary file
55
    outputTmp.withWriter(outputEncoding) { writer ->
56
    if (head != null && head.length() > 0)
57
            writer.print(head)
58
        inputFile.withReader(inputEncoding) { reader ->
59
                        reader.eachLine { inputLine ->                               // for each line
60
                                writer.println inputLine
61
                        }
62
                }
63
        if (tail != null && tail.length() > 0)
64
                writer.print(tail)
65
        writer.close()
66
    }
67
        assert outputTmp.renameTo(inputFile)                                                // save results
68
}