Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / importer / ApplyXsl.groovy @ 479

History | View | Annotate | Download (7 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
// This file is part of the TXM platform.
24
//
25
// The TXM platform is free software: you can redistribute it and/or modif y
26
// it under the terms of the GNU General Public License as published by
27
// the Free Software Foundation, either version 3 of the License, or
28
// (at your option) any later version.
29
//
30
// The TXM platform is distributed in the hope that it will be useful,
31
// but WITHOUT ANY WARRANTY; without even the implied warranty of
32
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33
// GNU General Public License for more details.
34
//
35
// You should have received a copy of the GNU General Public License
36
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
37
// 
38
// 
39
// 
40
// $LastChangedDate: 2016-03-01 10:10:58 +0100 (mar. 01 mars 2016) $
41
// $LastChangedRevision: 3133 $
42
// $LastChangedBy: mdecorde $ 
43
//
44
package org.txm.importer;
45

    
46
/**
47
 * @author mdecorde
48
 */
49

    
50
import java.io.File;
51
import javax.xml.transform.Transformer
52
import javax.xml.transform.TransformerFactory
53
import javax.xml.transform.stream.StreamResult
54
import javax.xml.transform.stream.StreamSource;
55

    
56
import org.txm.searchengine.cqp.clientExceptions.CqiClientException;
57
import net.sf.saxon.*;
58
import net.sf.saxon.s9api.*;
59

    
60
// TODO: Auto-generated Javadoc
61
/**
62
 * apply a xsl file to a file : use process(File infile, File outfile, String[]
63
 * args) to apply the xslt file loaded by the constructor args =
64
 * ["name1","value1","name2",value2, ...]
65
 * 
66
 * @author mdecorde
67
 */
68

    
69
public class ApplyXsl {
70
        
71
//        /** The proc. */
72
//        private Processor proc;
73
//        
74
//        /** The comp. */
75
//        private XsltCompiler comp;
76
//        
77
//        /** The exp. */
78
//        private XsltExecutable exp;
79
//
80
//        /** The source. */
81
//        private XdmNode source;
82
//        
83
//        /** The out. */
84
//        private Serializer out;
85
//        
86
//        /** The trans. */
87
//        private XsltTransformer trans;
88

    
89
        TransformerFactory tFactory;
90
        Transformer transformer;
91
        
92
        /**
93
         * initialize with the xslt file.
94
         *
95
         * @param xsltfile the xsltfile
96
         */
97
        public ApplyXsl(String xsltfile) {
98
//                proc = new Processor(false);
99
//                comp = proc.newXsltCompiler();
100
//                exp = comp.compile(new StreamSource(new File(xsltfile)));
101
                tFactory = new net.sf.saxon.TransformerFactoryImpl();
102
                transformer = tFactory.newTransformer(new StreamSource(xsltfile));
103

    
104
        }
105
        
106
        /**
107
         * initialize with the xslt file.
108
         *
109
         * @param xsltfile the xsltfile
110
         */
111
        public ApplyXsl(File xsltfile) {
112
//                proc = new Processor(false);
113
//                comp = proc.newXsltCompiler();
114
//                exp = comp.compile(new StreamSource(xsltfile));
115
                tFactory = new net.sf.saxon.TransformerFactoryImpl();
116
                transformer = tFactory.newTransformer(new StreamSource(xsltfile));
117
        }
118

    
119
        /**
120
         * Set in and out file.
121
         *
122
         * @param xmlinfile the xmlinfile
123
         * @param xmloutfile the xmloutfile
124
         * @return true, if successful
125
         */
126
        private boolean SetInOutSource(String xmlinfile, String xmloutfile) {
127
//                if (proc == null || exp == null || comp == null)
128
//                        return false;
129
//
130
//                source = proc.newDocumentBuilder().build(
131
//                                new StreamSource(new File(xmlinfile)));
132
//                out = new Serializer();
133
//                out.setOutputFile(new File(xmloutfile));
134
//
135
//                trans = exp.load();
136
//                transformer.set.setInitialContextNode(source);
137
//                transformer.setDestination(out);
138
                return true;
139
        }
140

    
141
        /**
142
         * Set a xslt param.
143
         *
144
         * @param name the name
145
         * @param value the value
146
         * @return true, if successful
147
         */
148
        public boolean SetParam(String name, Object value) {
149
//                if (trans != null)
150
//                        trans.setParameter(new QName(name), new XdmAtomicValue("" + value));
151
//                else
152
//                        return false;
153
                return true;
154
        }
155

    
156
        /**
157
         * Force java to clean memory after processing a xslt. If not we might catch
158
         * a JavaHeapspace Exception
159
         * 
160
         */
161
        private void cleanMemory() {
162
//                source = null;
163
//                trans = null;
164
//                out = null;
165
        }
166

    
167
        /**
168
         * Process files with xslt args.
169
         *
170
         * @param xmlinfile file to be processed
171
         * @param xmloutfile output file
172
         * @param args xslt args ["arg1","arg1value","arg2","arg2value"...]
173
         * @return true, if successful
174
         */
175

    
176
        public boolean process(String xmlinfile, String xmloutfile, String[] args) {
177
                if (!this.SetInOutSource(xmlinfile, xmloutfile))
178
                        return false;
179
                for (int i = 0; i < args.length; i = i + 2)// set params
180
                {
181
                        if (!this.SetParam(args[i], args[i + 1]))
182
                                return false;
183
                }
184

    
185
                transformer.transform(new StreamSource(xmlinfile), new StreamResult(new BufferedOutputStream(new FileOutputStream(xmloutfile))));
186
                cleanMemory(); // save memory
187
                // System.out.println("Done");
188
                return true;
189
        }
190

    
191
        /**
192
         * Process files without xslt args.
193
         *
194
         * @param xmlinfile the xmlinfile
195
         * @param xmloutfile the xmloutfile
196
         * @return true, if successful
197
         */
198
        public boolean process(String xmlinfile, String xmloutfile) {
199
                if (!this.SetInOutSource(xmlinfile, xmloutfile))
200
                        return false;
201

    
202
                transformer.transform(new StreamSource(xmlinfile), new StreamResult(new BufferedOutputStream(new FileOutputStream(xmloutfile))));
203
                cleanMemory();
204
                // System.out.println("Done");
205
                return true;
206
        }
207

    
208
        /**
209
         * The main method.
210
         *
211
         * @param args the arguments
212
         */
213
        public static void main(String[] args)
214
        {
215
                String xslfile = "pathtoxslfile";
216
                ApplyXsl a = new ApplyXsl(xslfile);
217
                
218
                File srcdirectory = new File("pathtodirectorytoprocess")
219
                assert(srcdirectory.exists())
220
                assert(srcdirectory.isDirectory())
221
                assert(srcdirectory.canRead())
222
                assert(srcdirectory.canExecute())
223
                
224
                List<File> files = srcdirectory.listFiles();
225
                
226
                files = files.findAll{item-> item.getName().endsWith(".xml")}
227
                
228
                long bigstart = System.currentTimeMillis()
229
                for(File infile : files)
230
                {
231
                        String outfile = infile.getAbsolutePath()+".out"
232
                        if(infile.canRead())
233
                        {
234
                                long start = System.currentTimeMillis()
235
                                println "Process : "+infile.getName()
236
                                // String[] params = ["pbval1",1,"pbval2",2];
237
                                // a.process(infile.getAbsolutePath(),outfile); // no parameters
238
                                // a.process( infile.getAbsolutePath(),outfile,params);
239
                                println "Done : "+(System.currentTimeMillis()-start)+"ms"
240
                        }
241
                }
242
                println "Total time : "+(System.currentTimeMillis()-bigstart)+"ms"
243
        }
244
}