Mercurial > dive4elements > river
annotate flys-artifacts/src/main/java/de/intevation/flys/utils/FileUtils.java @ 5076:cc50e1b9fc60 mapgenfix
Work on ArtifactMapfileGenerator.createUserShapefileLayer()
author | Christian Lins <christian.lins@intevation.de> |
---|---|
date | Wed, 20 Feb 2013 21:00:33 +0100 |
parents | a9243df307b1 |
children |
rev | line source |
---|---|
5022
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.utils; |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
2 |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
3 import de.intevation.artifacts.common.utils.FileTools; |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
4 import de.intevation.flys.artifacts.states.FloodMapState; |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
5 |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
6 import java.io.File; |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
7 import java.io.IOException; |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
8 |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
9 /** |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
10 * Various utility methods for handling of files and directories. |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
11 * @author <a href="mailto:christian.lins@intevation.de">Christian Lins</a> |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
12 */ |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
13 public class FileUtils { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
14 |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
15 public static boolean extractZipfile(File archive, File dir) { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
16 try { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
17 File tmpDir = new File(dir, "usr_tmp"); |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
18 FileTools.extractArchive(archive, tmpDir); |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
19 FileUtils.moveFiles(tmpDir, dir); |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
20 |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
21 return true; |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
22 } |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
23 catch (IOException ioe) { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
24 FloodMapState.logger.warn("Zip archive " + dir + "/" + FloodMapState.WSPLGEN_USER_ZIP + " could not be extracted."); |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
25 return false; |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
26 } |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
27 } |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
28 |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
29 public static void moveFiles(File source, final File target) |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
30 throws IOException |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
31 { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
32 if (!source.exists()) { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
33 return; |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
34 } |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
35 if (!target.exists()) { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
36 target.mkdir(); |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
37 } |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
38 FileTools.walkTree(source, new FileTools.FileVisitor() { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
39 @Override |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
40 public boolean visit(File file) { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
41 if (!file.isDirectory()) { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
42 String name = file.getName(); |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
43 String suffix = ""; |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
44 int pos = name.lastIndexOf('.'); |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
45 if (pos > 0 && pos < name.length() - 1) { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
46 suffix = name.substring(pos + 1); |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
47 } |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
48 else { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
49 return true; |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
50 } |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
51 try { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
52 FileTools.copyFile(file, new File(target, FloodMapState.WSPLGEN_USER_FILENAME + "." + suffix)); |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
53 } |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
54 catch (IOException ioe) { |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
55 FloodMapState.logger.warn ("Error while copying file " + file.getName()); |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
56 return true; |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
57 } |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
58 } |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
59 return true; |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
60 } |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
61 }); |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
62 |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
63 FileTools.deleteRecursive(source); |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
64 } |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
65 |
a9243df307b1
Move all classes of mapfile generation to de.intevation.flys.mapserver package.
Christian Lins <christian.lins@intevation.de>
parents:
diff
changeset
|
66 } |