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