svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / Utilities.java @ 44198
History | View | Annotate | Download (12.1 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
6 | 43102 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
11 | 43102 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
16 | 43102 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
20 | 43102 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
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 | 43102 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
51 | 40435 | jjdelcerro | import org.slf4j.Logger; |
52 | import org.slf4j.LoggerFactory; |
||
53 | |||
54 | /**
|
||
55 | 43102 | jjdelcerro | * This class offers several general purpose method, to perform common tasks in
|
56 | * an easy way.
|
||
57 | 40435 | jjdelcerro | *
|
58 | * @version $Revision: 29593 $
|
||
59 | */
|
||
60 | public class Utilities { |
||
61 | 43102 | jjdelcerro | |
62 | /**
|
||
63 | 40435 | jjdelcerro | * <b>key</b>: URL, <b>value</b>: path to the downloaded file.
|
64 | */
|
||
65 | private static Hashtable downloadedFiles; |
||
66 | |||
67 | 43102 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(Utilities.class.getName()); |
68 | 40435 | jjdelcerro | |
69 | /**
|
||
70 | 43102 | jjdelcerro | * @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 | 40435 | jjdelcerro | * 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 | 43102 | jjdelcerro | * @param baseComponent JInternalFrame whose memory is to be freed
|
146 | 40435 | jjdelcerro | */
|
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 | 43102 | jjdelcerro | { |
158 | 40435 | jjdelcerro | return;
|
159 | } |
||
160 | |||
161 | 43102 | jjdelcerro | if (baseComponent instanceof IWindow) { |
162 | return;
|
||
163 | 40435 | jjdelcerro | } |
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 | 43102 | jjdelcerro | { |
191 | 40435 | jjdelcerro | // 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 | 43102 | jjdelcerro | throws ZipException, IOException { |
215 | 40435 | jjdelcerro | ZipFile zip = new ZipFile(file); |
216 | Enumeration e = zip.entries();
|
||
217 | |||
218 | 43102 | jjdelcerro | // Pasada para crear las carpetas
|
219 | while (e.hasMoreElements()) {
|
||
220 | ZipEntry entry = (ZipEntry) e.nextElement(); |
||
221 | 40435 | jjdelcerro | |
222 | 43102 | jjdelcerro | if (entry.isDirectory()) {
|
223 | File directorio = new File(dir.getAbsolutePath() |
||
224 | + File.separator + entry.getName());
|
||
225 | 40435 | jjdelcerro | |
226 | 43102 | jjdelcerro | directorio.mkdirs(); |
227 | } |
||
228 | 40435 | jjdelcerro | |
229 | 43102 | jjdelcerro | } |
230 | 40435 | jjdelcerro | |
231 | 43102 | jjdelcerro | // 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 | 40435 | jjdelcerro | |
243 | 43102 | jjdelcerro | int i;
|
244 | 40435 | jjdelcerro | |
245 | 43102 | jjdelcerro | while ((i = bin.read()) != -1) { |
246 | bout.write(i); |
||
247 | } |
||
248 | 40435 | jjdelcerro | |
249 | 43102 | jjdelcerro | bout.flush(); |
250 | bout.close(); |
||
251 | bin.close(); |
||
252 | 40435 | jjdelcerro | |
253 | 43102 | jjdelcerro | } |
254 | 40435 | jjdelcerro | |
255 | 43102 | jjdelcerro | } |
256 | 40435 | jjdelcerro | |
257 | 43102 | jjdelcerro | zip.close(); |
258 | zip = null;
|
||
259 | System.gc();
|
||
260 | 40435 | jjdelcerro | |
261 | 43102 | jjdelcerro | } |
262 | |||
263 | 40435 | jjdelcerro | /**
|
264 | * Returns the content of this URL as a file from the file system.<br>
|
||
265 | * <p>
|
||
266 | 43102 | jjdelcerro | * 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 | 40435 | jjdelcerro | * method, it can be restored faster from the file system avoiding to
|
269 | * download it again.
|
||
270 | * </p>
|
||
271 | 43102 | jjdelcerro | *
|
272 | 40435 | jjdelcerro | * @param url
|
273 | * @return File containing this URL's content or null if no file was found.
|
||
274 | */
|
||
275 | 43102 | jjdelcerro | private static File getPreviousDownloadedURL(URL url) { |
276 | 40435 | jjdelcerro | File f = null; |
277 | 43102 | jjdelcerro | if (downloadedFiles != null && downloadedFiles.containsKey(url)) { |
278 | 40435 | jjdelcerro | 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 | 43102 | jjdelcerro | * 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 | 40435 | jjdelcerro | *
|
289 | * @param url
|
||
290 | * @param filePath
|
||
291 | */
|
||
292 | 43102 | jjdelcerro | private static void addDownloadedURL(URL url, String filePath) { |
293 | if (downloadedFiles == null) { |
||
294 | 40435 | jjdelcerro | downloadedFiles = new Hashtable(); |
295 | 43102 | jjdelcerro | } |
296 | 40435 | jjdelcerro | 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 | 43102 | jjdelcerro | if (fileName!=null){
|
301 | File f = new File(fileName);
|
||
302 | if (f.exists())
|
||
303 | f.delete();
|
||
304 | }
|
||
305 | */
|
||
306 | 40435 | jjdelcerro | } |
307 | |||
308 | /**
|
||
309 | * Downloads an URL into a temporary file that is removed the next time the
|
||
310 | 43102 | jjdelcerro | * tempFileManager class is called, which means the next time gvSIG is
|
311 | * launched.
|
||
312 | 40435 | jjdelcerro | *
|
313 | * @param url
|
||
314 | * @param name
|
||
315 | * @return
|
||
316 | * @throws IOException
|
||
317 | * @throws ServerErrorResponseException
|
||
318 | * @throws ConnectException
|
||
319 | * @throws UnknownHostException
|
||
320 | */
|
||
321 | 43102 | jjdelcerro | public static File downloadFile(URL url, String name) throws IOException, ConnectException, UnknownHostException { |
322 | File f = null; |
||
323 | 40435 | jjdelcerro | |
324 | 43102 | jjdelcerro | try {
|
325 | if ((f = getPreviousDownloadedURL(url)) == null) { |
||
326 | File tempDirectory = new File(TEMPDIRECTORYPATH); |
||
327 | if (!tempDirectory.exists()) {
|
||
328 | tempDirectory.mkdir(); |
||
329 | } |
||
330 | 40435 | jjdelcerro | |
331 | 43102 | jjdelcerro | f = new File(TEMPDIRECTORYPATH + "/" + name + System.currentTimeMillis()); |
332 | 40435 | jjdelcerro | |
333 | 43102 | jjdelcerro | System.out.println("downloading '" + url.toString() + "' to: " + f.getAbsolutePath()); |
334 | 40435 | jjdelcerro | |
335 | 43102 | jjdelcerro | f.deleteOnExit(); |
336 | DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f))); |
||
337 | byte[] buffer = new byte[1024 * 256]; |
||
338 | 40435 | jjdelcerro | InputStream is = url.openStream();
|
339 | long readed = 0; |
||
340 | 43102 | jjdelcerro | for (int i = is.read(buffer); i > 0; i = is.read(buffer)) { |
341 | 40435 | jjdelcerro | dos.write(buffer, 0, i);
|
342 | readed += i; |
||
343 | } |
||
344 | dos.close(); |
||
345 | addDownloadedURL(url, f.getAbsolutePath()); |
||
346 | 43102 | jjdelcerro | } |
347 | } catch (IOException io) { |
||
348 | io.printStackTrace(); |
||
349 | } |
||
350 | 40435 | jjdelcerro | |
351 | 43102 | jjdelcerro | return f;
|
352 | 40435 | jjdelcerro | } |
353 | |||
354 | /**
|
||
355 | * Remove an URL from the system cache. The file will remain in the file
|
||
356 | * system for further eventual uses.
|
||
357 | 43102 | jjdelcerro | *
|
358 | 40435 | jjdelcerro | * @param request
|
359 | */
|
||
360 | 43102 | jjdelcerro | public static void removeURL(URL url) { |
361 | if (downloadedFiles != null && downloadedFiles.containsKey(url)) { |
||
362 | downloadedFiles.remove(url); |
||
363 | } |
||
364 | } |
||
365 | |||
366 | 40435 | jjdelcerro | } |