Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / scripts / filters / CutHeader / CutHeader.groovy @ 1000

History | View | Annotate | Download (2.7 kB)

1 321 mdecorde
// Copyright © 2010-2013 ENS de Lyon.
2 321 mdecorde
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3 321 mdecorde
// Lyon 2, University of Franche-Comté, University of Nice
4 321 mdecorde
// Sophia Antipolis, University of Paris 3.
5 321 mdecorde
//
6 321 mdecorde
// The TXM platform is free software: you can redistribute it
7 321 mdecorde
// and/or modify it under the terms of the GNU General Public
8 321 mdecorde
// License as published by the Free Software Foundation,
9 321 mdecorde
// either version 2 of the License, or (at your option) any
10 321 mdecorde
// later version.
11 321 mdecorde
//
12 321 mdecorde
// The TXM platform is distributed in the hope that it will be
13 321 mdecorde
// useful, but WITHOUT ANY WARRANTY; without even the implied
14 321 mdecorde
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 321 mdecorde
// PURPOSE. See the GNU General Public License for more
16 321 mdecorde
// details.
17 321 mdecorde
//
18 321 mdecorde
// You should have received a copy of the GNU General
19 321 mdecorde
// Public License along with the TXM platform. If not, see
20 321 mdecorde
// http://www.gnu.org/licenses.
21 321 mdecorde
//
22 321 mdecorde
//
23 321 mdecorde
//
24 321 mdecorde
// $LastChangedDate:$
25 321 mdecorde
// $LastChangedRevision:$
26 321 mdecorde
// $LastChangedBy:$
27 321 mdecorde
//
28 1000 mdecorde
package org.txm.scripts.filters.CutHeader;
29 321 mdecorde
30 321 mdecorde
import java.util.regex.Matcher
31 321 mdecorde
import java.util.regex.Pattern
32 321 mdecorde
33 1000 mdecorde
import org.txm.importer.scripts.filters.Filter;
34 321 mdecorde
35 321 mdecorde
// TODO: Auto-generated Javadoc
36 321 mdecorde
/**
37 321 mdecorde
 * The Class CutHeader.
38 321 mdecorde
 */
39 321 mdecorde
class CutHeader extends Filter
40 321 mdecorde
{
41 321 mdecorde
42 321 mdecorde
        /** The xml header file. */
43 321 mdecorde
        String xmlHeaderFile = "";
44 321 mdecorde
45 321 mdecorde
        /** The header. */
46 321 mdecorde
        boolean header = true;
47 321 mdecorde
48 321 mdecorde
        /** The HEADER. */
49 321 mdecorde
        OutputStreamWriter HEADER;
50 321 mdecorde
51 321 mdecorde
        /* (non-Javadoc)
52 321 mdecorde
         * @see org.txm.importer.filters.Filter#SetUsedParam(java.lang.Object)
53 321 mdecorde
         */
54 321 mdecorde
        void SetUsedParam(Object args) {
55 321 mdecorde
                if (args.size() == 1) {
56 321 mdecorde
                        xmlHeaderFile = args[0].toString();
57 321 mdecorde
                } else {
58 321 mdecorde
                        System.err.println("CutHeader needs 1 args\nString:xmlHEADERFilePath");
59 321 mdecorde
                        System.exit(-1);
60 321 mdecorde
                }
61 321 mdecorde
        }
62 321 mdecorde
63 321 mdecorde
        /* (non-Javadoc)
64 321 mdecorde
         * @see org.txm.importer.filters.Filter#after()
65 321 mdecorde
         */
66 321 mdecorde
        void after()
67 321 mdecorde
        {
68 321 mdecorde
                HEADER.close();
69 321 mdecorde
                //println("AFTER CUTHEADER");
70 321 mdecorde
        }
71 321 mdecorde
72 321 mdecorde
        /* (non-Javadoc)
73 321 mdecorde
         * @see org.txm.importer.filters.Filter#filter()
74 321 mdecorde
         */
75 321 mdecorde
        protected void filter()
76 321 mdecorde
        {
77 321 mdecorde
                if(header) {
78 321 mdecorde
                        //if(line ==~ /\s*<text>\s*/ )
79 321 mdecorde
                        if (line.startsWith("<text ") || line.startsWith("<text>")) {
80 321 mdecorde
                                header = false;
81 321 mdecorde
                                HEADER.close();
82 321 mdecorde
                                output.print(line+lineSeparator);
83 321 mdecorde
                        } else {
84 321 mdecorde
                                //print(line+lineSeparator);
85 321 mdecorde
                                HEADER.print(line+lineSeparator);
86 321 mdecorde
                        }
87 321 mdecorde
                } else {
88 321 mdecorde
                        output.write(line+lineSeparator);
89 321 mdecorde
                }
90 321 mdecorde
                //System.out.println( "!"+linecounter);
91 321 mdecorde
        }
92 321 mdecorde
93 321 mdecorde
        /* (non-Javadoc)
94 321 mdecorde
         * @see org.txm.importer.filters.Filter#before()
95 321 mdecorde
         */
96 321 mdecorde
        boolean before() {
97 321 mdecorde
                if (xmlHeaderFile == "") {
98 321 mdecorde
                        println("need to define xmlHeaderFile before lauching this filter");
99 321 mdecorde
                        return false;
100 321 mdecorde
                }
101 321 mdecorde
                File outputFile = new File(xmlHeaderFile)
102 321 mdecorde
                //HEADER = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
103 321 mdecorde
                HEADER = new OutputStreamWriter(new FileOutputStream(outputFile) , this.encodageSortie);
104 321 mdecorde
                //println("BEFORE CUTHEADER");
105 321 mdecorde
                return true;
106 321 mdecorde
        }
107 321 mdecorde
}