svn-gvsig-desktop / tags / v2_0_0_Build_2020 / libraries / libRemoteServices / src / org / gvsig / remoteclient / utils / Utilities.java @ 33910
History | View | Annotate | Download (12.1 KB)
1 | 29671 | jpiera | package org.gvsig.remoteclient.utils; |
---|---|---|---|
2 | 3323 | ldiaz | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
3 | 5180 | jaume | *
|
4 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
19 | *
|
||
20 | * For more information, contact:
|
||
21 | *
|
||
22 | * Generalitat Valenciana
|
||
23 | * Conselleria d'Infraestructures i Transport
|
||
24 | * Av. Blasco Ib??ez, 50
|
||
25 | * 46010 VALENCIA
|
||
26 | * SPAIN
|
||
27 | *
|
||
28 | * +34 963862235
|
||
29 | * gvsig@gva.es
|
||
30 | * www.gvsig.gva.es
|
||
31 | *
|
||
32 | * or
|
||
33 | *
|
||
34 | * IVER T.I. S.A
|
||
35 | * Salamanca 50
|
||
36 | * 46005 Valencia
|
||
37 | * Spain
|
||
38 | *
|
||
39 | * +34 963163400
|
||
40 | * dac@iver.es
|
||
41 | */
|
||
42 | 3323 | ldiaz | |
43 | 5180 | jaume | import java.io.BufferedOutputStream; |
44 | import java.io.DataOutputStream; |
||
45 | 3516 | jaume | import java.io.File; |
46 | import java.io.FileNotFoundException; |
||
47 | 5180 | jaume | import java.io.FileOutputStream; |
48 | 3516 | jaume | import java.io.FileReader; |
49 | 3323 | ldiaz | import java.io.IOException; |
50 | import java.io.InputStream; |
||
51 | import java.io.OutputStream; |
||
52 | 5409 | jaume | import java.net.ConnectException; |
53 | 5180 | jaume | import java.net.URL; |
54 | 5409 | jaume | import java.net.UnknownHostException; |
55 | 5180 | jaume | import java.util.Hashtable; |
56 | 3377 | ldiaz | import java.util.StringTokenizer; |
57 | 3345 | ldiaz | import java.util.Vector; |
58 | 3323 | ldiaz | |
59 | 33738 | jpiera | import org.gvsig.compat.CompatLocator; |
60 | import org.gvsig.compat.net.Downloader; |
||
61 | 29671 | jpiera | import org.gvsig.remoteclient.wms.ICancellable; |
62 | 3323 | ldiaz | |
63 | 5409 | jaume | |
64 | 15505 | jpiera | |
65 | |||
66 | 3323 | ldiaz | /**
|
67 | 5180 | jaume | * Clase con m?todos de utilidad en el protocolo WMS
|
68 | *
|
||
69 | * @authors Laura D?az, jaume dominguez faus
|
||
70 | */
|
||
71 | 3323 | ldiaz | public class Utilities { |
72 | 33738 | jpiera | private static final Downloader downloader = CompatLocator.getDownloader(); |
73 | private static String characters; |
||
74 | 5536 | jaume | static boolean canceled; |
75 | static final long latency = 500; |
||
76 | 5409 | jaume | /**
|
77 | 14548 | jmvivo | * Used to cancel a group of files
|
78 | 15505 | jpiera | * <b>key</b>: Group id, <b>value</b>: Boolean (true if
|
79 | * the group has to be canceled. Otherwise it is
|
||
80 | * false)
|
||
81 | */
|
||
82 | 14548 | jmvivo | static Hashtable canceledGroup = new Hashtable(); |
83 | /**
|
||
84 | 15505 | jpiera | * <b>key</b>: URL, <b>value</b>: path to the downloaded file.
|
85 | */
|
||
86 | private static Hashtable downloadedFiles; |
||
87 | 5536 | jaume | static Exception downloadException; |
88 | 15505 | jpiera | private static final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"/tmp-andami"; |
89 | 6042 | jaume | |
90 | |||
91 | 5180 | jaume | static {
|
92 | characters = "";
|
||
93 | for (int j = 32; j<=127; j++){ |
||
94 | characters += (char) j;
|
||
95 | } |
||
96 | characters += "?????????????????????????????????????????????????\n\r\f\t??";
|
||
97 | } |
||
98 | 6042 | jaume | |
99 | |||
100 | 5180 | jaume | /**
|
101 | * Checks a File and tries to figure if the file is a text or a binary file.<br>
|
||
102 | * Keep in mind that binary files are those that contains at least one
|
||
103 | * non-printable character.
|
||
104 | 6042 | jaume | *
|
105 | 5180 | jaume | * @param file
|
106 | 6042 | jaume | * @return <b>true</b> when the file is <b>pretty problably</b> text,
|
107 | 5180 | jaume | * <b>false</b> if the file <b>is</b> binary.
|
108 | */
|
||
109 | public static boolean isTextFile(File file){ |
||
110 | return isTextFile(file, 1024); |
||
111 | } |
||
112 | 6042 | jaume | |
113 | 5180 | jaume | /**
|
114 | * Checks a File and tries to figure if the file is a text or a binary file.<br>
|
||
115 | * Keep in mind that binary files are those that contains at least one
|
||
116 | 6042 | jaume | * non-printable character.
|
117 | *
|
||
118 | 5180 | jaume | * @param file
|
119 | * @param byteAmount, number of bytes to check.
|
||
120 | 6042 | jaume | * @return <b>true</b> when the file is <b>pretty problably</b> text,
|
121 | 5180 | jaume | * <b>false</b> if the file <b>is</b> binary.
|
122 | */
|
||
123 | public static boolean isTextFile(File file, int byteAmount){ |
||
124 | 6042 | jaume | int umbral = byteAmount;
|
125 | 5180 | jaume | try {
|
126 | FileReader fr = new FileReader(file); |
||
127 | for (int i = 0; i < umbral; i++) { |
||
128 | int c = fr.read();
|
||
129 | if (c==-1){ |
||
130 | // End of file. If we reach this
|
||
131 | // everything before is printable data.
|
||
132 | return true; |
||
133 | } |
||
134 | char ch = (char) c; |
||
135 | if (characters.indexOf(ch)==-1){ |
||
136 | // We've found a non-printable character.
|
||
137 | // Then we'll assume that this file is binary.
|
||
138 | return false; |
||
139 | } |
||
140 | } |
||
141 | } catch (FileNotFoundException e) { |
||
142 | e.printStackTrace(); |
||
143 | } catch (IOException e) { |
||
144 | e.printStackTrace(); |
||
145 | } |
||
146 | return true; |
||
147 | } |
||
148 | 6042 | jaume | |
149 | 5180 | jaume | /**
|
150 | * Checks a byte array and tells if it contains only text or contains
|
||
151 | * any binary data.
|
||
152 | 6042 | jaume | *
|
153 | 5180 | jaume | * @param file
|
154 | * @return <b>true</b> when the data is <b>only</b> text, <b>false</b> otherwise.
|
||
155 | * @deprecated
|
||
156 | */
|
||
157 | public static boolean isTextData(byte[] data){ |
||
158 | char[] charData = new char[data.length]; |
||
159 | for (int i = 0; i<data.length; i++){ |
||
160 | charData[i] = (char) data[i];
|
||
161 | } |
||
162 | 6042 | jaume | |
163 | 5180 | jaume | for (int i = 0; i < data.length; i++) { |
164 | int c = charData[i];
|
||
165 | 6042 | jaume | |
166 | |||
167 | 5180 | jaume | if (c==-1){ |
168 | // End of file. If we reach this
|
||
169 | // everything before is printable data.
|
||
170 | return true; |
||
171 | } |
||
172 | char ch = (char) c; |
||
173 | if (characters.indexOf(ch)==-1){ |
||
174 | // We've found a non-printable character.
|
||
175 | // Then we'll assume that this file is binary.
|
||
176 | 6042 | jaume | |
177 | 5180 | jaume | //System.out.println(ch+" at "+i);
|
178 | return false; |
||
179 | } |
||
180 | } |
||
181 | return true; |
||
182 | } |
||
183 | 6042 | jaume | |
184 | |||
185 | |||
186 | |||
187 | 5180 | jaume | /**
|
188 | * Copia el contenido de un InputStream en un OutputStream
|
||
189 | *
|
||
190 | * @param in InputStream
|
||
191 | * @param out OutputStream
|
||
192 | */
|
||
193 | public static void serializar(InputStream in, OutputStream out) { |
||
194 | byte[] buffer = new byte[102400]; |
||
195 | 6042 | jaume | |
196 | 5180 | jaume | int n;
|
197 | 6042 | jaume | |
198 | 5180 | jaume | try {
|
199 | while ((n = in.read(buffer)) != -1) { |
||
200 | out.write(buffer, 0, n);
|
||
201 | } |
||
202 | } catch (IOException e) { |
||
203 | e.printStackTrace(); |
||
204 | } |
||
205 | } |
||
206 | 6042 | jaume | |
207 | 5180 | jaume | /**
|
208 | * Elimina del xml la declaraci?n del DTD
|
||
209 | *
|
||
210 | * @param bytes bytes del fichero XML de respuesta a getCapabilities
|
||
211 | * @param startTag Tag raiz del xml respuesta a getCapabilities
|
||
212 | *
|
||
213 | * @return bytes del fichero XML sin la declaraci?n del DTD
|
||
214 | */
|
||
215 | public static byte[] eliminarDTD(byte[] bytes, String startTag) { |
||
216 | String text = new String(bytes); |
||
217 | int index1 = text.indexOf("?>") + 2; |
||
218 | int index2;
|
||
219 | 6042 | jaume | |
220 | 5180 | jaume | try {
|
221 | index2 = findBeginIndex(bytes, startTag); |
||
222 | 33738 | jpiera | } catch (Exception e) { |
223 | 5180 | jaume | return bytes;
|
224 | } |
||
225 | 6042 | jaume | |
226 | 5180 | jaume | byte[] buffer = new byte[bytes.length - (index2 - index1)]; |
227 | System.arraycopy(bytes, 0, buffer, 0, index1); |
||
228 | System.arraycopy(bytes, index2, buffer, index1, bytes.length - index2);
|
||
229 | 6042 | jaume | |
230 | 5180 | jaume | return buffer;
|
231 | } |
||
232 | 6042 | jaume | |
233 | 5180 | jaume | /**
|
234 | * Obtiene el ?ndice del comienzo del xml
|
||
235 | *
|
||
236 | * @param bytes bytes del fichero XML en el que se busca
|
||
237 | * @param tagRaiz Tag raiz del xml respuesta a getCapabilities
|
||
238 | *
|
||
239 | * @return ?ndice donde empieza el tag raiz
|
||
240 | *
|
||
241 | 33738 | jpiera | * @throws Exception Si no se encuentra el tag
|
242 | 5180 | jaume | */
|
243 | private static int findBeginIndex(byte[] bytes, String tagRaiz) |
||
244 | 33738 | jpiera | throws Exception { |
245 | 5180 | jaume | try {
|
246 | int nodo = 0; |
||
247 | int ret = -1; |
||
248 | 6042 | jaume | |
249 | 5180 | jaume | int i = 0; |
250 | 6042 | jaume | |
251 | 5180 | jaume | while (true) { |
252 | switch (nodo) {
|
||
253 | case 0: |
||
254 | 6042 | jaume | |
255 | 5180 | jaume | if (bytes[i] == '<') { |
256 | ret = i; |
||
257 | nodo = 1;
|
||
258 | } |
||
259 | 6042 | jaume | |
260 | 5180 | jaume | break;
|
261 | 6042 | jaume | |
262 | 5180 | jaume | case 1: |
263 | 6042 | jaume | |
264 | 5180 | jaume | if (bytes[i] == ' ') { |
265 | } else if (bytes[i] == tagRaiz.charAt(0)) { |
||
266 | nodo = 2;
|
||
267 | } else {
|
||
268 | nodo = 0;
|
||
269 | } |
||
270 | 6042 | jaume | |
271 | 5180 | jaume | break;
|
272 | 6042 | jaume | |
273 | 5180 | jaume | case 2: |
274 | 6042 | jaume | |
275 | 5180 | jaume | String aux = new String(bytes, i, 18); |
276 | 6042 | jaume | |
277 | 5180 | jaume | if (aux.equalsIgnoreCase(tagRaiz.substring(1))) { |
278 | return ret;
|
||
279 | } |
||
280 | 6042 | jaume | |
281 | 5180 | jaume | nodo = 0;
|
282 | 6042 | jaume | |
283 | 5180 | jaume | break;
|
284 | } |
||
285 | 6042 | jaume | |
286 | 5180 | jaume | i++; |
287 | } |
||
288 | } catch (Exception e) { |
||
289 | 33738 | jpiera | throw new Exception("No se pudo parsear el xml", e); |
290 | 5180 | jaume | } |
291 | } |
||
292 | 6042 | jaume | |
293 | 5180 | jaume | /**
|
294 | * Converts the contents of a Vector to a comma separated list
|
||
295 | 6042 | jaume | *
|
296 | 5180 | jaume | * */
|
297 | public static String Vector2CS(Vector v) |
||
298 | { |
||
299 | 6042 | jaume | String str = new String(); |
300 | 5180 | jaume | if (v != null) |
301 | { |
||
302 | int i;
|
||
303 | for (i=0; i<v.size() ;i++) |
||
304 | { |
||
305 | str = str + v.elementAt(i); |
||
306 | if (i<v.size()-1) |
||
307 | str = str + ",";
|
||
308 | } |
||
309 | } |
||
310 | return str;
|
||
311 | } |
||
312 | 6042 | jaume | |
313 | 3345 | ldiaz | public static boolean isValidVersion(String version) |
314 | { |
||
315 | 5180 | jaume | if(version.trim().length() == 5) |
316 | 3345 | ldiaz | { |
317 | 5180 | jaume | if ( (version.charAt(1)=='.') && (version.charAt(3)=='.')) |
318 | { |
||
319 | char x = version.charAt(0); |
||
320 | char y = version.charAt(2); |
||
321 | char z = version.charAt(4); |
||
322 | 6042 | jaume | |
323 | 5180 | jaume | if ((Character.isDigit(x)) && (Character.isDigit(y)) && (Character.isDigit(z))) |
324 | { |
||
325 | return true; |
||
326 | } |
||
327 | else
|
||
328 | { |
||
329 | return false; |
||
330 | } |
||
331 | } |
||
332 | else
|
||
333 | { |
||
334 | return false; |
||
335 | } |
||
336 | 3345 | ldiaz | } |
337 | else
|
||
338 | { |
||
339 | 5180 | jaume | return false; |
340 | 3345 | ldiaz | } |
341 | } |
||
342 | 6042 | jaume | |
343 | 3377 | ldiaz | /**
|
344 | 5180 | jaume | * Crea un fichero temporal con un nombre concreto y unos datos pasados por
|
345 | * par?metro.
|
||
346 | * @param fileName Nombre de fichero
|
||
347 | * @param data datos a guardar en el fichero
|
||
348 | */
|
||
349 | public static void createTemp(String fileName, String data)throws IOException{ |
||
350 | File f = new File(fileName); |
||
351 | DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)) ); |
||
352 | dos.writeBytes(data); |
||
353 | dos.close(); |
||
354 | f.deleteOnExit(); |
||
355 | } |
||
356 | 6042 | jaume | |
357 | 5180 | jaume | /**
|
358 | * Checks if a String is a number or not
|
||
359 | 6042 | jaume | *
|
360 | 5180 | jaume | * @param String, s
|
361 | * @return boolean, true if s is a number
|
||
362 | */
|
||
363 | 6042 | jaume | public static boolean isNumber(String s) |
364 | 5180 | jaume | { |
365 | try
|
||
366 | { |
||
367 | //double d = Double.parseDouble(s);
|
||
368 | return true; |
||
369 | } |
||
370 | catch(NumberFormatException e) |
||
371 | { |
||
372 | return false; |
||
373 | } |
||
374 | 6042 | jaume | |
375 | } |
||
376 | |||
377 | 5180 | jaume | /**
|
378 | 3377 | ldiaz | * Parses the String containing different items [character] separated and
|
379 | * creates a vector with them.
|
||
380 | * @param str String contains item1[c]item2[c]item3...
|
||
381 | * @param c is the string value for separating the items
|
||
382 | * @return Vector containing all the items
|
||
383 | */
|
||
384 | public static Vector createVector(String str, String c) |
||
385 | { |
||
386 | StringTokenizer tokens = new StringTokenizer(str, c); |
||
387 | Vector v = new Vector(); |
||
388 | try
|
||
389 | { |
||
390 | while (tokens.hasMoreTokens())
|
||
391 | { |
||
392 | v.addElement(tokens.nextToken()); |
||
393 | } |
||
394 | return v;
|
||
395 | } |
||
396 | catch (Exception e) |
||
397 | { |
||
398 | return new Vector(); |
||
399 | } |
||
400 | } |
||
401 | 6042 | jaume | |
402 | 5180 | jaume | /**
|
403 | * @param dimensions
|
||
404 | * @return
|
||
405 | */
|
||
406 | public static String Vector2URLParamString(Vector v) { |
||
407 | if (v==null) return ""; |
||
408 | String s = ""; |
||
409 | for (int i = 0; i < v.size(); i++) { |
||
410 | s += v.get(i); |
||
411 | if (i<v.size()-1) |
||
412 | s += "&";
|
||
413 | } |
||
414 | return s;
|
||
415 | } |
||
416 | 33738 | jpiera | |
417 | 5409 | jaume | /**
|
418 | 15505 | jpiera | * Downloads an URL into a temporary file that is removed the next time the
|
419 | * tempFileManager class is called, which means the next time gvSIG is launched.
|
||
420 | *
|
||
421 | * @param url
|
||
422 | * @param name
|
||
423 | * @return
|
||
424 | * @throws IOException
|
||
425 | * @throws ServerErrorResponseException
|
||
426 | * @throws ConnectException
|
||
427 | * @throws UnknownHostException
|
||
428 | */
|
||
429 | public static synchronized File downloadFile(URL url, String name, ICancellable cancel) throws IOException,ConnectException, UnknownHostException{ |
||
430 | 33738 | jpiera | return downloader.downloadFile(url, name, new CancellableAdapter(cancel)); |
431 | 5409 | jaume | } |
432 | 29671 | jpiera | |
433 | private static String calculateFileName(String name){ |
||
434 | int index = name.lastIndexOf("."); |
||
435 | if (index > 0){ |
||
436 | return tempDirectoryPath + "/" + name.substring(0,index) + System.currentTimeMillis() + |
||
437 | 31833 | jpiera | name.substring(index, name.length()); |
438 | 29671 | jpiera | } |
439 | return tempDirectoryPath+"/"+name+System.currentTimeMillis(); |
||
440 | } |
||
441 | 5409 | jaume | |
442 | 15505 | jpiera | /**
|
443 | 18333 | jpiera | * Downloads a URL using the HTTP Post protocol
|
444 | * @param url
|
||
445 | * The server URL
|
||
446 | * @param data
|
||
447 | * The data to send in the request
|
||
448 | * @param name
|
||
449 | * A common name for all the retrieved files
|
||
450 | * @param cancel
|
||
451 | * Used to cancel the downloads
|
||
452 | * @return
|
||
453 | * The retrieved file
|
||
454 | * @throws IOException
|
||
455 | * @throws ConnectException
|
||
456 | * @throws UnknownHostException
|
||
457 | */
|
||
458 | public static synchronized File downloadFile(URL url, String data, String name, ICancellable cancel) throws IOException,ConnectException, UnknownHostException{ |
||
459 | 33738 | jpiera | return downloader.downloadFile(url, data, name, new CancellableAdapter(cancel)); |
460 | 18333 | jpiera | } |
461 | |||
462 | /**
|
||
463 | 15505 | jpiera | * Cleans every temporal file previously downloaded.
|
464 | */
|
||
465 | public static void cleanUpTempFiles() { |
||
466 | 33738 | jpiera | downloader.cleanUpTempFiles(); |
467 | 15505 | jpiera | } |
468 | 6042 | jaume | |
469 | |||
470 | 15505 | jpiera | /**
|
471 | * Remove an URL from the system cache. The file will remain in the file
|
||
472 | * system for further eventual uses.
|
||
473 | * @param request
|
||
474 | */
|
||
475 | 5409 | jaume | public static void removeURL(URL url) { |
476 | 33738 | jpiera | downloader.removeURL(url); |
477 | 5409 | jaume | } |
478 | 5536 | jaume | |
479 | 18333 | jpiera | /**
|
480 | * Remove an URL from the system cache. The file will remain in the file
|
||
481 | * system for further eventual uses.
|
||
482 | * @param request
|
||
483 | */
|
||
484 | public static void removeURL(Object url) { |
||
485 | 33738 | jpiera | downloader.removeURL(url); |
486 | 18333 | jpiera | } |
487 | 33738 | jpiera | |
488 | /**
|
||
489 | * This class has to be deleted when all the classes uses the ICancellable
|
||
490 | * method from libCompat
|
||
491 | * @author gvSIG Team
|
||
492 | * @version $Id$
|
||
493 | *
|
||
494 | */
|
||
495 | private static class CancellableAdapter implements org.gvsig.compat.net.ICancellable{ |
||
496 | private ICancellable cancellable = null; |
||
497 | 5536 | jaume | |
498 | 33738 | jpiera | public CancellableAdapter(
|
499 | org.gvsig.remoteclient.wms.ICancellable cancellable) { |
||
500 | super();
|
||
501 | this.cancellable = cancellable;
|
||
502 | } |
||
503 | 6421 | jaume | |
504 | 33738 | jpiera | public Object getID() { |
505 | return cancellable.getID();
|
||
506 | } |
||
507 | 6421 | jaume | |
508 | 33738 | jpiera | public boolean isCanceled() { |
509 | return cancellable.isCanceled();
|
||
510 | } |
||
511 | 5536 | jaume | } |
512 | } |