svn-gvsig-desktop / branches / v10 / frameworks / _fwAndami / src / com / iver / andami / Utilities.java @ 11996
History | View | Annotate | Download (12 KB)
1 |
/* 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 |
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 |
import java.io.DataOutputStream; |
48 |
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 |
import java.net.ConnectException; |
54 |
import java.net.MalformedURLException; |
55 |
import java.net.URL; |
56 |
import java.net.UnknownHostException; |
57 |
import java.util.Enumeration; |
58 |
import java.util.Hashtable; |
59 |
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.mdiManager.IWindow; |
69 |
import com.iver.andami.ui.splash.MultiSplashWindow; |
70 |
|
71 |
|
72 |
|
73 |
/**
|
74 |
* This class offers several general purpose method, to perform common
|
75 |
* tasks in an easy way.
|
76 |
*
|
77 |
* @version $Revision: 11996 $
|
78 |
*/
|
79 |
public class Utilities { |
80 |
/**
|
81 |
* <b>key</b>: URL, <b>value</b>: path to the downloaded file.
|
82 |
*/
|
83 |
private static Hashtable downloadedFiles; |
84 |
/** DOCUMENT ME! */
|
85 |
private static Logger logger = Logger.getLogger(Utilities.class.getName()); |
86 |
private static final String tempDirectoryPath = System.getProperty("java.io.tmpdir")+"/tmp-andami"; |
87 |
|
88 |
|
89 |
/**
|
90 |
* Creates an icon from an image path.
|
91 |
*
|
92 |
* @param path Path to the image to be loaded
|
93 |
*
|
94 |
* @return ImageIcon if the image is found, null otherwise
|
95 |
*/
|
96 |
public static ImageIcon createImageIcon(String path) { |
97 |
URL imgURL = null; |
98 |
|
99 |
try {
|
100 |
imgURL = new URL("file:" + path); |
101 |
} catch (MalformedURLException e) { |
102 |
e.printStackTrace(); |
103 |
} |
104 |
|
105 |
if (imgURL != null) { |
106 |
return new ImageIcon(imgURL); |
107 |
} else {
|
108 |
return null; |
109 |
} |
110 |
} |
111 |
|
112 |
/**
|
113 |
* Method which frees the memory from JInternalFrames
|
114 |
*
|
115 |
* @param baseComponent JInternalFrame whose memory is to be
|
116 |
* freed
|
117 |
*/
|
118 |
public static void cleanComponent(Component baseComponent) { |
119 |
try {
|
120 |
cleanComponent(baseComponent, 0);
|
121 |
} catch (Exception ignore) { // give some exception handling... |
122 |
} |
123 |
} |
124 |
|
125 |
/* * 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. */
|
126 |
protected static void cleanComponent(Component baseComponent, int depth) { |
127 |
if (baseComponent == null) // recursion terminating clause |
128 |
{ |
129 |
return;
|
130 |
} |
131 |
|
132 |
if (baseComponent instanceof IWindow){ |
133 |
return;
|
134 |
} |
135 |
|
136 |
Container cont;
|
137 |
Component[] childComponents; |
138 |
int numChildren; // clean up component containers |
139 |
|
140 |
if (baseComponent instanceof Container) { // now clean up container instance variables |
141 |
|
142 |
if (baseComponent instanceof RootPaneContainer) { // Swing specialised container |
143 |
cont = (Container) baseComponent;
|
144 |
numChildren = cont.getComponentCount(); |
145 |
childComponents = cont.getComponents(); |
146 |
|
147 |
for (int i = 0; i < numChildren; i++) { // remove each component from the current container |
148 |
|
149 |
// each child component may be a container itself
|
150 |
cleanComponent(childComponents[i], depth + 1);
|
151 |
((RootPaneContainer) cont).getContentPane().remove(childComponents[i]);
|
152 |
} |
153 |
|
154 |
((RootPaneContainer) cont).getContentPane().setLayout(null); |
155 |
} else { // General Swing, and AWT, Containers |
156 |
cont = (Container) baseComponent;
|
157 |
numChildren = cont.getComponentCount(); |
158 |
childComponents = cont.getComponents(); |
159 |
|
160 |
for (int i = 0; i < numChildren; i++) //for(int i = 0;i < numChildren;i++) |
161 |
{ |
162 |
// remove each component from the current container // each child component may be a container itself
|
163 |
cleanComponent(childComponents[i], depth + 1);
|
164 |
cont.remove(childComponents[i]); |
165 |
} |
166 |
|
167 |
cont.setLayout(null);
|
168 |
} |
169 |
} |
170 |
|
171 |
// if component is also a container
|
172 |
} |
173 |
|
174 |
|
175 |
|
176 |
/**
|
177 |
* Extracts a ZIP file in the provided directory
|
178 |
*
|
179 |
* @param file Compressed file
|
180 |
* @param dir Directory to extract the files
|
181 |
* @param splash The splash window to show the extraction progress
|
182 |
*
|
183 |
* @throws ZipException If there is some problem in the file format
|
184 |
* @throws IOException If there is a problem reading the file
|
185 |
*/
|
186 |
public static void extractTo(File file, File dir, MultiSplashWindow splash) |
187 |
throws ZipException, IOException { |
188 |
ZipFile zip = new ZipFile(file); |
189 |
Enumeration e = zip.entries();
|
190 |
|
191 |
// Pasada para crear las carpetas
|
192 |
while (e.hasMoreElements()) {
|
193 |
ZipEntry entry = (ZipEntry) e.nextElement(); |
194 |
|
195 |
if (entry.isDirectory()) {
|
196 |
File directorio = new File(dir.getAbsolutePath() |
197 |
+ File.separator + entry.getName());
|
198 |
|
199 |
directorio.mkdirs(); |
200 |
} |
201 |
|
202 |
} |
203 |
|
204 |
// Pasada para crear los ficheros
|
205 |
e = zip.entries(); |
206 |
while (e.hasMoreElements()) {
|
207 |
ZipEntry entry = (ZipEntry) e.nextElement(); |
208 |
splash.process(30, "Procesando " + entry.getName() + "..."); |
209 |
if (!entry.isDirectory()) {
|
210 |
InputStream in = zip.getInputStream(entry);
|
211 |
OutputStream out = new FileOutputStream(dir + File.separator |
212 |
+ entry.getName()); |
213 |
BufferedInputStream bin = new BufferedInputStream(in); |
214 |
BufferedOutputStream bout = new BufferedOutputStream(out); |
215 |
|
216 |
int i;
|
217 |
|
218 |
while ((i = bin.read()) != -1) { |
219 |
bout.write(i); |
220 |
} |
221 |
|
222 |
bout.flush(); |
223 |
bout.close(); |
224 |
bin.close(); |
225 |
|
226 |
} |
227 |
|
228 |
} |
229 |
|
230 |
zip.close(); |
231 |
zip = null;
|
232 |
System.gc();
|
233 |
|
234 |
} |
235 |
/**
|
236 |
* Returns the content of this URL as a file from the file system.<br>
|
237 |
* <p>
|
238 |
* If the URL has been already downloaded in this session and notified
|
239 |
* to the system using the static <b>Utilities.addDownloadedURL(URL)</b>
|
240 |
* method, it can be restored faster from the file system avoiding to
|
241 |
* download it again.
|
242 |
* </p>
|
243 |
* @param url
|
244 |
* @return File containing this URL's content or null if no file was found.
|
245 |
*/
|
246 |
private static File getPreviousDownloadedURL(URL url){ |
247 |
File f = null; |
248 |
if (downloadedFiles!=null && downloadedFiles.containsKey(url)){ |
249 |
String filePath = (String) downloadedFiles.get(url); |
250 |
f = new File(filePath); |
251 |
} |
252 |
return f;
|
253 |
} |
254 |
|
255 |
/**
|
256 |
* Adds an URL to the table of downloaded files for further uses. If the URL
|
257 |
* already exists in the table its filePath value is updated to the new one and
|
258 |
* the old file itself is removed from the file system.
|
259 |
*
|
260 |
* @param url
|
261 |
* @param filePath
|
262 |
*/
|
263 |
private static void addDownloadedURL(URL url, String filePath){ |
264 |
if (downloadedFiles==null) |
265 |
downloadedFiles = new Hashtable(); |
266 |
String fileName = (String) downloadedFiles.put(url, filePath); |
267 |
//JMV: No se puede eliminar el anterior porque puede que alguien lo
|
268 |
// este usando
|
269 |
/*
|
270 |
if (fileName!=null){
|
271 |
File f = new File(fileName);
|
272 |
if (f.exists())
|
273 |
f.delete();
|
274 |
}
|
275 |
*/
|
276 |
} |
277 |
|
278 |
/**
|
279 |
* Downloads an URL into a temporary file that is removed the next time the
|
280 |
* tempFileManager class is called, which means the next time gvSIG is launched.
|
281 |
*
|
282 |
* @param url
|
283 |
* @param name
|
284 |
* @return
|
285 |
* @throws IOException
|
286 |
* @throws ServerErrorResponseException
|
287 |
* @throws ConnectException
|
288 |
* @throws UnknownHostException
|
289 |
*/
|
290 |
public static File downloadFile(URL url, String name) throws IOException,ConnectException, UnknownHostException{ |
291 |
File f = null; |
292 |
|
293 |
try{
|
294 |
if ((f=getPreviousDownloadedURL(url))==null){ |
295 |
createTempDirectory(); |
296 |
|
297 |
f = new File(tempDirectoryPath+"/"+name+System.currentTimeMillis()); |
298 |
|
299 |
System.out.println("downloading '"+url.toString()+"' to: "+f.getAbsolutePath()); |
300 |
|
301 |
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 |
|
317 |
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 |
|
327 |
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 |
|
338 |
} |
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 |
|
350 |
} |
351 |
|
352 |
/**
|
353 |
* Creates a temporary file with a the provided name and data. The file
|
354 |
* will be automatically deleted when the application exits.
|
355 |
*
|
356 |
* @param fileName Name of the temporary file to create
|
357 |
* @param data The data to store in the file
|
358 |
*/
|
359 |
public static void createTemp(String fileName, String data)throws IOException{ |
360 |
File f = new File(fileName); |
361 |
DataOutputStream dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(f)) ); |
362 |
dos.writeBytes(data); |
363 |
dos.close(); |
364 |
f.deleteOnExit(); |
365 |
} |
366 |
|
367 |
/**
|
368 |
* Remove an URL from the system cache. The file will remain in the file
|
369 |
* system for further eventual uses.
|
370 |
* @param request
|
371 |
*/
|
372 |
public static void removeURL(URL url) { |
373 |
if (downloadedFiles != null && downloadedFiles.containsKey(url)) |
374 |
downloadedFiles.remove(url); |
375 |
} |
376 |
|
377 |
/**
|
378 |
* Creates the directory for temporary files, and returns the path of
|
379 |
* this directory. If the directory already exists, it just returns
|
380 |
* its path. Any file or directory created in this special directory
|
381 |
* will be delete when the application finishes.
|
382 |
*
|
383 |
* @return An String containing the full path to the temporary directory
|
384 |
*/
|
385 |
public static String createTempDirectory(){ |
386 |
File tempDirectory = new File(tempDirectoryPath); |
387 |
if (!tempDirectory.exists())
|
388 |
tempDirectory.mkdir(); |
389 |
return tempDirectoryPath;
|
390 |
} |
391 |
} |