root / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / utils / Utilities.java @ 33983
History | View | Annotate | Download (12.1 KB)
1 |
package org.gvsig.remoteclient.utils; |
---|---|
2 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
3 |
*
|
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 |
|
43 |
import java.io.BufferedOutputStream; |
44 |
import java.io.DataOutputStream; |
45 |
import java.io.File; |
46 |
import java.io.FileNotFoundException; |
47 |
import java.io.FileOutputStream; |
48 |
import java.io.FileReader; |
49 |
import java.io.IOException; |
50 |
import java.io.InputStream; |
51 |
import java.io.OutputStream; |
52 |
import java.net.ConnectException; |
53 |
import java.net.URL; |
54 |
import java.net.UnknownHostException; |
55 |
import java.util.Hashtable; |
56 |
import java.util.StringTokenizer; |
57 |
import java.util.Vector; |
58 |
|
59 |
import org.gvsig.compat.CompatLocator; |
60 |
import org.gvsig.compat.net.Downloader; |
61 |
import org.gvsig.compat.net.ICancellable; |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
/**
|
67 |
* Clase con m?todos de utilidad en el protocolo WMS
|
68 |
*
|
69 |
* @authors Laura D?az, jaume dominguez faus
|
70 |
*/
|
71 |
public class Utilities { |
72 |
private static final Downloader downloader = CompatLocator.getDownloader(); |
73 |
private static String characters; |
74 |
static boolean canceled; |
75 |
static final long latency = 500; |
76 |
/**
|
77 |
* Used to cancel a group of files
|
78 |
* <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 |
static Hashtable canceledGroup = new Hashtable(); |
83 |
/**
|
84 |
* <b>key</b>: URL, <b>value</b>: path to the downloaded file.
|
85 |
*/
|
86 |
private static Hashtable downloadedFiles; |
87 |
static Exception downloadException; |
88 |
private static final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"/tmp-andami"; |
89 |
|
90 |
|
91 |
static {
|
92 |
characters = "";
|
93 |
for (int j = 32; j<=127; j++){ |
94 |
characters += (char) j;
|
95 |
} |
96 |
characters += "?????????????????????????????????????????????????\n\r\f\t??";
|
97 |
} |
98 |
|
99 |
|
100 |
/**
|
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 |
*
|
105 |
* @param file
|
106 |
* @return <b>true</b> when the file is <b>pretty problably</b> text,
|
107 |
* <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 |
|
113 |
/**
|
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 |
* non-printable character.
|
117 |
*
|
118 |
* @param file
|
119 |
* @param byteAmount, number of bytes to check.
|
120 |
* @return <b>true</b> when the file is <b>pretty problably</b> text,
|
121 |
* <b>false</b> if the file <b>is</b> binary.
|
122 |
*/
|
123 |
public static boolean isTextFile(File file, int byteAmount){ |
124 |
int umbral = byteAmount;
|
125 |
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 |
|
149 |
/**
|
150 |
* Checks a byte array and tells if it contains only text or contains
|
151 |
* any binary data.
|
152 |
*
|
153 |
* @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 |
|
163 |
for (int i = 0; i < data.length; i++) { |
164 |
int c = charData[i];
|
165 |
|
166 |
|
167 |
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 |
|
177 |
//System.out.println(ch+" at "+i);
|
178 |
return false; |
179 |
} |
180 |
} |
181 |
return true; |
182 |
} |
183 |
|
184 |
|
185 |
|
186 |
|
187 |
/**
|
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 |
|
196 |
int n;
|
197 |
|
198 |
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 |
|
207 |
/**
|
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 |
|
220 |
try {
|
221 |
index2 = findBeginIndex(bytes, startTag); |
222 |
} catch (Exception e) { |
223 |
return bytes;
|
224 |
} |
225 |
|
226 |
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 |
|
230 |
return buffer;
|
231 |
} |
232 |
|
233 |
/**
|
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 |
* @throws Exception Si no se encuentra el tag
|
242 |
*/
|
243 |
private static int findBeginIndex(byte[] bytes, String tagRaiz) |
244 |
throws Exception { |
245 |
try {
|
246 |
int nodo = 0; |
247 |
int ret = -1; |
248 |
|
249 |
int i = 0; |
250 |
|
251 |
while (true) { |
252 |
switch (nodo) {
|
253 |
case 0: |
254 |
|
255 |
if (bytes[i] == '<') { |
256 |
ret = i; |
257 |
nodo = 1;
|
258 |
} |
259 |
|
260 |
break;
|
261 |
|
262 |
case 1: |
263 |
|
264 |
if (bytes[i] == ' ') { |
265 |
} else if (bytes[i] == tagRaiz.charAt(0)) { |
266 |
nodo = 2;
|
267 |
} else {
|
268 |
nodo = 0;
|
269 |
} |
270 |
|
271 |
break;
|
272 |
|
273 |
case 2: |
274 |
|
275 |
String aux = new String(bytes, i, 18); |
276 |
|
277 |
if (aux.equalsIgnoreCase(tagRaiz.substring(1))) { |
278 |
return ret;
|
279 |
} |
280 |
|
281 |
nodo = 0;
|
282 |
|
283 |
break;
|
284 |
} |
285 |
|
286 |
i++; |
287 |
} |
288 |
} catch (Exception e) { |
289 |
throw new Exception("No se pudo parsear el xml", e); |
290 |
} |
291 |
} |
292 |
|
293 |
/**
|
294 |
* Converts the contents of a Vector to a comma separated list
|
295 |
*
|
296 |
* */
|
297 |
public static String Vector2CS(Vector v) |
298 |
{ |
299 |
String str = new String(); |
300 |
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 |
|
313 |
public static boolean isValidVersion(String version) |
314 |
{ |
315 |
if(version.trim().length() == 5) |
316 |
{ |
317 |
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 |
|
323 |
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 |
} |
337 |
else
|
338 |
{ |
339 |
return false; |
340 |
} |
341 |
} |
342 |
|
343 |
/**
|
344 |
* 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 |
|
357 |
/**
|
358 |
* Checks if a String is a number or not
|
359 |
*
|
360 |
* @param String, s
|
361 |
* @return boolean, true if s is a number
|
362 |
*/
|
363 |
public static boolean isNumber(String s) |
364 |
{ |
365 |
try
|
366 |
{ |
367 |
//double d = Double.parseDouble(s);
|
368 |
return true; |
369 |
} |
370 |
catch(NumberFormatException e) |
371 |
{ |
372 |
return false; |
373 |
} |
374 |
|
375 |
} |
376 |
|
377 |
/**
|
378 |
* 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 |
|
402 |
/**
|
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 |
|
417 |
/**
|
418 |
* 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 |
return downloader.downloadFile(url, name, cancel);
|
431 |
} |
432 |
|
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 |
name.substring(index, name.length()); |
438 |
} |
439 |
return tempDirectoryPath+"/"+name+System.currentTimeMillis(); |
440 |
} |
441 |
|
442 |
/**
|
443 |
* 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 |
return downloader.downloadFile(url, data, name, cancel);
|
460 |
} |
461 |
|
462 |
/**
|
463 |
* Cleans every temporal file previously downloaded.
|
464 |
*/
|
465 |
public static void cleanUpTempFiles() { |
466 |
downloader.cleanUpTempFiles(); |
467 |
} |
468 |
|
469 |
|
470 |
/**
|
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 |
public static void removeURL(URL url) { |
476 |
downloader.removeURL(url); |
477 |
} |
478 |
|
479 |
/**
|
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 |
downloader.removeURL(url); |
486 |
} |
487 |
|
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: Utilities.java 33983 2010-10-27 12:37:48Z nbrodin $
|
493 |
* @deprecated You should use always org.gvsig.compat.net.ICancellable
|
494 |
*/
|
495 |
/*private static class CancellableAdapter implements org.gvsig.compat.net.ICancellable {
|
496 |
private ICancellable cancellable = null;
|
497 |
|
498 |
public CancellableAdapter(
|
499 |
org.gvsig.remoteclient.wms.ICancellable cancellable) {
|
500 |
super();
|
501 |
this.cancellable = cancellable;
|
502 |
}
|
503 |
|
504 |
public Object getID() {
|
505 |
return cancellable.getID();
|
506 |
}
|
507 |
|
508 |
public boolean isCanceled() {
|
509 |
return cancellable.isCanceled();
|
510 |
}
|
511 |
}*/
|
512 |
} |