Révision 2705
tmp/org.txm.core/src/java/org/txm/importer/ApplyXsl2.java (revision 2705) | ||
---|---|---|
18 | 18 |
// You should have received a copy of the GNU General |
19 | 19 |
// Public License along with the TXM platform. If not, see |
20 | 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: 2011-11-03 17:59:23 +0100 (jeu., 03 nov. 2011) $
|
|
41 |
// $LastChangedRevision: 2051 $
|
|
42 |
// $LastChangedBy: mdecorde $
|
|
43 |
//
|
|
44 |
package org.txm.importer;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* @author mdecorde
|
|
48 |
*/
|
|
49 |
|
|
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: 2011-11-03 17:59:23 +0100 (jeu., 03 nov. 2011) $ |
|
41 |
// $LastChangedRevision: 2051 $ |
|
42 |
// $LastChangedBy: mdecorde $ |
|
43 |
// |
|
44 |
package org.txm.importer; |
|
45 |
|
|
46 |
/** |
|
47 |
* @author mdecorde |
|
48 |
*/ |
|
49 |
|
|
50 | 50 |
import java.io.BufferedOutputStream; |
51 | 51 |
import java.io.BufferedWriter; |
52 | 52 |
import java.io.File; |
... | ... | |
79 | 79 |
import org.txm.utils.io.IOUtils; |
80 | 80 |
import org.txm.utils.logger.Log; |
81 | 81 |
import org.txm.utils.saxon.SaxonNodeSet; |
82 |
|
|
83 |
// TODO: Auto-generated Javadoc |
|
84 |
/** |
|
85 |
* apply a xsl file to a file : use process(File infile, File outfile, String[] |
|
86 |
* args) to apply the xslt file loaded by the constructor args = |
|
87 |
* ["name1","value1","name2",value2, ...] |
|
88 |
* |
|
89 |
* @author mdecorde |
|
90 |
*/ |
|
91 |
|
|
92 |
public class ApplyXsl2 { |
|
93 |
|
|
94 |
// /** The proc. */ |
|
95 |
// private Processor proc; |
|
96 |
// |
|
97 |
// /** The comp. */ |
|
98 |
// private XsltCompiler comp; |
|
99 |
// |
|
100 |
// /** The exp. */ |
|
101 |
// private XsltExecutable exp; |
|
102 |
// |
|
103 |
// /** The source. */ |
|
104 |
// private XdmNode source; |
|
105 |
// |
|
106 |
// /** The out. */ |
|
107 |
// private Serializer out; |
|
108 |
// |
|
109 |
// /** The trans. */ |
|
110 |
// private XsltTransformer trans; |
|
111 |
|
|
112 |
net.sf.saxon.TransformerFactoryImpl tFactory; |
|
113 |
Transformer transformer; |
|
114 |
private HashMap<String, Object> params = new HashMap<String, Object>(); |
|
82 |
|
|
83 |
// TODO: Auto-generated Javadoc |
|
84 |
/** |
|
85 |
* apply a xsl file to a file : use process(File infile, File outfile, String[] |
|
86 |
* args) to apply the xslt file loaded by the constructor args = |
|
87 |
* ["name1","value1","name2",value2, ...] |
|
88 |
* |
|
89 |
* @author mdecorde |
|
90 |
*/ |
|
91 |
|
|
92 |
public class ApplyXsl2 { |
|
93 |
|
|
94 |
// /** The proc. */ |
|
95 |
// private Processor proc; |
|
96 |
// |
|
97 |
// /** The comp. */ |
|
98 |
// private XsltCompiler comp; |
|
99 |
// |
|
100 |
// /** The exp. */ |
|
101 |
// private XsltExecutable exp; |
|
102 |
// |
|
103 |
// /** The source. */ |
|
104 |
// private XdmNode source; |
|
105 |
// |
|
106 |
// /** The out. */ |
|
107 |
// private Serializer out; |
|
108 |
// |
|
109 |
// /** The trans. */ |
|
110 |
// private XsltTransformer trans; |
|
111 |
|
|
112 |
net.sf.saxon.TransformerFactoryImpl tFactory; |
|
113 |
|
|
114 |
Transformer transformer; |
|
115 |
|
|
116 |
private HashMap<String, Object> params = new HashMap<>(); |
|
117 |
|
|
115 | 118 |
private File xsltfile; |
116 |
boolean debug = false; |
|
117 |
|
|
118 |
/** |
|
119 |
* initialize with the xslt file. |
|
120 |
* |
|
121 |
* @param xsltfile the xsltfile |
|
122 |
* @throws TransformerConfigurationException |
|
123 |
*/ |
|
124 |
public ApplyXsl2(String xsltfile) throws TransformerConfigurationException { |
|
125 |
|
|
126 |
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); |
|
127 |
|
|
128 |
this.xsltfile = new File(xsltfile); |
|
119 |
|
|
120 |
boolean debug = false; |
|
121 |
|
|
122 |
/** |
|
123 |
* initialize with the xslt file. |
|
124 |
* |
|
125 |
* @param xsltfile the xsltfile |
|
126 |
* @throws TransformerConfigurationException |
|
127 |
*/ |
|
128 |
public ApplyXsl2(String xsltfile) throws TransformerConfigurationException { |
|
129 |
|
|
130 |
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); |
|
131 |
|
|
132 |
this.xsltfile = new File(xsltfile); |
|
129 | 133 |
tFactory = new TransformerFactoryImpl(); |
130 | 134 |
transformer = tFactory.newTransformer(new StreamSource(this.xsltfile)); |
131 |
|
|
132 |
|
|
133 |
//Log.info("new tFactory: "+tFactory);
|
|
134 |
//Log.info("new transformer: "+transformer);
|
|
135 |
}
|
|
136 |
|
|
137 |
/**
|
|
138 |
* initialize with the xslt file.
|
|
139 |
*
|
|
135 |
|
|
136 |
|
|
137 |
// Log.info("new tFactory: "+tFactory);
|
|
138 |
// Log.info("new transformer: "+transformer);
|
|
139 |
} |
|
140 |
|
|
141 |
/** |
|
142 |
* initialize with the xslt file. |
|
143 |
* |
|
140 | 144 |
* @param xsltfile the xsltfile |
141 |
* @throws TransformerConfigurationException
|
|
142 |
*/
|
|
143 |
public ApplyXsl2(File xsltfile) throws TransformerConfigurationException {
|
|
144 |
this.xsltfile = xsltfile;
|
|
145 |
|
|
146 |
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
|
|
147 |
|
|
148 |
tFactory = new net.sf.saxon.TransformerFactoryImpl();
|
|
149 |
//Log.info("new tFactory: "+tFactory); |
|
150 |
tFactory.getConfiguration().registerExtensionFunction(new SaxonNodeSet());
|
|
151 |
Log.info(NLS.bind("ApplyXsl2 with the {0} stylesheet.", xsltfile));
|
|
152 |
reload();
|
|
153 |
//Log.info("new transformer: "+transformer);
|
|
154 |
}
|
|
155 |
|
|
145 |
* @throws TransformerConfigurationException |
|
146 |
*/ |
|
147 |
public ApplyXsl2(File xsltfile) throws TransformerConfigurationException { |
|
148 |
this.xsltfile = xsltfile; |
|
149 |
|
|
150 |
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); |
|
151 |
|
|
152 |
tFactory = new net.sf.saxon.TransformerFactoryImpl(); |
|
153 |
// Log.info("new tFactory: "+tFactory);
|
|
154 |
tFactory.getConfiguration().registerExtensionFunction(new SaxonNodeSet()); |
|
155 |
Log.info(NLS.bind("ApplyXsl2 with the {0} stylesheet.", xsltfile)); |
|
156 |
reload(); |
|
157 |
// Log.info("new transformer: "+transformer);
|
|
158 |
} |
|
159 |
|
|
156 | 160 |
/** |
157 | 161 |
* Set a xslt param. |
158 | 162 |
* |
... | ... | |
162 | 166 |
*/ |
163 | 167 |
public boolean setParams(HashMap<String, String> params) { |
164 | 168 |
boolean b = true; |
165 |
for (String k : params.keySet()) b = b & setParam(k, params.get(k)); |
|
169 |
for (String k : params.keySet()) |
|
170 |
b = b & setParam(k, params.get(k)); |
|
166 | 171 |
return b; |
167 | 172 |
} |
168 | 173 |
|
169 | 174 |
public void setDebug(boolean d) { |
170 | 175 |
this.debug = d; |
171 | 176 |
} |
172 |
|
|
173 |
/** |
|
174 |
* Set a xslt param. |
|
175 |
* |
|
176 |
* @param name the name |
|
177 |
* @param value the value |
|
178 |
* @return true, if successful |
|
179 |
*/ |
|
180 |
public boolean setParam(String name, Object value) { |
|
181 |
if (transformer != null) { |
|
182 |
transformer.setParameter(name, value); |
|
183 |
params.put(name, value); |
|
184 |
return true; |
|
185 |
} else |
|
186 |
return false; |
|
187 |
} |
|
188 |
|
|
177 |
|
|
189 | 178 |
/** |
190 | 179 |
* Set a xslt param. |
191 | 180 |
* |
... | ... | |
193 | 182 |
* @param value the value |
194 | 183 |
* @return true, if successful |
195 | 184 |
*/ |
185 |
public boolean setParam(String name, Object value) { |
|
186 |
if (transformer != null) { |
|
187 |
transformer.setParameter(name, value); |
|
188 |
params.put(name, value); |
|
189 |
return true; |
|
190 |
} |
|
191 |
else |
|
192 |
return false; |
|
193 |
} |
|
194 |
|
|
195 |
/** |
|
196 |
* Set a xslt param. |
|
197 |
* |
|
198 |
* @param name the name |
|
199 |
* @param value the value |
|
200 |
* @return true, if successful |
|
201 |
*/ |
|
196 | 202 |
@Deprecated |
197 | 203 |
public boolean SetParam(String name, Object value) { |
198 | 204 |
if (transformer != null) { |
199 | 205 |
transformer.setParameter(name, value); |
200 | 206 |
params.put(name, value); |
201 | 207 |
return true; |
202 |
} else |
|
208 |
} |
|
209 |
else |
|
203 | 210 |
return false; |
204 |
} |
|
205 |
public boolean resetParams() { |
|
206 |
if (transformer != null) { |
|
207 |
transformer.reset(); |
|
208 |
params.clear(); |
|
209 |
} else |
|
210 |
return false; |
|
211 |
return true; |
|
212 |
} |
|
213 |
|
|
214 |
/** |
|
215 |
* Force java to clean memory after processing a xslt. If not we might catch |
|
211 |
} |
|
212 |
|
|
213 |
public boolean resetParams() { |
|
214 |
if (transformer != null) { |
|
215 |
transformer.reset(); |
|
216 |
params.clear(); |
|
217 |
} |
|
218 |
else |
|
219 |
return false; |
|
220 |
return true; |
|
221 |
} |
|
222 |
|
|
223 |
/** |
|
224 |
* Force java to clean memory after processing a xslt. If not we might catch |
|
216 | 225 |
* a JavaHeapspace Exception |
217 |
* @throws TransformerConfigurationException |
|
218 |
* |
|
219 |
*/ |
|
220 |
private void cleanMemory() throws TransformerConfigurationException { |
|
221 |
reload(); |
|
222 |
for (String name : params.keySet()) { // reload parameters |
|
223 |
transformer.setParameter(name, params.get(name)); |
|
224 |
} |
|
225 |
} |
|
226 |
|
|
227 |
private boolean reload() throws TransformerConfigurationException { |
|
228 |
//try { |
|
229 |
transformer = null; |
|
230 |
transformer = tFactory.newTransformer(new StreamSource(xsltfile)); |
|
231 |
// } catch (TransformerConfigurationException e) { |
|
232 |
// org.txm.utils.logger.Log.printStackTrace(e); |
|
233 |
// Log.severe("Error while reloading transformer: "+e); |
|
234 |
// System.out.println("Error while reloading transformer: "+e); |
|
235 |
// return false; |
|
236 |
// } |
|
237 |
return true; |
|
238 |
} |
|
239 |
|
|
240 |
/** |
|
241 |
* Process files with xslt args. |
|
242 |
* |
|
243 |
* @param xmlinfile file to be processed |
|
244 |
* @param xmloutfile output file |
|
245 |
* @param args xslt args ["arg1","arg1value","arg2","arg2value"...] |
|
246 |
* @return true, if successful |
|
247 |
* @throws TransformerException |
|
248 |
* @throws FileNotFoundException |
|
249 |
*/ |
|
250 |
|
|
251 |
public boolean process(String xmlinfile, String xmloutfile, String[] args) throws Exception { |
|
252 |
File infile = new File(xmlinfile); |
|
253 |
File outfile = new File(xmloutfile); |
|
254 |
|
|
255 |
//System.out.println(xmlinfile+" Params: "+Arrays.toString(args)); |
|
256 |
for (int i = 0; i < args.length; i = i + 2) { |
|
257 |
if (!this.setParam(args[i], args[i + 1])) |
|
258 |
return false; |
|
259 |
} |
|
260 |
|
|
261 |
transformer.transform(new StreamSource(xmlinfile), new StreamResult(new BufferedOutputStream(new FileOutputStream(xmloutfile)))); |
|
262 |
cleanMemory(); // save memory |
|
263 |
// System.out.println("Done"); |
|
264 |
return true; |
|
265 |
} |
|
266 |
|
|
267 |
/** |
|
268 |
* Process files without xslt args. |
|
269 |
* |
|
270 |
* @param xmlinfile the xmlinfile |
|
271 |
* @param xmloutfile the xmloutfile. If not set, xmlinfile content is replaced |
|
272 |
* @return true, if successful |
|
273 |
* @throws TransformerException |
|
274 |
* @throws IOException |
|
275 |
*/ |
|
276 |
public boolean process(File xmlinfile, File xmloutfile) throws TransformerException, IOException { |
|
226 |
* |
|
227 |
* @throws TransformerConfigurationException |
|
228 |
* |
|
229 |
*/ |
|
230 |
private void cleanMemory() throws TransformerConfigurationException { |
|
231 |
reload(); |
|
232 |
for (String name : params.keySet()) { // reload parameters |
|
233 |
transformer.setParameter(name, params.get(name)); |
|
234 |
} |
|
235 |
} |
|
236 |
|
|
237 |
private boolean reload() throws TransformerConfigurationException { |
|
238 |
// try { |
|
239 |
transformer = null; |
|
240 |
transformer = tFactory.newTransformer(new StreamSource(xsltfile)); |
|
241 |
// } catch (TransformerConfigurationException e) { |
|
242 |
// org.txm.utils.logger.Log.printStackTrace(e); |
|
243 |
// Log.severe("Error while reloading transformer: "+e); |
|
244 |
// System.out.println("Error while reloading transformer: "+e); |
|
245 |
// return false; |
|
246 |
// } |
|
247 |
return true; |
|
248 |
} |
|
249 |
|
|
250 |
/** |
|
251 |
* Process files with xslt args. |
|
252 |
* |
|
253 |
* @param xmlinfile file to be processed |
|
254 |
* @param xmloutfile output file |
|
255 |
* @param args xslt args ["arg1","arg1value","arg2","arg2value"...] |
|
256 |
* @return true, if successful |
|
257 |
* @throws TransformerException |
|
258 |
* @throws FileNotFoundException |
|
259 |
*/ |
|
260 |
|
|
261 |
public boolean process(String xmlinfile, String xmloutfile, String[] args) throws Exception { |
|
262 |
File infile = new File(xmlinfile); |
|
263 |
File outfile = new File(xmloutfile); |
|
277 | 264 |
|
265 |
// System.out.println(xmlinfile+" Params: "+Arrays.toString(args)); |
|
266 |
for (int i = 0; i < args.length; i = i + 2) { |
|
267 |
if (!this.setParam(args[i], args[i + 1])) |
|
268 |
return false; |
|
269 |
} |
|
270 |
|
|
271 |
transformer.transform(new StreamSource(xmlinfile), new StreamResult(new BufferedOutputStream(new FileOutputStream(xmloutfile)))); |
|
272 |
cleanMemory(); // save memory |
|
273 |
// System.out.println("Done"); |
|
274 |
return true; |
|
275 |
} |
|
276 |
|
|
277 |
/** |
|
278 |
* Process files without xslt args. |
|
279 |
* |
|
280 |
* @param xmlinfile the xmlinfile |
|
281 |
* @param xmloutfile the xmloutfile. If not set, xmlinfile content is replaced |
|
282 |
* @return true, if successful |
|
283 |
* @throws TransformerException |
|
284 |
* @throws IOException |
|
285 |
*/ |
|
286 |
public boolean process(File xmlinfile, File xmloutfile) throws TransformerException, IOException { |
|
287 |
|
|
278 | 288 |
// bufferize result in a String |
279 | 289 |
StringWriter strWriter = new StringWriter(); |
280 | 290 |
StreamResult out = new StreamResult(strWriter); |
... | ... | |
283 | 293 |
File inputfile_copy = null; |
284 | 294 |
|
285 | 295 |
if (debug) { |
286 |
//FIXME: old version |
|
287 |
//File inputfile_copy = new File("/tmp", xmlinfile.getName()); |
|
288 |
//FIXME: new version |
|
296 |
// FIXME: old version
|
|
297 |
// File inputfile_copy = new File("/tmp", xmlinfile.getName());
|
|
298 |
// FIXME: new version
|
|
289 | 299 |
tmp_dir = new File(xmlinfile.getParentFile().getAbsolutePath() + "/tmp"); |
290 | 300 |
tmp_dir.mkdirs(); |
291 | 301 |
tmp_dir.deleteOnExit(); |
292 | 302 |
|
293 | 303 |
inputfile_copy = new File(tmp_dir, xmlinfile.getName()); |
294 | 304 |
|
295 |
System.out.println("copying inputfile to: "+inputfile_copy.getAbsolutePath()); |
|
296 |
//FIXME: old version |
|
297 |
//inputfile_copy.delete(); |
|
298 |
//FIXME: new version |
|
305 |
// System.out.println("copying inputfile to: "+inputfile_copy.getAbsolutePath());
|
|
306 |
// FIXME: old version
|
|
307 |
// inputfile_copy.delete();
|
|
308 |
// FIXME: new version
|
|
299 | 309 |
inputfile_copy.deleteOnExit(); |
300 | 310 |
FileCopy.copy(xmlinfile, inputfile_copy); |
301 | 311 |
|
302 | 312 |
} |
303 |
|
|
313 |
|
|
304 | 314 |
transformer.transform(new StreamSource(xmlinfile), out); |
305 |
|
|
315 |
|
|
306 | 316 |
// then write the result in the result file |
307 | 317 |
if (xmloutfile == null) { |
308 | 318 |
xmloutfile = xmlinfile; |
309 | 319 |
} |
310 | 320 |
|
311 |
BufferedWriter w = new BufferedWriter(new FileWriter(xmloutfile)); |
|
312 |
w.write(strWriter.toString()); |
|
313 |
// System.out.println("DEBUG: "+xmloutfile); |
|
314 |
// System.out.println(" : "+strWriter.toString().substring(0, 60)); |
|
315 |
w.close(); |
|
316 |
|
|
317 |
// if (debug && inputfile_copy != null) { |
|
318 |
// //FIXME: old version |
|
319 |
// //File inputfile_copy = new File("/tmp", xmlinfile.getName()); |
|
320 |
// |
|
321 |
// String s = Diff.diffFile(inputfile_copy, xmloutfile); |
|
322 |
// if (s.length() > 0) { // for ultra debug? |
|
323 |
// System.out.println("Result "+xmlinfile.getName()+" VS "+ xmloutfile.getName()+" diff="+s); |
|
324 |
// } else { |
|
325 |
// System.out.println("Warning: no diff between "+inputfile_copy+" and "+ xmloutfile); |
|
326 |
// } |
|
327 |
// } |
|
321 |
BufferedWriter w = new BufferedWriter(new FileWriter(xmloutfile)); |
|
322 |
w.write(strWriter.toString()); |
|
323 |
// System.out.println("DEBUG: "+xmloutfile); |
|
324 |
// System.out.println(" : "+strWriter.toString().substring(0, 60)); |
|
325 |
w.close(); |
|
328 | 326 |
|
329 |
|
|
330 |
cleanMemory(); |
|
331 |
return true; |
|
332 |
} |
|
333 |
|
|
327 |
// if (debug && inputfile_copy != null) { |
|
328 |
// //FIXME: old version |
|
329 |
// //File inputfile_copy = new File("/tmp", xmlinfile.getName()); |
|
330 |
// |
|
331 |
// String s = Diff.diffFile(inputfile_copy, xmloutfile); |
|
332 |
// if (s.length() > 0) { // for ultra debug? |
|
333 |
// System.out.println("Result "+xmlinfile.getName()+" VS "+ xmloutfile.getName()+" diff="+s); |
|
334 |
// } else { |
|
335 |
// System.out.println("Warning: no diff between "+inputfile_copy+" and "+ xmloutfile); |
|
336 |
// } |
|
337 |
// } |
|
338 |
|
|
339 |
|
|
340 |
cleanMemory(); |
|
341 |
return true; |
|
342 |
} |
|
343 |
|
|
334 | 344 |
/** |
335 | 345 |
* Transform files and select files : nor hidden and nor technical (DTD, XSL, etc.) |
336 | 346 |
* |
... | ... | |
339 | 349 |
* @param outdir |
340 | 350 |
* @return |
341 | 351 |
* @throws TransformerException |
342 |
* @throws IOException |
|
343 |
*/ |
|
344 |
public static boolean processImportSources(File xslFile, File srcdir, File outdir) throws TransformerException, IOException |
|
345 |
{ |
|
346 |
HashMap<String, Object> params = new HashMap<String, Object>(); |
|
347 |
return processImportSources(xslFile, srcdir, outdir, params); |
|
352 |
* @throws IOException |
|
353 |
*/ |
|
354 |
public static boolean processImportSources(File xslFile, File srcdir, File outdir) throws TransformerException, IOException { |
|
355 |
HashMap<String, Object> params = new HashMap<>(); |
|
356 |
return processImportSources(xslFile, srcdir, outdir, params); |
|
348 | 357 |
} |
349 |
|
|
358 |
|
|
350 | 359 |
/** |
351 | 360 |
* Transform files and select files : nor hidden and nor technical (DTD, XSL, etc.) |
352 | 361 |
* |
... | ... | |
356 | 365 |
* @param params XSL parameters |
357 | 366 |
* @return |
358 | 367 |
* @throws TransformerException |
359 |
* @throws IOException
|
|
368 |
* @throws IOException |
|
360 | 369 |
*/ |
361 |
public static boolean processImportSources(File xslFile, File srcdir, File outdir, HashMap<String, Object> params) throws TransformerException, IOException |
|
362 |
{ |
|
370 |
public static boolean processImportSources(File xslFile, File srcdir, File outdir, HashMap<String, Object> params) throws TransformerException, IOException { |
|
363 | 371 |
return processImportSources(xslFile, srcdir, outdir, params, false); |
364 | 372 |
} |
365 |
|
|
373 |
|
|
366 | 374 |
/** |
367 | 375 |
* |
368 | 376 |
* @param srcdir directory |
... | ... | |
370 | 378 |
*/ |
371 | 379 |
public static File[] listFiles(File srcdir) { |
372 | 380 |
File[] files = srcdir.listFiles(new FileFilter() { |
381 |
|
|
373 | 382 |
@Override |
374 | 383 |
public boolean accept(File f) { |
375 | 384 |
String name = f.getName(); |
... | ... | |
387 | 396 |
}); |
388 | 397 |
return files; |
389 | 398 |
} |
390 |
|
|
399 |
|
|
391 | 400 |
/** |
392 | 401 |
* Transform files and select files : nor hidden and nor technical (DTD, XSL, etc.) |
393 | 402 |
* |
... | ... | |
397 | 406 |
* @param params XSL parameters |
398 | 407 |
* @return |
399 | 408 |
* @throws TransformerException |
400 |
* @throws IOException |
|
401 |
*/ |
|
402 |
public static boolean processImportSources(File xslFile, File srcdir, File outdir, HashMap<String, Object> params, boolean deleteOutput) throws TransformerException, IOException |
|
403 |
{ |
|
404 |
outdir.mkdir(); |
|
405 |
if (xslFile.exists()) { |
|
409 |
* @throws IOException |
|
410 |
*/ |
|
411 |
public static boolean processImportSources(File xslFile, File srcdir, File outdir, HashMap<String, Object> params, boolean deleteOutput) throws TransformerException, IOException { |
|
412 |
outdir.mkdir(); |
|
413 |
if (xslFile.exists()) { |
|
406 | 414 |
ApplyXsl2 builder = new ApplyXsl2(xslFile); |
407 | 415 |
boolean debug = Log.getLevel().intValue() < Level.INFO.intValue(); |
408 | 416 |
if (debug) { |
... | ... | |
411 | 419 |
} |
412 | 420 |
if (!params.containsKey("output-directory")) |
413 | 421 |
params.put("output-directory", outdir.getAbsoluteFile().toURI().toString()); |
414 |
|
|
415 |
for (String name : params.keySet())
|
|
422 |
|
|
423 |
for (String name : params.keySet()) |
|
416 | 424 |
builder.setParam(name, params.get(name)); |
417 | 425 |
|
418 | 426 |
File[] files = listFiles(srcdir); // get files XSL compatible |
419 | 427 |
if (files == null) return false; |
420 |
|
|
421 |
System.out.println("-- Applying "+xslFile+" XSL to "+files.length+" (from "+srcdir+") files with parameters: "+params+" on directory "+srcdir+" result written in "+outdir); |
|
422 |
|
|
423 |
ConsoleProgressBar cpb = new ConsoleProgressBar(files.length); |
|
428 |
|
|
429 |
System.out.println("-- Applying " + xslFile + " XSL to " + files.length + " (from " + srcdir + ") files with parameters: " + params + " on directory " + srcdir + " result written in " |
|
430 |
+ outdir); |
|
431 |
|
|
432 |
ConsoleProgressBar cpb = new ConsoleProgressBar(files.length); |
|
424 | 433 |
for (File f : files) { |
425 |
String name = f.getName();
|
|
426 |
|
|
427 |
cpb.tick();
|
|
434 |
String name = f.getName(); |
|
435 |
|
|
436 |
cpb.tick(); |
|
428 | 437 |
File outfile = new File(outdir, name); |
429 | 438 |
|
430 | 439 |
if (!f.getAbsolutePath().equals(outfile.getAbsolutePath()) && f.lastModified() <= outfile.lastModified()) { |
431 |
if (debug) System.out.println("skipped: "+f);
|
|
440 |
if (debug) System.out.println("skipped: " + f);
|
|
432 | 441 |
continue; // input file is older than output file |
433 | 442 |
} |
434 | 443 |
|
435 | 444 |
if (deleteOutput) outfile = null; // if outfile is null builder.process won't create the output file adn will replace f |
436 |
|
|
437 |
if (!builder.process(f, outfile)) {
|
|
438 |
System.out.println("Failed to process file "+ f);
|
|
439 |
if (outfile != null) outfile.delete();
|
|
445 |
|
|
446 |
if (!builder.process(f, outfile)) { |
|
447 |
System.out.println("Failed to process file " + f);
|
|
448 |
if (outfile != null) outfile.delete(); |
|
440 | 449 |
} |
441 |
} |
|
442 |
System.out.println(""); |
|
443 |
return true; |
|
444 |
} else { |
|
445 |
System.out.println("XSL file does not exists: "+ xslFile); |
|
446 |
} |
|
447 |
return false; |
|
450 |
} |
|
451 |
System.out.println(""); |
|
452 |
return true; |
|
453 |
} |
|
454 |
else { |
|
455 |
System.out.println("XSL file does not exists: " + xslFile); |
|
456 |
} |
|
457 |
return false; |
|
448 | 458 |
} |
449 |
|
|
459 |
|
|
450 | 460 |
/** |
451 | 461 |
* Transform files and select files : nor hidden and nor technical (DTD, XSL, etc.) |
452 | 462 |
* |
453 |
* Apply one XSL, XML files are replaced
|
|
463 |
* Apply one XSL, XML files are replaced |
|
454 | 464 |
* |
455 | 465 |
* @param xslFile |
456 | 466 |
* @param pSrcDirectory |
... | ... | |
458 | 468 |
* @param params XSL parameters |
459 | 469 |
* @return |
460 | 470 |
* @throws TransformerException |
461 |
* @throws IOException
|
|
471 |
* @throws IOException |
|
462 | 472 |
*/ |
463 |
public static boolean processImportSources(File xslFile, List<File> files, HashMap<String, Object> params) throws TransformerException, IOException |
|
464 |
{ |
|
473 |
public static boolean processImportSources(File xslFile, List<File> files, HashMap<String, Object> params) throws TransformerException, IOException { |
|
465 | 474 |
return processImportSources(xslFile, files.toArray(new File[files.size()]), params); |
466 | 475 |
} |
467 |
|
|
476 |
|
|
468 | 477 |
/** |
469 | 478 |
* Transform files and select files : nor hidden and nor technical (DTD, XSL, etc.) |
470 | 479 |
* |
471 |
* Apply one XSL, XML files are replaced
|
|
480 |
* Apply one XSL, XML files are replaced |
|
472 | 481 |
* |
473 | 482 |
* @param xslFile |
474 | 483 |
* @param pSrcDirectory |
... | ... | |
476 | 485 |
* @param params XSL parameters |
477 | 486 |
* @return |
478 | 487 |
* @throws TransformerException |
479 |
* @throws IOException
|
|
488 |
* @throws IOException |
|
480 | 489 |
*/ |
481 |
public static boolean processImportSources(File xslFile, File[] files, HashMap<String, Object> params) throws TransformerException, IOException |
|
482 |
{ |
|
490 |
public static boolean processImportSources(File xslFile, File[] files, HashMap<String, Object> params) throws TransformerException, IOException { |
|
483 | 491 |
if (xslFile.exists()) { |
484 | 492 |
ApplyXsl2 builder = new ApplyXsl2(xslFile); |
485 |
for(String name : params.keySet()) |
|
493 |
for (String name : params.keySet())
|
|
486 | 494 |
builder.setParam(name, params.get(name)); |
487 |
|
|
495 |
|
|
488 | 496 |
if (files == null) return false; |
489 | 497 |
|
490 |
System.out.println("-- Applying "+xslFile+" XSL to "+files.length+" files with parameters: "+params);
|
|
498 |
System.out.println("-- Applying " + xslFile + " XSL to " + files.length + " files with parameters: " + params);
|
|
491 | 499 |
|
492 | 500 |
ConsoleProgressBar cpb = new ConsoleProgressBar(files.length); |
493 |
|
|
501 |
|
|
494 | 502 |
for (File f : files) { |
495 | 503 |
String name = f.getName(); |
496 | 504 |
if (f.isHidden() || f.isDirectory()) continue; |
... | ... | |
499 | 507 |
if (name.endsWith(".csv")) continue; |
500 | 508 |
if (name.endsWith(".dtd")) continue; |
501 | 509 |
if (name.endsWith(".xsl")) continue; |
502 |
if (name.endsWith("~")) continue;
|
|
510 |
if (name.endsWith("~")) continue;
|
|
503 | 511 |
if (name.startsWith(".")) continue; |
504 | 512 |
|
505 | 513 |
if (!ValidateXml.test(f)) { |
506 |
System.out.println("src file "+f+", is not XML valid");
|
|
514 |
System.out.println("src file " + f + ", is not XML valid");
|
|
507 | 515 |
return false; |
508 | 516 |
} |
509 | 517 |
cpb.tick(); |
510 | 518 |
|
511 | 519 |
if (!builder.process(f, null)) { |
512 |
System.out.println("Failed with file "+ f); |
|
520 |
System.out.println("Failed with file " + f);
|
|
513 | 521 |
} |
514 | 522 |
} |
515 | 523 |
System.out.println(""); |
516 | 524 |
return true; |
517 |
} else { |
|
518 |
System.out.println("XSL file does not exists: "+ xslFile); |
|
519 | 525 |
} |
526 |
else { |
|
527 |
System.out.println("XSL file does not exists: " + xslFile); |
|
528 |
} |
|
520 | 529 |
return false; |
521 | 530 |
} |
522 |
|
|
523 |
/**
|
|
531 |
|
|
532 |
/** |
|
524 | 533 |
* Apply the XSL listed in the xslDirectory. Files are replaced |
525 | 534 |
* |
526 | 535 |
* @param filesToProcess |
... | ... | |
535 | 544 |
File[] xslFiles = xslDirectory.listFiles(IOUtils.HIDDENFILE_FILTER); |
536 | 545 |
Arrays.sort(xslFiles); |
537 | 546 |
return processWithMultipleXSL(filesToProcess, xslFiles, xslParams); |
538 |
} else { |
|
547 |
} |
|
548 |
else { |
|
539 | 549 |
System.out.println("Nothing to do."); |
540 | 550 |
} |
541 | 551 |
return true; |
542 | 552 |
} |
543 |
|
|
544 |
/**
|
|
553 |
|
|
554 |
/** |
|
545 | 555 |
* Apply the XSL list. Files are replaced |
546 | 556 |
* |
547 | 557 |
* @param filesToProcess |
... | ... | |
552 | 562 |
* @throws IOException |
553 | 563 |
*/ |
554 | 564 |
public static boolean processWithMultipleXSL(File[] filesToProcess, File[] xslFiles, HashMap<String, Object> xslParams) throws TransformerException, IOException { |
555 |
|
|
565 |
|
|
556 | 566 |
Arrays.sort(xslFiles); |
557 | 567 |
for (File xslFile : xslFiles) { |
558 | 568 |
if (xslFile.isDirectory() || xslFile.isHidden() || !xslFile.getName().endsWith(".xsl")) continue; |
559 | 569 |
|
560 | 570 |
if (processImportSources(xslFile, filesToProcess, xslParams)) { |
561 | 571 |
System.out.println(""); |
562 |
} else { |
|
572 |
} |
|
573 |
else { |
|
563 | 574 |
return false; |
564 | 575 |
} |
565 | 576 |
} |
566 |
|
|
577 |
|
|
567 | 578 |
return true; |
568 |
} |
|
569 |
|
|
570 |
/** |
|
571 |
* The main method. |
|
572 |
* |
|
573 |
* @param args the arguments |
|
574 |
* @throws TransformerException |
|
575 |
* @throws FileNotFoundException |
|
576 |
*/ |
|
577 |
public static void main(String[] args) throws Exception { |
|
578 |
//String xslfile = "/home/mdecorde/TXM/xsl/breakByMilestone.xsl"; |
|
579 |
String xslfile = "G:/Feuilles de style/filtre-frantext-query-export4txm.xsl"; |
|
580 |
File xslFile = new File(xslfile); |
|
581 |
ApplyXsl2 a = new ApplyXsl2(xslFile); |
|
582 |
|
|
583 |
//File srcdirectory = new File("/home/mdecorde/TXM/corpora/transcribertest/HTML"); |
|
584 |
File srcdirectory = new File("C:/Documents and Settings/alavrent/xml/applyxsl"); |
|
585 |
assert(srcdirectory.exists()); |
|
586 |
assert(srcdirectory.isDirectory()); |
|
587 |
assert(srcdirectory.canRead()); |
|
588 |
assert(srcdirectory.canExecute()); |
|
589 |
|
|
590 |
File[] files = srcdirectory.listFiles(IOUtils.HIDDENFILE_FILTER); |
|
591 |
System.out.println("process : "+srcdirectory+"/*"); |
|
592 |
System.out.println("with : "+xslfile); |
|
593 |
long bigstart = System.currentTimeMillis(); |
|
594 |
for (File infile : files) { |
|
595 |
if (infile.canRead() && infile.getName().endsWith(".xml")) { |
|
596 |
File outfile = new File(infile.getAbsolutePath()+".out.xml"); |
|
597 |
long start = System.currentTimeMillis(); |
|
598 |
System.out.println("Process : "+infile.getName()+ " >> "); |
|
599 |
//String[] params = {"pbval1",""+1,"pbval2",""+2}; |
|
600 |
a.process(infile,outfile); // no parameters |
|
601 |
//a.process( infile.getAbsolutePath() ,outfile,params); |
|
602 |
System.out.println("Done : "+(System.currentTimeMillis()-start)+"ms"); |
|
603 |
} |
|
604 |
} |
|
605 |
System.out.println("Total time : "+(System.currentTimeMillis()-bigstart)+"ms"); |
|
606 |
} |
|
607 |
} |
|
579 |
} |
|
580 |
|
|
581 |
/** |
|
582 |
* The main method. |
|
583 |
* |
|
584 |
* @param args the arguments |
|
585 |
* @throws TransformerException |
|
586 |
* @throws FileNotFoundException |
|
587 |
*/ |
|
588 |
public static void main(String[] args) throws Exception { |
|
589 |
// String xslfile = "/home/mdecorde/TXM/xsl/breakByMilestone.xsl"; |
|
590 |
String xslfile = "G:/Feuilles de style/filtre-frantext-query-export4txm.xsl"; |
|
591 |
File xslFile = new File(xslfile); |
|
592 |
ApplyXsl2 a = new ApplyXsl2(xslFile); |
|
593 |
|
|
594 |
// File srcdirectory = new File("/home/mdecorde/TXM/corpora/transcribertest/HTML"); |
|
595 |
File srcdirectory = new File("C:/Documents and Settings/alavrent/xml/applyxsl"); |
|
596 |
assert (srcdirectory.exists()); |
|
597 |
assert (srcdirectory.isDirectory()); |
|
598 |
assert (srcdirectory.canRead()); |
|
599 |
assert (srcdirectory.canExecute()); |
|
600 |
|
|
601 |
File[] files = srcdirectory.listFiles(IOUtils.HIDDENFILE_FILTER); |
|
602 |
System.out.println("process : " + srcdirectory + "/*"); |
|
603 |
System.out.println("with : " + xslfile); |
|
604 |
long bigstart = System.currentTimeMillis(); |
|
605 |
for (File infile : files) { |
|
606 |
if (infile.canRead() && infile.getName().endsWith(".xml")) { |
|
607 |
File outfile = new File(infile.getAbsolutePath() + ".out.xml"); |
|
608 |
long start = System.currentTimeMillis(); |
|
609 |
System.out.println("Process : " + infile.getName() + " >> "); |
|
610 |
// String[] params = {"pbval1",""+1,"pbval2",""+2}; |
|
611 |
a.process(infile, outfile); // no parameters |
|
612 |
// a.process( infile.getAbsolutePath() ,outfile,params); |
|
613 |
System.out.println("Done : " + (System.currentTimeMillis() - start) + "ms"); |
|
614 |
} |
|
615 |
} |
|
616 |
System.out.println("Total time : " + (System.currentTimeMillis() - bigstart) + "ms"); |
|
617 |
} |
|
618 |
} |
Formats disponibles : Unified diff