svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / Utilities.java @ 44093
History | View | Annotate | Download (12.1 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.andami; |
24 |
|
25 |
import java.awt.Component; |
26 |
import java.awt.Container; |
27 |
import java.io.BufferedInputStream; |
28 |
import java.io.BufferedOutputStream; |
29 |
import java.io.DataOutputStream; |
30 |
import java.io.File; |
31 |
import java.io.FileOutputStream; |
32 |
import java.io.IOException; |
33 |
import java.io.InputStream; |
34 |
import java.io.OutputStream; |
35 |
import java.net.ConnectException; |
36 |
import java.net.MalformedURLException; |
37 |
import java.net.URL; |
38 |
import java.net.UnknownHostException; |
39 |
import java.util.Enumeration; |
40 |
import java.util.Hashtable; |
41 |
import java.util.zip.ZipEntry; |
42 |
import java.util.zip.ZipException; |
43 |
import java.util.zip.ZipFile; |
44 |
|
45 |
import javax.swing.ImageIcon; |
46 |
import javax.swing.RootPaneContainer; |
47 |
|
48 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
49 |
import org.gvsig.andami.ui.splash.MultiSplashWindow; |
50 |
import org.gvsig.tools.ToolsLocator; |
51 |
import org.slf4j.Logger; |
52 |
import org.slf4j.LoggerFactory; |
53 |
|
54 |
/**
|
55 |
* This class offers several general purpose method, to perform common tasks in
|
56 |
* an easy way.
|
57 |
*
|
58 |
* @version $Revision: 29593 $
|
59 |
*/
|
60 |
public class Utilities { |
61 |
|
62 |
/**
|
63 |
* <b>key</b>: URL, <b>value</b>: path to the downloaded file.
|
64 |
*/
|
65 |
private static Hashtable downloadedFiles; |
66 |
|
67 |
private static final Logger logger = LoggerFactory.getLogger(Utilities.class.getName()); |
68 |
|
69 |
/**
|
70 |
* @deprecated use TemporaryFolderManager
|
71 |
*/
|
72 |
public static final String TEMPDIRECTORYPATH = getTempDirectory(); |
73 |
|
74 |
/**
|
75 |
* @deprecated use TemporaryFolderManager.getTemporaryFolder()
|
76 |
*/
|
77 |
public static String getTempDirectory() { |
78 |
return ToolsLocator.getFoldersManager().getTemporaryFolder().getAbsolutePath();
|
79 |
} |
80 |
|
81 |
/**
|
82 |
* Cleans every temporal file previously downloaded.
|
83 |
*
|
84 |
* @deprecated use TemporaryFolderManager.cleanAll()
|
85 |
*/
|
86 |
public static void cleanUpTempFiles() { |
87 |
ToolsLocator.getFoldersManager().cleanTemporaryFiles(); |
88 |
} |
89 |
|
90 |
/**
|
91 |
* Creates a temporary file with a the provided name and data. The file will
|
92 |
* be automatically deleted when the application exits.
|
93 |
*
|
94 |
* @param fileName Name of the temporary file to create
|
95 |
* @param data The data to store in the file
|
96 |
* @return
|
97 |
* @throws java.io.IOException
|
98 |
*
|
99 |
* @deprecated use TemporaryFolderManager.createTemporaryFile()
|
100 |
*/
|
101 |
public static File createTemp(String fileName, String data) throws IOException { |
102 |
return ToolsLocator.getFoldersManager().createTemporaryFile(fileName, data);
|
103 |
} |
104 |
|
105 |
/**
|
106 |
* Creates the directory for temporary files, and returns the path of this
|
107 |
* directory. If the directory already exists, it just returns its path. Any
|
108 |
* file or directory created in this special directory will be delete when
|
109 |
* the application finishes.
|
110 |
*
|
111 |
* @return An String containing the full path to the temporary directory
|
112 |
*
|
113 |
* @deprecated use TemporaryFolderManager.createTemporaryFolder()
|
114 |
*/
|
115 |
public static String createTempDirectory() { |
116 |
return ToolsLocator.getFoldersManager().createTemporaryFolder().getAbsolutePath();
|
117 |
} |
118 |
|
119 |
/**
|
120 |
* Creates an icon from an image path.
|
121 |
*
|
122 |
* @param path Path to the image to be loaded
|
123 |
*
|
124 |
* @return ImageIcon if the image is found, null otherwise
|
125 |
*/
|
126 |
public static ImageIcon createImageIcon(String path) { |
127 |
URL imgURL = null; |
128 |
|
129 |
try {
|
130 |
imgURL = new URL("file:" + path); |
131 |
} catch (MalformedURLException e) { |
132 |
e.printStackTrace(); |
133 |
} |
134 |
|
135 |
if (imgURL != null) { |
136 |
return new ImageIcon(imgURL); |
137 |
} else {
|
138 |
return null; |
139 |
} |
140 |
} |
141 |
|
142 |
/**
|
143 |
* Method which frees the memory from JInternalFrames
|
144 |
*
|
145 |
* @param baseComponent JInternalFrame whose memory is to be freed
|
146 |
*/
|
147 |
public static void cleanComponent(Component baseComponent) { |
148 |
try {
|
149 |
cleanComponent(baseComponent, 0);
|
150 |
} catch (Exception ignore) { // give some exception handling... |
151 |
} |
152 |
} |
153 |
|
154 |
/* * 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. */
|
155 |
protected static void cleanComponent(Component baseComponent, int depth) { |
156 |
if (baseComponent == null) // recursion terminating clause |
157 |
{ |
158 |
return;
|
159 |
} |
160 |
|
161 |
if (baseComponent instanceof IWindow) { |
162 |
return;
|
163 |
} |
164 |
|
165 |
Container cont;
|
166 |
Component[] childComponents; |
167 |
int numChildren; // clean up component containers |
168 |
|
169 |
if (baseComponent instanceof Container) { // now clean up container instance variables |
170 |
|
171 |
if (baseComponent instanceof RootPaneContainer) { // Swing specialised container |
172 |
cont = (Container) baseComponent;
|
173 |
numChildren = cont.getComponentCount(); |
174 |
childComponents = cont.getComponents(); |
175 |
|
176 |
for (int i = 0; i < numChildren; i++) { // remove each component from the current container |
177 |
|
178 |
// each child component may be a container itself
|
179 |
cleanComponent(childComponents[i], depth + 1);
|
180 |
((RootPaneContainer) cont).getContentPane().remove(childComponents[i]);
|
181 |
} |
182 |
|
183 |
((RootPaneContainer) cont).getContentPane().setLayout(null); |
184 |
} else { // General Swing, and AWT, Containers |
185 |
cont = (Container) baseComponent;
|
186 |
numChildren = cont.getComponentCount(); |
187 |
childComponents = cont.getComponents(); |
188 |
|
189 |
for (int i = 0; i < numChildren; i++) //for(int i = 0;i < numChildren;i++) |
190 |
{ |
191 |
// remove each component from the current container // each child component may be a container itself
|
192 |
cleanComponent(childComponents[i], depth + 1);
|
193 |
cont.remove(childComponents[i]); |
194 |
} |
195 |
|
196 |
cont.setLayout(null);
|
197 |
} |
198 |
} |
199 |
|
200 |
// if component is also a container
|
201 |
} |
202 |
|
203 |
/**
|
204 |
* Extracts a ZIP file in the provided directory
|
205 |
*
|
206 |
* @param file Compressed file
|
207 |
* @param dir Directory to extract the files
|
208 |
* @param splash The splash window to show the extraction progress
|
209 |
*
|
210 |
* @throws ZipException If there is some problem in the file format
|
211 |
* @throws IOException If there is a problem reading the file
|
212 |
*/
|
213 |
public static void extractTo(File file, File dir, MultiSplashWindow splash) |
214 |
throws ZipException, IOException { |
215 |
ZipFile zip = new ZipFile(file); |
216 |
Enumeration e = zip.entries();
|
217 |
|
218 |
// Pasada para crear las carpetas
|
219 |
while (e.hasMoreElements()) {
|
220 |
ZipEntry entry = (ZipEntry) e.nextElement(); |
221 |
|
222 |
if (entry.isDirectory()) {
|
223 |
File directorio = new File(dir.getAbsolutePath() |
224 |
+ File.separator + entry.getName());
|
225 |
|
226 |
directorio.mkdirs(); |
227 |
} |
228 |
|
229 |
} |
230 |
|
231 |
// Pasada para crear los ficheros
|
232 |
e = zip.entries(); |
233 |
while (e.hasMoreElements()) {
|
234 |
ZipEntry entry = (ZipEntry) e.nextElement(); |
235 |
splash.process(30, "Procesando " + entry.getName() + "..."); |
236 |
if (!entry.isDirectory()) {
|
237 |
InputStream in = zip.getInputStream(entry);
|
238 |
OutputStream out = new FileOutputStream(dir + File.separator |
239 |
+ entry.getName()); |
240 |
BufferedInputStream bin = new BufferedInputStream(in); |
241 |
BufferedOutputStream bout = new BufferedOutputStream(out); |
242 |
|
243 |
int i;
|
244 |
|
245 |
while ((i = bin.read()) != -1) { |
246 |
bout.write(i); |
247 |
} |
248 |
|
249 |
bout.flush(); |
250 |
bout.close(); |
251 |
bin.close(); |
252 |
|
253 |
} |
254 |
|
255 |
} |
256 |
|
257 |
zip.close(); |
258 |
zip = null;
|
259 |
System.gc();
|
260 |
|
261 |
} |
262 |
|
263 |
/**
|
264 |
* Returns the content of this URL as a file from the file system.<br>
|
265 |
* <p>
|
266 |
* If the URL has been already downloaded in this session and notified to
|
267 |
* the system using the static <b>Utilities.addDownloadedURL(URL)</b>
|
268 |
* method, it can be restored faster from the file system avoiding to
|
269 |
* download it again.
|
270 |
* </p>
|
271 |
*
|
272 |
* @param url
|
273 |
* @return File containing this URL's content or null if no file was found.
|
274 |
*/
|
275 |
private static File getPreviousDownloadedURL(URL url) { |
276 |
File f = null; |
277 |
if (downloadedFiles != null && downloadedFiles.containsKey(url)) { |
278 |
String filePath = (String) downloadedFiles.get(url); |
279 |
f = new File(filePath); |
280 |
} |
281 |
return f;
|
282 |
} |
283 |
|
284 |
/**
|
285 |
* Adds an URL to the table of downloaded files for further uses. If the URL
|
286 |
* already exists in the table its filePath value is updated to the new one
|
287 |
* and the old file itself is removed from the file system.
|
288 |
*
|
289 |
* @param url
|
290 |
* @param filePath
|
291 |
*/
|
292 |
private static void addDownloadedURL(URL url, String filePath) { |
293 |
if (downloadedFiles == null) { |
294 |
downloadedFiles = new Hashtable(); |
295 |
} |
296 |
String fileName = (String) downloadedFiles.put(url, filePath); |
297 |
//JMV: No se puede eliminar el anterior porque puede que alguien lo
|
298 |
// este usando
|
299 |
/*
|
300 |
if (fileName!=null){
|
301 |
File f = new File(fileName);
|
302 |
if (f.exists())
|
303 |
f.delete();
|
304 |
}
|
305 |
*/
|
306 |
} |
307 |
|
308 |
/**
|
309 |
* Downloads an URL into a temporary file that is removed the next time the
|
310 |
* tempFileManager class is called, which means the next time gvSIG is
|
311 |
* launched.
|
312 |
*
|
313 |
* @param url
|
314 |
* @param name
|
315 |
* @return
|
316 |
* @throws IOException
|
317 |
* @throws ServerErrorResponseException
|
318 |
* @throws ConnectException
|
319 |
* @throws UnknownHostException
|
320 |
*/
|
321 |
public static File downloadFile(URL url, String name) throws IOException, ConnectException, UnknownHostException { |
322 |
File f = null; |
323 |
|
324 |
try {
|
325 |
if ((f = getPreviousDownloadedURL(url)) == null) { |
326 |
File tempDirectory = new File(TEMPDIRECTORYPATH); |
327 |
if (!tempDirectory.exists()) {
|
328 |
tempDirectory.mkdir(); |
329 |
} |
330 |
|
331 |
f = new File(TEMPDIRECTORYPATH + "/" + name + System.currentTimeMillis()); |
332 |
|
333 |
System.out.println("downloading '" + url.toString() + "' to: " + f.getAbsolutePath()); |
334 |
|
335 |
f.deleteOnExit(); |
336 |
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f))); |
337 |
byte[] buffer = new byte[1024 * 256]; |
338 |
InputStream is = url.openStream();
|
339 |
long readed = 0; |
340 |
for (int i = is.read(buffer); i > 0; i = is.read(buffer)) { |
341 |
dos.write(buffer, 0, i);
|
342 |
readed += i; |
343 |
} |
344 |
dos.close(); |
345 |
addDownloadedURL(url, f.getAbsolutePath()); |
346 |
} |
347 |
} catch (IOException io) { |
348 |
io.printStackTrace(); |
349 |
} |
350 |
|
351 |
return f;
|
352 |
} |
353 |
|
354 |
/**
|
355 |
* Remove an URL from the system cache. The file will remain in the file
|
356 |
* system for further eventual uses.
|
357 |
*
|
358 |
* @param request
|
359 |
*/
|
360 |
public static void removeURL(URL url) { |
361 |
if (downloadedFiles != null && downloadedFiles.containsKey(url)) { |
362 |
downloadedFiles.remove(url); |
363 |
} |
364 |
} |
365 |
|
366 |
} |