Révision 148
tmp/org.txm.core/src/java/org/txm/utils/DiffPrint.java (revision 148) | ||
---|---|---|
1 |
package org.txm.utils; |
|
2 |
/* |
|
3 |
* $Log: DiffPrint.java,v $ |
|
4 |
* Revision 1.10 2009/04/20 18:53:29 stuart |
|
5 |
* Make print_header public. |
|
6 |
* |
|
7 |
* Revision 1.9 2009/01/19 03:05:26 stuart |
|
8 |
* Fix StackOverflow bug with heuristic on reported by Jimmy Han. |
|
9 |
* |
|
10 |
* Revision 1.8 2007/12/20 05:04:21 stuart |
|
11 |
* Can no longer import from default package. |
|
12 |
* |
|
13 |
* Revision 1.7 2007/12/20 04:29:53 stuart |
|
14 |
* Fix setupOutput permission. Thanks to Sargon Benjamin |
|
15 |
* |
|
16 |
* Revision 1.6 2005/04/27 02:13:40 stuart |
|
17 |
* Add Str.dup() |
|
18 |
* |
|
19 |
* Revision 1.5 2004/01/29 02:35:35 stuart |
|
20 |
* Test for out of bounds exception in UnifiedPrint.print_hunk. |
|
21 |
* Add setOutput() to DiffPrint.Base. |
|
22 |
* |
|
23 |
* Revision 1.4 2003/04/22 01:50:47 stuart |
|
24 |
* add Unified format diff |
|
25 |
* |
|
26 |
* Revision 1.3 2003/04/22 01:00:32 stuart |
|
27 |
* added context diff format |
|
28 |
* |
|
29 |
* Revision 1.2 2000/03/02 16:59:54 stuart |
|
30 |
* add GPL |
|
31 |
* |
|
32 |
*/ |
|
33 |
|
|
34 |
import java.io.*; |
|
35 |
import java.util.Vector; |
|
36 |
import java.util.Date; |
|
37 |
|
|
38 |
interface UnaryPredicate { |
|
39 |
boolean execute(Object obj); |
|
40 |
} |
|
41 |
|
|
42 |
/** A simple framework for printing change lists produced by <code>Diff</code>. |
|
43 |
@see bmsi.util.Diff |
|
44 |
@author Stuart D. Gathman |
|
45 |
Copyright (C) 2000 Business Management Systems, Inc. |
|
46 |
<p> |
|
47 |
This program is free software; you can redistribute it and/or modify |
|
48 |
it under the terms of the GNU General Public License as published by |
|
49 |
the Free Software Foundation; either version 1, or (at your option) |
|
50 |
any later version. |
|
51 |
<p> |
|
52 |
This program is distributed in the hope that it will be useful, |
|
53 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
54 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
55 |
GNU General Public License for more details. |
|
56 |
<p> |
|
57 |
You should have received a copy of the GNU General Public License |
|
58 |
along with this program; if not, write to the Free Software |
|
59 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
60 |
*/ |
|
61 |
public class DiffPrint { |
|
62 |
/** A Base class for printing edit scripts produced by Diff. |
|
63 |
This class divides the change list into "hunks", and calls |
|
64 |
<code>print_hunk</code> for each hunk. Various utility methods |
|
65 |
are provided as well. |
|
66 |
*/ |
|
67 |
public static abstract class Base { |
|
68 |
protected PrintWriter outfile; |
|
69 |
public void setOutput(Writer wtr) { |
|
70 |
outfile = new PrintWriter(wtr); |
|
71 |
} |
|
72 |
protected void setupOutput() { |
|
73 |
if (outfile == null) |
|
74 |
outfile = new PrintWriter(new OutputStreamWriter(System.out)); |
|
75 |
} |
|
76 |
protected Base(Object[] a,Object[] b) { |
|
77 |
file0 = a; |
|
78 |
file1 = b; |
|
79 |
} |
|
80 |
/** Set to ignore certain kinds of lines when printing |
|
81 |
an edit script. For example, ignoring blank lines or comments. |
|
82 |
*/ |
|
83 |
protected UnaryPredicate ignore = null; |
|
84 |
|
|
85 |
/** Set to the lines of the files being compared. |
|
86 |
*/ |
|
87 |
protected Object[] file0, file1; |
|
88 |
|
|
89 |
/** Divide SCRIPT into pieces by calling HUNKFUN and |
|
90 |
print each piece with PRINTFUN. |
|
91 |
Both functions take one arg, an edit script. |
|
92 |
|
|
93 |
PRINTFUN takes a subscript which belongs together (with a null |
|
94 |
link at the end) and prints it. */ |
|
95 |
public void print_script(Diff.change script) { |
|
96 |
setupOutput(); |
|
97 |
Diff.change next = script; |
|
98 |
|
|
99 |
while (next != null) |
|
100 |
{ |
|
101 |
Diff.change t, end; |
|
102 |
|
|
103 |
/* Find a set of changes that belong together. */ |
|
104 |
t = next; |
|
105 |
end = hunkfun(next); |
|
106 |
|
|
107 |
/* Disconnect them from the rest of the changes, |
|
108 |
making them a hunk, and remember the rest for next iteration. */ |
|
109 |
next = end.link; |
|
110 |
end.link = null; |
|
111 |
//if (DEBUG) |
|
112 |
// debug_script(t); |
|
113 |
|
|
114 |
/* Print this hunk. */ |
|
115 |
print_hunk(t); |
|
116 |
|
|
117 |
/* Reconnect the script so it will all be freed properly. */ |
|
118 |
end.link = next; |
|
119 |
} |
|
120 |
outfile.flush(); |
|
121 |
} |
|
122 |
|
|
123 |
/** Called with the tail of the script |
|
124 |
and returns the last link that belongs together with the start |
|
125 |
of the tail. */ |
|
126 |
|
|
127 |
protected Diff.change hunkfun(Diff.change hunk) { |
|
128 |
return hunk; |
|
129 |
} |
|
130 |
|
|
131 |
protected int first0, last0, first1, last1, deletes, inserts; |
|
132 |
|
|
133 |
/** Look at a hunk of edit script and report the range of lines in each file |
|
134 |
that it applies to. HUNK is the start of the hunk, which is a chain |
|
135 |
of `struct change'. The first and last line numbers of file 0 are stored |
|
136 |
in *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1. |
|
137 |
Note that these are internal line numbers that count from 0. |
|
138 |
|
|
139 |
If no lines from file 0 are deleted, then FIRST0 is LAST0+1. |
|
140 |
|
|
141 |
Also set *DELETES nonzero if any lines of file 0 are deleted |
|
142 |
and set *INSERTS nonzero if any lines of file 1 are inserted. |
|
143 |
If only ignorable lines are inserted or deleted, both are |
|
144 |
set to 0. */ |
|
145 |
|
|
146 |
protected void analyze_hunk(Diff.change hunk) { |
|
147 |
int f0, l0 = 0, f1, l1 = 0, show_from = 0, show_to = 0; |
|
148 |
int i; |
|
149 |
Diff.change next; |
|
150 |
boolean nontrivial = (ignore == null); |
|
151 |
|
|
152 |
show_from = show_to = 0; |
|
153 |
|
|
154 |
f0 = hunk.line0; |
|
155 |
f1 = hunk.line1; |
|
156 |
|
|
157 |
for (next = hunk; next != null; next = next.link) |
|
158 |
{ |
|
159 |
l0 = next.line0 + next.deleted - 1; |
|
160 |
l1 = next.line1 + next.inserted - 1; |
|
161 |
show_from += next.deleted; |
|
162 |
show_to += next.inserted; |
|
163 |
for (i = next.line0; i <= l0 && ! nontrivial; i++) |
|
164 |
if (!ignore.execute(file0[i])) |
|
165 |
nontrivial = true; |
|
166 |
for (i = next.line1; i <= l1 && ! nontrivial; i++) |
|
167 |
if (!ignore.execute(file1[i])) |
|
168 |
nontrivial = true; |
|
169 |
} |
|
170 |
|
|
171 |
first0 = f0; |
|
172 |
last0 = l0; |
|
173 |
first1 = f1; |
|
174 |
last1 = l1; |
|
175 |
|
|
176 |
/* If all inserted or deleted lines are ignorable, |
|
177 |
tell the caller to ignore this hunk. */ |
|
178 |
|
|
179 |
if (!nontrivial) |
|
180 |
show_from = show_to = 0; |
|
181 |
|
|
182 |
deletes = show_from; |
|
183 |
inserts = show_to; |
|
184 |
} |
|
185 |
|
|
186 |
/** Called to print the script header which identifies the files compared. |
|
187 |
The default does nothing (except set output to system.out if |
|
188 |
not otherwise set). Derived style classes can override to print |
|
189 |
the files compared in the format for that style. |
|
190 |
*/ |
|
191 |
public void print_header(String filea, String fileb) { |
|
192 |
setupOutput(); |
|
193 |
} |
|
194 |
|
|
195 |
protected abstract void print_hunk(Diff.change hunk); |
|
196 |
|
|
197 |
protected void print_1_line(String pre,Object linbuf) { |
|
198 |
outfile.println(pre + linbuf.toString()); |
|
199 |
} |
|
200 |
|
|
201 |
/** Print a pair of line numbers with SEPCHAR, translated for file FILE. |
|
202 |
If the two numbers are identical, print just one number. |
|
203 |
|
|
204 |
Args A and B are internal line numbers. |
|
205 |
We print the translated (real) line numbers. */ |
|
206 |
|
|
207 |
protected void print_number_range (char sepchar, int a, int b) { |
|
208 |
/* Note: we can have B < A in the case of a range of no lines. |
|
209 |
In this case, we should print the line number before the range, |
|
210 |
which is B. */ |
|
211 |
if (++b > ++a) |
|
212 |
outfile.print("" + a + sepchar + b); |
|
213 |
else |
|
214 |
outfile.print(b); |
|
215 |
} |
|
216 |
|
|
217 |
public static char change_letter(int inserts, int deletes) { |
|
218 |
if (inserts == 0) |
|
219 |
return 'd'; |
|
220 |
else if (deletes == 0) |
|
221 |
return 'a'; |
|
222 |
else |
|
223 |
return 'c'; |
|
224 |
} |
|
225 |
} |
|
226 |
|
|
227 |
/** Print a change list in the standard diff format. |
|
228 |
*/ |
|
229 |
public static class NormalPrint extends Base { |
|
230 |
|
|
231 |
public NormalPrint(Object[] a,Object[] b) { |
|
232 |
super(a,b); |
|
233 |
} |
|
234 |
|
|
235 |
/** Print a hunk of a normal diff. |
|
236 |
This is a contiguous portion of a complete edit script, |
|
237 |
describing changes in consecutive lines. */ |
|
238 |
|
|
239 |
protected void print_hunk (Diff.change hunk) { |
|
240 |
|
|
241 |
/* Determine range of line numbers involved in each file. */ |
|
242 |
analyze_hunk(hunk); |
|
243 |
if (deletes == 0 && inserts == 0) |
|
244 |
return; |
|
245 |
|
|
246 |
/* Print out the line number header for this hunk */ |
|
247 |
print_number_range (',', first0, last0); |
|
248 |
outfile.print(change_letter(inserts, deletes)); |
|
249 |
print_number_range (',', first1, last1); |
|
250 |
outfile.println(); |
|
251 |
|
|
252 |
/* Print the lines that the first file has. */ |
|
253 |
if (deletes != 0) |
|
254 |
for (int i = first0; i <= last0; i++) |
|
255 |
print_1_line ("< ", file0[i]); |
|
256 |
|
|
257 |
if (inserts != 0 && deletes != 0) |
|
258 |
outfile.println("---"); |
|
259 |
|
|
260 |
/* Print the lines that the second file has. */ |
|
261 |
if (inserts != 0) |
|
262 |
for (int i = first1; i <= last1; i++) |
|
263 |
print_1_line ("> ", file1[i]); |
|
264 |
} |
|
265 |
} |
|
266 |
|
|
267 |
/** Prints an edit script in a format suitable for input to <code>ed</code>. |
|
268 |
The edit script must be generated with the reverse option to |
|
269 |
be useful as actual <code>ed</code> input. |
|
270 |
*/ |
|
271 |
public static class EdPrint extends Base { |
|
272 |
|
|
273 |
public EdPrint(Object[] a,Object[] b) { |
|
274 |
super(a,b); |
|
275 |
} |
|
276 |
|
|
277 |
/** Print a hunk of an ed diff */ |
|
278 |
protected void print_hunk(Diff.change hunk) { |
|
279 |
|
|
280 |
/* Determine range of line numbers involved in each file. */ |
|
281 |
analyze_hunk (hunk); |
|
282 |
if (deletes == 0 && inserts == 0) |
|
283 |
return; |
|
284 |
|
|
285 |
/* Print out the line number header for this hunk */ |
|
286 |
print_number_range (',', first0, last0); |
|
287 |
outfile.println(change_letter(inserts, deletes)); |
|
288 |
|
|
289 |
/* Print new/changed lines from second file, if needed */ |
|
290 |
if (inserts != 0) |
|
291 |
{ |
|
292 |
boolean inserting = true; |
|
293 |
for (int i = first1; i <= last1; i++) |
|
294 |
{ |
|
295 |
/* Resume the insert, if we stopped. */ |
|
296 |
if (! inserting) |
|
297 |
outfile.println(i - first1 + first0 + "a"); |
|
298 |
inserting = true; |
|
299 |
|
|
300 |
/* If the file's line is just a dot, it would confuse `ed'. |
|
301 |
So output it with a double dot, and set the flag LEADING_DOT |
|
302 |
so that we will output another ed-command later |
|
303 |
to change the double dot into a single dot. */ |
|
304 |
|
|
305 |
if (".".equals(file1[i])) |
|
306 |
{ |
|
307 |
outfile.println(".."); |
|
308 |
outfile.println("."); |
|
309 |
/* Now change that double dot to the desired single dot. */ |
|
310 |
outfile.println(i - first1 + first0 + 1 + "s/^\\.\\././"); |
|
311 |
inserting = false; |
|
312 |
} |
|
313 |
else |
|
314 |
/* Line is not `.', so output it unmodified. */ |
|
315 |
print_1_line ("", file1[i]); |
|
316 |
} |
|
317 |
|
|
318 |
/* End insert mode, if we are still in it. */ |
|
319 |
if (inserting) |
|
320 |
outfile.println("."); |
|
321 |
} |
|
322 |
} |
|
323 |
} |
|
324 |
|
|
325 |
/** Prints an edit script in context diff format. This and its |
|
326 |
'unified' variation is used for source code patches. |
|
327 |
*/ |
|
328 |
public static class ContextPrint extends Base { |
|
329 |
|
|
330 |
protected int context = 3; |
|
331 |
|
|
332 |
public ContextPrint(Object[] a,Object[] b) { |
|
333 |
super(a,b); |
|
334 |
} |
|
335 |
|
|
336 |
protected void print_context_label (String mark, File inf, String label) { |
|
337 |
setupOutput(); |
|
338 |
if (label != null) |
|
339 |
outfile.println(mark + ' ' + label); |
|
340 |
else if (inf.lastModified() > 0) |
|
341 |
// FIXME: use DateFormat to get precise format needed. |
|
342 |
outfile.println( |
|
343 |
mark + ' ' + inf.getPath() + '\t' + new Date(inf.lastModified()) |
|
344 |
); |
|
345 |
else |
|
346 |
/* Don't pretend that standard input is ancient. */ |
|
347 |
outfile.println(mark + ' ' + inf.getPath()); |
|
348 |
} |
|
349 |
|
|
350 |
public void print_header(String filea,String fileb) { |
|
351 |
print_context_label ("***", new File(filea), filea); |
|
352 |
print_context_label ("---", new File(fileb), fileb); |
|
353 |
} |
|
354 |
|
|
355 |
/** If function_regexp defined, search for start of function. */ |
|
356 |
private String find_function(Object[] lines, int start) { |
|
357 |
return null; |
|
358 |
} |
|
359 |
|
|
360 |
protected void print_function(Object[] file,int start) { |
|
361 |
String function = find_function (file0, first0); |
|
362 |
if (function != null) { |
|
363 |
outfile.print(" "); |
|
364 |
outfile.print( |
|
365 |
(function.length() < 40) ? function : function.substring(0,40) |
|
366 |
); |
|
367 |
} |
|
368 |
} |
|
369 |
|
|
370 |
protected void print_hunk(Diff.change hunk) { |
|
371 |
|
|
372 |
/* Determine range of line numbers involved in each file. */ |
|
373 |
|
|
374 |
analyze_hunk (hunk); |
|
375 |
|
|
376 |
if (deletes == 0 && inserts == 0) |
|
377 |
return; |
|
378 |
|
|
379 |
/* Include a context's width before and after. */ |
|
380 |
|
|
381 |
first0 = Math.max(first0 - context, 0); |
|
382 |
first1 = Math.max(first1 - context, 0); |
|
383 |
last0 = Math.min(last0 + context, file0.length - 1); |
|
384 |
last1 = Math.min(last1 + context, file1.length - 1); |
|
385 |
|
|
386 |
|
|
387 |
outfile.print("***************"); |
|
388 |
|
|
389 |
/* If we looked for and found a function this is part of, |
|
390 |
include its name in the header of the diff section. */ |
|
391 |
print_function (file0, first0); |
|
392 |
|
|
393 |
outfile.println(); |
|
394 |
outfile.print("*** "); |
|
395 |
print_number_range (',', first0, last0); |
|
396 |
outfile.println(" ****"); |
|
397 |
|
|
398 |
if (deletes != 0) { |
|
399 |
Diff.change next = hunk; |
|
400 |
|
|
401 |
for (int i = first0; i <= last0; i++) { |
|
402 |
/* Skip past changes that apply (in file 0) |
|
403 |
only to lines before line I. */ |
|
404 |
|
|
405 |
while (next != null && next.line0 + next.deleted <= i) |
|
406 |
next = next.link; |
|
407 |
|
|
408 |
/* Compute the marking for line I. */ |
|
409 |
|
|
410 |
String prefix = " "; |
|
411 |
if (next != null && next.line0 <= i) |
|
412 |
/* The change NEXT covers this line. |
|
413 |
If lines were inserted here in file 1, this is "changed". |
|
414 |
Otherwise it is "deleted". */ |
|
415 |
prefix = (next.inserted > 0) ? "!" : "-"; |
|
416 |
|
|
417 |
print_1_line (prefix, file0[i]); |
|
418 |
} |
|
419 |
} |
|
420 |
|
|
421 |
outfile.print("--- "); |
|
422 |
print_number_range (',', first1, last1); |
|
423 |
outfile.println(" ----"); |
|
424 |
|
|
425 |
if (inserts != 0) { |
|
426 |
Diff.change next = hunk; |
|
427 |
|
|
428 |
for (int i = first1; i <= last1; i++) { |
|
429 |
/* Skip past changes that apply (in file 1) |
|
430 |
only to lines before line I. */ |
|
431 |
|
|
432 |
while (next != null && next.line1 + next.inserted <= i) |
|
433 |
next = next.link; |
|
434 |
|
|
435 |
/* Compute the marking for line I. */ |
|
436 |
|
|
437 |
String prefix = " "; |
|
438 |
if (next != null && next.line1 <= i) |
|
439 |
/* The change NEXT covers this line. |
|
440 |
If lines were deleted here in file 0, this is "changed". |
|
441 |
Otherwise it is "inserted". */ |
|
442 |
prefix = (next.deleted > 0) ? "!" : "+"; |
|
443 |
|
|
444 |
print_1_line (prefix, file1[i]); |
|
445 |
} |
|
446 |
} |
|
447 |
} |
|
448 |
} |
|
449 |
|
|
450 |
/** Prints an edit script in context diff format. This and its |
|
451 |
'unified' variation is used for source code patches. |
|
452 |
*/ |
|
453 |
public static class UnifiedPrint extends ContextPrint { |
|
454 |
|
|
455 |
public UnifiedPrint(Object[] a,Object[] b) { |
|
456 |
super(a,b); |
|
457 |
} |
|
458 |
|
|
459 |
public void print_header(String filea,String fileb) { |
|
460 |
print_context_label ("---", new File(filea), filea); |
|
461 |
print_context_label ("+++", new File(fileb), fileb); |
|
462 |
} |
|
463 |
|
|
464 |
private void print_number_range (int a, int b) { |
|
465 |
//translate_range (file, a, b, &trans_a, &trans_b); |
|
466 |
|
|
467 |
/* Note: we can have B < A in the case of a range of no lines. |
|
468 |
In this case, we should print the line number before the range, |
|
469 |
which is B. */ |
|
470 |
if (b < a) |
|
471 |
outfile.print(b + ",0"); |
|
472 |
else |
|
473 |
super.print_number_range(',',a,b); |
|
474 |
} |
|
475 |
|
|
476 |
protected void print_hunk(Diff.change hunk) { |
|
477 |
/* Determine range of line numbers involved in each file. */ |
|
478 |
analyze_hunk (hunk); |
|
479 |
|
|
480 |
if (deletes == 0 && inserts == 0) |
|
481 |
return; |
|
482 |
|
|
483 |
/* Include a context's width before and after. */ |
|
484 |
|
|
485 |
first0 = Math.max(first0 - context, 0); |
|
486 |
first1 = Math.max(first1 - context, 0); |
|
487 |
last0 = Math.min(last0 + context, file0.length - 1); |
|
488 |
last1 = Math.min(last1 + context, file1.length - 1); |
|
489 |
|
|
490 |
outfile.print("@@ -"); |
|
491 |
print_number_range (first0, last0); |
|
492 |
outfile.print(" +"); |
|
493 |
print_number_range (first1, last1); |
|
494 |
outfile.print(" @@"); |
|
495 |
|
|
496 |
/* If we looked for and found a function this is part of, |
|
497 |
include its name in the header of the diff section. */ |
|
498 |
print_function(file0,first0); |
|
499 |
|
|
500 |
outfile.println(); |
|
501 |
|
|
502 |
Diff.change next = hunk; |
|
503 |
int i = first0; |
|
504 |
int j = first1; |
|
505 |
|
|
506 |
while (i <= last0 || j <= last1) { |
|
507 |
|
|
508 |
/* If the line isn't a difference, output the context from file 0. */ |
|
509 |
|
|
510 |
if (next == null || i < next.line0) { |
|
511 |
if (i < file0.length) { |
|
512 |
outfile.print(' '); |
|
513 |
print_1_line ("", file0[i++]); |
|
514 |
} |
|
515 |
j++; |
|
516 |
} |
|
517 |
else { |
|
518 |
/* For each difference, first output the deleted part. */ |
|
519 |
|
|
520 |
int k = next.deleted; |
|
521 |
while (k-- > 0) { |
|
522 |
outfile.print('-'); |
|
523 |
print_1_line ("", file0[i++]); |
|
524 |
} |
|
525 |
|
|
526 |
/* Then output the inserted part. */ |
|
527 |
|
|
528 |
k = next.inserted; |
|
529 |
while (k-- > 0) { |
|
530 |
outfile.print('+'); |
|
531 |
print_1_line ("", file1[j++]); |
|
532 |
} |
|
533 |
|
|
534 |
/* We're done with this hunk, so on to the next! */ |
|
535 |
|
|
536 |
next = next.link; |
|
537 |
} |
|
538 |
} |
|
539 |
} |
|
540 |
} |
|
541 |
|
|
542 |
|
|
543 |
/** Read a text file into an array of String. This provides basic diff |
|
544 |
functionality. A more advanced diff utility will use specialized |
|
545 |
objects to represent the text lines, with options to, for example, |
|
546 |
convert sequences of whitespace to a single space for comparison |
|
547 |
purposes. |
|
548 |
*/ |
|
549 |
static String[] slurp(String file) throws IOException { |
|
550 |
BufferedReader rdr = new BufferedReader(new FileReader(file)); |
|
551 |
Vector s = new Vector(); |
|
552 |
for (;;) { |
|
553 |
String line = rdr.readLine(); |
|
554 |
if (line == null) break; |
|
555 |
s.addElement(line); |
|
556 |
} |
|
557 |
String[] a = new String[s.size()]; |
|
558 |
s.copyInto(a); |
|
559 |
return a; |
|
560 |
} |
|
561 |
|
|
562 |
public static void main(String[] argv) throws IOException { |
|
563 |
String filea = argv[argv.length - 2]; |
|
564 |
String fileb = argv[argv.length - 1]; |
|
565 |
String[] a = slurp(filea); |
|
566 |
String[] b = slurp(fileb); |
|
567 |
Diff d = new Diff(a,b); |
|
568 |
char style = 'n'; |
|
569 |
d.heuristic = false; |
|
570 |
for (int i = 0; i < argv.length - 2; ++i) { |
|
571 |
String f = argv[i]; |
|
572 |
if (f.startsWith("-")) { |
|
573 |
for (int j = 1; j < f.length(); ++j) { |
|
574 |
switch (f.charAt(j)) { |
|
575 |
case 'H': // heuristic on |
|
576 |
d.heuristic = true; break; |
|
577 |
case 'e': // Ed style |
|
578 |
style = 'e'; break; |
|
579 |
case 'c': // Context diff |
|
580 |
style = 'c'; break; |
|
581 |
case 'u': |
|
582 |
style = 'u'; break; |
|
583 |
} |
|
584 |
} |
|
585 |
} |
|
586 |
} |
|
587 |
boolean reverse = style == 'e'; |
|
588 |
Diff.change script = d.diff_2(reverse); |
|
589 |
if (script == null) |
|
590 |
System.err.println("No differences"); |
|
591 |
else { |
|
592 |
Base p; |
|
593 |
switch (style) { |
|
594 |
case 'e': |
|
595 |
p = new EdPrint(a,b); break; |
|
596 |
case 'c': |
|
597 |
p = new ContextPrint(a,b); break; |
|
598 |
case 'u': |
|
599 |
p = new UnifiedPrint(a,b); break; |
|
600 |
default: |
|
601 |
p = new NormalPrint(a,b); |
|
602 |
} |
|
603 |
p.print_header(filea,fileb); |
|
604 |
p.print_script(script); |
|
605 |
} |
|
606 |
} |
|
607 |
|
|
608 |
} |
tmp/org.txm.core/src/java/org/txm/utils/ExecTimer.java (revision 148) | ||
---|---|---|
1 |
package org.txm.utils; |
|
2 |
|
|
3 |
import java.util.Date; |
|
4 |
|
|
5 |
import org.txm.Messages; |
|
6 |
|
|
7 |
public class ExecTimer { |
|
8 |
static long startTime = 0, endTime = 0; |
|
9 |
static String message = ""; //$NON-NLS-1$ |
|
10 |
public static void start() { |
|
11 |
startTime = new Date().getTime(); |
|
12 |
} |
|
13 |
|
|
14 |
public static String stop() { |
|
15 |
endTime = new Date().getTime(); |
|
16 |
return setMessage(endTime - startTime); |
|
17 |
} |
|
18 |
|
|
19 |
static float t; |
|
20 |
private static String setMessage(float time) { |
|
21 |
t = time; |
|
22 |
if (t < 1000) { |
|
23 |
message = ""+((int)t)+Messages.ExecTimer_0; //$NON-NLS-1$ |
|
24 |
} |
|
25 |
else { |
|
26 |
t = t/1000; |
|
27 |
if (t < 3) { |
|
28 |
message = ""+t; //$NON-NLS-1$ |
|
29 |
message = ""+(message.substring(0, 3))+Messages.ExecTimer_1; //$NON-NLS-1$ |
|
30 |
} else if (t < 60) |
|
31 |
message = ""+((int)t)+Messages.ExecTimer_1; //$NON-NLS-1$ |
|
32 |
else if (t < 3600) |
|
33 |
message = ""+((int)t/60)+Messages.ExecTimer_2+((int)t%60)+Messages.ExecTimer_1; //$NON-NLS-1$ |
|
34 |
else |
|
35 |
message = ""+((int)t/3600)+Messages.ExecTimer_3+((int)(t%3600)/60)+Messages.ExecTimer_2+((int)(t%3600)%60)+ Messages.ExecTimer_1; //$NON-NLS-1$ |
|
36 |
} |
|
37 |
return message + " ("+(int)time+" ms)"; //$NON-NLS-1$ //$NON-NLS-2$ |
|
38 |
} |
|
39 |
|
|
40 |
public static String getMessage() { |
|
41 |
return message; |
|
42 |
} |
|
43 |
} |
tmp/org.txm.core/src/java/org/txm/utils/IOUtils.java (revision 148) | ||
---|---|---|
1 |
package org.txm.utils; |
|
2 |
|
|
3 |
import java.io.BufferedOutputStream; |
|
4 |
import java.io.BufferedReader; |
|
5 |
import java.io.File; |
|
6 |
import java.io.FileInputStream; |
|
7 |
import java.io.FileNotFoundException; |
|
8 |
import java.io.FileOutputStream; |
|
9 |
import java.io.IOException; |
|
10 |
import java.io.InputStreamReader; |
|
11 |
import java.io.OutputStreamWriter; |
|
12 |
import java.io.PrintWriter; |
|
13 |
import java.io.UnsupportedEncodingException; |
|
14 |
|
|
15 |
import org.txm.utils.i18n.DetectBOM; |
|
16 |
|
|
17 |
public class IOUtils { |
|
18 |
public static final String UTF8 = "UTF-8"; |
|
19 |
|
|
20 |
public static BufferedReader getReader(File file) throws UnsupportedEncodingException, FileNotFoundException { |
|
21 |
return getReader(file, UTF8); |
|
22 |
} |
|
23 |
|
|
24 |
public static BufferedReader getReader(File file, String encoding) throws UnsupportedEncodingException, FileNotFoundException { |
|
25 |
FileInputStream input = new FileInputStream(file); |
|
26 |
DetectBOM db = new DetectBOM(file); |
|
27 |
for (int ibom = 0 ; ibom < db.getBOMSize() ; ibom++) |
|
28 |
try { |
|
29 |
input.read(); |
|
30 |
} catch (IOException e) { |
|
31 |
e.printStackTrace(); |
|
32 |
} |
|
33 |
return new BufferedReader(new InputStreamReader(input , "UTF-8")); //$NON-NLS-1$ |
|
34 |
} |
|
35 |
|
|
36 |
public static BufferedReader getReader(String file) throws UnsupportedEncodingException, FileNotFoundException { |
|
37 |
return getReader(new File(file), UTF8); |
|
38 |
} |
|
39 |
|
|
40 |
public static String getText(File xmlWFile, String string) throws IOException { |
|
41 |
BufferedReader reader = getReader(xmlWFile); |
|
42 |
StringBuilder builder = new StringBuilder(); |
|
43 |
try{ |
|
44 |
String line = reader.readLine(); |
|
45 |
while (line != null) { |
|
46 |
builder.append(line); |
|
47 |
line = reader.readLine(); |
|
48 |
if (line != null) builder.append("\n"); |
|
49 |
} |
|
50 |
} finally { |
|
51 |
reader.close(); |
|
52 |
} |
|
53 |
return builder.toString(); |
|
54 |
} |
|
55 |
|
|
56 |
public static PrintWriter getWriter(File file) throws UnsupportedEncodingException, FileNotFoundException { |
|
57 |
return getWriter(file, UTF8); |
|
58 |
} |
|
59 |
|
|
60 |
public static PrintWriter getWriter(File file, String encoding) throws UnsupportedEncodingException, FileNotFoundException { |
|
61 |
return getWriter(file, encoding, false); |
|
62 |
} |
|
63 |
|
|
64 |
public static PrintWriter getWriter(File file, boolean append) throws UnsupportedEncodingException, FileNotFoundException { |
|
65 |
return getWriter(file, "UTF-8", append); |
|
66 |
} |
|
67 |
|
|
68 |
public static PrintWriter getWriter(File file, String encoding, boolean append) throws UnsupportedEncodingException, FileNotFoundException { |
|
69 |
return new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file, append)) , "UTF-8")); //$NON-NLS-1$ |
|
70 |
} |
|
71 |
|
|
72 |
public static PrintWriter getWriter(String file) throws UnsupportedEncodingException, FileNotFoundException { |
|
73 |
return getWriter(new File(file), UTF8); |
|
74 |
} |
|
75 |
|
|
76 |
public static void write(File file, String str) throws UnsupportedEncodingException, FileNotFoundException { |
|
77 |
PrintWriter writer = getWriter(file); |
|
78 |
writer.write(str); |
|
79 |
writer.close(); |
|
80 |
|
|
81 |
} |
|
82 |
} |
tmp/org.txm.core/src/java/org/txm/utils/SparseList.java (revision 148) | ||
---|---|---|
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 |
// |
|
24 |
// $LastChangedDate: 2013-05-06 17:38:43 +0200 (lun., 06 mai 2013) $ |
|
25 |
// $LastChangedRevision: 2386 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.utils; |
|
29 |
|
|
30 |
import java.util.AbstractList; |
|
31 |
import java.util.ArrayList; |
|
32 |
import java.util.List; |
|
33 |
import java.util.SortedMap; |
|
34 |
import java.util.TreeMap; |
|
35 |
|
|
36 |
// TODO: Auto-generated Javadoc |
|
37 |
/** |
|
38 |
* The Class SparseList. |
|
39 |
* |
|
40 |
* @param <E> the element type |
|
41 |
*/ |
|
42 |
public class SparseList<E> extends AbstractList<E> { |
|
43 |
|
|
44 |
/** The map. */ |
|
45 |
private SortedMap<Integer, E> map; |
|
46 |
|
|
47 |
/** |
|
48 |
* Instantiates a new sparse list. |
|
49 |
*/ |
|
50 |
public SparseList() { |
|
51 |
map = new TreeMap<Integer, E>(); |
|
52 |
} |
|
53 |
|
|
54 |
/* (non-Javadoc) |
|
55 |
* @see java.util.AbstractList#get(int) |
|
56 |
*/ |
|
57 |
@Override |
|
58 |
public E get(int n) { |
|
59 |
return map.get(n); |
|
60 |
} |
|
61 |
|
|
62 |
/** |
|
63 |
* Returns all the elements between from (included) and to(excluded). Values |
|
64 |
* not in the list are returned as null |
|
65 |
* |
|
66 |
* @param from the from |
|
67 |
* @param to the to |
|
68 |
* @return A list whose size is to-form |
|
69 |
*/ |
|
70 |
public List<E> getRange(int from, int to) { |
|
71 |
List<E> res = new ArrayList<E>(); |
|
72 |
//if (from > to) System.out.println("ERROR: "+from+" > "+to); |
|
73 |
SortedMap<Integer, E> subMap = map.subMap(from, to); |
|
74 |
if (subMap.size() == (to - from)) |
|
75 |
res.addAll(subMap.values()); |
|
76 |
else |
|
77 |
for (int i = from; i < to; i++) |
|
78 |
res.add(map.get(i)); |
|
79 |
return res; |
|
80 |
} |
|
81 |
|
|
82 |
/* (non-Javadoc) |
|
83 |
* @see java.util.AbstractCollection#size() |
|
84 |
*/ |
|
85 |
@Override |
|
86 |
public int size() { |
|
87 |
return map.size(); |
|
88 |
} |
|
89 |
|
|
90 |
/* (non-Javadoc) |
|
91 |
* @see java.util.AbstractList#add(int, java.lang.Object) |
|
92 |
*/ |
|
93 |
@Override |
|
94 |
public void add(int n, E obj) { |
|
95 |
map.put(n, obj); |
|
96 |
} |
|
97 |
|
|
98 |
/** |
|
99 |
* Gets the not null. |
|
100 |
* |
|
101 |
* @return the not null |
|
102 |
*/ |
|
103 |
public List<Integer> getNotNull() { |
|
104 |
return new ArrayList<Integer>(map.keySet()); |
|
105 |
} |
|
106 |
} |
tmp/org.txm.core/src/java/org/txm/utils/Tuple.java (revision 148) | ||
---|---|---|
1 |
package org.txm.utils; |
|
2 |
|
|
3 |
import java.util.HashSet; |
|
4 |
|
|
5 |
public class Tuple<T> { |
|
6 |
|
|
7 |
public T a, b; |
|
8 |
|
|
9 |
public Tuple(T a, T b) { this.a = a ; this.b = b;} |
|
10 |
|
|
11 |
static String format = "%s %s"; |
|
12 |
public String toString() { return String.format(format, a, b);} |
|
13 |
@Override |
|
14 |
public int hashCode() {return a.hashCode()*b.hashCode();} |
|
15 |
@Override |
|
16 |
public boolean equals(Object t) {if (t instanceof Tuple) return equals((Tuple<?>)t); else return super.equals(t);}; |
|
17 |
public boolean equals(Tuple<Object> t) {return a.equals(t.a) & b.equals(t.b);}; |
|
18 |
|
|
19 |
public static void main(String[] args) { |
|
20 |
Tuple<Integer> t1 = new Tuple<Integer>(1,1); |
|
21 |
Tuple<Integer> t2 = new Tuple<Integer>(1,2); |
|
22 |
Tuple<Integer> t3 = new Tuple<Integer>(1,3); |
|
23 |
Tuple<Integer> t33 = new Tuple<Integer>(1,3); |
|
24 |
|
|
25 |
HashSet<Tuple<Integer>> set = new HashSet<Tuple<Integer>>(); |
|
26 |
set.add(t1); |
|
27 |
set.add(t2); |
|
28 |
set.add(t3); |
|
29 |
set.add(t33); |
|
30 |
|
|
31 |
System.out.println(set); |
|
32 |
} |
|
33 |
} |
tmp/org.txm.core/src/java/org/txm/utils/StreamHog.java (revision 148) | ||
---|---|---|
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 |
// |
|
24 |
// $LastChangedDate: 2014-09-15 10:51:15 +0200 (Mon, 15 Sep 2014) $ |
|
25 |
// $LastChangedRevision: 2836 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.utils; |
|
29 |
|
|
30 |
import java.io.BufferedReader; |
|
31 |
import java.io.IOException; |
|
32 |
import java.io.InputStream; |
|
33 |
import java.io.InputStreamReader; |
|
34 |
import java.util.ArrayList; |
|
35 |
|
|
36 |
import org.txm.utils.logger.Log; |
|
37 |
|
|
38 |
// TODO: Auto-generated Javadoc |
|
39 |
/** |
|
40 |
* The Class StreamHog. |
|
41 |
* |
|
42 |
* If a MessagePrinter is set, then the messages are displayed with the MessagePrinter and not in sys.out |
|
43 |
*/ |
|
44 |
public class StreamHog extends Thread { |
|
45 |
|
|
46 |
InputStream is; |
|
47 |
boolean capture; |
|
48 |
String installPath; |
|
49 |
boolean go = true; |
|
50 |
|
|
51 |
BufferedReader br; |
|
52 |
ArrayList<MessagePrinter> messagePrinters = new ArrayList<MessagePrinter>(); |
|
53 |
|
|
54 |
/** The lastline. */ |
|
55 |
public String lastline; |
|
56 |
|
|
57 |
/** |
|
58 |
* Instantiates a new stream hog. |
|
59 |
* |
|
60 |
* @param is the is |
|
61 |
* @param capture the capture |
|
62 |
*/ |
|
63 |
public StreamHog(InputStream is, boolean capture) { |
|
64 |
this.is = is; |
|
65 |
this.capture = capture; |
|
66 |
start(); |
|
67 |
} |
|
68 |
|
|
69 |
public String getInstallPath() { |
|
70 |
return installPath; |
|
71 |
} |
|
72 |
|
|
73 |
public InputStream getInputStream() { |
|
74 |
return is; |
|
75 |
} |
|
76 |
|
|
77 |
public BufferedReader getBufferedReader() { |
|
78 |
return br; |
|
79 |
} |
|
80 |
|
|
81 |
/* (non-Javadoc) |
|
82 |
* @see java.lang.Thread#run() |
|
83 |
*/ |
|
84 |
@Override |
|
85 |
public void run() { |
|
86 |
try { |
|
87 |
br = new BufferedReader(new InputStreamReader(is)); |
|
88 |
String line = null; |
|
89 |
while ((line = br.readLine()) != null) { |
|
90 |
if (capture) { // we are supposed to capture the output from |
|
91 |
int i = line.indexOf("InstallPath"); //$NON-NLS-1$ |
|
92 |
if (i >= 0) { |
|
93 |
String s = line.substring(i + 11).trim(); |
|
94 |
int j = s.indexOf("REG_SZ"); //$NON-NLS-1$ |
|
95 |
if (j >= 0) |
|
96 |
s = s.substring(j + 6).trim(); |
|
97 |
installPath = s; |
|
98 |
System.out.println("R InstallPath = " + s); |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
printMessage(line); |
|
103 |
Log.info("Rserve>" + line); //$NON-NLS-1$ |
|
104 |
lastline = line; |
|
105 |
|
|
106 |
} |
|
107 |
} catch (IOException e) { |
|
108 |
System.out.println("ERROR: R logging is broken: "+e); |
|
109 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
110 |
} |
|
111 |
} |
|
112 |
|
|
113 |
/** |
|
114 |
* print the message if any MessagePrinter is set |
|
115 |
* @param msg |
|
116 |
*/ |
|
117 |
public void printMessage(String msg) { |
|
118 |
if (messagePrinters.size() > 0) { |
|
119 |
for (MessagePrinter mp : messagePrinters) |
|
120 |
mp.println(msg); //$NON-NLS-1$ |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 |
public void addMessagePrinter(MessagePrinter mp) { |
|
125 |
this.messagePrinters.add(mp); |
|
126 |
} |
|
127 |
|
|
128 |
public ArrayList<MessagePrinter> getMessagePrinter() { |
|
129 |
return this.messagePrinters; |
|
130 |
} |
|
131 |
|
|
132 |
/** |
|
133 |
* Sets the prints the. |
|
134 |
* |
|
135 |
* @param capture the new prints the |
|
136 |
*/ |
|
137 |
public void setPrint(boolean capture) |
|
138 |
{ |
|
139 |
this.capture = capture; |
|
140 |
} |
|
141 |
|
|
142 |
public void stopLogging() { |
|
143 |
go = false; |
|
144 |
} |
|
145 |
} |
tmp/org.txm.core/src/java/org/txm/utils/ProcessBuilderBuilder.java (revision 148) | ||
---|---|---|
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 |
// |
|
24 |
// $LastChangedDate: 2013-08-29 14:23:55 +0200 (jeu., 29 août 2013) $ |
|
25 |
// $LastChangedRevision: 2519 $ |
|
26 |
// $LastChangedBy: nilskredens $ |
|
27 |
// |
|
28 |
package org.txm.utils; |
|
29 |
|
|
30 |
import java.io.File; |
|
31 |
import java.io.FileOutputStream; |
|
32 |
import java.io.IOException; |
|
33 |
import java.io.OutputStreamWriter; |
|
34 |
import java.util.ArrayList; |
|
35 |
import java.util.HashMap; |
|
36 |
|
|
37 |
import javax.xml.parsers.DocumentBuilder; |
|
38 |
import javax.xml.parsers.DocumentBuilderFactory; |
|
39 |
import javax.xml.parsers.ParserConfigurationException; |
|
40 |
|
|
41 |
import org.w3c.dom.Document; |
|
42 |
import org.w3c.dom.Element; |
|
43 |
import org.w3c.dom.NodeList; |
|
44 |
import org.xml.sax.SAXException; |
|
45 |
|
|
46 |
// TODO: Auto-generated Javadoc |
|
47 |
/** |
|
48 |
* The Class ProcessBuilderBuilder. |
|
49 |
* |
|
50 |
* @deprecated |
|
51 |
* |
|
52 |
* Reads a shell command line interface definition in an xml file |
|
53 |
* (CLIX) then creates the corresponding java wrapper API code. |
|
54 |
* |
|
55 |
* example xml definition file : |
|
56 |
* |
|
57 |
* <application name="progname" version="0.0.0" desc="..."> <progs> |
|
58 |
* <prog exec="prog.exe" desc="..."> <args> <arg state="optional" |
|
59 |
* type="none" name="thing" desc="the thing arg"/> <arg state="must" |
|
60 |
* type="none" name="compulsorything" desc=""/> <arg |
|
61 |
* state="multiple" type="none" name="onethingatleast" desc=""/> |
|
62 |
* </args> </prog> </progs> </application> |
|
63 |
* @author mdecorde, sheiden |
|
64 |
*/ |
|
65 |
@Deprecated |
|
66 |
public class ProcessBuilderBuilder { |
|
67 |
|
|
68 |
/** |
|
69 |
* build the java file from the definition file. |
|
70 |
* |
|
71 |
* @param xmlFile the xml file |
|
72 |
* @param outFile the out file |
|
73 |
* @throws IOException Signals that an I/O exception has occurred. |
|
74 |
* @throws SAXException the sAX exception |
|
75 |
* @throws ParserConfigurationException the parser configuration exception |
|
76 |
*/ |
|
77 |
public static void build(File xmlFile, File outFile) throws IOException, |
|
78 |
SAXException, ParserConfigurationException { |
|
79 |
OutputStreamWriter writer = new OutputStreamWriter( |
|
80 |
new FileOutputStream(outFile), "UTF-8"); //$NON-NLS-1$ |
|
81 |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
|
82 |
DocumentBuilder db = dbf.newDocumentBuilder(); |
|
83 |
Document doc = db.parse(xmlFile); |
|
84 |
Element root = doc.getDocumentElement(); |
|
85 |
String appliname = root.getAttribute("name"); //$NON-NLS-1$ |
|
86 |
String version = root.getAttribute("version"); //$NON-NLS-1$ |
|
87 |
String applidesc = root.getAttribute("desc"); //$NON-NLS-1$ |
|
88 |
writer.write("import java.util.Date;\n"); //$NON-NLS-1$ |
|
89 |
writer.write("import java.util.Locale;\n"); //$NON-NLS-1$ |
|
90 |
writer.write("import java.text.DateFormat;\n"); //$NON-NLS-1$ |
|
91 |
writer.write("import java.io.BufferedReader;\n"); //$NON-NLS-1$ |
|
92 |
writer.write("import java.io.IOException;\n"); //$NON-NLS-1$ |
|
93 |
writer.write("import java.io.InputStream;\n"); //$NON-NLS-1$ |
|
94 |
writer.write("import java.io.InputStreamReader;\n"); //$NON-NLS-1$ |
|
95 |
writer.write("import java.util.ArrayList;\n"); //$NON-NLS-1$ |
|
96 |
|
|
97 |
writer.write("\n// " + applidesc + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
98 |
writer.write("class " + appliname + "\n{\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
99 |
writer.write("\tString binpath = \"\";\n"); //$NON-NLS-1$ |
|
100 |
writer.write("\tpublic " + appliname + "(String bindir){this.binpath=bindir;}\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
101 |
writer.write("\tpublic " + appliname + "(File bindir){this.binpath=bindir.getAbsolutePath();}\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
102 |
writer.write("\tString version = \"" + version + "\";\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
103 |
writer.write("\tString desc = \"" + applidesc + "\";\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
104 |
|
|
105 |
writer.write("\tboolean debug = false;\n"); //$NON-NLS-1$ |
|
106 |
writer.write("\tpublic void debug(boolean b){debug=b;};\n"); //$NON-NLS-1$ |
|
107 |
|
|
108 |
NodeList nodeLst = doc.getElementsByTagName("prog"); //$NON-NLS-1$ |
|
109 |
ArrayList<String> dejavu = new ArrayList<String>(); |
|
110 |
|
|
111 |
for (int s = 0; s < nodeLst.getLength(); s++) { |
|
112 |
HashMap<String, String> atester = new HashMap<String, String>(); |
|
113 |
ArrayList<String> order = new ArrayList<String>(); |
|
114 |
ArrayList<String> musts = new ArrayList<String>(); |
|
115 |
ArrayList<String> multiples = new ArrayList<String>(); |
|
116 |
Element elem = (Element) nodeLst.item(s); |
|
117 |
NodeList children = elem.getElementsByTagName("arg"); //$NON-NLS-1$ |
|
118 |
|
|
119 |
String execfile = elem.getAttribute("exec"); //$NON-NLS-1$ |
|
120 |
String execfiledesc = elem.getAttribute("desc"); //$NON-NLS-1$ |
|
121 |
String func = "\n\t// " + execfiledesc + //$NON-NLS-1$ |
|
122 |
"\n\tpublic void " //$NON-NLS-1$ |
|
123 |
+ execfile.replace("-", "").replace(".", "") + "("; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ |
|
124 |
String argsdesc = ""; //$NON-NLS-1$ |
|
125 |
for (int i = 0; i < children.getLength(); i++) { |
|
126 |
Element child = (Element) children.item(i); |
|
127 |
String name = child.getAttribute("name"); //$NON-NLS-1$ |
|
128 |
String type = child.getAttribute("type"); //$NON-NLS-1$ |
|
129 |
String desc = child.getAttribute("desc"); //$NON-NLS-1$ |
|
130 |
String state = child.getAttribute("state"); //$NON-NLS-1$ |
|
131 |
if (!order.contains(name)) |
|
132 |
order.add(name); |
|
133 |
if (state.equals("optional")) //$NON-NLS-1$ |
|
134 |
{ |
|
135 |
if (!dejavu.contains(name)) { |
|
136 |
dejavu.add(name); |
|
137 |
writer.write("\n\t// " + desc + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
138 |
writer |
|
139 |
.write("\tprivate Boolean is" + name.replace("-", "") + " = false;\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
140 |
|
|
141 |
if (!type.equals("none")) //$NON-NLS-1$ |
|
142 |
{ |
|
143 |
writer |
|
144 |
.write("\tprivate " + type + " " + name.replace("-", "") + ";\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ |
|
145 |
writer |
|
146 |
.write("\tpublic void set" + name.replace("-", "") + "(" + type + " arg)\n\t{" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ |
|
147 |
"\tthis.is" //$NON-NLS-1$ |
|
148 |
+ name.replace("-", "") + "=true;\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
149 |
"\t\tthis." //$NON-NLS-1$ |
|
150 |
+ name.replace("-", "") + "=arg;\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
151 |
"\t}\n" //$NON-NLS-1$ |
|
152 |
); |
|
153 |
} else { |
|
154 |
writer |
|
155 |
.write("\tpublic void set" + name.replace("-", "") + "()\n\t{" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
156 |
"\tthis.is" //$NON-NLS-1$ |
|
157 |
+ name.replace("-", "") + "=true;\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
158 |
"\t}\n" //$NON-NLS-1$ |
|
159 |
); |
|
160 |
} |
|
161 |
writer |
|
162 |
.write("\tpublic void unset" + name.replace("-", "") + "()\n\t{" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
163 |
"\tthis.is" //$NON-NLS-1$ |
|
164 |
+ name.replace("-", "") + "=false;\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
165 |
"\t}\n" //$NON-NLS-1$ |
|
166 |
); |
|
167 |
} |
|
168 |
|
|
169 |
atester.put(name, type); |
|
170 |
} else if (state.equals("multiple")) //$NON-NLS-1$ |
|
171 |
{ |
|
172 |
if (!dejavu.contains(name)) { |
|
173 |
dejavu.add(name); |
|
174 |
writer.write("\n\t// " + desc + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
175 |
writer |
|
176 |
.write("\tprivate Boolean is" + name.replace("-", "") + " = false;\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
177 |
|
|
178 |
if (!type.equals("none")) //$NON-NLS-1$ |
|
179 |
{ |
|
180 |
writer |
|
181 |
.write("\tprivate List<" + type + "> " + name + ";\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
182 |
writer |
|
183 |
.write("\tpublic void set" + name.replace("-", "") + "(List<" + type + "> arg)\n\t{" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ |
|
184 |
"\tthis.is" //$NON-NLS-1$ |
|
185 |
+ name.replace("-", "") + "=true;\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
186 |
"\t\tthis." //$NON-NLS-1$ |
|
187 |
+ name.replace("-", "") + "=arg;\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
188 |
"\t}\n" //$NON-NLS-1$ |
|
189 |
); |
|
190 |
} else { |
|
191 |
writer |
|
192 |
.write("\tpublic void set" + name.replace("-", "") + "()\n\t{" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
193 |
"\tthis.is" //$NON-NLS-1$ |
|
194 |
+ name.replace("-", "") + "=true;\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
195 |
"\t}\n" //$NON-NLS-1$ |
|
196 |
); |
|
197 |
} |
|
198 |
writer |
|
199 |
.write("\tpublic void unset" + name.replace("-", "") + "()\n\t{" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
200 |
"\tthis.is" //$NON-NLS-1$ |
|
201 |
+ name.replace("-", "") + "=false;\n" + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
202 |
"\t}\n" //$NON-NLS-1$ |
|
203 |
); |
|
204 |
} |
|
205 |
multiples.add(name); |
|
206 |
atester.put(name, type); |
|
207 |
} else { |
|
208 |
func = func + type + " " + name.replace("-", "") + ", "; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
209 |
argsdesc += "\t// arg : " + desc + "\n"; //$NON-NLS-1$ //$NON-NLS-2$ |
|
210 |
musts.add(name); |
|
211 |
|
|
212 |
if (!dejavu.contains(name)) |
|
213 |
dejavu.add(name); |
|
214 |
} |
|
215 |
} |
|
216 |
if (musts.size() > 0) |
|
217 |
func = func.substring(0, func.length() - 2) + ")"; //$NON-NLS-1$ |
|
218 |
else |
|
219 |
func = func + ")"; //$NON-NLS-1$ |
|
220 |
|
|
221 |
writer.write("\n" + func + " throws IOException\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
222 |
writer.write(argsdesc); |
|
223 |
writer.write("\t{\n"); //$NON-NLS-1$ |
|
224 |
writer |
|
225 |
.write("\t\tArrayList<String> args = new ArrayList<String>();\n"); //$NON-NLS-1$ |
|
226 |
writer.write("\t\targs.add(binpath+\"/" + execfile + "\");\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
227 |
for (String name : order) { |
|
228 |
if (atester.containsKey(name)) { |
|
229 |
writer.write("\t\tif (is" + name.replace("-", "") + ")\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
230 |
if (!atester.get(name).equals("none")) //$NON-NLS-1$ |
|
231 |
{ |
|
232 |
if (multiples.contains(name)) { |
|
233 |
writer |
|
234 |
.write("\t\t\tfor(int c = 0;c < " + name.replace("-", "") + ".size();c++)\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
235 |
writer |
|
236 |
.write("\t\t\t{args.add(\"-" + name + "\");\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
237 |
writer |
|
238 |
.write("\t\t\targs.add(\"\"+" + name.replace("-", "") + ".get(c));}\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
239 |
} else { |
|
240 |
writer |
|
241 |
.write("\t\t\t{args.add(\"-" + name + "\");\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
242 |
writer |
|
243 |
.write("\t\t\targs.add(\"\"+" + name.replace("-", "") + ");}\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
244 |
} |
|
245 |
} else { |
|
246 |
writer.write("\t\t\targs.add(\"-" + name + "\");\n"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
247 |
} |
|
248 |
} else { |
|
249 |
if (musts.contains(name)) |
|
250 |
writer |
|
251 |
.write("\t\targs.add(\"\"+" + name.replace("-", "") + ");\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
|
252 |
} |
|
253 |
} |
|
254 |
|
|
255 |
writer |
|
256 |
.write("\n\t\tProcessBuilder pb = new ProcessBuilder(args);\n" + //$NON-NLS-1$ |
|
257 |
"\t\tpb.redirectErrorStream(true);\n" + //$NON-NLS-1$ |
|
258 |
"\t\tProcess process = null;\n" + //$NON-NLS-1$ |
|
259 |
"\t\ttry {\n\t\t\tprocess = pb.start();\n\t\t} catch (IOException e) {\n\t\t\tSystem.err.println(e);\n\t\t}\n"); //$NON-NLS-1$ |
|
260 |
writer.write("\t\tInputStream is = process.getInputStream();\n"); //$NON-NLS-1$ |
|
261 |
writer |
|
262 |
.write("\t\tInputStreamReader isr = new InputStreamReader(is);\n"); //$NON-NLS-1$ |
|
263 |
writer.write("\t\tBufferedReader br = new BufferedReader(isr);\n"); //$NON-NLS-1$ |
|
264 |
writer.write("\t\tString line;\n"); //$NON-NLS-1$ |
|
265 |
writer |
|
266 |
.write("\t\twhile ((line = br.readLine()) != null)\n\t\t{\n\t\t\tSystem.out.println(line);\n\t\t}\n"); //$NON-NLS-1$ |
|
267 |
writer |
|
268 |
.write("\t\tint e=0;\n\ttry {e = process.waitFor();}catch(Exception err){}\n\t\tif (e != 0) {\n\t\t\t" + //$NON-NLS-1$ |
|
269 |
"System.err.println(\"Process exited abnormally with code \"+e+\" at \"+" //$NON-NLS-1$ |
|
270 |
+ |
|
271 |
"DateFormat.getDateInstance(DateFormat.FULL, Locale.UK).format(new Date()));\n\t\t\n"); //$NON-NLS-1$ |
|
272 |
writer.write("\t\n\n"); //$NON-NLS-1$ |
|
273 |
writer |
|
274 |
.write("for(int c=0;c <args.size();c++)System.out.print(\"\"+args.get(c)+\" \");\n"); //$NON-NLS-1$ |
|
275 |
writer.write("System.out.println();\n}\n\t}\n"); //$NON-NLS-1$ |
|
276 |
} |
|
277 |
|
|
278 |
writer.write("\tpublic static void main(String[] args) \n"); //$NON-NLS-1$ |
|
279 |
writer.write("\t{\n"); //$NON-NLS-1$ |
|
280 |
writer |
|
281 |
.write("\t\t" + appliname + " tt = new " + appliname + "(\"\");\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ |
|
282 |
writer.write("\t}\n"); //$NON-NLS-1$ |
|
283 |
|
|
284 |
writer.write("}"); //$NON-NLS-1$ |
|
285 |
writer.close(); |
|
286 |
} |
|
287 |
|
|
288 |
/** |
|
289 |
* The main method. |
|
290 |
* |
|
291 |
* @param args the arguments |
|
292 |
*/ |
|
293 |
public static void main(String[] args) { |
|
294 |
if (args.length < 2) { |
|
295 |
System.err.println("Need 2 args : xmlfile & outfile paths"); //$NON-NLS-1$ |
|
296 |
return; |
|
297 |
} |
|
298 |
File xmlfile = new File(args[0]); |
|
299 |
File outfile = new File(args[1]); |
|
300 |
|
|
301 |
if (xmlfile.exists()) |
|
302 |
System.out.println("xmlfile ok"); //$NON-NLS-1$ |
|
303 |
try { |
|
304 |
ProcessBuilderBuilder.build(xmlfile, outfile); |
|
305 |
} catch (IOException e) { |
|
306 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
307 |
} catch (SAXException e) { |
|
308 |
// TODO Auto-generated catch block |
|
309 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
310 |
} catch (ParserConfigurationException e) { |
|
311 |
// TODO Auto-generated catch block |
|
312 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
313 |
} |
|
314 |
System.out.println("done"); //$NON-NLS-1$ |
|
315 |
} |
|
316 |
} |
tmp/org.txm.core/src/java/org/txm/utils/UpdateXSLParameters.java (revision 148) | ||
---|---|---|
1 |
package org.txm.utils; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.IOException; |
|
5 |
import java.util.HashMap; |
|
6 |
|
|
7 |
import javax.xml.parsers.ParserConfigurationException; |
|
8 |
|
|
9 |
import org.txm.importer.DomUtils; |
|
10 |
import org.w3c.dom.Document; |
|
11 |
import org.w3c.dom.Element; |
|
12 |
import org.w3c.dom.NodeList; |
|
13 |
import org.xml.sax.SAXException; |
|
14 |
|
|
15 |
/** |
|
16 |
* change parameters value of an XSL file |
|
17 |
* |
|
18 |
* @author mdecorde |
|
19 |
* |
|
20 |
*/ |
|
21 |
public class UpdateXSLParameters { |
|
22 |
|
|
23 |
File xslFile; |
|
24 |
|
|
25 |
public UpdateXSLParameters(File xslFile) { |
|
26 |
this.xslFile = xslFile; |
|
27 |
} |
|
28 |
|
|
29 |
/** |
|
30 |
* update the content of "param" elements which name attribute exists in the parameters map with the parameter map value. |
|
31 |
* |
|
32 |
* @param parameters |
|
33 |
* @return |
|
34 |
* @throws ParserConfigurationException |
|
35 |
* @throws SAXException |
|
36 |
* @throws IOException |
|
37 |
*/ |
|
38 |
public boolean process(HashMap<String, String> parameters) throws ParserConfigurationException, SAXException, IOException { |
|
39 |
if (!xslFile.exists()) { |
|
40 |
System.out.println("Error: $xslFile doest not exists. Aborting."); |
|
41 |
return false; |
|
42 |
} |
|
43 |
|
|
44 |
Document doc = DomUtils.load(xslFile); |
|
45 |
NodeList list = doc.getElementsByTagName("xsl:param"); |
|
46 |
for (int i = 0 ; i < list.getLength() ; i++) { |
|
47 |
Element e = (Element) list.item(i); |
|
48 |
String name = e.getAttribute("name"); |
|
49 |
|
|
50 |
if (parameters.keySet().contains(name)) { |
|
51 |
e.setTextContent(parameters.get(name)); |
|
52 |
} |
|
53 |
} |
|
54 |
|
|
55 |
return DomUtils.save(doc, xslFile); |
|
56 |
} |
|
57 |
|
|
58 |
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { |
|
59 |
File xslFile = new File("/home/mdecorde/TEMP/testori/mss-dates/txm/mss-dates-w/xsl/4-edition/1-facsimile-pager.xsl"); |
|
60 |
HashMap<String, String> parameters = new HashMap<String, String>(); |
|
61 |
parameters.put("image-directory", "/home/mdecorde/TEMP/testori/mss-dates/img222222222222"); |
|
62 |
UpdateXSLParameters p = new UpdateXSLParameters(xslFile); |
|
63 |
if (!p.process(parameters)) { |
|
64 |
System.out.println("Fail"); |
|
65 |
} else { |
|
66 |
System.out.println("Success"); |
|
67 |
} |
|
68 |
} |
|
69 |
} |
tmp/org.txm.core/src/java/org/txm/utils/DeleteDir.java (revision 148) | ||
---|---|---|
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 |
// |
|
24 |
// $LastChangedDate: 2015-12-09 09:48:50 +0100 (Wed, 09 Dec 2015) $ |
|
25 |
// $LastChangedRevision: 3084 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.utils; |
|
29 |
|
|
30 |
import java.io.File; |
|
31 |
import java.util.ArrayList; |
|
32 |
import java.util.LinkedList; |
|
33 |
|
|
34 |
import org.apache.commons.lang.SystemUtils; |
|
35 |
|
|
36 |
// TODO: Auto-generated Javadoc |
|
37 |
/** |
|
38 |
* tools to delete files from a directory. |
|
39 |
* |
|
40 |
* @author mdecorde |
|
41 |
*/ |
|
42 |
public class DeleteDir { |
|
43 |
|
|
44 |
/** |
|
45 |
* The main method. |
|
46 |
* |
|
47 |
* @param args the arguments |
|
48 |
*/ |
|
49 |
public static void main(String args[]) { |
|
50 |
deleteDirectory(new File(args[0])); |
|
51 |
} |
|
52 |
|
|
53 |
/** |
Formats disponibles : Unified diff