Statistics
| Revision:

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

History | View | Annotate | Download (3.2 kB)

1
// Copyright © 2010-2013 ENS de Lyon.
2
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3
// Lyon 2, University of Franche-Comté, University of Nice
4
// Sophia Antipolis, University of Paris 3.
5
// 
6
// The TXM platform is free software: you can redistribute it
7
// and/or modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation,
9
// either version 2 of the License, or (at your option) any
10
// later version.
11
// 
12
// The TXM platform is distributed in the hope that it will be
13
// useful, but WITHOUT ANY WARRANTY; without even the implied
14
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15
// PURPOSE. See the GNU General Public License for more
16
// details.
17
// 
18
// You should have received a copy of the GNU General
19
// Public License along with the TXM platform. If not, see
20
// http://www.gnu.org/licenses.
21
// 
22
// 
23
// 
24
// $LastChangedDate:$
25
// $LastChangedRevision:$
26
// $LastChangedBy:$ 
27
//
28
package org.txm.scripts.filters.FusionXmlHeaderBody;
29

    
30
import org.txm.importer.scripts.filters.*;
31
import org.xml.sax.InputSource
32
import org.xml.sax.helpers.ParserAdapter
33
import org.xml.sax.helpers.*;
34
import javax.xml.parsers.SAXParserFactory;
35
import javax.xml.parsers.SAXParser;
36
import java.io.Reader;
37
import groovy.xml.XmlUtil
38

    
39
import javax.xml.transform.*;
40
import javax.xml.transform.stream.StreamResult;
41
import javax.xml.transform.stream.StreamSource;
42

    
43
import org.xml.sax.helpers.DefaultHandler
44

    
45
// TODO: Auto-generated Javadoc
46
/**
47
 * The Class FusionXmlHeaderBody.
48
 */
49
class FusionXmlHeaderBody extends Filter
50
{
51
        
52
        /** The body file. */
53
        String bodyFile = "";
54
        
55
        /** The header file. */
56
        String headerFile = "";
57
        
58
        /** The Lastinput. */
59
        Reader Lastinput;
60
        
61
        /* (non-Javadoc)
62
         * @see org.txm.importer.filters.Filter#SetUsedParam(java.lang.Object)
63
         */
64
        void SetUsedParam(Object args)
65
        {
66
                
67
        }
68
        
69
        /* (non-Javadoc)
70
         * @see org.txm.importer.filters.Filter#before()
71
         */
72
        boolean before()
73
        {
74
                if(bodyFile == "" || headerFile == "")
75
                {
76
                        System.out.println("FusionXmlHeaderBody need 2 args : \nString:XmlBodyFile\nString:XmlHeaderFile");
77
                }
78
                System.out.println("start fusionxmlheaderbody "+this);
79
                Lastinput = this.input;
80
                this.input = new BufferedReader(new java.io.StringReader("") );
81
        }
82
        
83
        /**
84
         * Sets the used param.
85
         *
86
         * @param args the args
87
         */
88
        void SetUsedParam(Object[] args)
89
        {
90
                if(args.size() == 2)
91
                {
92
                        bodyFile = args[0];//needed to get back its ID
93
                        headerFile = args[1];
94
                }
95
                else
96
                {
97
                        System.out.println("FusionXmlHeaderBody need 2 args : \nString:XmlBodyFile\nString:XmlHeaderFile");
98
                }
99
        }
100
        
101
        /* (non-Javadoc)
102
         * @see org.txm.importer.filters.Filter#after()
103
         */
104
        void after()
105
        {
106
                this.input = this.Lastinput;
107
                //System.out.println("input ready "+this.input.ready());
108
                def body = new XmlSlurper().parse(this.input);
109
                
110
                def headers = new XmlSlurper().parse(headerFile)
111
                
112
                def id = bodyFile.split('\\.')[0]
113
                def header = headers.discours.find{it.@file == id}
114
                
115
                this.output.withWriter{writer ->
116
                        def xmlBuilder = new groovy.xml.StreamingMarkupBuilder();
117
                        def xml = xmlBuilder.bind{
118
                                mkp.xmlDeclaration();
119
                                top (header <<body.children());        
120
                        };
121
                
122
                        writer <<XmlUtil.serialize(xml);
123
                }                
124
        }
125
        
126
        /* (non-Javadoc)
127
         * @see org.txm.importer.filters.Filter#filter()
128
         */
129
        void filter()
130
        {
131
        }
132
        
133
}