Statistics
| Revision:

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

History | View | Annotate | Download (3.2 kB)

1 187 sjacqu01
// Copyright © 2010-2013 ENS de Lyon.
2 187 sjacqu01
// Copyright © 2007-2010 ENS de Lyon, CNRS, INRP, University of
3 187 sjacqu01
// Lyon 2, University of Franche-Comté, University of Nice
4 187 sjacqu01
// Sophia Antipolis, University of Paris 3.
5 187 sjacqu01
//
6 187 sjacqu01
// The TXM platform is free software: you can redistribute it
7 187 sjacqu01
// and/or modify it under the terms of the GNU General Public
8 187 sjacqu01
// License as published by the Free Software Foundation,
9 187 sjacqu01
// either version 2 of the License, or (at your option) any
10 187 sjacqu01
// later version.
11 187 sjacqu01
//
12 187 sjacqu01
// The TXM platform is distributed in the hope that it will be
13 187 sjacqu01
// useful, but WITHOUT ANY WARRANTY; without even the implied
14 187 sjacqu01
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 187 sjacqu01
// PURPOSE. See the GNU General Public License for more
16 187 sjacqu01
// details.
17 187 sjacqu01
//
18 187 sjacqu01
// You should have received a copy of the GNU General
19 187 sjacqu01
// Public License along with the TXM platform. If not, see
20 187 sjacqu01
// http://www.gnu.org/licenses.
21 187 sjacqu01
//
22 187 sjacqu01
//
23 187 sjacqu01
//
24 187 sjacqu01
// $LastChangedDate:$
25 187 sjacqu01
// $LastChangedRevision:$
26 187 sjacqu01
// $LastChangedBy:$
27 187 sjacqu01
//
28 187 sjacqu01
package filters.FusionXmlHeaderBody;
29 187 sjacqu01
30 187 sjacqu01
import org.txm.importer.filters.*;
31 187 sjacqu01
import org.xml.sax.InputSource
32 187 sjacqu01
import org.xml.sax.helpers.ParserAdapter
33 187 sjacqu01
import org.xml.sax.helpers.*;
34 187 sjacqu01
import javax.xml.parsers.SAXParserFactory;
35 187 sjacqu01
import javax.xml.parsers.SAXParser;
36 187 sjacqu01
import java.io.Reader;
37 187 sjacqu01
import groovy.xml.XmlUtil
38 187 sjacqu01
39 187 sjacqu01
import javax.xml.transform.*;
40 187 sjacqu01
import javax.xml.transform.stream.StreamResult;
41 187 sjacqu01
import javax.xml.transform.stream.StreamSource;
42 187 sjacqu01
43 187 sjacqu01
import org.xml.sax.helpers.DefaultHandler
44 187 sjacqu01
45 187 sjacqu01
// TODO: Auto-generated Javadoc
46 187 sjacqu01
/**
47 187 sjacqu01
 * The Class FusionXmlHeaderBody.
48 187 sjacqu01
 */
49 187 sjacqu01
class FusionXmlHeaderBody extends Filter
50 187 sjacqu01
{
51 187 sjacqu01
52 187 sjacqu01
        /** The body file. */
53 187 sjacqu01
        String bodyFile = "";
54 187 sjacqu01
55 187 sjacqu01
        /** The header file. */
56 187 sjacqu01
        String headerFile = "";
57 187 sjacqu01
58 187 sjacqu01
        /** The Lastinput. */
59 187 sjacqu01
        Reader Lastinput;
60 187 sjacqu01
61 187 sjacqu01
        /* (non-Javadoc)
62 187 sjacqu01
         * @see org.txm.importer.filters.Filter#SetUsedParam(java.lang.Object)
63 187 sjacqu01
         */
64 187 sjacqu01
        void SetUsedParam(Object args)
65 187 sjacqu01
        {
66 187 sjacqu01
67 187 sjacqu01
        }
68 187 sjacqu01
69 187 sjacqu01
        /* (non-Javadoc)
70 187 sjacqu01
         * @see org.txm.importer.filters.Filter#before()
71 187 sjacqu01
         */
72 187 sjacqu01
        boolean before()
73 187 sjacqu01
        {
74 187 sjacqu01
                if(bodyFile == "" || headerFile == "")
75 187 sjacqu01
                {
76 187 sjacqu01
                        System.out.println("FusionXmlHeaderBody need 2 args : \nString:XmlBodyFile\nString:XmlHeaderFile");
77 187 sjacqu01
                }
78 187 sjacqu01
                System.out.println("start fusionxmlheaderbody "+this);
79 187 sjacqu01
                Lastinput = this.input;
80 187 sjacqu01
                this.input = new BufferedReader(new java.io.StringReader("") );
81 187 sjacqu01
        }
82 187 sjacqu01
83 187 sjacqu01
        /**
84 187 sjacqu01
         * Sets the used param.
85 187 sjacqu01
         *
86 187 sjacqu01
         * @param args the args
87 187 sjacqu01
         */
88 187 sjacqu01
        void SetUsedParam(Object[] args)
89 187 sjacqu01
        {
90 187 sjacqu01
                if(args.size() == 2)
91 187 sjacqu01
                {
92 187 sjacqu01
                        bodyFile = args[0];//needed to get back its ID
93 187 sjacqu01
                        headerFile = args[1];
94 187 sjacqu01
                }
95 187 sjacqu01
                else
96 187 sjacqu01
                {
97 187 sjacqu01
                        System.out.println("FusionXmlHeaderBody need 2 args : \nString:XmlBodyFile\nString:XmlHeaderFile");
98 187 sjacqu01
                }
99 187 sjacqu01
        }
100 187 sjacqu01
101 187 sjacqu01
        /* (non-Javadoc)
102 187 sjacqu01
         * @see org.txm.importer.filters.Filter#after()
103 187 sjacqu01
         */
104 187 sjacqu01
        void after()
105 187 sjacqu01
        {
106 187 sjacqu01
                this.input = this.Lastinput;
107 187 sjacqu01
                //System.out.println("input ready "+this.input.ready());
108 187 sjacqu01
                def body = new XmlSlurper().parse(this.input);
109 187 sjacqu01
110 187 sjacqu01
                def headers = new XmlSlurper().parse(headerFile)
111 187 sjacqu01
112 187 sjacqu01
                def id = bodyFile.split('\\.')[0]
113 187 sjacqu01
                def header = headers.discours.find{it.@file == id}
114 187 sjacqu01
115 187 sjacqu01
                this.output.withWriter{writer ->
116 187 sjacqu01
                        def xmlBuilder = new groovy.xml.StreamingMarkupBuilder();
117 187 sjacqu01
                        def xml = xmlBuilder.bind{
118 187 sjacqu01
                                mkp.xmlDeclaration();
119 187 sjacqu01
                                top (header <<body.children());
120 187 sjacqu01
                        };
121 187 sjacqu01
122 187 sjacqu01
                        writer <<XmlUtil.serialize(xml);
123 187 sjacqu01
                }
124 187 sjacqu01
        }
125 187 sjacqu01
126 187 sjacqu01
        /* (non-Javadoc)
127 187 sjacqu01
         * @see org.txm.importer.filters.Filter#filter()
128 187 sjacqu01
         */
129 187 sjacqu01
        void filter()
130 187 sjacqu01
        {
131 187 sjacqu01
        }
132 187 sjacqu01
133 187 sjacqu01
}