root / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / utils / Utilities.java @ 3526
History | View | Annotate | Download (11.8 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.File; |
44 |
import java.io.FileNotFoundException; |
45 |
import java.io.FileReader; |
46 |
import java.io.IOException; |
47 |
import java.io.InputStream; |
48 |
import java.io.OutputStream; |
49 |
import java.net.URL; |
50 |
import java.rmi.NoSuchObjectException; |
51 |
import java.util.Hashtable; |
52 |
import java.util.StringTokenizer; |
53 |
import java.util.Vector; |
54 |
|
55 |
|
56 |
/**
|
57 |
* Clase con m?todos de utilidad en el protocolo WMS
|
58 |
*
|
59 |
* @authors Laura D?az, jaume dominguez faus
|
60 |
*/
|
61 |
public class Utilities { |
62 |
private static String characters; |
63 |
/**
|
64 |
* <b>key</b>: URL, <b>value</b>: path to the downloaded file.
|
65 |
*/
|
66 |
private static Hashtable downloadedFiles; |
67 |
|
68 |
static {
|
69 |
characters = "";
|
70 |
for (int j = 32; j<=127; j++){ |
71 |
characters += (char) j;
|
72 |
} |
73 |
characters += "?????????????????????????????????????????????????\n\r\f\t";
|
74 |
} |
75 |
|
76 |
|
77 |
/**
|
78 |
* Checks a File and tries to figure if the file is a text or a binary file.<br>
|
79 |
* Keep in mind that binary files are those that contains at least one
|
80 |
* non-printable character.
|
81 |
*
|
82 |
* @param file
|
83 |
* @return <b>true</b> when the file is <b>pretty problably</b> text,
|
84 |
* <b>false</b> if the file <b>is</b> binary.
|
85 |
*/
|
86 |
public static boolean isTextFile(File file){ |
87 |
return isTextFile(file, 1024); |
88 |
} |
89 |
|
90 |
/**
|
91 |
* Checks a File and tries to figure if the file is a text or a binary file.<br>
|
92 |
* Keep in mind that binary files are those that contains at least one
|
93 |
* non-printable character.
|
94 |
*
|
95 |
* @param file
|
96 |
* @param byteAmount, number of bytes to check.
|
97 |
* @return <b>true</b> when the file is <b>pretty problably</b> text,
|
98 |
* <b>false</b> if the file <b>is</b> binary.
|
99 |
*/
|
100 |
public static boolean isTextFile(File file, int byteAmount){ |
101 |
int umbral = byteAmount;
|
102 |
try {
|
103 |
FileReader fr = new FileReader(file); |
104 |
for (int i = 0; i < umbral; i++) { |
105 |
int c = fr.read();
|
106 |
if (c==-1){ |
107 |
// End of file. If we reach this
|
108 |
// everything before is printable data.
|
109 |
return true; |
110 |
} |
111 |
char ch = (char) c; |
112 |
if (characters.indexOf(ch)==-1){ |
113 |
// We've found a non-printable character.
|
114 |
// Then we'll assume that this file is binary.
|
115 |
return false; |
116 |
} |
117 |
} |
118 |
} catch (FileNotFoundException e) { |
119 |
e.printStackTrace(); |
120 |
} catch (IOException e) { |
121 |
e.printStackTrace(); |
122 |
} |
123 |
return true; |
124 |
} |
125 |
|
126 |
/**
|
127 |
* Checks a byte array and tells if it contains only text or contains
|
128 |
* any binary data.
|
129 |
*
|
130 |
* @param file
|
131 |
* @return <b>true</b> when the data is <b>only</b> text, <b>false</b> otherwise.
|
132 |
*/
|
133 |
public static boolean isTextData(byte[] data){ |
134 |
char[] charData = new char[data.length]; |
135 |
for (int i = 0; i<data.length; i++){ |
136 |
charData[i] = (char) data[i];
|
137 |
} |
138 |
|
139 |
for (int i = 0; i < data.length; i++) { |
140 |
int c = charData[i];
|
141 |
|
142 |
|
143 |
if (c==-1){ |
144 |
// End of file. If we reach this
|
145 |
// everything before is printable data.
|
146 |
return true; |
147 |
} |
148 |
char ch = (char) c; |
149 |
if (characters.indexOf(ch)==-1){ |
150 |
// We've found a non-printable character.
|
151 |
// Then we'll assume that this file is binary.
|
152 |
return false; |
153 |
} |
154 |
} |
155 |
return true; |
156 |
} |
157 |
|
158 |
/**
|
159 |
* Returns the content of this URL as a file from the file system.<br>
|
160 |
* <p>
|
161 |
* If the URL has been already downloaded in this session and notified
|
162 |
* to the system using the static <b>Utilities.addDownloadedURL(URL)</b>
|
163 |
* method, it can be restored faster from the file system avoiding to
|
164 |
* download it again.
|
165 |
* </p>
|
166 |
* @param url
|
167 |
* @return File containing this URL's content or null if no file was found.
|
168 |
*/
|
169 |
public static File getPreviousDownloadedURL(URL url){ |
170 |
File f = null; |
171 |
if (downloadedFiles!=null && downloadedFiles.containsKey(url)){ |
172 |
String filePath = (String) downloadedFiles.get(url); |
173 |
f = new File(filePath); |
174 |
} |
175 |
return f;
|
176 |
} |
177 |
|
178 |
/**
|
179 |
* Adds an URL to the table of downloaded files for further uses. If the URL
|
180 |
* already exists in the table its filePath value is updated to the new one and
|
181 |
* the old file itself is removed from the file system.
|
182 |
*
|
183 |
* @param url
|
184 |
* @param filePath
|
185 |
*/
|
186 |
public static void addDownloadedURL(URL url, String filePath){ |
187 |
if (downloadedFiles==null) |
188 |
downloadedFiles = new Hashtable(); |
189 |
String fileName = (String) downloadedFiles.put(url, filePath); |
190 |
if (fileName!=null){ |
191 |
File f = new File(fileName); |
192 |
if (f.exists())
|
193 |
f.delete(); |
194 |
} |
195 |
} |
196 |
|
197 |
/**
|
198 |
* Remove an URL to the table of downloaded files. Once removed, the system
|
199 |
* will not be able to restore the URL from the file system.
|
200 |
* @param url
|
201 |
* @return the value of this entry in the table of downloaded files just
|
202 |
* before it is removed.
|
203 |
*/
|
204 |
public static String removeDownloadedURL(URL url){ |
205 |
if (downloadedFiles!=null){ |
206 |
return (String) downloadedFiles.remove(url); |
207 |
} |
208 |
return null; |
209 |
} |
210 |
|
211 |
/**
|
212 |
* Copia el contenido de un InputStream en un OutputStream
|
213 |
*
|
214 |
* @param in InputStream
|
215 |
* @param out OutputStream
|
216 |
*/
|
217 |
public static void serializar(InputStream in, OutputStream out) { |
218 |
byte[] buffer = new byte[102400]; |
219 |
|
220 |
int n;
|
221 |
|
222 |
try {
|
223 |
while ((n = in.read(buffer)) != -1) { |
224 |
out.write(buffer, 0, n);
|
225 |
} |
226 |
} catch (IOException e) { |
227 |
e.printStackTrace(); |
228 |
} |
229 |
} |
230 |
|
231 |
|
232 |
|
233 |
/**
|
234 |
* Elimina del xml la declaraci?n del DTD
|
235 |
*
|
236 |
* @param bytes bytes del fichero XML de respuesta a getCapabilities
|
237 |
* @param startTag Tag raiz del xml respuesta a getCapabilities
|
238 |
*
|
239 |
* @return bytes del fichero XML sin la declaraci?n del DTD
|
240 |
*/
|
241 |
public static byte[] eliminarDTD(byte[] bytes, String startTag) { |
242 |
String text = new String(bytes); |
243 |
int index1 = text.indexOf("?>") + 2; |
244 |
int index2;
|
245 |
|
246 |
try {
|
247 |
index2 = findBeginIndex(bytes, startTag); |
248 |
} catch (NoSuchObjectException e) { |
249 |
return bytes;
|
250 |
} |
251 |
|
252 |
byte[] buffer = new byte[bytes.length - (index2 - index1)]; |
253 |
System.arraycopy(bytes, 0, buffer, 0, index1); |
254 |
System.arraycopy(bytes, index2, buffer, index1, bytes.length - index2);
|
255 |
|
256 |
return buffer;
|
257 |
} |
258 |
|
259 |
/**
|
260 |
* Obtiene el ?ndice del comienzo del xml
|
261 |
*
|
262 |
* @param bytes bytes del fichero XML en el que se busca
|
263 |
* @param tagRaiz Tag raiz del xml respuesta a getCapabilities
|
264 |
*
|
265 |
* @return ?ndice donde empieza el tag raiz
|
266 |
*
|
267 |
* @throws NoSuchObjectException Si no se encuentra el tag
|
268 |
*/
|
269 |
private static int findBeginIndex(byte[] bytes, String tagRaiz) |
270 |
throws NoSuchObjectException { |
271 |
try {
|
272 |
int nodo = 0; |
273 |
int ret = -1; |
274 |
|
275 |
int i = 0; |
276 |
|
277 |
while (true) { |
278 |
switch (nodo) {
|
279 |
case 0: |
280 |
|
281 |
if (bytes[i] == '<') { |
282 |
ret = i; |
283 |
nodo = 1;
|
284 |
} |
285 |
|
286 |
break;
|
287 |
|
288 |
case 1: |
289 |
|
290 |
if (bytes[i] == ' ') { |
291 |
} else if (bytes[i] == tagRaiz.charAt(0)) { |
292 |
nodo = 2;
|
293 |
} else {
|
294 |
nodo = 0;
|
295 |
} |
296 |
|
297 |
break;
|
298 |
|
299 |
case 2: |
300 |
|
301 |
String aux = new String(bytes, i, 18); |
302 |
|
303 |
if (aux.equalsIgnoreCase(tagRaiz.substring(1))) { |
304 |
return ret;
|
305 |
} |
306 |
|
307 |
nodo = 0;
|
308 |
|
309 |
break;
|
310 |
} |
311 |
|
312 |
i++; |
313 |
} |
314 |
} catch (Exception e) { |
315 |
throw new NoSuchObjectException("No se pudo parsear el xml"); |
316 |
} |
317 |
} |
318 |
|
319 |
/**
|
320 |
* Converts the contents of a Vector to a comma separated list
|
321 |
*
|
322 |
* */
|
323 |
public static String Vector2CS(Vector v) |
324 |
{ |
325 |
String str = new String(); |
326 |
int i;
|
327 |
for (i=0; i<v.size() ;i++) |
328 |
{ |
329 |
str = str + v.elementAt(i); |
330 |
if (i<v.size()-1) |
331 |
str = str + ",";
|
332 |
} |
333 |
return str;
|
334 |
} |
335 |
|
336 |
public static boolean isValidVersion(String version) |
337 |
{ |
338 |
if(version.trim().length() == 5) |
339 |
{ |
340 |
if ( (version.charAt(1)=='.') && (version.charAt(3)=='.')) |
341 |
{ |
342 |
char x = version.charAt(0); |
343 |
char y = version.charAt(2); |
344 |
char z = version.charAt(4); |
345 |
|
346 |
if ((Character.isDigit(x)) && (Character.isDigit(y)) && (Character.isDigit(z))) |
347 |
{ |
348 |
return true; |
349 |
} |
350 |
else
|
351 |
{ |
352 |
return false; |
353 |
} |
354 |
} |
355 |
else
|
356 |
{ |
357 |
return false; |
358 |
} |
359 |
} |
360 |
else
|
361 |
{ |
362 |
return false; |
363 |
} |
364 |
} |
365 |
|
366 |
/**
|
367 |
* Checks if a String is a number or not
|
368 |
*
|
369 |
* @param String, s
|
370 |
* @return boolean, true if s is a number
|
371 |
*/
|
372 |
public static boolean isNumber(String s) |
373 |
{ |
374 |
try
|
375 |
{ |
376 |
double d = Double.parseDouble(s); |
377 |
return true; |
378 |
} |
379 |
catch(NumberFormatException e) |
380 |
{ |
381 |
return false; |
382 |
} |
383 |
// int pointCount = 0;
|
384 |
// int commaCount = 0;
|
385 |
//
|
386 |
// char[] c = s.toCharArray();
|
387 |
// for (int i = c.length; --i >= 0; )
|
388 |
// {
|
389 |
// char cc = c[i];
|
390 |
//
|
391 |
// if (Character.isDigit(cc))
|
392 |
// {
|
393 |
// continue;
|
394 |
// }
|
395 |
// else
|
396 |
// {
|
397 |
// /* 'cc' is not a digit. */
|
398 |
//
|
399 |
// if (cc == '.')
|
400 |
// {
|
401 |
// pointCount++;
|
402 |
// if (pointCount > 1)
|
403 |
// {
|
404 |
// return false;
|
405 |
// }
|
406 |
// }
|
407 |
// else if (cc == ',')
|
408 |
// {
|
409 |
// commaCount++;
|
410 |
// if (commaCount > 1)
|
411 |
// {
|
412 |
// return false;
|
413 |
// }
|
414 |
// }
|
415 |
// else if (cc != '-')
|
416 |
// {
|
417 |
// return false;
|
418 |
// }
|
419 |
// }
|
420 |
// }
|
421 |
// return true;
|
422 |
} |
423 |
/**
|
424 |
* Parses the String containing different items [character] separated and
|
425 |
* creates a vector with them.
|
426 |
* @param str String contains item1[c]item2[c]item3...
|
427 |
* @param c is the string value for separating the items
|
428 |
* @return Vector containing all the items
|
429 |
*/
|
430 |
public static Vector createVector(String str, String c) |
431 |
{ |
432 |
StringTokenizer tokens = new StringTokenizer(str, c); |
433 |
Vector v = new Vector(); |
434 |
try
|
435 |
{ |
436 |
while (tokens.hasMoreTokens())
|
437 |
{ |
438 |
v.addElement(tokens.nextToken()); |
439 |
} |
440 |
return v;
|
441 |
} |
442 |
catch (Exception e) |
443 |
{ |
444 |
return new Vector(); |
445 |
} |
446 |
} |
447 |
|
448 |
} |