svn-gvsig-desktop / tags / v10_RC2c / frameworks / _fwAndami / src / com / iver / andami / Utilities.java @ 8745
History | View | Annotate | Download (11.4 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 | import com.iver.andami.ui.SplashWindow; |
||
69 | 6877 | cesar | import com.iver.andami.ui.mdiManager.IWindow; |
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 | public static void extractTo(File file, File dir, SplashWindow splash) |
||
184 | 7513 | caballero | 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 | 7513 | caballero | } |
200 | |||
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 | |||
233 | 3706 | jaume | /**
|
234 | * Returns the content of this URL as a file from the file system.<br>
|
||
235 | * <p>
|
||
236 | 7513 | caballero | * If the URL has been already downloaded in this session and notified
|
237 | 3706 | jaume | * to the system using the static <b>Utilities.addDownloadedURL(URL)</b>
|
238 | * method, it can be restored faster from the file system avoiding to
|
||
239 | * download it again.
|
||
240 | * </p>
|
||
241 | * @param url
|
||
242 | * @return File containing this URL's content or null if no file was found.
|
||
243 | */
|
||
244 | private static File getPreviousDownloadedURL(URL url){ |
||
245 | File f = null; |
||
246 | if (downloadedFiles!=null && downloadedFiles.containsKey(url)){ |
||
247 | String filePath = (String) downloadedFiles.get(url); |
||
248 | f = new File(filePath); |
||
249 | } |
||
250 | return f;
|
||
251 | } |
||
252 | 7513 | caballero | |
253 | 3706 | jaume | /**
|
254 | * Adds an URL to the table of downloaded files for further uses. If the URL
|
||
255 | * already exists in the table its filePath value is updated to the new one and
|
||
256 | * the old file itself is removed from the file system.
|
||
257 | 7513 | caballero | *
|
258 | 3706 | jaume | * @param url
|
259 | * @param filePath
|
||
260 | */
|
||
261 | private static void addDownloadedURL(URL url, String filePath){ |
||
262 | if (downloadedFiles==null) |
||
263 | downloadedFiles = new Hashtable(); |
||
264 | String fileName = (String) downloadedFiles.put(url, filePath); |
||
265 | 5276 | jmvivo | //JMV: No se puede eliminar el anterior porque puede que alguien lo
|
266 | // este usando
|
||
267 | /*
|
||
268 | 3706 | jaume | if (fileName!=null){
|
269 | File f = new File(fileName);
|
||
270 | if (f.exists())
|
||
271 | f.delete();
|
||
272 | }
|
||
273 | 5276 | jmvivo | */
|
274 | 3706 | jaume | } |
275 | 7513 | caballero | |
276 | 3706 | jaume | /**
|
277 | 7513 | caballero | * Downloads an URL into a temporary file that is removed the next time the
|
278 | 3706 | jaume | * tempFileManager class is called, which means the next time gvSIG is launched.
|
279 | 7513 | caballero | *
|
280 | 3706 | jaume | * @param url
|
281 | * @param name
|
||
282 | * @return
|
||
283 | * @throws IOException
|
||
284 | * @throws ServerErrorResponseException
|
||
285 | * @throws ConnectException
|
||
286 | * @throws UnknownHostException
|
||
287 | */
|
||
288 | public static File downloadFile(URL url, String name) throws IOException,ConnectException, UnknownHostException{ |
||
289 | File f = null; |
||
290 | |||
291 | try{
|
||
292 | if ((f=getPreviousDownloadedURL(url))==null){ |
||
293 | File tempDirectory = new File(tempDirectoryPath); |
||
294 | if (!tempDirectory.exists())
|
||
295 | tempDirectory.mkdir(); |
||
296 | 7513 | caballero | |
297 | 3706 | jaume | f = new File(tempDirectoryPath+"/"+name+System.currentTimeMillis()); |
298 | 7513 | caballero | |
299 | 3706 | jaume | System.out.println("downloading '"+url.toString()+"' to: "+f.getAbsolutePath()); |
300 | 7513 | caballero | |
301 | 3706 | jaume | f.deleteOnExit(); |
302 | DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f))); |
||
303 | byte[] buffer = new byte[1024*256]; |
||
304 | InputStream is = url.openStream();
|
||
305 | long readed = 0; |
||
306 | for (int i = is.read(buffer); i>0; i = is.read(buffer)){ |
||
307 | dos.write(buffer, 0, i);
|
||
308 | readed += i; |
||
309 | } |
||
310 | dos.close(); |
||
311 | addDownloadedURL(url, f.getAbsolutePath()); |
||
312 | } |
||
313 | } catch (IOException io) { |
||
314 | io.printStackTrace(); |
||
315 | } |
||
316 | 7513 | caballero | |
317 | 3706 | jaume | return f;
|
318 | } |
||
319 | |||
320 | /**
|
||
321 | * Cleans every temporal file previously downloaded.
|
||
322 | */
|
||
323 | public static void cleanUpTempFiles() { |
||
324 | try{
|
||
325 | File tempDirectory = new File(tempDirectoryPath); |
||
326 | 7513 | caballero | |
327 | 3706 | jaume | File[] files = tempDirectory.listFiles(); |
328 | if (files!=null) { |
||
329 | for (int i = 0; i < files.length; i++) { |
||
330 | // s?lo por si en un futuro se necesitan crear directorios temporales
|
||
331 | if (files[i].isDirectory()) deleteDirectory(files[i]);
|
||
332 | files[i].delete(); |
||
333 | } |
||
334 | } |
||
335 | tempDirectory.delete(); |
||
336 | } catch (Exception e) { } |
||
337 | 7513 | caballero | |
338 | 3706 | jaume | } |
339 | /**
|
||
340 | * Recursive directory delete.
|
||
341 | * @param f
|
||
342 | */
|
||
343 | private static void deleteDirectory(File f) { |
||
344 | File[] files = f.listFiles(); |
||
345 | for (int i = 0; i < files.length; i++) { |
||
346 | if (files[i].isDirectory()) deleteDirectory(files[i]);
|
||
347 | files[i].delete(); |
||
348 | } |
||
349 | 7513 | caballero | |
350 | 3706 | jaume | } |
351 | 7513 | caballero | |
352 | 4099 | fjp | /**
|
353 | * Crea un fichero temporal con un nombre concreto y unos datos pasados por
|
||
354 | * par?metro.
|
||
355 | * @param fileName Nombre de fichero
|
||
356 | * @param data datos a guardar en el fichero
|
||
357 | */
|
||
358 | public static void createTemp(String fileName, String data)throws IOException{ |
||
359 | File f = new File(fileName); |
||
360 | DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)) ); |
||
361 | dos.writeBytes(data); |
||
362 | dos.close(); |
||
363 | f.deleteOnExit(); |
||
364 | } |
||
365 | 7513 | caballero | |
366 | 4099 | fjp | /**
|
367 | * Remove an URL from the system cache. The file will remain in the file
|
||
368 | * system for further eventual uses.
|
||
369 | * @param request
|
||
370 | */
|
||
371 | public static void removeURL(URL url) { |
||
372 | 4428 | jaume | if (downloadedFiles != null && downloadedFiles.containsKey(url)) |
373 | 4424 | jaume | downloadedFiles.remove(url); |
374 | 4099 | fjp | } |
375 | 598 | fernando | } |