Révision 296
tmp/org.txm.utils/src/org/txm/utils/FileCopy.java (revision 296) | ||
---|---|---|
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: 2016-11-17 17:01:31 +0100 (Thu, 17 Nov 2016) $ |
|
25 |
// $LastChangedRevision: 3341 $ |
|
26 |
// $LastChangedBy: mdecorde $ |
|
27 |
// |
|
28 |
package org.txm.utils; |
|
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 |
|
|
38 |
import org.txm.core.messages.TXMCoreMessages; |
|
39 |
|
|
40 |
/** |
|
41 |
* File copy methods |
|
42 |
*/ |
|
43 |
public class FileCopy { |
|
44 |
|
|
45 |
private static final boolean isWindows = System.getProperty("os.name").contains("Windows"); |
|
46 |
|
|
47 |
/** |
|
48 |
* replace the file if it exists |
|
49 |
* |
|
50 |
* @param source |
|
51 |
* @param dest |
|
52 |
* @return |
|
53 |
* @throws IOException |
|
54 |
*/ |
|
55 |
public static boolean copy(File source, File dest) throws IOException { |
|
56 |
return copy(source, dest, true); |
|
57 |
} |
|
58 |
|
|
59 |
|
|
60 |
/** |
|
61 |
* copy one file with nio package to accelerate the copy |
|
62 |
* |
|
63 |
* !!! on windows : max size = 64mo !!!. |
|
64 |
* |
|
65 |
* @param source the source |
|
66 |
* @param dest the dest |
|
67 |
* @return true, if successful |
|
68 |
* @throws IOException Signals that an I/O exception has occurred. |
|
69 |
*/ |
|
70 |
public static boolean copy(File source, File dest, boolean replace) throws IOException { |
|
71 |
if (dest.exists() && !replace) return true; // don't replace file |
|
72 |
|
|
73 |
if (isWindows || source.length() >= Integer.MAX_VALUE) { |
|
74 |
//System.out.println("COPY BIG FILE"); |
|
75 |
FileInputStream instream = null; |
|
76 |
BufferedOutputStream outstream = null; |
|
77 |
try { |
|
78 |
//Open a stream of bytes to read the file bytes into the program |
|
79 |
instream = new FileInputStream(source); |
|
80 |
//A stream of bytes from the program to the destination |
|
81 |
outstream = new BufferedOutputStream(new FileOutputStream(dest)); |
|
82 |
byte[] buffer = new byte[4096]; |
|
83 |
int bytesRead; |
|
84 |
//Write the bytes from the inputstream to the outputstream |
|
85 |
while((bytesRead = instream.read(buffer))!=-1) { |
|
86 |
outstream.write(buffer, 0, bytesRead); |
|
87 |
} |
|
88 |
} catch (Exception e) {org.txm.utils.logger.Log.printStackTrace(e); return false; |
|
89 |
} finally { |
|
90 |
instream.close(); |
|
91 |
outstream.close(); |
|
92 |
instream = null; |
|
93 |
outstream = null; |
|
94 |
} |
|
95 |
} else { |
|
96 |
//System.out.println("COPY NORMAL FILE"); |
|
97 |
FileChannel in = null, out = null; |
|
98 |
FileInputStream fis = null; |
|
99 |
FileOutputStream fos = null; |
|
100 |
try { |
|
101 |
fis = new FileInputStream(source); |
|
102 |
fos = new FileOutputStream(dest); |
|
103 |
in = fis.getChannel(); |
|
104 |
out = fos.getChannel(); |
|
105 |
|
|
106 |
long size = in.size(); |
|
107 |
MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size); |
|
108 |
|
|
109 |
out.write(buf); |
|
110 |
} catch(Exception e) { |
|
111 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
112 |
|
|
113 |
if (fis != null) fis.close(); |
|
114 |
if (fos != null) fos.close(); |
|
115 |
if (in != null) in.close(); |
|
116 |
if (out != null) out.close(); |
|
117 |
|
|
118 |
return false; |
|
119 |
} finally { |
|
120 |
if (fis != null) fis.close(); |
|
121 |
if (fos != null) fos.close(); |
|
122 |
if (in != null) in.close(); |
|
123 |
if (out != null) out.close(); |
|
124 |
} |
|
125 |
} |
|
126 |
return true; |
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* Copy files. Don't erase the destination directory and replace files if they already exists |
|
131 |
* |
|
132 |
* @param src the src directory |
|
133 |
* @param dest the dest directory |
|
134 |
* @throws IOException Signals that an I/O exception has occurred. |
|
135 |
*/ |
|
136 |
public static void copyFiles(File src, File dest) throws IOException { |
|
137 |
|
|
138 |
// Check to ensure that the source is valid... |
|
139 |
if (!src.exists()) { |
|
140 |
throw new IOException(TXMCoreMessages.FileCopy_0 + src.getAbsolutePath() |
|
141 |
+ "."); //$NON-NLS-1$ |
|
142 |
} else if (!src.canRead()) { // check to ensure we have rights to the |
|
143 |
// source... |
|
144 |
throw new IOException(TXMCoreMessages.FileCopy_2 + src.getAbsolutePath() |
|
145 |
+ "."); //$NON-NLS-1$ |
|
146 |
} |
|
147 |
|
|
148 |
// is it a directory ? |
|
149 |
if (src.isDirectory()) { |
|
150 |
if (!dest.exists()) { // does the destination already exist? |
|
151 |
// if not create the file hierarchy |
|
152 |
if (!dest.mkdirs()) { |
|
153 |
throw new IOException(TXMCoreMessages.FileCopy_4 |
|
154 |
+ dest.getAbsolutePath() + "."); //$NON-NLS-1$ |
|
155 |
} |
|
156 |
} |
|
157 |
|
|
158 |
// get a listing of files... |
|
159 |
String list[] = src.list(); |
|
160 |
// copy all the files in the list. |
|
161 |
for (int i = 0; i < list.length; i++) { |
|
162 |
File dest1 = new File(dest, list[i]); |
|
163 |
File src1 = new File(src, list[i]); |
|
164 |
copyFiles(src1, dest1); |
|
165 |
|
|
166 |
} |
|
167 |
} else { |
|
168 |
// This was not a directory, so lets just copy the file |
|
169 |
copy(src, dest); |
|
170 |
} |
|
171 |
} |
|
172 |
|
|
173 |
public static void main(String[] args) throws IOException { |
|
174 |
// File test1 = File.createTempFile("source", ".large"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
175 |
// File test2 = File.createTempFile("source", ".verylarge"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
176 |
// |
|
177 |
// System.out.println("write "+test1); //$NON-NLS-1$ |
|
178 |
// writeBigFile(test1, Integer.MAX_VALUE / 2, 1); |
|
179 |
// System.out.println("write "+test2); //$NON-NLS-1$ |
|
180 |
// writeBigFile(test2, Integer.MAX_VALUE / 2, 3); |
|
181 |
// |
|
182 |
// System.out.println("test1: "+test1+" : "+test1.length()); //$NON-NLS-1$ //$NON-NLS-2$ |
|
183 |
// System.out.println("test2: "+test2+" : "+test2.length()); //$NON-NLS-1$ //$NON-NLS-2$ |
|
184 |
// |
|
185 |
// File copy1 = File.createTempFile("copy", ".large"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
186 |
// File copy2 = File.createTempFile("copy", ".verylarge"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
187 |
// |
|
188 |
// System.out.println("Copy1 "+copy1); //$NON-NLS-1$ |
|
189 |
// FileCopy.copy(test1, copy1); |
|
190 |
// System.out.println("Copy2 "+copy2); //$NON-NLS-1$ |
|
191 |
// FileCopy.copy(test2, copy2); |
|
192 |
// |
|
193 |
// System.out.println("copy1: "+copy1+" : "+copy1.length()); //$NON-NLS-1$ //$NON-NLS-2$ |
|
194 |
// System.out.println("copy2: "+copy2+" : "+copy2.length()); //$NON-NLS-1$ //$NON-NLS-2$ |
|
195 |
// |
|
196 |
// System.out.println("delete 1 "+test1.delete()); //$NON-NLS-1$ |
|
197 |
// System.out.println("delete 2 "+test2.delete()); //$NON-NLS-1$ |
|
198 |
// System.out.println("delete c1 "+copy1.delete()); //$NON-NLS-1$ |
|
199 |
// System.out.println("delete c2 "+copy2.delete()); //$NON-NLS-1$ |
|
200 |
|
|
201 |
File dir1 = new File("/home/mdecorde/TEMP/copy1"); |
|
202 |
File dir2 = new File("/home/mdecorde/TEMP/copy2"); |
|
203 |
FileCopy.copyFiles(dir1, dir2); |
|
204 |
} |
|
205 |
} |
|
206 |
|
tmp/org.txm.utils/src/org/txm/utils/IOUtils.java (revision 296) | ||
---|---|---|
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.utils/src/org/txm/utils/SystemProxyDetector.java (revision 296) | ||
---|---|---|
1 |
package org.txm.utils; |
|
2 |
|
|
3 |
import java.net.InetSocketAddress; |
|
4 |
import java.net.Proxy; |
|
5 |
import java.net.ProxySelector; |
|
6 |
import java.net.URI; |
|
7 |
import java.net.URISyntaxException; |
|
8 |
import java.util.List; |
|
9 |
|
|
10 |
import org.eclipse.core.net.proxy.IProxyService; |
|
11 |
import org.osgi.framework.Bundle; |
|
12 |
import org.osgi.framework.BundleContext; |
|
13 |
import org.osgi.framework.FrameworkUtil; |
|
14 |
import org.osgi.util.tracker.ServiceTracker; |
|
15 |
import org.txm.utils.logger.Log; |
|
16 |
|
|
17 |
/** |
|
18 |
* This class implements various manner to retrieve the system proxy configuration. |
|
19 |
* It tries to get the configuration from the RCP store and from the Java system properties. |
|
20 |
* |
|
21 |
* @author mdecorde, sjacquot |
|
22 |
* |
|
23 |
*/ |
|
24 |
public class SystemProxyDetector { |
|
25 |
|
|
26 |
public String proxy_user = ""; |
|
27 |
public String proxy_password = ""; |
|
28 |
public String proxy_host = ""; |
|
29 |
public int proxy_port = 0; |
|
30 |
|
|
31 |
|
|
32 |
/** |
|
33 |
* Creates a system proxy detector and tries to retrieve the system proxy configuration from the RCP store and from the Java system properties. |
|
34 |
*/ |
|
35 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
|
36 |
public SystemProxyDetector() { |
|
37 |
ServiceTracker proxyTracker = null; |
|
38 |
try { |
|
39 |
Bundle c = FrameworkUtil.getBundle(this.getClass()); |
|
40 |
if (c == null) return; // abord; |
|
41 |
BundleContext bc = c.getBundleContext(); |
|
42 |
if (bc == null) return; // abord; |
|
43 |
proxyTracker = new ServiceTracker(bc, IProxyService.class.getName(), null); |
|
44 |
proxyTracker.open(); |
|
45 |
|
|
46 |
IProxyService proxyService = (IProxyService)proxyTracker.getService(); |
|
47 |
if (proxyService.hasSystemProxies()) { // Always response false for Mac OS X, because there is no Eclipse plugin that manages the system proxies |
|
48 |
try { |
|
49 |
List<Proxy> proxies = ProxySelector.getDefault().select(new URI("http://textometrie.ens-lyon.fr")); |
|
50 |
if (proxies != null) { |
|
51 |
for (Proxy proxy : proxies) { |
|
52 |
if (proxy.type().equals(Proxy.Type.HTTP)) { |
|
53 |
InetSocketAddress addr = (InetSocketAddress) proxy.address(); |
|
54 |
if(addr != null) { |
|
55 |
proxy_host = addr.getHostName(); |
|
56 |
proxy_port = addr.getPort(); |
|
57 |
break; // ok done |
|
58 |
} |
|
59 |
} |
|
60 |
} |
|
61 |
} |
|
62 |
} catch(URISyntaxException e) { |
|
63 |
Log.printStackTrace(e); |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
// last resort to find out proxy configuration |
|
68 |
if (proxy_host == null || proxy_host.length() == 0) { |
|
69 |
proxy_host = System.getProperty("http.proxyHost"); |
|
70 |
String s = System.getProperty("http.proxyPort"); |
|
71 |
if (s != null) proxy_port = Integer.parseInt(s); |
|
72 |
proxy_password = System.getProperty("http.proxyPassword"); |
|
73 |
proxy_user = System.getProperty("http.proxyUser"); |
|
74 |
} |
|
75 |
|
|
76 |
|
|
77 |
// Log |
|
78 |
if(proxy_host != null && !proxy_host.isEmpty()) { |
|
79 |
Log.info("System proxy network configuration detected."); |
|
80 |
} |
|
81 |
|
|
82 |
// TODO: useless ? |
|
83 |
// if (proxyTracker == null) { |
|
84 |
// IProxyData[] data = proxyService.getProxyData(); |
|
85 |
// for (IProxyData d : data) { |
|
86 |
// proxy_host = d.getHost(); |
|
87 |
// if (IProxyData.HTTP_PROXY_TYPE.equals(d.getType()) && proxy_host != null && proxy_host.length() > 0) { |
|
88 |
// proxy_port = d.getPort(); |
|
89 |
// proxy_password = d.getPassword(); |
|
90 |
// proxy_user = d.getUserId(); |
|
91 |
// //System.out.println("proxy_server="+proxy_server+" proxy_port="+proxy_port); |
|
92 |
// |
|
93 |
// break; // ok done |
|
94 |
// } else { |
|
95 |
// proxy_host = null; |
|
96 |
// } |
|
97 |
// } |
|
98 |
// } |
|
99 |
} catch (Exception e) { |
|
100 |
Log.printStackTrace(e); |
|
101 |
} finally { |
|
102 |
if (proxyTracker != null) proxyTracker.close(); |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
/** |
|
107 |
* Checks if a system proxy has been detected. |
|
108 |
* @return |
|
109 |
*/ |
|
110 |
public boolean isSystemProxyDetected() { |
|
111 |
return proxy_host != null && !proxy_host.isEmpty(); |
|
112 |
} |
|
113 |
|
|
114 |
|
|
115 |
/** |
|
116 |
* Returns the HTTP system proxy configuration as URL if exists otherwise false. |
|
117 |
* @return |
|
118 |
*/ |
|
119 |
public String getHttpProxyUrl() { |
|
120 |
|
|
121 |
String proxyUrl = null; |
|
122 |
String userData = ""; |
|
123 |
|
|
124 |
if(this.isSystemProxyDetected()) { |
|
125 |
proxyUrl = proxy_host + ":" + proxy_port; |
|
126 |
|
|
127 |
if (proxy_user != null && !proxy_user.isEmpty()) { |
|
128 |
userData = proxy_user; |
|
129 |
proxyUrl = "@" + proxyUrl; //$NON-NLS-1$ |
|
130 |
} |
|
131 |
if (proxy_password != null && !proxy_password.isEmpty()) { |
|
132 |
userData += ":" + proxy_password; //$NON-NLS-1$ |
|
133 |
} |
|
134 |
} |
|
135 |
else { |
|
136 |
Log.warning("HTTP system proxy configuration is not set."); |
|
137 |
} |
|
138 |
|
|
139 |
if(proxyUrl != null) { |
|
140 |
proxyUrl = "http://" + userData + proxyUrl; //$NON-NLS-1$ |
|
141 |
} |
|
142 |
|
|
143 |
return proxyUrl; |
|
144 |
} |
|
145 |
|
|
146 |
public String toString() { |
|
147 |
return getHttpProxyUrl(); |
|
148 |
} |
|
149 |
} |
tmp/org.txm.utils/src/org/txm/utils/Activator.java (revision 296) | ||
---|---|---|
1 | 1 |
package org.txm.utils; |
2 | 2 |
|
3 |
import org.eclipse.ui.plugin.AbstractUIPlugin;
|
|
3 |
import org.eclipse.core.runtime.Plugin;
|
|
4 | 4 |
import org.osgi.framework.BundleContext; |
5 | 5 |
|
6 | 6 |
/** |
7 | 7 |
* The activator class controls the plug-in life cycle |
8 | 8 |
*/ |
9 |
public class Activator extends AbstractUIPlugin {
|
|
9 |
public class Activator extends Plugin { |
|
10 | 10 |
|
11 | 11 |
// The plug-in ID |
12 | 12 |
public static final String PLUGIN_ID = "org.txm.utils"; //$NON-NLS-1$ |
tmp/org.txm.utils/src/org/txm/utils/i18n/Localizer.java (revision 296) | ||
---|---|---|
32 | 32 |
import java.util.MissingResourceException; |
33 | 33 |
import java.util.ResourceBundle; |
34 | 34 |
|
35 |
import org.txm.core.messages.TXMCoreMessages;
|
|
35 |
import org.txm.utils.messages.UtilsCoreMessages;
|
|
36 | 36 |
|
37 | 37 |
// TODO: Auto-generated Javadoc |
38 | 38 |
/** |
... | ... | |
147 | 147 |
return getBundle().getString(key); |
148 | 148 |
} catch (MissingResourceException e1) { |
149 | 149 |
try { |
150 |
return TXMCoreMessages.Localizer_0 + getDefaultBundle().getString(key);
|
|
150 |
return UtilsCoreMessages.Localizer_0 + getDefaultBundle().getString(key);
|
|
151 | 151 |
} catch (MissingResourceException e2) { |
152 | 152 |
return key; |
153 | 153 |
} |
... | ... | |
163 | 163 |
if (bundle == null) { |
164 | 164 |
String s = getRessourcePrefix(); |
165 | 165 |
bundle = loc == null ? ResourceBundle.getBundle(s |
166 |
+ TXMCoreMessages.Localizer_1) : ResourceBundle.getBundle(s
|
|
167 |
+ TXMCoreMessages.Localizer_1, loc);
|
|
166 |
+ UtilsCoreMessages.Localizer_1) : ResourceBundle.getBundle(s
|
|
167 |
+ UtilsCoreMessages.Localizer_1, loc);
|
|
168 | 168 |
} |
169 | 169 |
return bundle; |
170 | 170 |
} |
... | ... | |
177 | 177 |
private ResourceBundle getDefaultBundle() { |
178 | 178 |
if (default_bundle == null) { |
179 | 179 |
String s = getRessourcePrefix(); |
180 |
default_bundle = ResourceBundle.getBundle(s + TXMCoreMessages.Localizer_1,
|
|
180 |
default_bundle = ResourceBundle.getBundle(s + UtilsCoreMessages.Localizer_1,
|
|
181 | 181 |
new Locale("en", "US")); //$NON-NLS-1$//$NON-NLS-2$ |
182 | 182 |
} |
183 | 183 |
return default_bundle; |
tmp/org.txm.utils/src/org/txm/utils/ExecTimer.java (revision 296) | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.util.Date; |
4 | 4 |
|
5 |
import org.txm.core.messages.TXMCoreMessages;
|
|
5 |
import org.txm.utils.messages.UtilsCoreMessages;
|
|
6 | 6 |
|
7 | 7 |
public class ExecTimer { |
8 | 8 |
static long startTime = 0, endTime = 0; |
... | ... | |
20 | 20 |
private static String setMessage(float time) { |
21 | 21 |
t = time; |
22 | 22 |
if (t < 1000) { |
23 |
message = ""+((int)t)+TXMCoreMessages.ExecTimer_0; //$NON-NLS-1$
|
|
23 |
message = ""+((int)t)+UtilsCoreMessages.ExecTimer_0; //$NON-NLS-1$
|
|
24 | 24 |
} |
25 | 25 |
else { |
26 | 26 |
t = t/1000; |
27 | 27 |
if (t < 3) { |
28 | 28 |
message = ""+t; //$NON-NLS-1$ |
29 |
message = ""+(message.substring(0, 3))+TXMCoreMessages.ExecTimer_1; //$NON-NLS-1$
|
|
29 |
message = ""+(message.substring(0, 3))+UtilsCoreMessages.ExecTimer_1; //$NON-NLS-1$
|
|
30 | 30 |
} else if (t < 60) |
31 |
message = ""+((int)t)+TXMCoreMessages.ExecTimer_1; //$NON-NLS-1$
|
|
31 |
message = ""+((int)t)+UtilsCoreMessages.ExecTimer_1; //$NON-NLS-1$
|
|
32 | 32 |
else if (t < 3600) |
33 |
message = ""+((int)t/60)+TXMCoreMessages.ExecTimer_2+((int)t%60)+TXMCoreMessages.ExecTimer_1; //$NON-NLS-1$
|
|
33 |
message = ""+((int)t/60)+UtilsCoreMessages.ExecTimer_2+((int)t%60)+UtilsCoreMessages.ExecTimer_1; //$NON-NLS-1$
|
|
34 | 34 |
else |
35 |
message = ""+((int)t/3600)+TXMCoreMessages.ExecTimer_3+((int)(t%3600)/60)+TXMCoreMessages.ExecTimer_2+((int)(t%3600)%60)+ TXMCoreMessages.ExecTimer_1; //$NON-NLS-1$
|
|
35 |
message = ""+((int)t/3600)+UtilsCoreMessages.ExecTimer_3+((int)(t%3600)/60)+UtilsCoreMessages.ExecTimer_2+((int)(t%3600)%60)+ UtilsCoreMessages.ExecTimer_1; //$NON-NLS-1$
|
|
36 | 36 |
} |
37 | 37 |
return message + " ("+(int)time+" ms)"; //$NON-NLS-1$ //$NON-NLS-2$ |
38 | 38 |
} |
tmp/org.txm.utils/src/org/txm/utils/io/IOUtils.java (revision 296) | ||
---|---|---|
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.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 |
} |
|
0 | 83 |
tmp/org.txm.utils/src/org/txm/utils/io/FileCopy.java (revision 296) | ||
---|---|---|
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: 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 |
|
|
38 |
import org.txm.utils.messages.UtilsCoreMessages; |
|
39 |
|
|
40 |
/** |
|
41 |
* File copy methods |
|
42 |
*/ |
|
43 |
public class FileCopy { |
|
44 |
|
|
45 |
private static final boolean isWindows = System.getProperty("os.name").contains("Windows"); |
|
46 |
|
|
47 |
/** |
|
48 |
* replace the file if it exists |
|
49 |
* |
|
50 |
* @param source |
|
51 |
* @param dest |
|
52 |
* @return |
|
53 |
* @throws IOException |
|
54 |
*/ |
|
55 |
public static boolean copy(File source, File dest) throws IOException { |
|
56 |
return copy(source, dest, true); |
|
57 |
} |
|
58 |
|
|
59 |
|
|
60 |
/** |
|
61 |
* copy one file with nio package to accelerate the copy |
|
62 |
* |
|
63 |
* !!! on windows : max size = 64mo !!!. |
|
64 |
* |
|
65 |
* @param source the source |
|
66 |
* @param dest the dest |
|
67 |
* @return true, if successful |
|
68 |
* @throws IOException Signals that an I/O exception has occurred. |
|
69 |
*/ |
|
70 |
public static boolean copy(File source, File dest, boolean replace) throws IOException { |
|
71 |
if (dest.exists() && !replace) return true; // don't replace file |
|
72 |
|
|
73 |
if (isWindows || source.length() >= Integer.MAX_VALUE) { |
|
74 |
//System.out.println("COPY BIG FILE"); |
|
75 |
FileInputStream instream = null; |
|
76 |
BufferedOutputStream outstream = null; |
|
77 |
try { |
|
78 |
//Open a stream of bytes to read the file bytes into the program |
|
79 |
instream = new FileInputStream(source); |
|
80 |
//A stream of bytes from the program to the destination |
|
81 |
outstream = new BufferedOutputStream(new FileOutputStream(dest)); |
|
82 |
byte[] buffer = new byte[4096]; |
|
83 |
int bytesRead; |
|
84 |
//Write the bytes from the inputstream to the outputstream |
|
85 |
while((bytesRead = instream.read(buffer))!=-1) { |
|
86 |
outstream.write(buffer, 0, bytesRead); |
|
87 |
} |
|
88 |
} catch (Exception e) {org.txm.utils.logger.Log.printStackTrace(e); return false; |
|
89 |
} finally { |
|
90 |
instream.close(); |
|
91 |
outstream.close(); |
|
92 |
instream = null; |
|
93 |
outstream = null; |
|
94 |
} |
|
95 |
} else { |
|
96 |
//System.out.println("COPY NORMAL FILE"); |
|
97 |
FileChannel in = null, out = null; |
|
98 |
FileInputStream fis = null; |
|
99 |
FileOutputStream fos = null; |
|
100 |
try { |
|
101 |
fis = new FileInputStream(source); |
|
102 |
fos = new FileOutputStream(dest); |
|
103 |
in = fis.getChannel(); |
|
104 |
out = fos.getChannel(); |
|
105 |
|
|
106 |
long size = in.size(); |
|
107 |
MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size); |
|
108 |
|
|
109 |
out.write(buf); |
|
110 |
} catch(Exception e) { |
|
111 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
112 |
|
|
113 |
if (fis != null) fis.close(); |
|
114 |
if (fos != null) fos.close(); |
|
115 |
if (in != null) in.close(); |
|
116 |
if (out != null) out.close(); |
|
117 |
|
|
118 |
return false; |
|
119 |
} finally { |
|
120 |
if (fis != null) fis.close(); |
|
121 |
if (fos != null) fos.close(); |
|
122 |
if (in != null) in.close(); |
|
123 |
if (out != null) out.close(); |
|
124 |
} |
|
125 |
} |
|
126 |
return true; |
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* Copy files. Don't erase the destination directory and replace files if they already exists |
|
131 |
* |
|
132 |
* @param src the src directory |
|
133 |
* @param dest the dest directory |
|
134 |
* @throws IOException Signals that an I/O exception has occurred. |
|
135 |
*/ |
|
136 |
public static void copyFiles(File src, File dest) throws IOException { |
|
137 |
|
|
138 |
// Check to ensure that the source is valid... |
|
139 |
if (!src.exists()) { |
|
140 |
throw new IOException(UtilsCoreMessages.FileCopy_0 + src.getAbsolutePath() |
|
141 |
+ "."); //$NON-NLS-1$ |
|
142 |
} else if (!src.canRead()) { // check to ensure we have rights to the |
|
143 |
// source... |
|
144 |
throw new IOException(UtilsCoreMessages.FileCopy_2 + src.getAbsolutePath() |
|
145 |
+ "."); //$NON-NLS-1$ |
|
146 |
} |
|
147 |
|
|
148 |
// is it a directory ? |
|
149 |
if (src.isDirectory()) { |
|
150 |
if (!dest.exists()) { // does the destination already exist? |
|
151 |
// if not create the file hierarchy |
|
152 |
if (!dest.mkdirs()) { |
|
153 |
throw new IOException(UtilsCoreMessages.FileCopy_4 |
|
154 |
+ dest.getAbsolutePath() + "."); //$NON-NLS-1$ |
|
155 |
} |
|
156 |
} |
|
157 |
|
|
158 |
// get a listing of files... |
|
159 |
String list[] = src.list(); |
|
160 |
// copy all the files in the list. |
|
161 |
for (int i = 0; i < list.length; i++) { |
|
162 |
File dest1 = new File(dest, list[i]); |
|
163 |
File src1 = new File(src, list[i]); |
|
164 |
copyFiles(src1, dest1); |
|
165 |
|
|
166 |
} |
|
167 |
} else { |
|
168 |
// This was not a directory, so lets just copy the file |
|
169 |
copy(src, dest); |
|
170 |
} |
|
171 |
} |
|
172 |
|
|
173 |
public static void main(String[] args) throws IOException { |
|
174 |
// File test1 = File.createTempFile("source", ".large"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
175 |
// File test2 = File.createTempFile("source", ".verylarge"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
176 |
// |
|
177 |
// System.out.println("write "+test1); //$NON-NLS-1$ |
|
178 |
// writeBigFile(test1, Integer.MAX_VALUE / 2, 1); |
|
179 |
// System.out.println("write "+test2); //$NON-NLS-1$ |
|
180 |
// writeBigFile(test2, Integer.MAX_VALUE / 2, 3); |
|
181 |
// |
|
182 |
// System.out.println("test1: "+test1+" : "+test1.length()); //$NON-NLS-1$ //$NON-NLS-2$ |
|
183 |
// System.out.println("test2: "+test2+" : "+test2.length()); //$NON-NLS-1$ //$NON-NLS-2$ |
|
184 |
// |
|
185 |
// File copy1 = File.createTempFile("copy", ".large"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
186 |
// File copy2 = File.createTempFile("copy", ".verylarge"); //$NON-NLS-1$ //$NON-NLS-2$ |
|
187 |
// |
|
188 |
// System.out.println("Copy1 "+copy1); //$NON-NLS-1$ |
|
189 |
// FileCopy.copy(test1, copy1); |
|
190 |
// System.out.println("Copy2 "+copy2); //$NON-NLS-1$ |
|
191 |
// FileCopy.copy(test2, copy2); |
|
192 |
// |
|
193 |
// System.out.println("copy1: "+copy1+" : "+copy1.length()); //$NON-NLS-1$ //$NON-NLS-2$ |
|
194 |
// System.out.println("copy2: "+copy2+" : "+copy2.length()); //$NON-NLS-1$ //$NON-NLS-2$ |
|
195 |
// |
|
196 |
// System.out.println("delete 1 "+test1.delete()); //$NON-NLS-1$ |
|
197 |
// System.out.println("delete 2 "+test2.delete()); //$NON-NLS-1$ |
|
198 |
// System.out.println("delete c1 "+copy1.delete()); //$NON-NLS-1$ |
|
199 |
// System.out.println("delete c2 "+copy2.delete()); //$NON-NLS-1$ |
|
200 |
|
|
201 |
File dir1 = new File("/home/mdecorde/TEMP/copy1"); |
|
202 |
File dir2 = new File("/home/mdecorde/TEMP/copy2"); |
|
203 |
FileCopy.copyFiles(dir1, dir2); |
|
204 |
} |
|
205 |
} |
|
206 |
|
|
0 | 207 |
tmp/org.txm.utils/src/org/txm/utils/Pair.java (revision 296) | ||
---|---|---|
29 | 29 |
|
30 | 30 |
import java.util.HashMap; |
31 | 31 |
|
32 |
import org.txm.core.messages.TXMCoreMessages;
|
|
32 |
import org.txm.utils.messages.UtilsCoreMessages;
|
|
33 | 33 |
|
34 | 34 |
// TODO: Auto-generated Javadoc |
35 | 35 |
/** |
... | ... | |
153 | 153 |
Pair<String, String> p4 = new Pair<String, String>(null, null); |
154 | 154 |
System.out.println(p1.equals(new Pair<Integer, Integer>(new Integer(1), |
155 | 155 |
new Integer(2))) |
156 |
+ TXMCoreMessages.Pair_0);
|
|
157 |
System.out.println(p4.equals(p2) + TXMCoreMessages.Pair_0);
|
|
158 |
System.out.println(p2.equals(p4) + TXMCoreMessages.Pair_0);
|
|
159 |
System.out.println(p1.equals(p3) + TXMCoreMessages.Pair_1);
|
|
160 |
System.out.println(p4.equals(p4) + TXMCoreMessages.Pair_1);
|
|
156 |
+ UtilsCoreMessages.Pair_0);
|
|
157 |
System.out.println(p4.equals(p2) + UtilsCoreMessages.Pair_0);
|
|
158 |
System.out.println(p2.equals(p4) + UtilsCoreMessages.Pair_0);
|
|
159 |
System.out.println(p1.equals(p3) + UtilsCoreMessages.Pair_1);
|
|
160 |
System.out.println(p4.equals(p4) + UtilsCoreMessages.Pair_1);
|
|
161 | 161 |
hashpair.containsKey(p1); |
162 | 162 |
hashpair.put(p1,"p1"); //$NON-NLS-1$ |
163 | 163 |
hashpair.put(p2,"p2"); //$NON-NLS-1$ |
tmp/org.txm.utils/src/org/txm/utils/Watcher.java (revision 296) | ||
---|---|---|
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:$ |
|
25 |
// $LastChangedRevision:$ |
|
26 |
// $LastChangedBy:$ |
|
27 |
// |
|
28 |
package org.txm.utils; |
|
29 |
|
|
30 |
// TODO: Auto-generated Javadoc |
|
31 |
/** |
|
32 |
* The Class Watcher. |
|
33 |
* |
|
34 |
* @author mdecorde |
|
35 |
* |
|
36 |
* Watch for an external tool (such as CWB or R) to be OK and try to restart it |
|
37 |
*/ |
|
38 |
public abstract class Watcher implements Runnable{ |
|
39 |
|
|
40 |
/** The process. */ |
|
41 |
boolean process = true; |
|
42 |
|
|
43 |
/** The processname. */ |
|
44 |
protected String processname="noname"; //$NON-NLS-1$ |
|
45 |
|
|
46 |
/** The restartcount. */ |
|
47 |
int restartcount = 0; |
|
48 |
|
|
49 |
/** The MAXRESTARTCOUNT. */ |
|
50 |
static int MAXRESTARTCOUNT = 10; |
|
51 |
|
|
52 |
/** The DELAY. */ |
|
53 |
static int DELAY = 10000; |
|
54 |
|
|
55 |
/** The DEBUG. */ |
|
56 |
static boolean DEBUG = true; |
|
57 |
|
|
58 |
|
|
59 |
/** |
|
60 |
* Sets the debug. |
|
61 |
* |
|
62 |
* @param state the new debug |
|
63 |
*/ |
|
64 |
public static void setDebug(boolean state) |
|
65 |
{ |
|
66 |
DEBUG = state; |
|
67 |
} |
|
68 |
|
|
69 |
/* (non-Javadoc) |
|
70 |
* @see java.lang.Runnable#run() |
|
71 |
*/ |
|
72 |
@Override |
|
73 |
public void run() { |
|
74 |
|
|
75 |
while (process) { |
|
76 |
try { |
|
77 |
Thread.sleep(DELAY); |
|
78 |
} catch (InterruptedException e) { |
|
79 |
// TODO Auto-generated catch block |
|
80 |
org.txm.utils.logger.Log.printStackTrace(e); |
|
81 |
return; |
|
82 |
} |
|
83 |
if (DEBUG) {System.out.println("check "+processname);} //$NON-NLS-1$ |
|
84 |
if (!check()) |
|
85 |
{ |
|
86 |
if (DEBUG){System.out.println("restart "+processname);} //$NON-NLS-1$ |
|
87 |
if (!restart()) |
|
88 |
{ |
|
89 |
System.out.println("Failed to restart : "+processname); //$NON-NLS-1$ |
|
90 |
return; |
|
91 |
} |
|
92 |
|
|
93 |
restartcount++; |
|
94 |
if (restartcount >= MAXRESTARTCOUNT) |
|
95 |
{ |
|
96 |
System.out.println(processname+" have been restarted "+MAXRESTARTCOUNT+" times, You should restart TXM."); //$NON-NLS-1$ //$NON-NLS-2$ |
|
97 |
return; |
|
98 |
} |
|
99 |
} |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
/** |
|
104 |
* Check. |
|
105 |
* |
|
106 |
* @return true, if successful |
|
107 |
*/ |
|
108 |
abstract protected boolean check(); |
|
109 |
|
|
110 |
/** |
|
111 |
* Restart. |
|
112 |
* |
|
113 |
* @return true, if successful |
|
114 |
*/ |
|
115 |
abstract protected boolean restart(); |
|
116 |
} |
|
0 | 117 |
tmp/org.txm.utils/src/org/txm/utils/logger/MessageBox.java (revision 296) | ||
---|---|---|
29 | 29 |
|
30 | 30 |
import java.util.logging.Level; |
31 | 31 |
|
32 |
import org.txm.core.messages.TXMCoreMessages;
|
|
32 |
import org.txm.utils.messages.UtilsCoreMessages;
|
|
33 | 33 |
|
34 | 34 |
// TODO: Auto-generated Javadoc |
35 | 35 |
/** |
... | ... | |
111 | 111 |
* @param message the message |
112 | 112 |
*/ |
113 | 113 |
public static void severe(String message) { |
114 |
severe(TXMCoreMessages.MessageBox_0, message);
|
|
114 |
severe(UtilsCoreMessages.MessageBox_0, message);
|
|
115 | 115 |
} |
116 | 116 |
|
117 | 117 |
/** |
... | ... | |
139 | 139 |
* @param message the message |
140 | 140 |
*/ |
141 | 141 |
public static void warning(String message) { |
142 |
warning(TXMCoreMessages.MessageBox_1, message);
|
|
142 |
warning(UtilsCoreMessages.MessageBox_1, message);
|
|
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
/** |
... | ... | |
148 | 148 |
* @param message the message |
149 | 149 |
*/ |
150 | 150 |
public static void info(String message) { |
151 |
info(TXMCoreMessages.MessageBox_2, message);
|
|
151 |
info(UtilsCoreMessages.MessageBox_2, message);
|
|
152 | 152 |
} |
153 | 153 |
|
154 | 154 |
/** |
tmp/org.txm.utils/src/org/txm/utils/logger/Log.java (revision 296) | ||
---|---|---|
38 | 38 |
import java.util.logging.Level; |
39 | 39 |
import java.util.logging.SimpleFormatter; |
40 | 40 |
|
41 |
import org.txm.core.messages.TXMCoreMessages;
|
|
41 |
import org.txm.utils.messages.UtilsCoreMessages;
|
|
42 | 42 |
|
43 | 43 |
// TODO: Auto-generated Javadoc |
44 | 44 |
/** |
... | ... | |
96 | 96 |
String filename = "/tmp/TXM-" + f.format(new Date()) + ".log"; //$NON-NLS-1$ //$NON-NLS-2$ |
97 | 97 |
|
98 | 98 |
fh = new FileHandler(filename, false); |
99 |
System.out.println(TXMCoreMessages.Log_3
|
|
99 |
System.out.println(UtilsCoreMessages.Log_3
|
|
100 | 100 |
+ new File(filename).getAbsolutePath()); |
101 | 101 |
fh.setFormatter(new SimpleFormatter()); |
102 | 102 |
fh.setEncoding("UTF-8"); //$NON-NLS-1$ |
... | ... | |
126 | 126 |
fh = new FileHandler( |
127 | 127 |
logfiledir.getAbsolutePath() |
128 | 128 |
+ "/TXM-" + f.format(new Date()) + ".log", false); //$NON-NLS-1$ //$NON-NLS-2$ |
129 |
System.out.println(TXMCoreMessages.Log_5
|
|
129 |
System.out.println(UtilsCoreMessages.Log_5
|
|
130 | 130 |
+ logfiledir.getAbsolutePath() |
131 | 131 |
+ "TXM-" + f.format(new Date()) + ".log"); //$NON-NLS-2$ //$NON-NLS-1$ |
132 | 132 |
fh.setFormatter(new SimpleFormatter()); |
... | ... | |
167 | 167 |
+ t.getLineNumber() + " -> " + e.toString()); //$NON-NLS-1$ |
168 | 168 |
|
169 | 169 |
if ("true".equals(log_stacktrace_boolean)) { |
170 |
message.append(TXMCoreMessages.Log_8);
|
|
170 |
message.append(UtilsCoreMessages.Log_8);
|
|
171 | 171 |
for (StackTraceElement se : e.getStackTrace()) |
172 | 172 |
message.append("\t" + se.getFileName() + "\t" + se.getMethodName() + "\tl." + se.getLineNumber() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ |
173 | 173 |
} |
tmp/org.txm.utils/src/org/txm/utils/network/SystemProxyDetector.java (revision 296) | ||
---|---|---|
1 |
package org.txm.utils.network; |
|
2 |
|
|
3 |
import java.net.InetSocketAddress; |
|
4 |
import java.net.Proxy; |
|
5 |
import java.net.ProxySelector; |
|
6 |
import java.net.URI; |
|
7 |
import java.net.URISyntaxException; |
|
8 |
import java.util.List; |
|
9 |
|
|
10 |
import org.eclipse.core.net.proxy.IProxyService; |
|
11 |
import org.osgi.framework.Bundle; |
|
12 |
import org.osgi.framework.BundleContext; |
|
13 |
import org.osgi.framework.FrameworkUtil; |
|
14 |
import org.osgi.util.tracker.ServiceTracker; |
|
15 |
import org.txm.utils.logger.Log; |
|
16 |
|
|
17 |
/** |
|
18 |
* This class implements various manner to retrieve the system proxy configuration. |
|
19 |
* It tries to get the configuration from the RCP store and from the Java system properties. |
|
20 |
* |
|
21 |
* @author mdecorde |
|
22 |
* @author sjacquot |
|
23 |
* |
|
24 |
*/ |
|
25 |
public class SystemProxyDetector { |
|
26 |
|
|
27 |
public String proxy_user = ""; |
|
28 |
public String proxy_password = ""; |
|
29 |
public String proxy_host = ""; |
|
30 |
public int proxy_port = 0; |
|
31 |
|
|
32 |
|
|
33 |
/** |
|
34 |
* Creates a system proxy detector and tries to retrieve the system proxy configuration from the RCP store and from the Java system properties. |
|
35 |
*/ |
|
36 |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
|
37 |
public SystemProxyDetector() { |
|
38 |
ServiceTracker proxyTracker = null; |
|
39 |
try { |
|
40 |
Bundle c = FrameworkUtil.getBundle(this.getClass()); |
|
41 |
if (c == null) return; // abord; |
|
42 |
BundleContext bc = c.getBundleContext(); |
|
43 |
if (bc == null) return; // abord; |
|
44 |
proxyTracker = new ServiceTracker(bc, IProxyService.class.getName(), null); |
|
45 |
proxyTracker.open(); |
|
46 |
|
|
47 |
IProxyService proxyService = (IProxyService)proxyTracker.getService(); |
|
48 |
if (proxyService.hasSystemProxies()) { // Always response false for Mac OS X, because there is no Eclipse plugin that manages the system proxies |
|
49 |
try { |
|
50 |
List<Proxy> proxies = ProxySelector.getDefault().select(new URI("http://textometrie.ens-lyon.fr")); |
|
51 |
if (proxies != null) { |
|
52 |
for (Proxy proxy : proxies) { |
|
53 |
if (proxy.type().equals(Proxy.Type.HTTP)) { |
|
54 |
InetSocketAddress addr = (InetSocketAddress) proxy.address(); |
|
55 |
if(addr != null) { |
|
56 |
proxy_host = addr.getHostName(); |
|
57 |
proxy_port = addr.getPort(); |
|
58 |
break; // ok done |
|
59 |
} |
|
60 |
} |
|
61 |
} |
|
62 |
} |
|
63 |
} catch(URISyntaxException e) { |
|
64 |
Log.printStackTrace(e); |
|
65 |
} |
|
66 |
} |
|
67 |
|
|
68 |
// last resort to find out proxy configuration |
|
69 |
if (proxy_host == null || proxy_host.length() == 0) { |
|
70 |
proxy_host = System.getProperty("http.proxyHost"); |
|
71 |
String s = System.getProperty("http.proxyPort"); |
|
72 |
if (s != null) proxy_port = Integer.parseInt(s); |
|
73 |
proxy_password = System.getProperty("http.proxyPassword"); |
|
74 |
proxy_user = System.getProperty("http.proxyUser"); |
|
75 |
} |
|
76 |
|
|
77 |
|
|
78 |
// Log |
|
79 |
if(proxy_host != null && !proxy_host.isEmpty()) { |
|
80 |
Log.info("System proxy network configuration detected."); |
|
81 |
} |
|
82 |
|
|
83 |
// TODO: useless ? |
|
84 |
// if (proxyTracker == null) { |
|
85 |
// IProxyData[] data = proxyService.getProxyData(); |
|
86 |
// for (IProxyData d : data) { |
|
87 |
// proxy_host = d.getHost(); |
|
88 |
// if (IProxyData.HTTP_PROXY_TYPE.equals(d.getType()) && proxy_host != null && proxy_host.length() > 0) { |
|
89 |
// proxy_port = d.getPort(); |
|
90 |
// proxy_password = d.getPassword(); |
|
91 |
// proxy_user = d.getUserId(); |
|
92 |
// //System.out.println("proxy_server="+proxy_server+" proxy_port="+proxy_port); |
|
93 |
// |
|
94 |
// break; // ok done |
|
95 |
// } else { |
|
96 |
// proxy_host = null; |
|
97 |
// } |
|
98 |
// } |
|
99 |
// } |
|
100 |
} catch (Exception e) { |
|
101 |
Log.printStackTrace(e); |
|
102 |
} finally { |
|
103 |
if (proxyTracker != null) proxyTracker.close(); |
|
104 |
} |
|
105 |
} |
|
106 |
|
|
107 |
/** |
|
108 |
* Checks if a system proxy has been detected. |
|
109 |
* @return |
|
110 |
*/ |
|
111 |
public boolean isSystemProxyDetected() { |
|
112 |
return proxy_host != null && !proxy_host.isEmpty(); |
|
113 |
} |
|
114 |
|
|
115 |
|
|
116 |
/** |
|
117 |
* Returns the HTTP system proxy configuration as URL if exists otherwise false. |
|
118 |
* @return |
|
119 |
*/ |
|
120 |
public String getHttpProxyUrl() { |
|
121 |
|
|
122 |
String proxyUrl = null; |
|
123 |
String userData = ""; |
|
124 |
|
|
125 |
if(this.isSystemProxyDetected()) { |
|
126 |
proxyUrl = proxy_host + ":" + proxy_port; |
|
127 |
|
|
128 |
if (proxy_user != null && !proxy_user.isEmpty()) { |
|
129 |
userData = proxy_user; |
|
130 |
proxyUrl = "@" + proxyUrl; //$NON-NLS-1$ |
|
131 |
} |
|
132 |
if (proxy_password != null && !proxy_password.isEmpty()) { |
|
133 |
userData += ":" + proxy_password; //$NON-NLS-1$ |
|
134 |
} |
|
135 |
} |
|
136 |
else { |
|
137 |
Log.warning("HTTP system proxy configuration is not set."); |
|
138 |
} |
|
139 |
|
|
140 |
if(proxyUrl != null) { |
|
141 |
proxyUrl = "http://" + userData + proxyUrl; //$NON-NLS-1$ |
|
142 |
} |
|
143 |
|
|
144 |
return proxyUrl; |
|
145 |
} |
|
146 |
|
|
147 |
public String toString() { |
|
148 |
return getHttpProxyUrl(); |
|
149 |
} |
|
150 |
} |
|
0 | 151 |
tmp/org.txm.utils/src/org/txm/utils/Sh.java (revision 296) | ||
---|---|---|
36 | 36 |
import java.util.Date; |
37 | 37 |
import java.util.Locale; |
38 | 38 |
|
39 |
import org.txm.core.messages.TXMCoreMessages; |
|
40 | 39 |
import org.txm.utils.logger.Log; |
40 |
import org.txm.utils.messages.UtilsCoreMessages; |
|
41 | 41 |
|
42 | 42 |
// TODO: Auto-generated Javadoc |
43 | 43 |
// Shell |
... | ... | |
124 | 124 |
org.txm.utils.logger.Log.printStackTrace(e1); |
125 | 125 |
} |
126 | 126 |
if (e != 0) { |
127 |
System.err.println(TXMCoreMessages.Sh_4
|
|
127 |
System.err.println(UtilsCoreMessages.Sh_4
|
|
128 | 128 |
+ e |
129 | 129 |
+ " at " //$NON-NLS-1$ |
130 | 130 |
+ DateFormat.getDateInstance(DateFormat.FULL, Locale.UK) |
... | ... | |
169 | 169 |
org.txm.utils.logger.Log.printStackTrace(e1); |
170 | 170 |
} |
171 | 171 |
if (e != 0) { |
172 |
Log.info(TXMCoreMessages.Sh_4
|
|
172 |
Log.info(UtilsCoreMessages.Sh_4
|
|
173 | 173 |
+ e |
174 | 174 |
+ " at " //$NON-NLS-1$ |
175 | 175 |
+ DateFormat.getDateInstance(DateFormat.FULL, Locale.UK) |
tmp/org.txm.utils/src/org/txm/utils/LangDetector.java (revision 296) | ||
---|---|---|
9 | 9 |
import javax.xml.parsers.ParserConfigurationException; |
10 | 10 |
|
11 | 11 |
import org.knallgrau.utils.textcat.TextCategorizer; |
12 |
import org.txm.utils.io.IOUtils; |
|
12 | 13 |
import org.txm.utils.xml.DomUtils; |
13 | 14 |
import org.w3c.dom.DOMException; |
14 | 15 |
import org.xml.sax.SAXException; |
tmp/org.txm.utils/src/org/txm/utils/LS.java (revision 296) | ||
---|---|---|
2 | 2 |
import java.io.File; |
3 | 3 |
import java.util.ArrayList; |
4 | 4 |
|
5 |
import org.txm.core.messages.TXMCoreMessages;
|
|
5 |
import org.txm.utils.messages.UtilsCoreMessages;
|
|
6 | 6 |
|
7 | 7 |
class LS { |
8 | 8 |
|
... | ... | |
32 | 32 |
} |
33 | 33 |
} |
34 | 34 |
} else { |
35 |
System.err.println(TXMCoreMessages.LS_0 + dir + TXMCoreMessages.LS_1);
|
|
35 |
System.err.println(UtilsCoreMessages.LS_0 + dir + UtilsCoreMessages.LS_1);
|
|
36 | 36 |
} |
37 | 37 |
return result; |
38 | 38 |
} |
tmp/org.txm.utils/src/org/txm/utils/messages/messages_fr.properties (revision 296) | ||
---|---|---|
1 |
|
|
2 |
ExecTimer_0 = \ msec |
|
3 |
ExecTimer_1 = \ sec |
|
4 |
ExecTimer_2 = \ min et |
|
5 |
ExecTimer_3 = h, |
|
6 |
|
|
7 |
FileCopy_0 = ** Copie de fichiers : échec d'accès à la source : |
|
8 |
FileCopy_2 = ** Copie de fichiers : droits d'accès insuffisants à la source : |
|
9 |
FileCopy_4 = ** Copie de fichiers : échec de création du dossier : |
|
10 |
|
|
11 |
LS_0 = Erreur : |
|
12 |
LS_1 = \ n'est pas un dossier |
|
13 |
|
|
14 |
Localizer_0 = [Traduction manquante] |
|
15 |
Localizer_1 = messages |
|
16 |
|
|
17 |
Log_3 = Copie des messages dans |
|
18 |
Log_5 = Ecriture des messages dans |
|
19 |
Log_8 = \nstack:\n |
|
20 |
|
|
21 |
MessageBox_0 = Erreur |
|
22 |
MessageBox_1 = Attention |
|
23 |
MessageBox_2 = Information |
|
24 |
|
|
25 |
Pair_0 = \ devrait valoir faux |
|
26 |
Pair_1 = \ devrait valoir vrai |
|
27 |
|
|
28 |
Sh_4 = Le processus a terminé avec le code d'erreur : |
|
0 | 29 |
tmp/org.txm.utils/src/org/txm/utils/messages/UtilsCoreMessages.java (revision 296) | ||
---|---|---|
1 |
package org.txm.utils.messages; |
|
2 |
|
|
3 |
import org.eclipse.osgi.util.NLS; |
|
4 |
|
|
5 |
/** |
|
6 |
* Utils core messages. |
|
7 |
* |
|
8 |
* @author mdecorde |
|
9 |
* @author sjacquot |
|
10 |
* |
|
11 |
*/ |
|
12 |
public class UtilsCoreMessages extends NLS { |
|
13 |
|
|
14 |
private static final String BUNDLE_NAME = "org.txm.utils.messages.messages"; //$NON-NLS-1$ |
|
15 |
|
|
16 |
public static String ExecTimer_0; |
|
17 |
public static String ExecTimer_1; |
|
18 |
public static String ExecTimer_2; |
|
19 |
public static String ExecTimer_3; |
|
20 |
public static String Localizer_0; |
|
21 |
public static String Localizer_1; |
|
22 |
public static String FileCopy_0; |
|
23 |
public static String FileCopy_2; |
|
24 |
public static String FileCopy_4; |
|
25 |
public static String LS_0; |
|
26 |
public static String LS_1; |
|
27 |
public static String Log_3; |
|
28 |
public static String Log_5; |
|
29 |
public static String Log_8; |
|
30 |
public static String MessageBox_0; |
|
31 |
public static String MessageBox_1; |
|
32 |
public static String MessageBox_2; |
|
33 |
public static String Pair_0; |
|
34 |
public static String Pair_1; |
|
35 |
public static String Sh_4; |
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
static { |
|
41 |
// initialize resource bundle |
|
42 |
Utf8NLS.initializeMessages(BUNDLE_NAME, UtilsCoreMessages.class); |
|
43 |
} |
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
} |
|
0 | 48 |
tmp/org.txm.utils/src/org/txm/utils/messages/messages.properties (revision 296) | ||
---|---|---|
1 |
|
|
2 |
ExecTimer_0 = msec |
|
3 |
ExecTimer_1 = sec |
|
4 |
ExecTimer_2 = min and |
|
5 |
ExecTimer_3 = h, |
|
6 |
|
|
7 |
FileCopy_0 = ** File copy: failed to find source: |
|
8 |
FileCopy_2 = ** File copy: access right restriction to source: |
|
9 |
FileCopy_4 = ** File copy: failed to create directory: |
|
10 |
|
|
11 |
LS_0 = Error: |
|
12 |
LS_1 = is not a Directory! |
|
13 |
|
|
14 |
Localizer_0 = [Missing translation] |
|
15 |
Localizer_1 = messages |
|
16 |
|
|
17 |
Log_3 = Copying logs in |
|
18 |
Log_5 = Start loging in |
|
19 |
Log_8 = \nstack:\n |
|
20 |
|
|
21 |
MessageBox_0 = Error |
|
22 |
MessageBox_1 = Warning |
|
23 |
MessageBox_2 = Information |
|
24 |
|
|
25 |
Pair_0 = \ should be false |
|
26 |
Pair_1 = \ should be true |
|
27 |
|
|
28 |
Sh_4 = Process exited abnormally with code |
|
0 | 29 |
tmp/org.txm.utils/src/org/txm/utils/messages/Utf8NLS.java (revision 296) | ||
---|---|---|
1 |
package org.txm.utils.messages; |
|
2 |
|
|
3 |
import java.lang.reflect.Field; |
|
4 |
|
|
5 |
import org.eclipse.osgi.util.NLS; |
|
6 |
|
|
7 |
|
|
8 |
/** |
|
9 |
* Class that manages UTF-8 encoding for .properties messages files. |
|
10 |
* @author sjacquot |
|
11 |
* |
|
12 |
*/ |
|
13 |
public class Utf8NLS extends NLS { |
|
14 |
|
|
15 |
|
|
16 |
/** |
|
17 |
* Initializes messages using UTF-8 encoding. |
|
18 |
* @param baseName |
|
19 |
* @param clazz |
|
20 |
*/ |
|
21 |
public static void initializeMessages(final String baseName, final Class<?> clazz) { |
|
22 |
// initialize resource bundle |
|
23 |
NLS.initializeMessages(baseName, clazz); |
|
24 |
|
|
25 |
// reencode the fields |
|
26 |
final Field[] fieldArray = clazz.getDeclaredFields(); |
|
27 |
final int len = fieldArray.length; |
Formats disponibles : Unified diff