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