Révision 3666
TXM/trunk/bundles/org.txm.utils.core/.project (revision 3666) | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<projectDescription> |
|
3 |
<name>org.txm.utils.core</name> |
|
4 |
<comment></comment> |
|
5 |
<projects> |
|
6 |
</projects> |
|
7 |
<buildSpec> |
|
8 |
<buildCommand> |
|
9 |
<name>org.eclipse.jdt.core.javabuilder</name> |
|
10 |
<arguments> |
|
11 |
</arguments> |
|
12 |
</buildCommand> |
|
13 |
<buildCommand> |
|
14 |
<name>org.eclipse.pde.ManifestBuilder</name> |
|
15 |
<arguments> |
|
16 |
</arguments> |
|
17 |
</buildCommand> |
|
18 |
<buildCommand> |
|
19 |
<name>org.eclipse.pde.SchemaBuilder</name> |
|
20 |
<arguments> |
|
21 |
</arguments> |
|
22 |
</buildCommand> |
|
23 |
</buildSpec> |
|
24 |
<natures> |
|
25 |
<nature>org.eclipse.pde.PluginNature</nature> |
|
26 |
<nature>org.eclipse.jdt.core.javanature</nature> |
|
27 |
</natures> |
|
28 |
</projectDescription> |
|
0 | 29 |
TXM/trunk/bundles/org.txm.utils.core/src/org/txm/utils/CharsetDetector.java (revision 3666) | ||
---|---|---|
1 |
package org.txm.utils; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.IOException; |
|
5 |
import java.util.LinkedHashMap; |
|
6 |
|
|
7 |
import org.mozilla.universalchardet.CharsetListener; |
|
8 |
import org.mozilla.universalchardet.UniversalDetector; |
|
9 |
import org.txm.utils.io.IOUtils; |
|
10 |
|
|
11 |
public class CharsetDetector { |
|
12 |
public static int MINIMALSIZE = 2000; |
|
13 |
String encoding = System.getProperty("file.encoding"); //$NON-NLS-1$ |
|
14 |
LinkedHashMap<String, Integer> encodings = new LinkedHashMap<String, Integer>(); |
|
15 |
|
|
16 |
public CharsetDetector(File file) { |
|
17 |
if (file.isDirectory()) |
|
18 |
processDirectory(file); |
|
19 |
else |
|
20 |
try { |
|
21 |
processFile(file); |
|
22 |
} catch (IOException e) { |
|
23 |
// TODO Auto-generated catch block |
|
24 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
25 |
} |
|
26 |
} |
|
27 |
|
|
28 |
private void processDirectory(File dir) { |
|
29 |
for (File file : dir.listFiles(IOUtils.HIDDENFILE_FILTER)) { |
|
30 |
if (file.isFile()) { |
|
31 |
try { |
|
32 |
processFile(file); |
|
33 |
} catch (IOException e) { |
|
34 |
// TODO Auto-generated catch block |
|
35 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
36 |
} |
|
37 |
} |
|
38 |
} |
|
39 |
} |
|
40 |
|
|
41 |
String guessedCharset = System.getProperty("file.encoding"); //$NON-NLS-1$ |
|
42 |
|
|
43 |
private void processFile(File file) throws IOException { |
|
44 |
guessedCharset = System.getProperty("file.encoding"); //$NON-NLS-1$ |
|
45 |
UniversalDetector detector = new UniversalDetector(new CharsetListener() { |
|
46 |
@Override |
|
47 |
public void report(String name) { |
|
48 |
guessedCharset = name; |
|
49 |
} |
|
50 |
}); |
|
51 |
|
|
52 |
byte[] buf = new byte[4096]; |
|
53 |
java.io.FileInputStream fis = new java.io.FileInputStream(file); |
|
54 |
|
|
55 |
int nread; |
|
56 |
while ((nread = fis.read(buf)) > 0 && !detector.isDone()) { |
|
57 |
detector.handleData(buf, 0, nread); |
|
58 |
} |
|
59 |
detector.dataEnd(); |
|
60 |
|
|
61 |
if (!encodings.containsKey(guessedCharset)) |
|
62 |
encodings.put(guessedCharset, 0); |
|
63 |
encodings.put(guessedCharset, encodings.get(guessedCharset) + 1); |
|
64 |
fis.close(); |
|
65 |
} |
|
66 |
|
|
67 |
public String getEncoding() { |
|
68 |
int max = 0; |
|
69 |
for (String s : encodings.keySet()) { |
|
70 |
if (max < encodings.get(s)) { |
|
71 |
max = encodings.get(s); |
|
72 |
encoding = s; |
|
73 |
} |
|
74 |
} |
|
75 |
return encoding; |
|
76 |
} |
|
77 |
|
|
78 |
public LinkedHashMap<String, Integer> getEncodings() { |
|
79 |
return encodings; |
|
80 |
} |
|
81 |
|
|
82 |
public static void main(String[] args) { |
|
83 |
CharsetDetector detector = new CharsetDetector(new File("/home/mdecorde/Bureau/testencoding")); //$NON-NLS-1$ |
|
84 |
System.out.println(detector.getEncodings()); |
|
85 |
} |
|
86 |
} |
|
0 | 87 |
TXM/trunk/bundles/org.txm.utils.core/src/org/txm/utils/ExecTimer.java (revision 3666) | ||
---|---|---|
1 |
package org.txm.utils; |
|
2 |
|
|
3 |
import java.util.Date; |
|
4 |
|
|
5 |
import org.txm.utils.messages.UtilsCoreMessages; |
|
6 |
|
|
7 |
public class ExecTimer { |
|
8 |
static long startTime = 0, endTime = 0; |
|
9 |
static String message = ""; //$NON-NLS-1$ |
|
10 |
|
|
11 |
public static void start() { |
|
12 |
startTime = new Date().getTime(); |
|
13 |
} |
|
14 |
|
|
15 |
public static String stop() { |
|
16 |
endTime = new Date().getTime(); |
|
17 |
return setMessage(endTime - startTime); |
|
18 |
} |
|
19 |
|
|
20 |
static float t; |
|
21 |
|
|
22 |
private static String setMessage(float time) { |
|
23 |
t = time; |
|
24 |
if (t < 1000) { |
|
25 |
message = "" + ((int) t) + UtilsCoreMessages.ExecTimer_0; //$NON-NLS-1$ |
|
26 |
} else { |
|
27 |
t = t / 1000; |
|
28 |
if (t < 3) { |
|
29 |
message = "" + t; //$NON-NLS-1$ |
|
30 |
message = "" + (message.substring(0, 3)) + UtilsCoreMessages.ExecTimer_1; //$NON-NLS-1$ |
|
31 |
} else if (t < 60) |
|
32 |
message = "" + ((int) t) + UtilsCoreMessages.ExecTimer_1; //$NON-NLS-1$ |
|
33 |
else if (t < 3600) |
|
34 |
message = "" + ((int) t / 60) + UtilsCoreMessages.ExecTimer_2 + ((int) t % 60) + UtilsCoreMessages.ExecTimer_1; //$NON-NLS-1$ |
|
35 |
else |
|
36 |
message = "" + ((int) t / 3600) + UtilsCoreMessages.ExecTimer_3 + ((int) (t % 3600) / 60) + UtilsCoreMessages.ExecTimer_2 + ((int) (t % 3600) % 60) + UtilsCoreMessages.ExecTimer_1; //$NON-NLS-1$ |
|
37 |
} |
|
38 |
return message + " (" + (int) time + " ms)"; //$NON-NLS-1$ //$NON-NLS-2$ |
|
39 |
} |
|
40 |
|
|
41 |
public static String getMessage() { |
|
42 |
return message; |
|
43 |
} |
|
44 |
} |
|
0 | 45 |
TXM/trunk/bundles/org.txm.utils.core/src/org/txm/utils/DiffPrint.java (revision 3666) | ||
---|---|---|
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 |
/** |
|
43 |
* A simple framework for printing change lists produced by <code>Diff</code>. |
|
44 |
* |
|
45 |
* @see bmsi.util.Diff |
|
46 |
* @author Stuart D. Gathman Copyright (C) 2000 Business Management Systems, |
|
47 |
* Inc. |
|
48 |
* <p> |
|
49 |
* This program is free software; you can redistribute it and/or modify |
|
50 |
* it under the terms of the GNU General Public License as published by |
|
51 |
* the Free Software Foundation; either version 1, or (at your option) |
|
52 |
* any later version. |
|
53 |
* <p> |
|
54 |
* This program is distributed in the hope that it will be useful, but |
|
55 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
56 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
57 |
* General Public License for more details. |
|
58 |
* <p> |
|
59 |
* You should have received a copy of the GNU General Public License |
|
60 |
* along with this program; if not, write to the Free Software |
|
61 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
62 |
*/ |
|
63 |
public class DiffPrint { |
|
64 |
/** |
|
65 |
* A Base class for printing edit scripts produced by Diff. This class divides |
|
66 |
* the change list into "hunks", and calls <code>print_hunk</code> for each |
|
67 |
* hunk. Various utility methods are provided as well. |
|
68 |
*/ |
|
69 |
public static abstract class Base { |
|
70 |
protected PrintWriter outfile; |
|
71 |
|
|
72 |
public void setOutput(Writer wtr) { |
|
73 |
outfile = new PrintWriter(wtr); |
|
74 |
} |
|
75 |
|
|
76 |
protected void setupOutput() { |
|
77 |
if (outfile == null) |
|
78 |
outfile = new PrintWriter(new OutputStreamWriter(System.out)); |
|
79 |
} |
|
80 |
|
|
81 |
protected Base(Object[] a, Object[] b) { |
|
82 |
file0 = a; |
|
83 |
file1 = b; |
|
84 |
} |
|
85 |
|
|
86 |
/** |
|
87 |
* Set to ignore certain kinds of lines when printing an edit script. For |
|
88 |
* example, ignoring blank lines or comments. |
|
89 |
*/ |
|
90 |
protected UnaryPredicate ignore = null; |
|
91 |
|
|
92 |
/** |
|
93 |
* Set to the lines of the files being compared. |
|
94 |
*/ |
|
95 |
protected Object[] file0, file1; |
|
96 |
|
|
97 |
/** |
|
98 |
* Divide SCRIPT into pieces by calling HUNKFUN and print each piece with |
|
99 |
* PRINTFUN. Both functions take one arg, an edit script. |
|
100 |
* |
|
101 |
* PRINTFUN takes a subscript which belongs together (with a null link at the |
|
102 |
* end) and prints it. |
|
103 |
*/ |
|
104 |
public void print_script(Diff.change script) { |
|
105 |
setupOutput(); |
|
106 |
Diff.change next = script; |
|
107 |
|
|
108 |
while (next != null) { |
|
109 |
Diff.change t, end; |
|
110 |
|
|
111 |
/* Find a set of changes that belong together. */ |
|
112 |
t = next; |
|
113 |
end = hunkfun(next); |
|
114 |
|
|
115 |
/* |
|
116 |
* Disconnect them from the rest of the changes, making them a hunk, and |
|
117 |
* remember the rest for next iteration. |
|
118 |
*/ |
|
119 |
next = end.link; |
|
120 |
end.link = null; |
|
121 |
// if (DEBUG) |
|
122 |
// debug_script(t); |
|
123 |
|
|
124 |
/* Print this hunk. */ |
|
125 |
print_hunk(t); |
|
126 |
|
|
127 |
/* Reconnect the script so it will all be freed properly. */ |
|
128 |
end.link = next; |
|
129 |
} |
|
130 |
outfile.flush(); |
|
131 |
} |
|
132 |
|
|
133 |
/** |
|
134 |
* Called with the tail of the script and returns the last link that belongs |
|
135 |
* together with the start of the tail. |
|
136 |
*/ |
|
137 |
|
|
138 |
protected Diff.change hunkfun(Diff.change hunk) { |
|
139 |
return hunk; |
|
140 |
} |
|
141 |
|
|
142 |
protected int first0, last0, first1, last1, deletes, inserts; |
|
143 |
|
|
144 |
/** |
|
145 |
* Look at a hunk of edit script and report the range of lines in each file that |
|
146 |
* it applies to. HUNK is the start of the hunk, which is a chain of `struct |
|
147 |
* change'. The first and last line numbers of file 0 are stored in *FIRST0 and |
|
148 |
* *LAST0, and likewise for file 1 in *FIRST1 and *LAST1. Note that these are |
|
149 |
* internal line numbers that count from 0. |
|
150 |
* |
|
151 |
* If no lines from file 0 are deleted, then FIRST0 is LAST0+1. |
|
152 |
* |
|
153 |
* Also set *DELETES nonzero if any lines of file 0 are deleted and set *INSERTS |
|
154 |
* nonzero if any lines of file 1 are inserted. If only ignorable lines are |
|
155 |
* inserted or deleted, both are set to 0. |
|
156 |
*/ |
|
157 |
|
|
158 |
protected void analyze_hunk(Diff.change hunk) { |
|
159 |
int f0, l0 = 0, f1, l1 = 0, show_from = 0, show_to = 0; |
|
160 |
int i; |
|
161 |
Diff.change next; |
|
162 |
boolean nontrivial = (ignore == null); |
|
163 |
|
|
164 |
show_from = show_to = 0; |
|
165 |
|
|
166 |
f0 = hunk.line0; |
|
167 |
f1 = hunk.line1; |
|
168 |
|
|
169 |
for (next = hunk; next != null; next = next.link) { |
|
170 |
l0 = next.line0 + next.deleted - 1; |
|
171 |
l1 = next.line1 + next.inserted - 1; |
|
172 |
show_from += next.deleted; |
|
173 |
show_to += next.inserted; |
|
174 |
for (i = next.line0; i <= l0 && !nontrivial; i++) |
|
175 |
if (!ignore.execute(file0[i])) |
|
176 |
nontrivial = true; |
|
177 |
for (i = next.line1; i <= l1 && !nontrivial; i++) |
|
178 |
if (!ignore.execute(file1[i])) |
|
179 |
nontrivial = true; |
|
180 |
} |
|
181 |
|
|
182 |
first0 = f0; |
|
183 |
last0 = l0; |
|
184 |
first1 = f1; |
|
185 |
last1 = l1; |
|
186 |
|
|
187 |
/* |
|
188 |
* If all inserted or deleted lines are ignorable, tell the caller to ignore |
|
189 |
* this hunk. |
|
190 |
*/ |
|
191 |
|
|
192 |
if (!nontrivial) |
|
193 |
show_from = show_to = 0; |
|
194 |
|
|
195 |
deletes = show_from; |
|
196 |
inserts = show_to; |
|
197 |
} |
|
198 |
|
|
199 |
/** |
|
200 |
* Called to print the script header which identifies the files compared. The |
|
201 |
* default does nothing (except set output to system.out if not otherwise set). |
|
202 |
* Derived style classes can override to print the files compared in the format |
|
203 |
* for that style. |
|
204 |
*/ |
|
205 |
public void print_header(String filea, String fileb) { |
|
206 |
setupOutput(); |
|
207 |
} |
|
208 |
|
|
209 |
protected abstract void print_hunk(Diff.change hunk); |
|
210 |
|
|
211 |
protected void print_1_line(String pre, Object linbuf) { |
|
212 |
outfile.println(pre + linbuf.toString()); |
|
213 |
} |
|
214 |
|
|
215 |
/** |
|
216 |
* Print a pair of line numbers with SEPCHAR, translated for file FILE. If the |
|
217 |
* two numbers are identical, print just one number. |
|
218 |
* |
|
219 |
* Args A and B are internal line numbers. We print the translated (real) line |
|
220 |
* numbers. |
|
221 |
*/ |
|
222 |
|
|
223 |
protected void print_number_range(char sepchar, int a, int b) { |
|
224 |
/* |
|
225 |
* Note: we can have B < A in the case of a range of no lines. In this case, we |
|
226 |
* should print the line number before the range, which is B. |
|
227 |
*/ |
|
228 |
if (++b > ++a) |
|
229 |
outfile.print("" + a + sepchar + b); |
|
230 |
else |
|
231 |
outfile.print(b); |
|
232 |
} |
|
233 |
|
|
234 |
public static char change_letter(int inserts, int deletes) { |
|
235 |
if (inserts == 0) |
|
236 |
return 'd'; |
|
237 |
else if (deletes == 0) |
|
238 |
return 'a'; |
|
239 |
else |
|
240 |
return 'c'; |
|
241 |
} |
|
242 |
} |
|
243 |
|
|
244 |
/** |
|
245 |
* Print a change list in the standard diff format. |
|
246 |
*/ |
|
247 |
public static class NormalPrint extends Base { |
|
248 |
|
|
249 |
public NormalPrint(Object[] a, Object[] b) { |
|
250 |
super(a, b); |
|
251 |
} |
|
252 |
|
|
253 |
/** |
|
254 |
* Print a hunk of a normal diff. This is a contiguous portion of a complete |
|
255 |
* edit script, describing changes in consecutive lines. |
|
256 |
*/ |
|
257 |
|
|
258 |
protected void print_hunk(Diff.change hunk) { |
|
259 |
|
|
260 |
/* Determine range of line numbers involved in each file. */ |
|
261 |
analyze_hunk(hunk); |
|
262 |
if (deletes == 0 && inserts == 0) |
|
263 |
return; |
|
264 |
|
|
265 |
/* Print out the line number header for this hunk */ |
|
266 |
print_number_range(',', first0, last0); |
|
267 |
outfile.print(change_letter(inserts, deletes)); |
|
268 |
print_number_range(',', first1, last1); |
|
269 |
outfile.println(); |
|
270 |
|
|
271 |
/* Print the lines that the first file has. */ |
|
272 |
if (deletes != 0) |
|
273 |
for (int i = first0; i <= last0; i++) |
|
274 |
print_1_line("< ", file0[i]); |
|
275 |
|
|
276 |
if (inserts != 0 && deletes != 0) |
|
277 |
outfile.println("---"); |
|
278 |
|
|
279 |
/* Print the lines that the second file has. */ |
|
280 |
if (inserts != 0) |
|
281 |
for (int i = first1; i <= last1; i++) |
|
282 |
print_1_line("> ", file1[i]); |
|
283 |
} |
|
284 |
} |
|
285 |
|
|
286 |
/** |
|
287 |
* Prints an edit script in a format suitable for input to <code>ed</code>. The |
|
288 |
* edit script must be generated with the reverse option to be useful as actual |
|
289 |
* <code>ed</code> input. |
|
290 |
*/ |
|
291 |
public static class EdPrint extends Base { |
|
292 |
|
|
293 |
public EdPrint(Object[] a, Object[] b) { |
|
294 |
super(a, b); |
|
295 |
} |
|
296 |
|
|
297 |
/** Print a hunk of an ed diff */ |
|
298 |
protected void print_hunk(Diff.change hunk) { |
|
299 |
|
|
300 |
/* Determine range of line numbers involved in each file. */ |
|
301 |
analyze_hunk(hunk); |
|
302 |
if (deletes == 0 && inserts == 0) |
|
303 |
return; |
|
304 |
|
|
305 |
/* Print out the line number header for this hunk */ |
|
306 |
print_number_range(',', first0, last0); |
|
307 |
outfile.println(change_letter(inserts, deletes)); |
|
308 |
|
|
309 |
/* Print new/changed lines from second file, if needed */ |
|
310 |
if (inserts != 0) { |
|
311 |
boolean inserting = true; |
|
312 |
for (int i = first1; i <= last1; i++) { |
|
313 |
/* Resume the insert, if we stopped. */ |
|
314 |
if (!inserting) |
|
315 |
outfile.println(i - first1 + first0 + "a"); |
|
316 |
inserting = true; |
|
317 |
|
|
318 |
/* |
|
319 |
* If the file's line is just a dot, it would confuse `ed'. So output it with a |
|
320 |
* double dot, and set the flag LEADING_DOT so that we will output another |
|
321 |
* ed-command later to change the double dot into a single dot. |
|
322 |
*/ |
|
323 |
|
|
324 |
if (".".equals(file1[i])) { |
|
325 |
outfile.println(".."); |
|
326 |
outfile.println("."); |
|
327 |
/* Now change that double dot to the desired single dot. */ |
|
328 |
outfile.println(i - first1 + first0 + 1 + "s/^\\.\\././"); |
|
329 |
inserting = false; |
|
330 |
} else |
|
331 |
/* Line is not `.', so output it unmodified. */ |
|
332 |
print_1_line("", file1[i]); |
|
333 |
} |
|
334 |
|
|
335 |
/* End insert mode, if we are still in it. */ |
|
336 |
if (inserting) |
|
337 |
outfile.println("."); |
|
338 |
} |
|
339 |
} |
|
340 |
} |
|
341 |
|
|
342 |
/** |
|
343 |
* Prints an edit script in context diff format. This and its 'unified' |
|
344 |
* variation is used for source code patches. |
|
345 |
*/ |
|
346 |
public static class ContextPrint extends Base { |
|
347 |
|
|
348 |
protected int context = 3; |
|
349 |
|
|
350 |
public ContextPrint(Object[] a, Object[] b) { |
|
351 |
super(a, b); |
|
352 |
} |
|
353 |
|
|
354 |
protected void print_context_label(String mark, File inf, String label) { |
|
355 |
setupOutput(); |
|
356 |
if (label != null) |
|
357 |
outfile.println(mark + ' ' + label); |
|
358 |
else if (inf.lastModified() > 0) |
|
359 |
// FIXME: use DateFormat to get precise format needed. |
|
360 |
outfile.println(mark + ' ' + inf.getPath() + '\t' + new Date(inf.lastModified())); |
|
361 |
else |
|
362 |
/* Don't pretend that standard input is ancient. */ |
|
363 |
outfile.println(mark + ' ' + inf.getPath()); |
|
364 |
} |
|
365 |
|
|
366 |
public void print_header(String filea, String fileb) { |
|
367 |
print_context_label("***", new File(filea), filea); |
|
368 |
print_context_label("---", new File(fileb), fileb); |
|
369 |
} |
|
370 |
|
|
371 |
/** If function_regexp defined, search for start of function. */ |
|
372 |
private String find_function(Object[] lines, int start) { |
|
373 |
return null; |
|
374 |
} |
|
375 |
|
|
376 |
protected void print_function(Object[] file, int start) { |
|
377 |
String function = find_function(file0, first0); |
|
378 |
if (function != null) { |
|
379 |
outfile.print(" "); |
|
380 |
outfile.print((function.length() < 40) ? function : function.substring(0, 40)); |
|
381 |
} |
|
382 |
} |
|
383 |
|
|
384 |
protected void print_hunk(Diff.change hunk) { |
|
385 |
|
|
386 |
/* Determine range of line numbers involved in each file. */ |
|
387 |
|
|
388 |
analyze_hunk(hunk); |
|
389 |
|
|
390 |
if (deletes == 0 && inserts == 0) |
|
391 |
return; |
|
392 |
|
|
393 |
/* Include a context's width before and after. */ |
|
394 |
|
|
395 |
first0 = Math.max(first0 - context, 0); |
|
396 |
first1 = Math.max(first1 - context, 0); |
|
397 |
last0 = Math.min(last0 + context, file0.length - 1); |
|
398 |
last1 = Math.min(last1 + context, file1.length - 1); |
|
399 |
|
|
400 |
outfile.print("***************"); |
|
401 |
|
|
402 |
/* |
|
403 |
* If we looked for and found a function this is part of, include its name in |
|
404 |
* the header of the diff section. |
|
405 |
*/ |
|
406 |
print_function(file0, first0); |
|
407 |
|
|
408 |
outfile.println(); |
|
409 |
outfile.print("*** "); |
|
410 |
print_number_range(',', first0, last0); |
|
411 |
outfile.println(" ****"); |
|
412 |
|
|
413 |
if (deletes != 0) { |
|
414 |
Diff.change next = hunk; |
|
415 |
|
|
416 |
for (int i = first0; i <= last0; i++) { |
|
417 |
/* |
|
418 |
* Skip past changes that apply (in file 0) only to lines before line I. |
|
419 |
*/ |
|
420 |
|
|
421 |
while (next != null && next.line0 + next.deleted <= i) |
|
422 |
next = next.link; |
|
423 |
|
|
424 |
/* Compute the marking for line I. */ |
|
425 |
|
|
426 |
String prefix = " "; |
|
427 |
if (next != null && next.line0 <= i) |
|
428 |
/* |
|
429 |
* The change NEXT covers this line. If lines were inserted here in file 1, this |
|
430 |
* is "changed". Otherwise it is "deleted". |
|
431 |
*/ |
|
432 |
prefix = (next.inserted > 0) ? "!" : "-"; |
|
433 |
|
|
434 |
print_1_line(prefix, file0[i]); |
|
435 |
} |
|
436 |
} |
|
437 |
|
|
438 |
outfile.print("--- "); |
|
439 |
print_number_range(',', first1, last1); |
|
440 |
outfile.println(" ----"); |
|
441 |
|
|
442 |
if (inserts != 0) { |
|
443 |
Diff.change next = hunk; |
|
444 |
|
|
445 |
for (int i = first1; i <= last1; i++) { |
|
446 |
/* |
|
447 |
* Skip past changes that apply (in file 1) only to lines before line I. |
|
448 |
*/ |
|
449 |
|
|
450 |
while (next != null && next.line1 + next.inserted <= i) |
|
451 |
next = next.link; |
|
452 |
|
|
453 |
/* Compute the marking for line I. */ |
|
454 |
|
|
455 |
String prefix = " "; |
|
456 |
if (next != null && next.line1 <= i) |
|
457 |
/* |
|
458 |
* The change NEXT covers this line. If lines were deleted here in file 0, this |
|
459 |
* is "changed". Otherwise it is "inserted". |
|
460 |
*/ |
|
461 |
prefix = (next.deleted > 0) ? "!" : "+"; |
|
462 |
|
|
463 |
print_1_line(prefix, file1[i]); |
|
464 |
} |
|
465 |
} |
|
466 |
} |
|
467 |
} |
|
468 |
|
|
469 |
/** |
|
470 |
* Prints an edit script in context diff format. This and its 'unified' |
|
471 |
* variation is used for source code patches. |
|
472 |
*/ |
|
473 |
public static class UnifiedPrint extends ContextPrint { |
|
474 |
|
|
475 |
public UnifiedPrint(Object[] a, Object[] b) { |
|
476 |
super(a, b); |
|
477 |
} |
|
478 |
|
|
479 |
public void print_header(String filea, String fileb) { |
|
480 |
print_context_label("---", new File(filea), filea); |
|
481 |
print_context_label("+++", new File(fileb), fileb); |
|
482 |
} |
|
483 |
|
|
484 |
private void print_number_range(int a, int b) { |
|
485 |
// translate_range (file, a, b, &trans_a, &trans_b); |
|
486 |
|
|
487 |
/* |
|
488 |
* Note: we can have B < A in the case of a range of no lines. In this case, we |
|
489 |
* should print the line number before the range, which is B. |
|
490 |
*/ |
|
491 |
if (b < a) |
|
492 |
outfile.print(b + ",0"); |
|
493 |
else |
|
494 |
super.print_number_range(',', a, b); |
|
495 |
} |
|
496 |
|
|
497 |
protected void print_hunk(Diff.change hunk) { |
|
498 |
/* Determine range of line numbers involved in each file. */ |
|
499 |
analyze_hunk(hunk); |
|
500 |
|
|
501 |
if (deletes == 0 && inserts == 0) |
|
502 |
return; |
|
503 |
|
|
504 |
/* Include a context's width before and after. */ |
|
505 |
|
|
506 |
first0 = Math.max(first0 - context, 0); |
|
507 |
first1 = Math.max(first1 - context, 0); |
|
508 |
last0 = Math.min(last0 + context, file0.length - 1); |
|
509 |
last1 = Math.min(last1 + context, file1.length - 1); |
|
510 |
|
|
511 |
outfile.print("@@ -"); |
|
512 |
print_number_range(first0, last0); |
|
513 |
outfile.print(" +"); |
|
514 |
print_number_range(first1, last1); |
|
515 |
outfile.print(" @@"); |
|
516 |
|
|
517 |
/* |
|
518 |
* If we looked for and found a function this is part of, include its name in |
|
519 |
* the header of the diff section. |
|
520 |
*/ |
|
521 |
print_function(file0, first0); |
|
522 |
|
|
523 |
outfile.println(); |
|
524 |
|
|
525 |
Diff.change next = hunk; |
|
526 |
int i = first0; |
|
527 |
int j = first1; |
|
528 |
|
|
529 |
while (i <= last0 || j <= last1) { |
|
530 |
|
|
531 |
/* If the line isn't a difference, output the context from file 0. */ |
|
532 |
|
|
533 |
if (next == null || i < next.line0) { |
|
534 |
if (i < file0.length) { |
|
535 |
outfile.print(' '); |
|
536 |
print_1_line("", file0[i++]); |
|
537 |
} |
|
538 |
j++; |
|
539 |
} else { |
|
540 |
/* For each difference, first output the deleted part. */ |
|
541 |
|
|
542 |
int k = next.deleted; |
|
543 |
while (k-- > 0) { |
|
544 |
outfile.print('-'); |
|
545 |
print_1_line("", file0[i++]); |
|
546 |
} |
|
547 |
|
|
548 |
/* Then output the inserted part. */ |
|
549 |
|
|
550 |
k = next.inserted; |
|
551 |
while (k-- > 0) { |
|
552 |
outfile.print('+'); |
|
553 |
print_1_line("", file1[j++]); |
|
554 |
} |
|
555 |
|
|
556 |
/* We're done with this hunk, so on to the next! */ |
|
557 |
|
|
558 |
next = next.link; |
|
559 |
} |
|
560 |
} |
|
561 |
} |
|
562 |
} |
|
563 |
|
|
564 |
/** |
|
565 |
* Read a text file into an array of String. This provides basic diff |
|
566 |
* functionality. A more advanced diff utility will use specialized objects to |
|
567 |
* represent the text lines, with options to, for example, convert sequences of |
|
568 |
* whitespace to a single space for comparison purposes. |
|
569 |
*/ |
|
570 |
static String[] slurp(String file) throws IOException { |
|
571 |
BufferedReader rdr = new BufferedReader(new FileReader(file)); |
|
572 |
Vector s = new Vector(); |
|
573 |
for (;;) { |
|
574 |
String line = rdr.readLine(); |
|
575 |
if (line == null) |
|
576 |
break; |
|
577 |
s.addElement(line); |
|
578 |
} |
|
579 |
String[] a = new String[s.size()]; |
|
580 |
s.copyInto(a); |
|
581 |
rdr.close(); |
|
582 |
return a; |
|
583 |
} |
|
584 |
|
|
585 |
public static void main(String[] argv) throws IOException { |
|
586 |
String filea = argv[argv.length - 2]; |
|
587 |
String fileb = argv[argv.length - 1]; |
|
588 |
String[] a = slurp(filea); |
|
589 |
String[] b = slurp(fileb); |
|
590 |
Diff d = new Diff(a, b); |
|
591 |
char style = 'n'; |
|
592 |
d.heuristic = false; |
|
593 |
for (int i = 0; i < argv.length - 2; ++i) { |
|
594 |
String f = argv[i]; |
|
595 |
if (f.startsWith("-")) { |
|
596 |
for (int j = 1; j < f.length(); ++j) { |
|
597 |
switch (f.charAt(j)) { |
|
598 |
case 'H': // heuristic on |
|
599 |
d.heuristic = true; |
|
600 |
break; |
|
601 |
case 'e': // Ed style |
|
602 |
style = 'e'; |
|
603 |
break; |
|
604 |
case 'c': // Context diff |
|
605 |
style = 'c'; |
|
606 |
break; |
|
607 |
case 'u': |
|
608 |
style = 'u'; |
|
609 |
break; |
|
610 |
} |
|
611 |
} |
|
612 |
} |
|
613 |
} |
|
614 |
boolean reverse = style == 'e'; |
|
615 |
Diff.change script = d.diff_2(reverse); |
|
616 |
if (script == null) |
|
617 |
System.err.println("No differences"); |
|
618 |
else { |
|
619 |
Base p; |
|
620 |
switch (style) { |
|
621 |
case 'e': |
|
622 |
p = new EdPrint(a, b); |
|
623 |
break; |
|
624 |
case 'c': |
|
625 |
p = new ContextPrint(a, b); |
|
626 |
break; |
|
627 |
case 'u': |
|
628 |
p = new UnifiedPrint(a, b); |
|
629 |
break; |
|
630 |
default: |
|
631 |
p = new NormalPrint(a, b); |
|
632 |
} |
|
633 |
p.print_header(filea, fileb); |
|
634 |
p.print_script(script); |
|
635 |
} |
|
636 |
} |
|
637 |
|
|
638 |
} |
|
0 | 639 |
TXM/trunk/bundles/org.txm.utils.core/src/org/txm/utils/io/IOUtils.java (revision 3666) | ||
---|---|---|
1 |
package org.txm.utils.io; |
|
2 |
|
|
3 |
import java.io.BufferedOutputStream; |
|
4 |
import java.io.BufferedReader; |
|
5 |
import java.io.File; |
|
6 |
import java.io.FileFilter; |
|
7 |
import java.io.FileInputStream; |
|
8 |
import java.io.FileNotFoundException; |
|
9 |
import java.io.FileOutputStream; |
|
10 |
import java.io.IOException; |
|
11 |
import java.io.InputStream; |
|
12 |
import java.io.InputStreamReader; |
|
13 |
import java.io.OutputStream; |
|
14 |
import java.io.OutputStreamWriter; |
|
15 |
import java.io.PrintWriter; |
|
16 |
import java.io.UnsupportedEncodingException; |
|
17 |
import java.net.MalformedURLException; |
|
18 |
import java.net.URL; |
|
19 |
import java.net.URLConnection; |
|
20 |
import java.util.ArrayList; |
|
21 |
import java.util.regex.Matcher; |
|
22 |
import java.util.regex.Pattern; |
|
23 |
|
|
24 |
import org.eclipse.osgi.util.NLS; |
|
25 |
import org.txm.utils.FileUtils; |
|
26 |
import org.txm.utils.i18n.DetectBOM; |
|
27 |
import org.txm.utils.logger.Log; |
|
28 |
|
|
29 |
public class IOUtils { |
|
30 |
|
|
31 |
public static final String UTF8 = "UTF-8"; //$NON-NLS-1$ |
|
32 |
|
|
33 |
public static ArrayList<String> findWithGroup(File file, String pattern) throws IOException { |
|
34 |
|
|
35 |
return findWithGroup(file, UTF8, pattern, false); |
|
36 |
} |
|
37 |
|
|
38 |
public static ArrayList<String> findWithGroup(File file, String pattern, boolean normalizeSeparators) throws IOException { |
|
39 |
|
|
40 |
return findWithGroup(file, UTF8, pattern, normalizeSeparators); |
|
41 |
} |
|
42 |
|
|
43 |
public static ArrayList<String> findWithGroup(File file, String encoding, String pattern) throws IOException { |
|
44 |
|
|
45 |
return findWithGroup(file, encoding, pattern, false); |
|
46 |
} |
|
47 |
|
|
48 |
/** |
|
49 |
* select only files not hidden @deprecated |
|
50 |
*/ |
|
51 |
public static final FileFilter HIDDENFILE_FILTER = FileUtils.HIDDENFILE_FILTER; |
|
52 |
|
|
53 |
|
|
54 |
public static ArrayList<String> findWithGroup(File file, String encoding, String pattern, boolean normalizeSeparators) throws IOException { |
|
55 |
|
|
56 |
ArrayList<String> matches = new ArrayList<String>(); |
|
57 |
String text = IOUtils.getText(file, encoding); |
|
58 |
if (normalizeSeparators) { |
|
59 |
text = text.replaceAll("\\s", " "); //$NON-NLS-1$ |
|
60 |
text = text.replaceAll("\\t", " "); //$NON-NLS-1$ |
|
61 |
text = text.replaceAll("\\n", " "); //$NON-NLS-1$ |
|
62 |
text = text.replaceAll("[ ]+", " "); //$NON-NLS-1$ |
|
63 |
} |
|
64 |
|
|
65 |
Pattern test = Pattern.compile(pattern); |
|
66 |
Matcher m = test.matcher(text); |
|
67 |
while (m.find()) { |
|
68 |
matches.add(m.group(1)); |
|
69 |
} |
|
70 |
return matches; |
|
71 |
} |
|
72 |
|
|
73 |
public static ArrayList<String> find(File file, String pattern) throws IOException { |
|
74 |
|
|
75 |
return find(file, UTF8, pattern, false); |
|
76 |
} |
|
77 |
|
|
78 |
public static ArrayList<String> find(File file, String pattern, boolean normalizeSeparators) throws IOException { |
|
79 |
|
|
80 |
return findWithGroup(file, UTF8, pattern, normalizeSeparators); |
|
81 |
} |
|
82 |
|
|
83 |
public static ArrayList<String> find(File file, String encoding, String pattern) throws IOException { |
|
84 |
|
|
85 |
return find(file, encoding, pattern, false); |
|
86 |
} |
|
87 |
|
|
88 |
public static ArrayList<String> find(File file, String encoding, String pattern, boolean normalizeSeparators) throws IOException { |
|
89 |
|
|
90 |
ArrayList<String> matches = new ArrayList<String>(); |
|
91 |
String text = IOUtils.getText(file, encoding); |
|
92 |
// System.out.println(text); |
|
93 |
if (normalizeSeparators) { |
|
94 |
text = text.replaceAll("\\s", " "); //$NON-NLS-1$ //$NON-NLS-2$ |
|
95 |
text = text.replaceAll("\\t", " "); //$NON-NLS-1$ //$NON-NLS-2$ |
|
96 |
text = text.replaceAll("\\n", " "); //$NON-NLS-1$ //$NON-NLS-2$ |
|
97 |
text = text.replaceAll("[ ]+", " "); //$NON-NLS-1$ //$NON-NLS-2$ |
|
98 |
} |
|
99 |
|
|
100 |
Pattern test = Pattern.compile(pattern); |
|
101 |
Matcher m = test.matcher(text); |
|
102 |
while (m.find()) { |
|
103 |
matches.add(m.group()); |
|
104 |
} |
|
105 |
return matches; |
|
106 |
} |
|
107 |
|
|
108 |
public static BufferedReader getReader(File file) throws UnsupportedEncodingException, FileNotFoundException { |
|
109 |
|
|
110 |
return getReader(file, UTF8); |
|
111 |
} |
|
112 |
|
|
113 |
/** |
|
114 |
* |
|
115 |
* @param file |
|
116 |
* @param encoding |
|
117 |
* if null the "UTF-8" encoding is used |
|
118 |
* @return |
|
119 |
* @throws UnsupportedEncodingException |
|
120 |
* @throws FileNotFoundException |
|
121 |
*/ |
|
122 |
public static BufferedReader getReader(File file, String encoding) throws UnsupportedEncodingException, FileNotFoundException { |
|
123 |
|
|
124 |
if (encoding == null) { |
|
125 |
encoding = UTF8; // prevent null exception |
|
126 |
} |
|
127 |
|
|
128 |
FileInputStream input = new FileInputStream(file); |
|
129 |
DetectBOM db = new DetectBOM(file); |
|
130 |
for (int ibom = 0; ibom < db.getBOMSize(); ibom++) { |
|
131 |
try { |
|
132 |
input.read(); |
|
133 |
} catch (IOException e) { |
|
134 |
e.printStackTrace(); |
|
135 |
} |
|
136 |
} |
|
137 |
return new BufferedReader(new InputStreamReader(input, encoding)); |
|
138 |
} |
|
139 |
|
|
140 |
public static BufferedReader getReader(String file) throws UnsupportedEncodingException, FileNotFoundException { |
|
141 |
|
|
142 |
return getReader(new File(file), UTF8); |
|
143 |
} |
|
144 |
|
|
145 |
public static BufferedReader getReader(URL url) throws IOException { |
|
146 |
|
|
147 |
return getReader(url, UTF8); |
|
148 |
} |
|
149 |
|
|
150 |
public static BufferedReader getReader(URL url, String encoding) throws IOException { |
|
151 |
|
|
152 |
if (encoding == null) { |
|
153 |
encoding = UTF8; // prevent null exception |
|
154 |
} |
|
155 |
|
|
156 |
URLConnection conn = url.openConnection(); |
|
157 |
InputStream inputStream = conn.getInputStream(); |
|
158 |
return new BufferedReader(new InputStreamReader(inputStream, encoding)); |
|
159 |
} |
|
160 |
|
|
161 |
public static void setText(File file, String text, String encoding) throws IOException { |
|
162 |
|
|
163 |
PrintWriter writer = getWriter(file, encoding); |
|
164 |
writer.print(text); |
|
165 |
writer.close(); |
|
166 |
} |
|
167 |
|
|
168 |
public static String getText(URL url, String encoding) throws IOException { |
|
169 |
|
|
170 |
BufferedReader reader = getReader(url, encoding); |
|
171 |
StringBuilder builder = new StringBuilder(); |
|
172 |
try { |
|
173 |
String line = reader.readLine(); |
|
174 |
while (line != null) { |
|
175 |
builder.append(line); |
|
176 |
line = reader.readLine(); |
|
177 |
if (line != null) |
|
178 |
builder.append("\n"); //$NON-NLS-1$ |
|
179 |
} |
|
180 |
} finally { |
|
181 |
reader.close(); |
|
182 |
} |
|
183 |
return builder.toString(); |
|
184 |
} |
|
185 |
|
|
186 |
public static String getText(File file) throws IOException { |
|
187 |
|
|
188 |
return getText(file, UTF8); |
|
189 |
} |
|
190 |
|
|
191 |
public static String getText(File file, String encoding) throws IOException { |
|
192 |
|
|
193 |
BufferedReader reader = getReader(file, encoding); |
|
194 |
StringBuilder builder = new StringBuilder(); |
|
195 |
try { |
|
196 |
String line = reader.readLine(); |
|
197 |
while (line != null) { |
|
198 |
builder.append(line); |
|
199 |
line = reader.readLine(); |
|
200 |
if (line != null) |
|
201 |
builder.append("\n"); //$NON-NLS-1$ |
|
202 |
} |
|
203 |
} finally { |
|
204 |
reader.close(); |
|
205 |
} |
|
206 |
return builder.toString(); |
|
207 |
} |
|
208 |
|
|
209 |
public static PrintWriter getWriter(File file) throws UnsupportedEncodingException, FileNotFoundException { |
|
210 |
|
|
211 |
return getWriter(file, UTF8); |
|
212 |
} |
|
213 |
|
|
214 |
public static PrintWriter getWriter(File file, String encoding) throws UnsupportedEncodingException, FileNotFoundException { |
|
215 |
|
|
216 |
return getWriter(file, encoding, false); |
|
217 |
} |
|
218 |
|
|
219 |
public static PrintWriter getWriter(File file, boolean append) throws UnsupportedEncodingException, FileNotFoundException { |
|
220 |
|
|
221 |
return getWriter(file, UTF8, append); |
|
222 |
} |
|
223 |
|
|
224 |
public static PrintWriter getWriter(File file, String encoding, boolean append) throws UnsupportedEncodingException, FileNotFoundException { |
|
225 |
|
|
226 |
if (encoding == null) { |
|
227 |
encoding = UTF8; // prevent null exception |
|
228 |
} |
|
229 |
|
|
230 |
return new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(file, append)), encoding)); |
|
231 |
} |
|
232 |
|
|
233 |
public static PrintWriter getWriter(String file) throws UnsupportedEncodingException, FileNotFoundException { |
|
234 |
|
|
235 |
return getWriter(new File(file), UTF8); |
|
236 |
} |
|
237 |
|
|
238 |
public static boolean write(File file, String str) { |
|
239 |
|
|
240 |
PrintWriter writer = null; |
|
241 |
try { |
|
242 |
writer = getWriter(file); |
|
243 |
writer.write(str); |
|
244 |
} catch(Exception e) { |
|
245 |
Log.warning(NLS.bind("Error while writing the {0} file: {1}", file, e)); |
|
246 |
return false; |
|
247 |
} finally { |
|
248 |
if (writer != null) { |
|
249 |
writer.close(); |
|
250 |
} |
|
251 |
} |
|
252 |
return true; |
|
253 |
} |
|
254 |
|
|
255 |
public static ArrayList<String> getLines(File file, String encoding) { |
|
256 |
|
|
257 |
ArrayList<String> lines = new ArrayList<String>(); |
|
258 |
try { |
|
259 |
BufferedReader reader = IOUtils.getReader(file, encoding); |
|
260 |
String line = reader.readLine(); |
|
261 |
while (line != null) { |
|
262 |
lines.add(line); |
|
263 |
line = reader.readLine(); |
|
264 |
} |
|
265 |
reader.close(); |
|
266 |
} catch (Exception e) { |
|
267 |
System.err.println(NLS.bind("Error while getting lines: {0}.", e.getLocalizedMessage())); |
|
268 |
} |
|
269 |
return lines; |
|
270 |
} |
|
271 |
|
|
272 |
/** |
|
273 |
* |
|
274 |
* @param manifest |
|
275 |
* @return an InputStream, dont forget to close it |
|
276 |
* @throws MalformedURLException |
|
277 |
* @throws IOException |
|
278 |
*/ |
|
279 |
public static InputStream getInput(File file) throws MalformedURLException, IOException { |
|
280 |
|
|
281 |
return file.toURI().toURL().openStream(); |
|
282 |
} |
|
283 |
|
|
284 |
public static OutputStream getOutputStream(File file) throws FileNotFoundException { |
|
285 |
|
|
286 |
return new BufferedOutputStream(new FileOutputStream(file, false)); |
|
287 |
} |
|
288 |
|
|
289 |
public static OutputStream getOutputStream(File file, boolean append) throws FileNotFoundException { |
|
290 |
|
|
291 |
return new BufferedOutputStream(new FileOutputStream(file, append)); |
|
292 |
} |
|
293 |
|
|
294 |
public static void replace(File file, String oldString, String newString) throws IOException { |
|
295 |
|
|
296 |
String text = getText(file, UTF8); |
|
297 |
text = text.replace(oldString, newString); |
|
298 |
IOUtils.write(file, text); |
|
299 |
} |
|
300 |
|
|
301 |
public static void replaceAll(File file, Pattern regex, String newString) throws IOException { |
|
302 |
|
|
303 |
String text = getText(file, UTF8); |
|
304 |
text = regex.matcher(text).replaceAll(newString); |
|
305 |
IOUtils.write(file, text); |
|
306 |
} |
|
307 |
|
|
308 |
public static String firstLine(File file) { |
|
309 |
try { |
|
310 |
BufferedReader reader = getReader(file); |
|
311 |
String line = reader.readLine(); |
|
312 |
reader.close(); |
|
313 |
return line; |
|
314 |
} catch (Exception e) { |
|
315 |
return null; |
|
316 |
} |
|
317 |
} |
|
318 |
} |
|
0 | 319 |
TXM/trunk/bundles/org.txm.utils.core/src/org/txm/utils/io/FileCopy.java (revision 3666) | ||
---|---|---|
1 |
// Copyright © 2010-2020 ENS de Lyon., University of Franche-Comté |
|
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: 2016-11-17 17:01:31 +0100 (Thu, 17 Nov 2016) $ |
|
25 |
// $LastChangedRevision: 3341 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.utils.io; |
|
29 |
|
|
30 |
import java.io.BufferedOutputStream; |
|
31 |
import java.io.File; |
|
32 |
import java.io.FileInputStream; |
|
33 |
import java.io.FileOutputStream; |
|
34 |
import java.io.IOException; |
|
35 |
import java.nio.MappedByteBuffer; |
|
36 |
import java.nio.channels.FileChannel; |
|
37 |
import java.nio.file.Files; |
|
38 |
import java.nio.file.Path; |
|
39 |
|
|
40 |
import org.txm.utils.messages.UtilsCoreMessages; |
|
41 |
|
|
42 |
/** |
|
43 |
* File copy methods |
|
44 |
*/ |
|
45 |
public class FileCopy { |
|
46 |
|
|
47 |
private static final boolean isWindows = System.getProperty("os.name").contains("Windows"); |
|
48 |
|
|
49 |
/** |
|
50 |
* replace the file if it exists |
|
51 |
* |
|
52 |
* @param source |
|
53 |
* @param dest |
|
54 |
* @return |
|
55 |
* @throws IOException |
|
56 |
*/ |
|
57 |
public static boolean copy(File source, File dest) throws IOException { |
|
58 |
|
|
59 |
return copy(source, dest, true); |
|
60 |
} |
|
61 |
|
|
62 |
/** |
|
63 |
* copy one file with nio package to accelerate the copy |
|
64 |
* |
|
65 |
* !!! on windows : max size = 64mo !!!. |
|
66 |
* |
|
67 |
* resolve symbolic links |
|
68 |
* |
|
69 |
* @param source |
|
70 |
* the source |
|
71 |
* @param dest |
|
72 |
* the dest |
|
73 |
* @return true, if successful |
|
74 |
* @throws IOException |
|
75 |
* Signals that an I/O exception has occurred. |
|
76 |
*/ |
|
77 |
public static boolean copy(File source, File dest, boolean replace) throws IOException { |
|
78 |
|
|
79 |
if (dest.exists() && !replace) |
|
80 |
return true; // don't replace file |
|
81 |
|
|
82 |
if (isWindows || source.length() >= Integer.MAX_VALUE) { |
|
83 |
// System.out.println("COPY BIG FILE"); |
|
84 |
FileInputStream instream = null; |
|
85 |
BufferedOutputStream outstream = null; |
|
86 |
try { |
|
87 |
// Open a stream of bytes to read the file bytes into the program |
|
88 |
instream = new FileInputStream(source); |
|
89 |
// A stream of bytes from the program to the destination |
|
90 |
outstream = new BufferedOutputStream(new FileOutputStream(dest)); |
|
91 |
byte[] buffer = new byte[4096]; |
|
92 |
int bytesRead; |
|
93 |
// Write the bytes from the inputstream to the outputstream |
|
94 |
while ((bytesRead = instream.read(buffer)) != -1) { |
|
95 |
outstream.write(buffer, 0, bytesRead); |
|
96 |
} |
|
97 |
} catch (Exception e) { |
|
98 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
99 |
return false; |
|
100 |
} finally { |
|
101 |
if (instream != null) |
|
102 |
instream.close(); |
|
103 |
if (outstream != null) |
|
104 |
outstream.close(); |
|
105 |
instream = null; |
|
106 |
outstream = null; |
|
107 |
} |
|
108 |
} else { |
|
109 |
// System.out.println("COPY NORMAL FILE"); |
|
110 |
FileChannel in = null, out = null; |
|
111 |
FileInputStream fis = null; |
|
112 |
FileOutputStream fos = null; |
|
113 |
try { |
|
114 |
fis = new FileInputStream(source); |
|
115 |
fos = new FileOutputStream(dest); |
|
116 |
in = fis.getChannel(); |
|
117 |
out = fos.getChannel(); |
|
118 |
|
|
119 |
long size = in.size(); |
|
120 |
MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size); |
|
121 |
|
|
122 |
out.write(buf); |
|
123 |
} catch (Exception e) { |
|
124 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
125 |
|
|
126 |
if (fis != null) |
|
127 |
fis.close(); |
|
128 |
if (fos != null) |
|
129 |
fos.close(); |
|
130 |
if (in != null) |
|
131 |
in.close(); |
|
132 |
if (out != null) |
|
133 |
out.close(); |
|
134 |
|
|
135 |
return false; |
|
136 |
} finally { |
|
137 |
if (fis != null) |
|
138 |
fis.close(); |
|
139 |
if (fos != null) |
|
140 |
fos.close(); |
|
141 |
if (in != null) |
|
142 |
in.close(); |
|
143 |
if (out != null) |
|
144 |
out.close(); |
|
145 |
} |
|
146 |
} |
|
147 |
return true; |
|
148 |
} |
|
149 |
|
|
150 |
/** |
|
151 |
* Copy files. Don't erase the destination directory and replace files if they |
|
152 |
* already exists |
|
153 |
* |
|
154 |
* warning: Symbolic links are not resolved and the link file is recreated |
|
155 |
* |
|
156 |
* @param src |
|
157 |
* the src directory |
|
158 |
* @param dest |
|
159 |
* the dest directory |
|
160 |
* @throws IOException |
|
161 |
* Signals that an I/O exception has occurred. |
|
162 |
*/ |
|
163 |
public static void copyFiles(File src, File dest) throws IOException { |
|
164 |
|
|
165 |
copyFiles(src, dest, true); |
|
166 |
} |
|
167 |
|
|
168 |
/** |
|
169 |
* Copy files. Don't erase the destination directory and replace files if they |
|
170 |
* already exists |
|
171 |
* |
|
172 |
* warning: Symbolic links are not resolved and the link file is recreated |
|
173 |
* |
|
174 |
* @param src |
|
175 |
* the src directory |
|
176 |
* @param dest |
|
177 |
* the dest directory |
|
178 |
* @param replace |
|
179 |
* replace files if true |
|
180 |
* @throws IOException |
|
181 |
* Signals that an I/O exception has occurred. |
|
182 |
*/ |
|
183 |
public static void copyFiles(File src, File dest, boolean replace) throws IOException { |
|
184 |
|
|
185 |
// Check to ensure that the source is valid... |
|
186 |
if (!src.exists()) { |
|
187 |
throw new IOException(UtilsCoreMessages.FileCopy_0 + src.getAbsolutePath() + "."); //$NON-NLS-1$ |
|
188 |
} else if (!src.canRead()) { // check to ensure we have rights to the |
|
189 |
// source... |
|
190 |
throw new IOException(UtilsCoreMessages.FileCopy_2 + src.getAbsolutePath() + "."); //$NON-NLS-1$ |
|
191 |
} |
|
192 |
|
|
193 |
if (Files.isSymbolicLink(src.toPath())) { // copy symbolic links |
|
194 |
Path target = Files.readSymbolicLink(src.toPath()); |
|
195 |
Files.createSymbolicLink(dest.toPath(), target); |
|
196 |
} else if (src.isDirectory()) { // is it a directory ? |
|
197 |
|
|
198 |
if (!dest.exists()) { // does the destination already exist? |
|
199 |
// if not create the file hierarchy |
|
200 |
dest.mkdirs(); |
|
201 |
if (!dest.exists()) { |
|
202 |
throw new IOException(UtilsCoreMessages.FileCopy_4 + dest.getAbsolutePath() + "."); //$NON-NLS-1$ |
|
203 |
} |
|
204 |
} |
|
205 |
|
|
206 |
// get a listing of files... |
|
207 |
String list[] = src.list(); |
|
208 |
// copy all the files in the list. |
|
209 |
for (int i = 0; i < list.length; i++) { |
|
210 |
File dest1 = new File(dest, list[i]); |
|
211 |
File src1 = new File(src, list[i]); |
|
212 |
copyFiles(src1, dest1); |
|
213 |
|
|
214 |
} |
|
215 |
} else { |
|
216 |
// This was not a directory, so lets just copy the file |
|
217 |
copy(src, dest, replace); |
|
218 |
} |
|
219 |
} |
|
220 |
|
|
221 |
public static void main(String[] args) throws IOException { |
|
222 |
// File test1 = File.createTempFile("source", ".large"); //$NON-NLS-1$ |
|
223 |
// //$NON-NLS-2$ |
|
224 |
// File test2 = File.createTempFile("source", ".verylarge"); //$NON-NLS-1$ |
|
225 |
// //$NON-NLS-2$ |
|
226 |
// |
|
227 |
// System.out.println("write "+test1); //$NON-NLS-1$ |
|
228 |
// writeBigFile(test1, Integer.MAX_VALUE / 2, 1); |
|
229 |
// System.out.println("write "+test2); //$NON-NLS-1$ |
|
230 |
// writeBigFile(test2, Integer.MAX_VALUE / 2, 3); |
|
231 |
// |
|
232 |
// System.out.println("test1: "+test1+" : "+test1.length()); //$NON-NLS-1$ |
|
233 |
// //$NON-NLS-2$ |
|
234 |
// System.out.println("test2: "+test2+" : "+test2.length()); //$NON-NLS-1$ |
|
235 |
// //$NON-NLS-2$ |
|
236 |
// |
|
237 |
// File copy1 = File.createTempFile("copy", ".large"); //$NON-NLS-1$ |
|
238 |
// //$NON-NLS-2$ |
|
239 |
// File copy2 = File.createTempFile("copy", ".verylarge"); //$NON-NLS-1$ |
|
240 |
// //$NON-NLS-2$ |
|
241 |
// |
|
242 |
// System.out.println("Copy1 "+copy1); //$NON-NLS-1$ |
|
243 |
// FileCopy.copy(test1, copy1); |
|
244 |
// System.out.println("Copy2 "+copy2); //$NON-NLS-1$ |
|
245 |
// FileCopy.copy(test2, copy2); |
|
246 |
// |
|
247 |
// System.out.println("copy1: "+copy1+" : "+copy1.length()); //$NON-NLS-1$ |
|
248 |
// //$NON-NLS-2$ |
|
249 |
// System.out.println("copy2: "+copy2+" : "+copy2.length()); //$NON-NLS-1$ |
|
250 |
// //$NON-NLS-2$ |
|
251 |
// |
|
252 |
// System.out.println("delete 1 "+test1.delete()); //$NON-NLS-1$ |
|
253 |
// System.out.println("delete 2 "+test2.delete()); //$NON-NLS-1$ |
|
254 |
// System.out.println("delete c1 "+copy1.delete()); //$NON-NLS-1$ |
|
255 |
// System.out.println("delete c2 "+copy2.delete()); //$NON-NLS-1$ |
|
256 |
|
|
257 |
File dir1 = new File("/home/mdecorde/TEMP/copydir"); |
|
258 |
File dir2 = new File("/home/mdecorde/TEMP/copydir3"); |
|
259 |
FileCopy.copyFiles(dir1, dir2); |
|
260 |
} |
|
261 |
} |
|
0 | 262 |
TXM/trunk/bundles/org.txm.utils.core/src/org/txm/utils/MessagePrinter.java (revision 3666) | ||
---|---|---|
1 |
package org.txm.utils; |
|
2 |
|
|
3 |
public interface MessagePrinter { |
|
4 |
public void println(String message); |
|
5 |
|
|
6 |
public void print(String message); |
|
7 |
} |
|
0 | 8 |
TXM/trunk/bundles/org.txm.utils.core/src/org/txm/utils/StreamHog.java (revision 3666) | ||
---|---|---|
1 |
// Copyright © 2010-2020 ENS de Lyon., University of Franche-Comté |
|
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 |
// |
Formats disponibles : Unified diff