Statistics
| Revision:

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

History | View | Annotate | Download (2 kB)

1 1000 mdecorde
package org.txm.scripts.sw
2 321 mdecorde
// Copyright © - ENS de Lyon - http://textometrie.ens-lyon.fr
3 321 mdecorde
// @author sheiden
4 321 mdecorde
//
5 321 mdecorde
// This file is part of the TXM platform.
6 321 mdecorde
//
7 321 mdecorde
// The TXM platform is free software: you can redistribute it and/or modify
8 321 mdecorde
// it under the terms of the GNU General Public License as published by
9 321 mdecorde
// the Free Software Foundation, either version 3 of the License, or
10 321 mdecorde
// (at your option) any later version.
11 321 mdecorde
//
12 321 mdecorde
// The TXM platform is distributed in the hope that it will be useful,
13 321 mdecorde
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14 321 mdecorde
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 321 mdecorde
// GNU General Public License for more details.
16 321 mdecorde
//
17 321 mdecorde
// You should have received a copy of the GNU General Public License
18 321 mdecorde
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
19 321 mdecorde
//
20 321 mdecorde
// $LastChangedDate: 2012-02-01 17:58:45 +0100 (mer., 01 févr. 2012) $
21 321 mdecorde
// $LastChangedRevision: 2122 $
22 321 mdecorde
// $LastChangedBy: mdecorde $
23 321 mdecorde
//
24 321 mdecorde
25 321 mdecorde
import static groovy.io.FileType.*
26 321 mdecorde
27 321 mdecorde
// PARAMETERS
28 321 mdecorde
29 321 mdecorde
// dir
30 321 mdecorde
def dir = new File("/home/mdecorde/TEMP/atelier")
31 321 mdecorde
32 321 mdecorde
// inputEncoding
33 321 mdecorde
def inputEncoding = "UTF-8"
34 321 mdecorde
35 321 mdecorde
// outputEncoding
36 321 mdecorde
def outputEncoding = "UTF-8"
37 321 mdecorde
38 321 mdecorde
// head
39 321 mdecorde
def head = "<?xml version=\"1.0\" encoding=\"${outputEncoding}\"?>\n<discours>\n" // can be empty
40 321 mdecorde
41 321 mdecorde
// tail
42 321 mdecorde
def tail = "</discours>\n"; // can be empty
43 321 mdecorde
44 321 mdecorde
// fileExtension
45 321 mdecorde
def fileExtension = "txt"
46 321 mdecorde
47 321 mdecorde
//END OF PARAMETERS
48 321 mdecorde
49 321 mdecorde
// filePattern
50 321 mdecorde
def filePattern = ".*\\.$fileExtension\$"
51 321 mdecorde
52 321 mdecorde
// main body
53 321 mdecorde
dir.eachFileMatch(~/$filePattern/) { inputFile ->                                // for each file matching extension
54 321 mdecorde
    def outputTmp = File.createTempFile("AddHeadInDirectoryTemp", '.tmp', dir) // create temporary file
55 321 mdecorde
    outputTmp.withWriter(outputEncoding) { writer ->
56 321 mdecorde
    if (head != null && head.length() > 0)
57 321 mdecorde
            writer.print(head)
58 321 mdecorde
        inputFile.withReader(inputEncoding) { reader ->
59 321 mdecorde
                        reader.eachLine { inputLine ->                               // for each line
60 321 mdecorde
                                writer.println inputLine
61 321 mdecorde
                        }
62 321 mdecorde
                }
63 321 mdecorde
        if (tail != null && tail.length() > 0)
64 321 mdecorde
                writer.print(tail)
65 321 mdecorde
        writer.close()
66 321 mdecorde
    }
67 321 mdecorde
        assert outputTmp.renameTo(inputFile)                                                // save results
68 321 mdecorde
}