svn-gvsig-desktop / tags / v1_0_RELEASE / frameworks / _fwAndami / src / com / iver / andami / Utilities.java @ 9167
History | View | Annotate | Download (11.7 KB)
1 | 1104 | fjp | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | 598 | fernando | package com.iver.andami; |
42 | |||
43 | import java.awt.Component; |
||
44 | import java.awt.Container; |
||
45 | import java.io.BufferedInputStream; |
||
46 | import java.io.BufferedOutputStream; |
||
47 | 3706 | jaume | import java.io.DataOutputStream; |
48 | 598 | fernando | import java.io.File; |
49 | import java.io.FileOutputStream; |
||
50 | import java.io.IOException; |
||
51 | import java.io.InputStream; |
||
52 | import java.io.OutputStream; |
||
53 | 3706 | jaume | import java.net.ConnectException; |
54 | 598 | fernando | import java.net.MalformedURLException; |
55 | import java.net.URL; |
||
56 | 3706 | jaume | import java.net.UnknownHostException; |
57 | 598 | fernando | import java.util.Enumeration; |
58 | 3706 | jaume | import java.util.Hashtable; |
59 | 598 | fernando | import java.util.zip.ZipEntry; |
60 | import java.util.zip.ZipException; |
||
61 | import java.util.zip.ZipFile; |
||
62 | |||
63 | import javax.swing.ImageIcon; |
||
64 | import javax.swing.RootPaneContainer; |
||
65 | |||
66 | import org.apache.log4j.Logger; |
||
67 | |||
68 | 6877 | cesar | import com.iver.andami.ui.mdiManager.IWindow; |
69 | 8765 | jjdelcerro | import com.iver.andami.ui.splash.MultiSplashWindow; |
70 | 598 | fernando | |
71 | |||
72 | |||
73 | /**
|
||
74 | * Clase de utilidad
|
||
75 | *
|
||
76 | * @version $Revision$
|
||
77 | */
|
||
78 | public class Utilities { |
||
79 | 3706 | jaume | /**
|
80 | * <b>key</b>: URL, <b>value</b>: path to the downloaded file.
|
||
81 | */
|
||
82 | private static Hashtable downloadedFiles; |
||
83 | 598 | fernando | /** DOCUMENT ME! */
|
84 | private static Logger logger = Logger.getLogger(Utilities.class.getName()); |
||
85 | 3706 | jaume | private static final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"/tmp-andami"; |
86 | 598 | fernando | |
87 | 7513 | caballero | |
88 | 598 | fernando | /**
|
89 | * Crea un icono a partir del path de una imagen
|
||
90 | *
|
||
91 | * @param path Path de la imagen dentro del jar de la aplicaci?n
|
||
92 | *
|
||
93 | * @return ImageIcon si encuentra la imagen y null si no la encuentra
|
||
94 | */
|
||
95 | public static ImageIcon createImageIcon(String path) { |
||
96 | URL imgURL = null; |
||
97 | |||
98 | try {
|
||
99 | imgURL = new URL("file:" + path); |
||
100 | } catch (MalformedURLException e) { |
||
101 | e.printStackTrace(); |
||
102 | } |
||
103 | |||
104 | if (imgURL != null) { |
||
105 | return new ImageIcon(imgURL); |
||
106 | } else {
|
||
107 | return null; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | /**
|
||
112 | * M?todo que libera la memoria de los jInternalFrame
|
||
113 | *
|
||
114 | * @param baseComponent JInternalFrame cuya memoria se quiere eliminar
|
||
115 | */
|
||
116 | public static void cleanComponent(Component baseComponent) { |
||
117 | try {
|
||
118 | cleanComponent(baseComponent, 0);
|
||
119 | 7513 | caballero | } catch (Exception ignore) { // give some exception handling... |
120 | 598 | fernando | } |
121 | } |
||
122 | |||
123 | /* * The "depth" parameter was being used for text output debugging. * But isn't essential now. I'll keep it anyways, as it avoids * calling the garbage collector every recursion. */
|
||
124 | protected static void cleanComponent(Component baseComponent, int depth) { |
||
125 | 7513 | caballero | if (baseComponent == null) // recursion terminating clause |
126 | 598 | fernando | { |
127 | return;
|
||
128 | } |
||
129 | 7513 | caballero | |
130 | 6877 | cesar | if (baseComponent instanceof IWindow){ |
131 | 901 | fernando | return;
|
132 | } |
||
133 | 598 | fernando | |
134 | Container cont;
|
||
135 | Component[] childComponents; |
||
136 | int numChildren; // clean up component containers |
||
137 | |||
138 | 7513 | caballero | if (baseComponent instanceof Container) { // now clean up container instance variables |
139 | 598 | fernando | |
140 | 7513 | caballero | if (baseComponent instanceof RootPaneContainer) { // Swing specialised container |
141 | 598 | fernando | cont = (Container) baseComponent;
|
142 | numChildren = cont.getComponentCount(); |
||
143 | childComponents = cont.getComponents(); |
||
144 | |||
145 | 7513 | caballero | for (int i = 0; i < numChildren; i++) { // remove each component from the current container |
146 | 598 | fernando | |
147 | 7513 | caballero | // each child component may be a container itself
|
148 | 598 | fernando | cleanComponent(childComponents[i], depth + 1);
|
149 | ((RootPaneContainer) cont).getContentPane().remove(childComponents[i]);
|
||
150 | } |
||
151 | |||
152 | ((RootPaneContainer) cont).getContentPane().setLayout(null); |
||
153 | 7513 | caballero | } else { // General Swing, and AWT, Containers |
154 | 598 | fernando | cont = (Container) baseComponent;
|
155 | numChildren = cont.getComponentCount(); |
||
156 | childComponents = cont.getComponents(); |
||
157 | |||
158 | 7513 | caballero | for (int i = 0; i < numChildren; i++) //for(int i = 0;i < numChildren;i++) |
159 | 598 | fernando | { |
160 | 7513 | caballero | // remove each component from the current container // each child component may be a container itself
|
161 | 598 | fernando | cleanComponent(childComponents[i], depth + 1);
|
162 | cont.remove(childComponents[i]); |
||
163 | } |
||
164 | |||
165 | cont.setLayout(null);
|
||
166 | } |
||
167 | } |
||
168 | |||
169 | 7513 | caballero | // if component is also a container
|
170 | 598 | fernando | } |
171 | |||
172 | 7513 | caballero | |
173 | |||
174 | 598 | fernando | /**
|
175 | * Extrae un fichero zip en un directorio
|
||
176 | *
|
||
177 | * @param file fichero comprimido
|
||
178 | * @param dir Directorio donde se extraera el fichero
|
||
179 | *
|
||
180 | * @throws ZipException Si hay un error con el formato del fichero
|
||
181 | * @throws IOException Si se produce un error de entrada salida gen?rico
|
||
182 | */
|
||
183 | 8765 | jjdelcerro | public static void extractTo(File file, File dir, MultiSplashWindow splash) |
184 | throws ZipException, IOException { |
||
185 | ZipFile zip = new ZipFile(file); |
||
186 | Enumeration e = zip.entries();
|
||
187 | 598 | fernando | |
188 | 7513 | caballero | // Pasada para crear las carpetas
|
189 | while (e.hasMoreElements()) {
|
||
190 | ZipEntry entry = (ZipEntry) e.nextElement(); |
||
191 | 598 | fernando | |
192 | 7513 | caballero | if (entry.isDirectory()) {
|
193 | File directorio = new File(dir.getAbsolutePath() |
||
194 | + File.separator + entry.getName());
|
||
195 | 598 | fernando | |
196 | 7513 | caballero | directorio.mkdirs(); |
197 | } |
||
198 | 598 | fernando | |
199 | 8765 | jjdelcerro | } |
200 | 7513 | caballero | |
201 | // Pasada para crear los ficheros
|
||
202 | e = zip.entries(); |
||
203 | while (e.hasMoreElements()) {
|
||
204 | ZipEntry entry = (ZipEntry) e.nextElement(); |
||
205 | splash.process(30, "Procesando " + entry.getName() + "..."); |
||
206 | if (!entry.isDirectory()) {
|
||
207 | InputStream in = zip.getInputStream(entry);
|
||
208 | OutputStream out = new FileOutputStream(dir + File.separator |
||
209 | + entry.getName()); |
||
210 | BufferedInputStream bin = new BufferedInputStream(in); |
||
211 | BufferedOutputStream bout = new BufferedOutputStream(out); |
||
212 | |||
213 | int i;
|
||
214 | |||
215 | while ((i = bin.read()) != -1) { |
||
216 | bout.write(i); |
||
217 | } |
||
218 | |||
219 | bout.flush(); |
||
220 | bout.close(); |
||
221 | bin.close(); |
||
222 | |||
223 | } |
||
224 | |||
225 | } |
||
226 | |||
227 | zip.close(); |
||
228 | zip = null;
|
||
229 | System.gc();
|
||
230 | |||
231 | } |
||
232 | 3706 | jaume | /**
|
233 | * Returns the content of this URL as a file from the file system.<br>
|
||
234 | * <p>
|
||
235 | 7513 | caballero | * If the URL has been already downloaded in this session and notified
|
236 | 3706 | jaume | * to the system using the static <b>Utilities.addDownloadedURL(URL)</b>
|
237 | * method, it can be restored faster from the file system avoiding to
|
||
238 | * download it again.
|
||
239 | * </p>
|
||
240 | * @param url
|
||
241 | * @return File containing this URL's content or null if no file was found.
|
||
242 | */
|
||
243 | private static File getPreviousDownloadedURL(URL url){ |
||
244 | File f = null; |
||
245 | if (downloadedFiles!=null && downloadedFiles.containsKey(url)){ |
||
246 | String filePath = (String) downloadedFiles.get(url); |
||
247 | f = new File(filePath); |
||
248 | } |
||
249 | return f;
|
||
250 | } |
||
251 | 7513 | caballero | |
252 | 3706 | jaume | /**
|
253 | * Adds an URL to the table of downloaded files for further uses. If the URL
|
||
254 | * already exists in the table its filePath value is updated to the new one and
|
||
255 | * the old file itself is removed from the file system.
|
||
256 | 7513 | caballero | *
|
257 | 3706 | jaume | * @param url
|
258 | * @param filePath
|
||
259 | */
|
||
260 | private static void addDownloadedURL(URL url, String filePath){ |
||
261 | if (downloadedFiles==null) |
||
262 | downloadedFiles = new Hashtable(); |
||
263 | String fileName = (String) downloadedFiles.put(url, filePath); |
||
264 | 5276 | jmvivo | //JMV: No se puede eliminar el anterior porque puede que alguien lo
|
265 | // este usando
|
||
266 | /*
|
||
267 | 3706 | jaume | if (fileName!=null){
|
268 | File f = new File(fileName);
|
||
269 | if (f.exists())
|
||
270 | f.delete();
|
||
271 | }
|
||
272 | 5276 | jmvivo | */
|
273 | 3706 | jaume | } |
274 | 7513 | caballero | |
275 | 3706 | jaume | /**
|
276 | 7513 | caballero | * Downloads an URL into a temporary file that is removed the next time the
|
277 | 3706 | jaume | * tempFileManager class is called, which means the next time gvSIG is launched.
|
278 | 7513 | caballero | *
|
279 | 3706 | jaume | * @param url
|
280 | * @param name
|
||
281 | * @return
|
||
282 | * @throws IOException
|
||
283 | * @throws ServerErrorResponseException
|
||
284 | * @throws ConnectException
|
||
285 | * @throws UnknownHostException
|
||
286 | */
|
||
287 | public static File downloadFile(URL url, String name) throws IOException,ConnectException, UnknownHostException{ |
||
288 | File f = null; |
||
289 | |||
290 | try{
|
||
291 | if ((f=getPreviousDownloadedURL(url))==null){ |
||
292 | 8919 | cesar | createTempDirectory(); |
293 | 7513 | caballero | |
294 | 3706 | jaume | f = new File(tempDirectoryPath+"/"+name+System.currentTimeMillis()); |
295 | 7513 | caballero | |
296 | 3706 | jaume | System.out.println("downloading '"+url.toString()+"' to: "+f.getAbsolutePath()); |
297 | 7513 | caballero | |
298 | 3706 | jaume | f.deleteOnExit(); |
299 | DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f))); |
||
300 | byte[] buffer = new byte[1024*256]; |
||
301 | InputStream is = url.openStream();
|
||
302 | long readed = 0; |
||
303 | for (int i = is.read(buffer); i>0; i = is.read(buffer)){ |
||
304 | dos.write(buffer, 0, i);
|
||
305 | readed += i; |
||
306 | } |
||
307 | dos.close(); |
||
308 | addDownloadedURL(url, f.getAbsolutePath()); |
||
309 | } |
||
310 | } catch (IOException io) { |
||
311 | io.printStackTrace(); |
||
312 | } |
||
313 | 7513 | caballero | |
314 | 3706 | jaume | return f;
|
315 | } |
||
316 | |||
317 | /**
|
||
318 | * Cleans every temporal file previously downloaded.
|
||
319 | */
|
||
320 | public static void cleanUpTempFiles() { |
||
321 | try{
|
||
322 | File tempDirectory = new File(tempDirectoryPath); |
||
323 | 7513 | caballero | |
324 | 3706 | jaume | File[] files = tempDirectory.listFiles(); |
325 | if (files!=null) { |
||
326 | for (int i = 0; i < files.length; i++) { |
||
327 | // s?lo por si en un futuro se necesitan crear directorios temporales
|
||
328 | if (files[i].isDirectory()) deleteDirectory(files[i]);
|
||
329 | files[i].delete(); |
||
330 | } |
||
331 | } |
||
332 | tempDirectory.delete(); |
||
333 | } catch (Exception e) { } |
||
334 | 7513 | caballero | |
335 | 3706 | jaume | } |
336 | /**
|
||
337 | * Recursive directory delete.
|
||
338 | * @param f
|
||
339 | */
|
||
340 | private static void deleteDirectory(File f) { |
||
341 | File[] files = f.listFiles(); |
||
342 | for (int i = 0; i < files.length; i++) { |
||
343 | if (files[i].isDirectory()) deleteDirectory(files[i]);
|
||
344 | files[i].delete(); |
||
345 | } |
||
346 | 7513 | caballero | |
347 | 3706 | jaume | } |
348 | 7513 | caballero | |
349 | 4099 | fjp | /**
|
350 | * Crea un fichero temporal con un nombre concreto y unos datos pasados por
|
||
351 | * par?metro.
|
||
352 | * @param fileName Nombre de fichero
|
||
353 | * @param data datos a guardar en el fichero
|
||
354 | */
|
||
355 | public static void createTemp(String fileName, String data)throws IOException{ |
||
356 | File f = new File(fileName); |
||
357 | DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)) ); |
||
358 | dos.writeBytes(data); |
||
359 | dos.close(); |
||
360 | f.deleteOnExit(); |
||
361 | } |
||
362 | 7513 | caballero | |
363 | 4099 | fjp | /**
|
364 | * Remove an URL from the system cache. The file will remain in the file
|
||
365 | * system for further eventual uses.
|
||
366 | * @param request
|
||
367 | */
|
||
368 | public static void removeURL(URL url) { |
||
369 | 4428 | jaume | if (downloadedFiles != null && downloadedFiles.containsKey(url)) |
370 | 4424 | jaume | downloadedFiles.remove(url); |
371 | 4099 | fjp | } |
372 | 8919 | cesar | |
373 | /**
|
||
374 | * Esta funci?n crea el directorio para temporales y devuelve el nombre de este para que cualquier
|
||
375 | * extensi?n en gvSIG pueda utilizar este directorio para guardar ficheros que se desee eliminar
|
||
376 | * al salir
|
||
377 | * @return
|
||
378 | */
|
||
379 | public static String createTempDirectory(){ |
||
380 | File tempDirectory = new File(tempDirectoryPath); |
||
381 | if (!tempDirectory.exists())
|
||
382 | tempDirectory.mkdir(); |
||
383 | return tempDirectoryPath;
|
||
384 | } |
||
385 | 598 | fernando | } |