root / tmp / org.txm.core / src / java / org / txm / scripts / importer / ApplyXsl.groovy @ 2473
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.scripts.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.utils.io.IOUtils |
57 |
|
58 |
import net.sf.saxon.*; |
59 |
import net.sf.saxon.s9api.*; |
60 |
|
61 |
// TODO: Auto-generated Javadoc
|
62 |
/**
|
63 |
* apply a xsl file to a file : use process(File infile, File outfile, String[]
|
64 |
* args) to apply the xslt file loaded by the constructor args =
|
65 |
* ["name1","value1","name2",value2, ...]
|
66 |
*
|
67 |
* @author mdecorde
|
68 |
*/
|
69 |
|
70 |
public class ApplyXsl { |
71 |
|
72 |
// /** The proc. */
|
73 |
// private Processor proc;
|
74 |
//
|
75 |
// /** The comp. */
|
76 |
// private XsltCompiler comp;
|
77 |
//
|
78 |
// /** The exp. */
|
79 |
// private XsltExecutable exp;
|
80 |
//
|
81 |
// /** The source. */
|
82 |
// private XdmNode source;
|
83 |
//
|
84 |
// /** The out. */
|
85 |
// private Serializer out;
|
86 |
//
|
87 |
// /** The trans. */
|
88 |
// private XsltTransformer trans;
|
89 |
|
90 |
TransformerFactory tFactory;
|
91 |
Transformer transformer;
|
92 |
|
93 |
/**
|
94 |
* initialize with the xslt file.
|
95 |
*
|
96 |
* @param xsltfile the xsltfile
|
97 |
*/
|
98 |
public ApplyXsl(String xsltfile) { |
99 |
// proc = new Processor(false);
|
100 |
// comp = proc.newXsltCompiler();
|
101 |
// exp = comp.compile(new StreamSource(new File(xsltfile)));
|
102 |
tFactory = new net.sf.saxon.TransformerFactoryImpl();
|
103 |
transformer = tFactory.newTransformer(new StreamSource(xsltfile)); |
104 |
|
105 |
} |
106 |
|
107 |
/**
|
108 |
* initialize with the xslt file.
|
109 |
*
|
110 |
* @param xsltfile the xsltfile
|
111 |
*/
|
112 |
public ApplyXsl(File xsltfile) { |
113 |
// proc = new Processor(false);
|
114 |
// comp = proc.newXsltCompiler();
|
115 |
// exp = comp.compile(new StreamSource(xsltfile));
|
116 |
tFactory = new net.sf.saxon.TransformerFactoryImpl();
|
117 |
transformer = tFactory.newTransformer(new StreamSource(xsltfile)); |
118 |
} |
119 |
|
120 |
/**
|
121 |
* Set in and out file.
|
122 |
*
|
123 |
* @param xmlinfile the xmlinfile
|
124 |
* @param xmloutfile the xmloutfile
|
125 |
* @return true, if successful
|
126 |
*/
|
127 |
private boolean SetInOutSource(String xmlinfile, String xmloutfile) { |
128 |
// if (proc == null || exp == null || comp == null)
|
129 |
// return false;
|
130 |
//
|
131 |
// source = proc.newDocumentBuilder().build(
|
132 |
// new StreamSource(new File(xmlinfile)));
|
133 |
// out = new Serializer();
|
134 |
// out.setOutputFile(new File(xmloutfile));
|
135 |
//
|
136 |
// trans = exp.load();
|
137 |
// transformer.set.setInitialContextNode(source);
|
138 |
// transformer.setDestination(out);
|
139 |
return true; |
140 |
} |
141 |
|
142 |
/**
|
143 |
* Set a xslt param.
|
144 |
*
|
145 |
* @param name the name
|
146 |
* @param value the value
|
147 |
* @return true, if successful
|
148 |
*/
|
149 |
public boolean SetParam(String name, Object value) { |
150 |
// if (trans != null)
|
151 |
// trans.setParameter(new QName(name), new XdmAtomicValue("" + value));
|
152 |
// else
|
153 |
// return false;
|
154 |
return true; |
155 |
} |
156 |
|
157 |
/**
|
158 |
* Force java to clean memory after processing a xslt. If not we might catch
|
159 |
* a JavaHeapspace Exception
|
160 |
*
|
161 |
*/
|
162 |
private void cleanMemory() { |
163 |
// source = null;
|
164 |
// trans = null;
|
165 |
// out = null;
|
166 |
} |
167 |
|
168 |
/**
|
169 |
* Process files with xslt args.
|
170 |
*
|
171 |
* @param xmlinfile file to be processed
|
172 |
* @param xmloutfile output file
|
173 |
* @param args xslt args ["arg1","arg1value","arg2","arg2value"...]
|
174 |
* @return true, if successful
|
175 |
*/
|
176 |
|
177 |
public boolean process(String xmlinfile, String xmloutfile, String[] args) { |
178 |
if (!this.SetInOutSource(xmlinfile, xmloutfile)) |
179 |
return false; |
180 |
for (int i = 0; i < args.length; i = i + 2)// set params |
181 |
{ |
182 |
if (!this.SetParam(args[i], args[i + 1])) |
183 |
return false; |
184 |
} |
185 |
|
186 |
transformer.transform(new StreamSource(xmlinfile), new StreamResult(new BufferedOutputStream(new FileOutputStream(xmloutfile)))); |
187 |
cleanMemory(); // save memory
|
188 |
// System.out.println("Done");
|
189 |
return true; |
190 |
} |
191 |
|
192 |
/**
|
193 |
* Process files without xslt args.
|
194 |
*
|
195 |
* @param xmlinfile the xmlinfile
|
196 |
* @param xmloutfile the xmloutfile
|
197 |
* @return true, if successful
|
198 |
*/
|
199 |
public boolean process(String xmlinfile, String xmloutfile) { |
200 |
if (!this.SetInOutSource(xmlinfile, xmloutfile)) |
201 |
return false; |
202 |
|
203 |
transformer.transform(new StreamSource(xmlinfile), new StreamResult(new BufferedOutputStream(new FileOutputStream(xmloutfile)))); |
204 |
cleanMemory(); |
205 |
// System.out.println("Done");
|
206 |
return true; |
207 |
} |
208 |
|
209 |
/**
|
210 |
* The main method.
|
211 |
*
|
212 |
* @param args the arguments
|
213 |
*/
|
214 |
public static void main(String[] args) |
215 |
{ |
216 |
String xslfile = "pathtoxslfile"; |
217 |
ApplyXsl a = new ApplyXsl(xslfile);
|
218 |
|
219 |
File srcdirectory = new File("pathtodirectorytoprocess") |
220 |
assert(srcdirectory.exists())
|
221 |
assert(srcdirectory.isDirectory())
|
222 |
assert(srcdirectory.canRead())
|
223 |
assert(srcdirectory.canExecute())
|
224 |
|
225 |
List<File> files = srcdirectory.listFiles(IOUtils.HIDDENFILE_FILTER); |
226 |
|
227 |
files = files.findAll{item-> item.getName().endsWith(".xml")}
|
228 |
|
229 |
long bigstart = System.currentTimeMillis() |
230 |
for(File infile : files) |
231 |
{ |
232 |
String outfile = infile.getAbsolutePath()+".out" |
233 |
if(infile.canRead())
|
234 |
{ |
235 |
long start = System.currentTimeMillis() |
236 |
println "Process : "+infile.getName()
|
237 |
// String[] params = ["pbval1",1,"pbval2",2];
|
238 |
// a.process(infile.getAbsolutePath(),outfile); // no parameters
|
239 |
// a.process( infile.getAbsolutePath(),outfile,params);
|
240 |
println "Done : "+(System.currentTimeMillis()-start)+"ms" |
241 |
} |
242 |
} |
243 |
println "Total time : "+(System.currentTimeMillis()-bigstart)+"ms" |
244 |
} |
245 |
} |