Revision 1350
org.gvsig.educa.portableview/tags/org.gvsig.educa.portableview-1.0.210/org.gvsig.educa.portableview.lib/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="ISO-8859-1"?> |
|
2 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
|
3 |
|
|
4 |
<modelVersion>4.0.0</modelVersion> |
|
5 |
<artifactId>org.gvsig.educa.portableview.lib</artifactId> |
|
6 |
<packaging>pom</packaging> |
|
7 |
<name>org.gvsig.educa.portableview.lib</name> |
|
8 |
<parent> |
|
9 |
<groupId>org.gvsig</groupId> |
|
10 |
<artifactId>org.gvsig.educa.portableview</artifactId> |
|
11 |
<version>1.0.210</version> |
|
12 |
</parent> |
|
13 |
<modules> |
|
14 |
<module>org.gvsig.educa.portableview.lib.api</module> |
|
15 |
<module>org.gvsig.educa.portableview.lib.impl</module> |
|
16 |
<module>org.gvsig.educa.portableview.lib.prov.installer</module> |
|
17 |
</modules> |
|
18 |
</project> |
org.gvsig.educa.portableview/tags/org.gvsig.educa.portableview-1.0.210/org.gvsig.educa.portableview.lib/org.gvsig.educa.portableview.lib.impl/src/main/java/org/gvsig/educa/portableview/impl/PortableViewFileFilter.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.educa.portableview.impl; |
|
23 |
|
|
24 |
import java.io.File; |
|
25 |
import java.io.FileFilter; |
|
26 |
|
|
27 |
/** |
|
28 |
* <p> |
|
29 |
* {@link FileFilter} implementation which identifies files that can be a |
|
30 |
* Portable View |
|
31 |
* </p> |
|
32 |
* <p> |
|
33 |
* This checks: |
|
34 |
* <ul> |
|
35 |
* <li>file exists</li> |
|
36 |
* <li>is a file</li> |
|
37 |
* <li>ends in "." + Portable view extension</li> |
|
38 |
* </ul> |
|
39 |
* </p> |
|
40 |
* |
|
41 |
* @author gvSIG Team |
|
42 |
* @version $Id$ |
|
43 |
* |
|
44 |
*/ |
|
45 |
public class PortableViewFileFilter implements FileFilter { |
|
46 |
|
|
47 |
private final String fileNameEnding; |
|
48 |
private final String fileExtension; |
|
49 |
|
|
50 |
public PortableViewFileFilter(String fileExtension) { |
|
51 |
this.fileExtension = fileExtension; |
|
52 |
fileNameEnding = ".".concat(fileExtension); |
|
53 |
} |
|
54 |
|
|
55 |
/** |
|
56 |
* Returns "."+{@link #getFileExtension()} |
|
57 |
* |
|
58 |
* @return |
|
59 |
*/ |
|
60 |
public String getFileNameEnding() { |
|
61 |
return fileNameEnding; |
|
62 |
} |
|
63 |
|
|
64 |
/** |
|
65 |
* Returns file name extension used to match files |
|
66 |
* |
|
67 |
* @return |
|
68 |
*/ |
|
69 |
public String getFileExtension() { |
|
70 |
return fileExtension; |
|
71 |
} |
|
72 |
|
|
73 |
/** {@inheridDoc} */ |
|
74 |
public boolean accept(File pathname) { |
|
75 |
return pathname.exists() && pathname.isFile() |
|
76 |
&& pathname.getName().endsWith(fileNameEnding); |
|
77 |
} |
|
78 |
|
|
79 |
} |
org.gvsig.educa.portableview/tags/org.gvsig.educa.portableview-1.0.210/org.gvsig.educa.portableview.lib/org.gvsig.educa.portableview.lib.impl/src/main/java/org/gvsig/educa/portableview/impl/DefaultPortableViewFileServices.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.educa.portableview.impl; |
|
23 |
|
|
24 |
import java.io.File; |
|
25 |
import java.io.IOException; |
|
26 |
import java.io.InputStream; |
|
27 |
import java.util.zip.ZipException; |
|
28 |
|
|
29 |
import org.gvsig.educa.portableview.PortableViewFileServices; |
|
30 |
import org.gvsig.educa.portableview.impl.util.FileUtils; |
|
31 |
import org.gvsig.educa.portableview.impl.util.FilenameUtils; |
|
32 |
import org.gvsig.educa.portableview.impl.util.IOUtils; |
|
33 |
import org.gvsig.educa.portableview.impl.util.PersistenceUtils; |
|
34 |
import org.gvsig.fmap.mapcontext.MapContext; |
|
35 |
import org.gvsig.tools.ToolsLocator; |
|
36 |
import org.gvsig.tools.persistence.PersistenceManager; |
|
37 |
import org.gvsig.tools.persistence.PersistentState; |
|
38 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
|
39 |
|
|
40 |
/** |
|
41 |
* <p> |
|
42 |
* Default implementation of {@link PortableViewFileServices} |
|
43 |
* </p> |
|
44 |
* <p> |
|
45 |
* Delegates almost all methods in {@link FilenameUtils} |
|
46 |
* </p> |
|
47 |
* |
|
48 |
* @author gvSIG Team |
|
49 |
* @version $Id$ |
|
50 |
* |
|
51 |
*/ |
|
52 |
public class DefaultPortableViewFileServices implements PortableViewFileServices { |
|
53 |
|
|
54 |
private final PersistenceManager persistenceManager; |
|
55 |
|
|
56 |
/** |
|
57 |
* |
|
58 |
*/ |
|
59 |
public DefaultPortableViewFileServices() { |
|
60 |
persistenceManager = ToolsLocator.getPersistenceManager(); |
|
61 |
} |
|
62 |
|
|
63 |
/** {@inheridDoc} */ |
|
64 |
public void unzipFile(File zipFile, File outputFloder) throws ZipException, |
|
65 |
IOException { |
|
66 |
FileUtils.unzipFile(zipFile, outputFloder); |
|
67 |
} |
|
68 |
|
|
69 |
/** {@inheridDoc} */ |
|
70 |
public void zipFolder(File rootFolder, File targetFile) throws IOException { |
|
71 |
FileUtils.zipFolder(rootFolder, targetFile); |
|
72 |
} |
|
73 |
|
|
74 |
/** {@inheridDoc} */ |
|
75 |
public boolean isWritableFolder(File folder) { |
|
76 |
return FileUtils.isWritableFolder(folder); |
|
77 |
} |
|
78 |
|
|
79 |
/** {@inheridDoc} */ |
|
80 |
public boolean isReadableFolder(File folder) { |
|
81 |
return FileUtils.isReadableFolder(folder); |
|
82 |
} |
|
83 |
|
|
84 |
/** {@inheridDoc} */ |
|
85 |
public boolean isReadableFile(File file) { |
|
86 |
return FileUtils.isReadableFile(file); |
|
87 |
} |
|
88 |
|
|
89 |
/** {@inheridDoc} */ |
|
90 |
public File getWritableFolder(File baseFolder, String baseName) { |
|
91 |
return FileUtils.getWritableFolder(baseFolder, baseName); |
|
92 |
} |
|
93 |
|
|
94 |
/** {@inheridDoc} */ |
|
95 |
public File getNewWritableFolder(File baseFolder, String baseName) { |
|
96 |
return FileUtils.getNewWritableFolder(baseFolder, baseName); |
|
97 |
} |
|
98 |
|
|
99 |
/** {@inheridDoc} */ |
|
100 |
public File getNewFileName(File baseFolder, String name) { |
|
101 |
return FileUtils.getNewFileName(baseFolder, name); |
|
102 |
} |
|
103 |
|
|
104 |
/** {@inheridDoc} */ |
|
105 |
public File getRelativeTo(File path, File basePath) { |
|
106 |
return FileUtils.getRelativeTo(path, basePath); |
|
107 |
} |
|
108 |
|
|
109 |
/** {@inheridDoc} */ |
|
110 |
public MapContext loadMapContext(File mapContext, File relativaPathFolder) |
|
111 |
throws IOException, PersistenceException { |
|
112 |
InputStream is = null; |
|
113 |
try { |
|
114 |
// open file |
|
115 |
is = FileUtils.openInputStream(mapContext); |
|
116 |
|
|
117 |
// load Persistence status from file |
|
118 |
PersistentState persistenceStatus = |
|
119 |
persistenceManager.loadState(is); |
|
120 |
|
|
121 |
// Adjust all path in persistence status to deploy folder |
|
122 |
PersistenceUtils.fixMapContextFilePaths(persistenceStatus, |
|
123 |
relativaPathFolder); |
|
124 |
|
|
125 |
// Create a new mapContext Instance and load it form persistence |
|
126 |
// status |
|
127 |
return (MapContext) persistenceManager.create(persistenceStatus); |
|
128 |
|
|
129 |
} finally { |
|
130 |
IOUtils.closeQuietly(is); |
|
131 |
} |
|
132 |
} |
|
133 |
} |
org.gvsig.educa.portableview/tags/org.gvsig.educa.portableview-1.0.210/org.gvsig.educa.portableview.lib/org.gvsig.educa.portableview.lib.impl/src/main/java/org/gvsig/educa/portableview/impl/map/DefaultPortableViewInformation.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.educa.portableview.impl.map; |
|
23 |
|
|
24 |
import java.text.DateFormat; |
|
25 |
import java.text.ParseException; |
|
26 |
import java.util.Date; |
|
27 |
|
|
28 |
import org.cresques.cts.IProjection; |
|
29 |
import org.gvsig.educa.portableview.map.PortableViewInformation; |
|
30 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
31 |
import org.gvsig.tools.ToolsLocator; |
|
32 |
import org.gvsig.tools.dataTypes.DataTypes; |
|
33 |
import org.gvsig.tools.dynobject.DynObject; |
|
34 |
import org.gvsig.tools.dynobject.DynObjectManager; |
|
35 |
import org.gvsig.tools.dynobject.DynStruct; |
|
36 |
import org.gvsig.tools.persistence.PersistenceManager; |
|
37 |
import org.gvsig.tools.persistence.Persistent; |
|
38 |
import org.gvsig.tools.persistence.PersistentState; |
|
39 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
|
40 |
|
|
41 |
/** |
|
42 |
* Default implementation of {@link PortableViewInformation} |
|
43 |
* |
|
44 |
* @author gvSIG Team |
|
45 |
* @version $Id$ |
|
46 |
* |
|
47 |
*/ |
|
48 |
public class DefaultPortableViewInformation implements PortableViewInformation, |
|
49 |
Persistent { |
|
50 |
|
|
51 |
public static DynStruct dynDefinition; |
|
52 |
|
|
53 |
private String id; |
|
54 |
private String name; |
|
55 |
private String description; |
|
56 |
private int version; |
|
57 |
private int buildNumber; |
|
58 |
private Date creationTimestamp; |
|
59 |
private Envelope envelope; |
|
60 |
private IProjection crs; |
|
61 |
|
|
62 |
/** {@inheridDoc} */ |
|
63 |
public String getId() { |
|
64 |
return id; |
|
65 |
} |
|
66 |
|
|
67 |
/** {@inheridDoc} */ |
|
68 |
public String getName() { |
|
69 |
return name; |
|
70 |
} |
|
71 |
|
|
72 |
/** {@inheridDoc} */ |
|
73 |
public String getDescription() { |
|
74 |
return description; |
|
75 |
} |
|
76 |
|
|
77 |
/** {@inheridDoc} */ |
|
78 |
public int getVersion() { |
|
79 |
return version; |
|
80 |
} |
|
81 |
|
|
82 |
/** {@inheridDoc} */ |
|
83 |
public int getBuildNumber() { |
|
84 |
return buildNumber; |
|
85 |
} |
|
86 |
|
|
87 |
/** {@inheridDoc} */ |
|
88 |
public Date getCreationTimestamp() { |
|
89 |
return creationTimestamp; |
|
90 |
} |
|
91 |
|
|
92 |
/** {@inheridDoc} */ |
|
93 |
public Envelope getFullEnvelope() { |
|
94 |
if (envelope == null) { |
|
95 |
return null; |
|
96 |
} |
|
97 |
try { |
|
98 |
return (Envelope) envelope.clone(); |
|
99 |
} catch (CloneNotSupportedException e) { |
|
100 |
throw new IllegalStateException(e); |
|
101 |
} |
|
102 |
} |
|
103 |
|
|
104 |
/** {@inheridDoc} */ |
|
105 |
public IProjection getCRS() { |
|
106 |
return crs; |
|
107 |
} |
|
108 |
|
|
109 |
/** {@inheridDoc} */ |
|
110 |
public void saveToState(PersistentState state) throws PersistenceException { |
|
111 |
state.set("id", id); |
|
112 |
state.set("name", name); |
|
113 |
state.set("description", description); |
|
114 |
state.set("version", version); |
|
115 |
state.set("buildNumber", buildNumber); |
|
116 |
state.set("creationTimestamp", creationTimestamp); |
|
117 |
state.set("envelope", envelope); |
|
118 |
state.set("crs", crs); |
|
119 |
} |
|
120 |
|
|
121 |
/** {@inheridDoc} */ |
|
122 |
public DynObject getDynObject() { |
|
123 |
DynObjectManager manager = ToolsLocator.getDynObjectManager(); |
|
124 |
DynObject obj = manager.createDynObject(dynDefinition); |
|
125 |
obj.setDynValue("id", id); |
|
126 |
obj.setDynValue("name", name); |
|
127 |
obj.setDynValue("description", description); |
|
128 |
obj.setDynValue("version", version); |
|
129 |
obj.setDynValue("buildNumber", buildNumber); |
|
130 |
obj.setDynValue("creationTimestamp", creationTimestamp); |
|
131 |
obj.setDynValue("envelope", envelope); |
|
132 |
obj.setDynValue("crs", crs); |
|
133 |
|
|
134 |
return obj; |
|
135 |
} |
|
136 |
|
|
137 |
/** {@inheridDoc} */ |
|
138 |
public void loadFromState(PersistentState state) |
|
139 |
throws PersistenceException { |
|
140 |
id = state.getString("id"); |
|
141 |
name = state.getString("name"); |
|
142 |
description = state.getString("description"); |
|
143 |
version = state.getInt("version"); |
|
144 |
buildNumber = state.getInt("buildNumber"); |
|
145 |
creationTimestamp = state.getDate("creationTimestamp"); |
|
146 |
envelope = (Envelope) state.get("envelope"); |
|
147 |
crs = (IProjection) state.get("crs"); |
|
148 |
} |
|
149 |
|
|
150 |
protected void setId(String id) { |
|
151 |
this.id = id; |
|
152 |
} |
|
153 |
|
|
154 |
protected void setName(String name) { |
|
155 |
this.name = name; |
|
156 |
} |
|
157 |
|
|
158 |
protected void setDescription(String description) { |
|
159 |
this.description = description; |
|
160 |
} |
|
161 |
|
|
162 |
protected void setVersion(int version) { |
|
163 |
this.version = version; |
|
164 |
} |
|
165 |
|
|
166 |
protected void setBuildNumber(int buildNumber) { |
|
167 |
this.buildNumber = buildNumber; |
|
168 |
} |
|
169 |
|
|
170 |
protected void setCreationTimestamp(Date creationTimestamp) { |
|
171 |
this.creationTimestamp = creationTimestamp; |
|
172 |
} |
|
173 |
|
|
174 |
protected void setFullEnvelope(Envelope envelope) { |
|
175 |
try { |
|
176 |
this.envelope = (Envelope) envelope.clone(); |
|
177 |
} catch (CloneNotSupportedException e) { |
|
178 |
throw new IllegalArgumentException(e); |
|
179 |
} |
|
180 |
} |
|
181 |
|
|
182 |
protected void setCrs(IProjection crs) { |
|
183 |
this.crs = crs; |
|
184 |
} |
|
185 |
|
|
186 |
public DynStruct getDynDefinition() { |
|
187 |
return dynDefinition; |
|
188 |
} |
|
189 |
|
|
190 |
/** |
|
191 |
* Utility method to register {@link PortableViewInformation} definition in |
|
192 |
* the {@link PersistenceManager} |
|
193 |
*/ |
|
194 |
public static void registerPersistent() { |
|
195 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
|
196 |
DynStruct definition = |
|
197 |
manager.addDefinition(DefaultPortableViewInformation.class, |
|
198 |
PortableViewInformation.DYN_DEFINITION_NAME, |
|
199 |
"Portable View information", null, null); |
|
200 |
|
|
201 |
definition.addDynFieldString("id").setMandatory(true); |
|
202 |
definition.addDynFieldString("name").setMandatory(true); |
|
203 |
definition.addDynFieldString("description").setMandatory(true); |
|
204 |
definition.addDynFieldInt("version").setMandatory(true); |
|
205 |
definition.addDynFieldInt("buildNumber").setMandatory(true); |
|
206 |
definition.addDynFieldDate("creationTimestamp").setMandatory(true); |
|
207 |
definition.addDynFieldObject("envelope") |
|
208 |
.setClassOfValue(Envelope.class).setMandatory(true); |
|
209 |
definition.addDynFieldObject("crs").setClassOfValue(IProjection.class) |
|
210 |
.setMandatory(true); |
|
211 |
|
|
212 |
dynDefinition = definition; |
|
213 |
|
|
214 |
} |
|
215 |
|
|
216 |
} |
org.gvsig.educa.portableview/tags/org.gvsig.educa.portableview-1.0.210/org.gvsig.educa.portableview.lib/org.gvsig.educa.portableview.lib.impl/src/main/java/org/gvsig/educa/portableview/impl/map/DefaultPortableView.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.educa.portableview.impl.map; |
|
23 |
|
|
24 |
import java.io.File; |
|
25 |
import java.io.IOException; |
|
26 |
|
|
27 |
import org.slf4j.Logger; |
|
28 |
import org.slf4j.LoggerFactory; |
|
29 |
import org.gvsig.educa.portableview.impl.MapResoucesManager; |
|
30 |
import org.gvsig.educa.portableview.map.CantLoadContextException; |
|
31 |
import org.gvsig.educa.portableview.map.InvalidPortableViewFormatException; |
|
32 |
import org.gvsig.educa.portableview.map.PortableView; |
|
33 |
import org.gvsig.educa.portableview.map.PortableViewInformation; |
|
34 |
import org.gvsig.fmap.mapcontext.MapContext; |
|
35 |
import org.gvsig.tools.ToolsLocator; |
|
36 |
import org.gvsig.tools.dispose.DisposableManager; |
|
37 |
|
|
38 |
/** |
|
39 |
* <p> |
|
40 |
* Default {@link PortableView} implementation |
|
41 |
* </p> |
|
42 |
* <p> |
|
43 |
* Delegates open/close operation on the {@link MapResoucesManager} |
|
44 |
* </p> |
|
45 |
* |
|
46 |
* @author gvSIG Team |
|
47 |
* @version $Id$ |
|
48 |
* |
|
49 |
*/ |
|
50 |
public class DefaultPortableView implements PortableView { |
|
51 |
|
|
52 |
private static final Logger LOG = LoggerFactory |
|
53 |
.getLogger(DefaultPortableView.class); |
|
54 |
|
|
55 |
private final DisposableManager disposableManager; |
|
56 |
private final File file; |
|
57 |
private final PortableViewInformation info; |
|
58 |
private final MapResoucesManager resourceManager; |
|
59 |
private MapContext mapContext; |
|
60 |
|
|
61 |
public DefaultPortableView(File file, PortableViewInformation info, |
|
62 |
MapResoucesManager resourceManager) { |
|
63 |
this.file = file; |
|
64 |
this.info = info; |
|
65 |
this.resourceManager = resourceManager; |
|
66 |
disposableManager = ToolsLocator.getDisposableManager(); |
|
67 |
disposableManager.bind(this); |
|
68 |
resourceManager.addResource(this); |
|
69 |
} |
|
70 |
|
|
71 |
/** {@inheridDoc} */ |
|
72 |
public PortableViewInformation getInformation() { |
|
73 |
return info; |
|
74 |
} |
|
75 |
|
|
76 |
/** |
|
77 |
* Returns source map file |
|
78 |
* |
|
79 |
* @return |
|
80 |
*/ |
|
81 |
public File getSourceFile() { |
|
82 |
return file; |
|
83 |
} |
|
84 |
|
|
85 |
/** {@inheridDoc} */ |
|
86 |
public void open() throws IOException, InvalidPortableViewFormatException, |
|
87 |
CantLoadContextException { |
|
88 |
if (isOpen()) { |
|
89 |
throw new IllegalStateException("Map is already opened"); |
|
90 |
} |
|
91 |
mapContext = resourceManager.openMap(this); |
|
92 |
} |
|
93 |
|
|
94 |
/** {@inheridDoc} */ |
|
95 |
public boolean isOpen() { |
|
96 |
return resourceManager.isOpen(this); |
|
97 |
} |
|
98 |
|
|
99 |
/** {@inheridDoc} */ |
|
100 |
public void close() throws IOException { |
|
101 |
if (!isOpen()) { |
|
102 |
// Nothing to do |
|
103 |
return; |
|
104 |
} |
|
105 |
resourceManager.closeMap(this); |
|
106 |
mapContext.dispose(); |
|
107 |
mapContext = null; |
|
108 |
} |
|
109 |
|
|
110 |
/** {@inheridDoc} */ |
|
111 |
public MapContext getMapContext() { |
|
112 |
if (!isOpen()) { |
|
113 |
throw new IllegalStateException("Map not open"); |
|
114 |
} |
|
115 |
return mapContext; |
|
116 |
} |
|
117 |
|
|
118 |
/** {@inheridDoc} */ |
|
119 |
public String getSourceFilePath() { |
|
120 |
return file.getAbsolutePath(); |
|
121 |
} |
|
122 |
|
|
123 |
public void dispose() { |
|
124 |
disposableManager.release(this); |
|
125 |
try { |
|
126 |
close(); |
|
127 |
} catch (IOException e) { |
|
128 |
LOG.warn("Problem dispossing PortableView", e); |
|
129 |
} |
|
130 |
|
|
131 |
} |
|
132 |
} |
org.gvsig.educa.portableview/tags/org.gvsig.educa.portableview-1.0.210/org.gvsig.educa.portableview.lib/org.gvsig.educa.portableview.lib.impl/src/main/java/org/gvsig/educa/portableview/impl/map/PortableViewLoader.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.educa.portableview.impl.map; |
|
23 |
|
|
24 |
import java.io.File; |
|
25 |
import java.io.IOException; |
|
26 |
import java.io.InputStream; |
|
27 |
import java.util.Enumeration; |
|
28 |
import java.util.zip.ZipEntry; |
|
29 |
import java.util.zip.ZipException; |
|
30 |
import java.util.zip.ZipFile; |
|
31 |
|
|
32 |
import org.slf4j.Logger; |
|
33 |
import org.slf4j.LoggerFactory; |
|
34 |
import org.gvsig.educa.portableview.PortableViewFileServices; |
|
35 |
import org.gvsig.educa.portableview.PortableViewLocator; |
|
36 |
import org.gvsig.educa.portableview.impl.util.FileUtils; |
|
37 |
import org.gvsig.educa.portableview.impl.util.IOUtils; |
|
38 |
import org.gvsig.educa.portableview.map.CantLoadContextException; |
|
39 |
import org.gvsig.educa.portableview.map.InvalidInstalledPortableViewException; |
|
40 |
import org.gvsig.educa.portableview.map.InvalidPortableViewFormatException; |
|
41 |
import org.gvsig.educa.portableview.map.PortableViewInformation; |
|
42 |
import org.gvsig.fmap.mapcontext.MapContext; |
|
43 |
import org.gvsig.installer.lib.api.InstallerLocator; |
|
44 |
import org.gvsig.installer.lib.api.InstallerManager; |
|
45 |
import org.gvsig.installer.lib.api.PackageInfo; |
|
46 |
import org.gvsig.installer.lib.api.PackageInfoReader; |
|
47 |
import org.gvsig.tools.ToolsLocator; |
|
48 |
import org.gvsig.tools.persistence.PersistenceManager; |
|
49 |
import org.gvsig.tools.persistence.PersistentState; |
|
50 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
|
51 |
|
|
52 |
/** |
|
53 |
* Utility class which contain methods to load data from a PortableView file |
|
54 |
* |
|
55 |
* @author gvSIG Team |
|
56 |
* @version $Id$ |
|
57 |
* |
|
58 |
*/ |
|
59 |
public class PortableViewLoader { |
|
60 |
|
|
61 |
@SuppressWarnings("unused") |
|
62 |
private static final Logger LOG = LoggerFactory |
|
63 |
.getLogger(PortableViewLoader.class); |
|
64 |
|
|
65 |
private final InstallerManager installerManager; |
|
66 |
|
|
67 |
private final PackageInfoReader packageInfoReader; |
|
68 |
|
|
69 |
private final PortableViewFileServices fileServices; |
|
70 |
|
|
71 |
/** |
|
72 |
* |
|
73 |
*/ |
|
74 |
public PortableViewLoader() { |
|
75 |
installerManager = InstallerLocator.getInstallerManager(); |
|
76 |
packageInfoReader = installerManager.getDefaultPackageInfoReader(); |
|
77 |
fileServices = PortableViewLocator.getFileServices(); |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* Loads the mapContext definition form the deploy folder of a portable view |
|
82 |
* |
|
83 |
* @param mapFile |
|
84 |
* portable view file name (for problems messages) |
|
85 |
* @param deployFolder |
|
86 |
* portable view deploy folder |
|
87 |
* @return |
|
88 |
* @throws InvalidPortableViewFormatException |
|
89 |
* @throws CantLoadContextException |
|
90 |
*/ |
|
91 |
public MapContext loadMapContextFromMapDeployFolder(File mapFile, |
|
92 |
File deployFolder) throws InvalidPortableViewFormatException, |
|
93 |
CantLoadContextException { |
|
94 |
|
|
95 |
// Prepares file path |
|
96 |
File mapContextFile = |
|
97 |
new File(deployFolder, "mapContext/mapContext.xml"); |
|
98 |
|
|
99 |
// Check if file exists |
|
100 |
if (!mapContextFile.exists()) { |
|
101 |
throw new InvalidPortableViewFormatException(mapFile, |
|
102 |
"Missing mapContext/mapContext.xml file in deployed map: " |
|
103 |
.concat(mapContextFile.getAbsolutePath())); |
|
104 |
} |
|
105 |
if (!mapContextFile.isFile()) { |
|
106 |
throw new InvalidPortableViewFormatException(mapFile, |
|
107 |
"mapContext/mapContext.xml file in deployed map is not a file: " |
|
108 |
.concat(mapContextFile.getAbsolutePath())); |
|
109 |
} |
|
110 |
|
|
111 |
// Load map context |
|
112 |
MapContext mapContext; |
|
113 |
try { |
|
114 |
|
|
115 |
mapContext = |
|
116 |
fileServices.loadMapContext(mapContextFile, deployFolder); |
|
117 |
|
|
118 |
if (mapContext == null) { |
|
119 |
throw new CantLoadContextException(mapFile, mapContextFile, |
|
120 |
"mapContext not loaded", null); |
|
121 |
} |
|
122 |
|
|
123 |
return mapContext; |
|
124 |
} catch (Exception e) { |
|
125 |
throw new CantLoadContextException(mapFile, mapContextFile, e); |
|
126 |
} |
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* Gets information from a portableViewFile without uncompress it |
|
131 |
* |
|
132 |
* @param portableViewfile |
|
133 |
* @return |
|
134 |
* @throws InvalidPortableViewFormatException |
|
135 |
*/ |
|
136 |
public PortableViewInformation getInformationFromMapFile(File portableViewfile) |
|
137 |
throws InvalidPortableViewFormatException { |
|
138 |
|
|
139 |
PersistenceManager persistenceManager = |
|
140 |
ToolsLocator.getPersistenceManager(); |
|
141 |
|
|
142 |
// Try to open file |
|
143 |
ZipFile zipFile; |
|
144 |
try { |
|
145 |
zipFile = new ZipFile(portableViewfile); |
|
146 |
} catch (ZipException e1) { |
|
147 |
throw new InvalidPortableViewFormatException(portableViewfile, |
|
148 |
"Zip format problem", e1); |
|
149 |
} catch (IOException e1) { |
|
150 |
throw new InvalidPortableViewFormatException(portableViewfile, e1); |
|
151 |
} |
|
152 |
|
|
153 |
PersistentState pState = null; |
|
154 |
try { |
|
155 |
// gets zip file entries |
|
156 |
Enumeration<? extends ZipEntry> entries = zipFile.entries(); |
|
157 |
ZipEntry entry; |
|
158 |
|
|
159 |
// look for MapInfo entry |
|
160 |
while (entries.hasMoreElements()) { |
|
161 |
entry = entries.nextElement(); |
|
162 |
if (!entry.getName().equals("MapInfo.xml")) { |
|
163 |
continue; |
|
164 |
} |
|
165 |
|
|
166 |
// Check it's a file |
|
167 |
if (entry.isDirectory()) { |
|
168 |
throw new InvalidPortableViewFormatException( |
|
169 |
portableViewfile, "MapInfo.xml is a directory"); |
|
170 |
} |
|
171 |
|
|
172 |
// Load file into a persistence state |
|
173 |
InputStream is = null; |
|
174 |
try { |
|
175 |
try { |
|
176 |
is = zipFile.getInputStream(entry); |
|
177 |
pState = persistenceManager.loadState(is); |
|
178 |
} catch (IOException e) { |
|
179 |
throw new InvalidPortableViewFormatException( |
|
180 |
portableViewfile, "problem opening MapInfo.xml", e); |
|
181 |
} catch (PersistenceException e) { |
|
182 |
throw new InvalidPortableViewFormatException( |
|
183 |
portableViewfile, "problem loading MapInfo.xml", e); |
|
184 |
} |
|
185 |
} finally { |
|
186 |
IOUtils.closeQuietly(is); |
|
187 |
} |
|
188 |
} |
|
189 |
|
|
190 |
} finally { |
|
191 |
// Close zip file |
|
192 |
IOUtils.closeQuietly(zipFile); |
|
193 |
} |
|
194 |
|
|
195 |
// If MapInfo file not found |
|
196 |
if (pState == null) { |
|
197 |
throw new IllegalArgumentException("Missing MapInfo.xml"); |
|
198 |
} |
|
199 |
|
|
200 |
// Load data form persistence state |
|
201 |
DefaultPortableViewInformation info = |
|
202 |
new DefaultPortableViewInformation(); |
|
203 |
try { |
|
204 |
info.loadFromState(pState); |
|
205 |
} catch (PersistenceException e) { |
|
206 |
throw new IllegalArgumentException("Invalid MapInfo.xml format", e); |
|
207 |
} |
|
208 |
return info; |
|
209 |
|
|
210 |
} |
|
211 |
|
|
212 |
public PackageInfo getPackageInfoFromInstalledPortableView(File folder) |
|
213 |
throws InvalidInstalledPortableViewException { |
|
214 |
// Check for package.info file |
|
215 |
File packageInfoFile = new File(folder, "package.info"); |
|
216 |
if (!FileUtils.isReadableFile(packageInfoFile)) { |
|
217 |
return null; |
|
218 |
} |
|
219 |
|
|
220 |
PackageInfo packageInfo = installerManager.createPackageInfo(); |
|
221 |
InputStream in = null; |
|
222 |
try { |
|
223 |
in = FileUtils.openInputStream(packageInfoFile); |
|
224 |
packageInfoReader.read(packageInfo, in); |
|
225 |
} catch (Exception ex) { |
|
226 |
throw new InvalidInstalledPortableViewException(folder, ex); |
|
227 |
} finally { |
|
228 |
IOUtils.closeQuietly(in); |
|
229 |
} |
|
230 |
return packageInfo; |
|
231 |
} |
|
232 |
} |
org.gvsig.educa.portableview/tags/org.gvsig.educa.portableview-1.0.210/org.gvsig.educa.portableview.lib/org.gvsig.educa.portableview.lib.impl/src/main/java/org/gvsig/educa/portableview/impl/util/FileUtils.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.educa.portableview.impl.util; |
|
23 |
|
|
24 |
import java.io.File; |
|
25 |
import java.io.IOException; |
|
26 |
import java.io.InputStream; |
|
27 |
import java.io.OutputStream; |
|
28 |
import java.util.Collection; |
|
29 |
import java.util.Enumeration; |
|
30 |
import java.util.zip.ZipEntry; |
|
31 |
import java.util.zip.ZipException; |
|
32 |
import java.util.zip.ZipFile; |
|
33 |
import java.util.zip.ZipOutputStream; |
|
34 |
|
|
35 |
import org.apache.commons.io.DirectoryWalker; |
|
36 |
import org.apache.commons.lang3.StringUtils; |
|
37 |
import org.slf4j.Logger; |
|
38 |
import org.slf4j.LoggerFactory; |
|
39 |
|
|
40 |
/** |
|
41 |
* <p> |
|
42 |
* File utilities. Extends the provides by <i>apache-common-IO</i> |
|
43 |
* </p> |
|
44 |
* |
|
45 |
* @author gvSIG Team |
|
46 |
* @version $Id$ |
|
47 |
* |
|
48 |
*/ |
|
49 |
public abstract class FileUtils extends org.apache.commons.io.FileUtils { |
|
50 |
|
|
51 |
private static final Logger LOG = LoggerFactory.getLogger(FileUtils.class); |
|
52 |
|
|
53 |
public static final boolean UNIXLIKE_FILESYSTEM = File.separatorChar == '/'; |
|
54 |
|
|
55 |
/** |
|
56 |
* <p> |
|
57 |
* Unzip a file into a folder |
|
58 |
* </p> |
|
59 |
* |
|
60 |
* @param zipFile |
|
61 |
* @param outputFloder |
|
62 |
* must exists. WARNING: <b>Contents will be removed before |
|
63 |
* unzip</b> |
|
64 |
* @throws IOException |
|
65 |
* @throws ZipException |
|
66 |
* @throws IllegalArgumentException |
|
67 |
* if zipFile is not a file or outputFloder doesn't exists or is |
|
68 |
* read-only |
|
69 |
*/ |
|
70 |
public static void unzipFile(File zipFile, File outputFloder) |
|
71 |
throws ZipException, IOException { |
|
72 |
|
|
73 |
if (LOG.isDebugEnabled()) { |
|
74 |
LOG.debug( |
|
75 |
"Unzip {} --> {}", |
|
76 |
new Object[] { zipFile.getAbsolutePath(), |
|
77 |
outputFloder.getAbsolutePath() }); |
|
78 |
} |
|
79 |
|
|
80 |
// basic checks input file |
|
81 |
if (!zipFile.exists()) { |
|
82 |
throw new IllegalArgumentException("File '".concat( |
|
83 |
zipFile.getAbsolutePath()).concat("' no exists")); |
|
84 |
} |
|
85 |
if (!zipFile.isFile()) { |
|
86 |
throw new IllegalArgumentException("'".concat( |
|
87 |
zipFile.getAbsolutePath()).concat("' is not a file")); |
|
88 |
} |
|
89 |
if (!zipFile.canRead()) { |
|
90 |
throw new IllegalArgumentException("File '".concat( |
|
91 |
zipFile.getAbsolutePath()).concat("' can not be readed")); |
|
92 |
} |
|
93 |
|
|
94 |
// basic checks output file |
|
95 |
if (!outputFloder.exists()) { |
|
96 |
throw new IllegalArgumentException("Folder '".concat( |
|
97 |
outputFloder.getAbsolutePath()).concat("' not exists.")); |
|
98 |
} |
|
99 |
if (!outputFloder.isDirectory()) { |
|
100 |
throw new IllegalArgumentException("'".concat( |
|
101 |
outputFloder.getAbsolutePath()) |
|
102 |
.concat("' is not a folder.")); |
|
103 |
} |
|
104 |
if (!outputFloder.canWrite()) { |
|
105 |
throw new IllegalArgumentException("Can't write on'".concat( |
|
106 |
outputFloder.getAbsolutePath()).concat("' folder.")); |
|
107 |
} |
|
108 |
|
|
109 |
if (LOG.isTraceEnabled()) { |
|
110 |
LOG.trace("cleaning directory {}", outputFloder.getAbsolutePath()); |
|
111 |
} |
|
112 |
|
|
113 |
cleanDirectory(outputFloder); |
|
114 |
|
|
115 |
// Extract file |
|
116 |
ZipFile openedZipFile = null; |
|
117 |
try { |
|
118 |
openedZipFile = new ZipFile(zipFile); |
|
119 |
Enumeration<? extends ZipEntry> entries = openedZipFile.entries(); |
|
120 |
ZipEntry entry; |
|
121 |
File targetFile; |
|
122 |
// iterate over entries |
|
123 |
while (entries.hasMoreElements()) { |
|
124 |
entry = entries.nextElement(); |
|
125 |
|
|
126 |
// compound target path |
|
127 |
targetFile = new File(outputFloder, entry.getName()); |
|
128 |
|
|
129 |
if (entry.isDirectory()) { |
|
130 |
// Create folder |
|
131 |
if (LOG.isTraceEnabled()) { |
|
132 |
LOG.trace("creating folder {}", |
|
133 |
targetFile.getAbsolutePath()); |
|
134 |
} |
|
135 |
|
|
136 |
if (!targetFile.mkdirs()) { |
|
137 |
throw new IOException("Can't create '".concat( |
|
138 |
targetFile.getAbsolutePath()).concat( |
|
139 |
"' folder.")); |
|
140 |
} |
|
141 |
} else { |
|
142 |
// extract file from zip |
|
143 |
if (LOG.isTraceEnabled()) { |
|
144 |
LOG.trace("extracting file {}", |
|
145 |
targetFile.getAbsolutePath()); |
|
146 |
} |
|
147 |
extractZipEntry(openedZipFile, entry, targetFile); |
|
148 |
} |
|
149 |
} |
|
150 |
|
|
151 |
} finally { |
|
152 |
IOUtils.closeQuietly(openedZipFile); |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
/** |
|
157 |
* Extract a zipEntry into a file |
|
158 |
* |
|
159 |
* @param zipFile |
|
160 |
* @param entry |
|
161 |
* @param target |
|
162 |
* (if exists will be overwrite) |
|
163 |
* @throws IOException |
|
164 |
*/ |
|
165 |
private static void extractZipEntry(ZipFile zipFile, ZipEntry entry, |
|
166 |
File target) throws IOException { |
|
167 |
InputStream is = null; |
|
168 |
OutputStream os = null; |
|
169 |
try { |
|
170 |
is = zipFile.getInputStream(entry); |
|
171 |
os = openOutputStream(target); |
|
172 |
IOUtils.copy(is, os); |
|
173 |
} finally { |
|
174 |
IOUtils.closeQuietly(is); |
|
175 |
IOUtils.closeQuietly(os); |
|
176 |
} |
|
177 |
} |
|
178 |
|
|
179 |
/** |
|
180 |
* <p> |
|
181 |
* Create a zip file which will contains all contents of a folder |
|
182 |
* </p> |
|
183 |
* <p> |
|
184 |
* Files will be use relative path to <code>rootFolder</code> |
|
185 |
* </p> |
|
186 |
* |
|
187 |
* @param rootFolder |
|
188 |
* rootFolder (must be an existing folder) |
|
189 |
* @param targetFile |
|
190 |
* final file (must not exists) |
|
191 |
* @throws IOException |
|
192 |
* @throws IllegalArgumentException |
|
193 |
* if there are any problem in parameters |
|
194 |
*/ |
|
195 |
public static void zipFolder(File rootFolder, File targetFile) |
|
196 |
throws IOException { |
|
197 |
if (LOG.isDebugEnabled()) { |
|
198 |
LOG.debug( |
|
199 |
"zip {} --> {}", |
|
200 |
new Object[] { rootFolder.getAbsolutePath(), |
|
201 |
targetFile.getAbsolutePath() }); |
|
202 |
} |
|
203 |
|
|
204 |
if (!isReadableFolder(rootFolder)) { |
|
205 |
throw new IllegalArgumentException("Invalid folder"); |
|
206 |
} |
|
207 |
if (targetFile.exists()) { |
|
208 |
throw new IllegalArgumentException("Target file already exists"); |
|
209 |
} |
|
210 |
if (!isWritableFolder(targetFile.getParentFile())) { |
|
211 |
throw new IllegalArgumentException("Can't write in target folder"); |
|
212 |
} |
|
213 |
RelativePathFolderZipper zipper = new RelativePathFolderZipper( |
|
214 |
rootFolder, targetFile); |
|
215 |
|
|
216 |
zipper.zip(); |
|
217 |
|
|
218 |
} |
|
219 |
|
|
220 |
/** |
|
221 |
* Informs if <code>folder</code> is a writable folder( exists & isDir & |
|
222 |
* canWrite) |
|
223 |
* |
|
224 |
* @param folder |
|
225 |
* @return |
|
226 |
*/ |
|
227 |
public static boolean isWritableFolder(File folder) { |
|
228 |
return folder.exists() && folder.isDirectory() && folder.canWrite(); |
|
229 |
} |
|
230 |
|
|
231 |
/** |
|
232 |
* Informs if <code>folder</code> is a readable folder( exists & isDir & |
|
233 |
* canRead) |
|
234 |
* |
|
235 |
* @param folder |
|
236 |
* @return |
|
237 |
*/ |
|
238 |
public static boolean isReadableFolder(File folder) { |
|
239 |
return folder.exists() && folder.isDirectory() && folder.canRead(); |
|
240 |
} |
|
241 |
|
|
242 |
/** |
|
243 |
* Informs if <code>file</code> is a readable file ( exists & isFile & |
|
244 |
* canRead) |
|
245 |
* |
|
246 |
* @param folder |
|
247 |
* @return |
|
248 |
*/ |
|
249 |
public static boolean isReadableFile(File folder) { |
|
250 |
return folder.exists() && folder.isFile() && folder.canRead(); |
|
251 |
} |
|
252 |
|
|
253 |
/** |
|
254 |
* <p> |
|
255 |
* Look for a folder on a <code>baseFolder</code> starts with |
|
256 |
* <code>baseName</code> which is writable. |
|
257 |
* </p> |
|
258 |
* <p> |
|
259 |
* The pattern is: <code>baseFolder/baseName.0000</code> where |
|
260 |
* <code>000</code> it's a counter. |
|
261 |
* </p> |
|
262 |
* |
|
263 |
* @param baseFolder |
|
264 |
* existing and writable folder |
|
265 |
* @param baseName |
|
266 |
* start of new folder |
|
267 |
* @return new folder |
|
268 |
* @throws IllegalArgumentException |
|
269 |
* if base folder is not a writable folder |
|
270 |
* @throws IllegalStateException |
|
271 |
* if can't found a suitable folder |
|
272 |
*/ |
|
273 |
public static File getWritableFolder(File baseFolder, String baseName) { |
|
274 |
if (!isWritableFolder(baseFolder)) { |
|
275 |
throw new IllegalArgumentException( |
|
276 |
"base folder is not a Writable folder"); |
|
277 |
} |
|
278 |
File folder = new File(baseFolder, baseName); |
|
279 |
|
|
280 |
int count = 0; |
|
281 |
String countStr; |
|
282 |
|
|
283 |
// while Exists but is not a writable folder |
|
284 |
while (folder.exists() && !isWritableFolder(folder)) { |
|
285 |
countStr = String.valueOf(count); |
|
286 |
if (countStr.length() > 4) { |
|
287 |
throw new IllegalStateException( |
|
288 |
" > than 9999 non-selectable folders"); |
|
289 |
} |
|
290 |
countStr = StringUtils.right("000".concat(countStr), 3); |
|
291 |
|
|
292 |
// Generate a new folder name based on 'baseTemp' and the counter |
|
293 |
folder = new File(baseFolder, baseName.concat(".").concat(countStr)); |
|
294 |
count++; |
|
295 |
} |
|
296 |
// Create folder if not exists |
|
297 |
if (!folder.exists()) { |
|
298 |
folder.mkdirs(); |
|
299 |
} |
|
300 |
return folder; |
|
301 |
} |
|
302 |
|
|
303 |
/** |
|
304 |
* <p> |
|
305 |
* Create a new folder on a <code>baseFolder</code> starts with |
|
306 |
* <code>baseName</code> which is new and writable. |
|
307 |
* </p> |
|
308 |
* <p> |
|
309 |
* The pattern is: <code>baseFolder/baseName.0000</code> where |
|
310 |
* <code>000</code> it's a counter. |
|
311 |
* </p> |
|
312 |
* |
|
313 |
* @param baseFolder |
|
314 |
* existing and writable folder |
|
315 |
* @param baseName |
|
316 |
* start of new folder |
|
317 |
* @return new folder |
|
318 |
* @throws IllegalArgumentException |
|
319 |
* if base folder is not a writable folder |
|
320 |
* @throws IllegalStateException |
|
321 |
* if can't found a suitable folder |
|
322 |
*/ |
|
323 |
public static File getNewWritableFolder(File baseFolder, String baseName) { |
|
324 |
if (!isWritableFolder(baseFolder)) { |
|
325 |
throw new IllegalArgumentException( |
|
326 |
"base folder is not a Writable folder: ".concat(baseFolder |
|
327 |
.getAbsolutePath())); |
|
328 |
} |
|
329 |
File folder = new File(baseFolder, baseName); |
|
330 |
|
|
331 |
int count = 0; |
|
332 |
String countStr; |
|
333 |
|
|
334 |
// while Exists but is not a writable folder |
|
335 |
while (folder.exists() && !isWritableFolder(folder)) { |
|
336 |
countStr = String.valueOf(count); |
|
337 |
if (countStr.length() > 4) { |
|
338 |
throw new IllegalStateException( |
|
339 |
" > than 999 non-selectable folders"); |
|
340 |
} |
|
341 |
countStr = StringUtils.right("000".concat(countStr), 3); |
|
342 |
|
|
343 |
// Generate a new folder name based on 'baseTemp' and the counter |
|
344 |
folder = new File(baseFolder, baseName.concat(".").concat(countStr)); |
|
345 |
count++; |
|
346 |
} |
|
347 |
// Create folder if not exists |
|
348 |
if (!folder.exists()) { |
|
349 |
folder.mkdirs(); |
|
350 |
} |
|
351 |
return folder; |
|
352 |
} |
|
353 |
|
|
354 |
/** |
|
355 |
* <p> |
|
356 |
* Create {@link File} instance for a new file on a <code>baseFolder</code> |
|
357 |
* starts with <code>baseName</code>. |
|
358 |
* </p> |
|
359 |
* <p> |
|
360 |
* The pattern is: <code>baseFolder/baseName.0000.ext</code> where |
|
361 |
* <code>000</code> it's a counter. |
|
362 |
* </p> |
|
363 |
* <p> |
|
364 |
* This <b>no create</b> the file into the file system. |
|
365 |
* </p> |
|
366 |
* |
|
367 |
* @param baseFolder |
|
368 |
* existing and writable folder |
|
369 |
* @param name |
|
370 |
* start of new file |
|
371 |
* @return new folder |
|
372 |
* @throws IllegalArgumentException |
|
373 |
* if base folder is not a writable folder |
|
374 |
* @throws IllegalStateException |
|
375 |
* if can't found a suitable folder |
|
376 |
*/ |
|
377 |
public static File getNewFileName(File baseFolder, String name) { |
|
378 |
if (!isWritableFolder(baseFolder)) { |
|
379 |
throw new IllegalArgumentException( |
|
380 |
"base folder is not a Writable folder"); |
|
381 |
} |
|
382 |
|
|
383 |
// Extract base |
|
384 |
String baseName = org.apache.commons.io.FilenameUtils.getBaseName(name); |
|
385 |
String extension = org.apache.commons.io.FilenameUtils |
|
386 |
.getExtension(name); |
|
387 |
|
|
388 |
File newFile = new File(baseFolder, name); |
|
389 |
|
|
390 |
int count = 0; |
|
391 |
String countStr; |
|
392 |
|
|
393 |
// while Exists but is not a writable folder |
|
394 |
while (newFile.exists()) { |
|
395 |
countStr = String.valueOf(count); |
|
396 |
if (countStr.length() > 4) { |
|
397 |
throw new IllegalStateException( |
|
398 |
" > than 999 non-selectable folders"); |
|
399 |
} |
|
400 |
countStr = StringUtils.right("000".concat(countStr), 3); |
|
401 |
|
|
402 |
// Generate a new file name based on 'basename', the counter and the |
|
403 |
// extension of original name |
|
404 |
newFile = new File(baseFolder, baseName.concat(".") |
|
405 |
.concat(countStr).concat(extension)); |
|
406 |
count++; |
|
407 |
} |
|
408 |
return newFile; |
|
409 |
} |
|
410 |
|
|
411 |
/** |
|
412 |
* <p> |
|
413 |
* Return <code>path</code> as relative from <code>basePath</code> only if |
|
414 |
* <code>basePath</code> is an ancestor of <code>path</code>: |
|
415 |
* </p> |
|
416 |
* |
|
417 |
* @param path |
|
418 |
* @param basePath |
|
419 |
* @return |
|
420 |
* @see FilenameUtils#getRelativeTo(String, String) |
|
421 |
*/ |
|
422 |
public static File getRelativeTo(File path, File basePath) { |
|
423 |
String basePathAbs = basePath.getAbsolutePath(); |
|
424 |
if (basePath.isDirectory()) { |
|
425 |
basePathAbs = basePathAbs + File.separatorChar; |
|
426 |
} |
|
427 |
return new File(FilenameUtils.getRelativeTo(path.getAbsolutePath(), |
|
428 |
basePathAbs)); |
|
429 |
} |
|
430 |
|
|
431 |
/** |
|
432 |
* <p> |
|
433 |
* This class generates a Zip file with the context of a folder. All entries |
|
434 |
* in generated zip will be relatives to source folder. |
|
435 |
* </p> |
|
436 |
* |
|
437 |
* @author gvSIG Team |
|
438 |
* @version $Id$ |
|
439 |
* |
|
440 |
*/ |
|
441 |
private static class RelativePathFolderZipper extends DirectoryWalker { |
|
442 |
|
|
443 |
private ZipOutputStream targetZip = null; |
|
444 |
private final File targetFile; |
|
445 |
private final File rootFolder; |
|
446 |
private final String rootFolderPath; |
|
447 |
|
|
448 |
/** |
|
449 |
* Prepares compression process |
|
450 |
* |
|
451 |
* @param rootFolder |
|
452 |
* folder to compress |
|
453 |
* @param targetFile |
|
454 |
* file to generate |
|
455 |
*/ |
|
456 |
public RelativePathFolderZipper(File rootFolder, File targetFile) { |
|
457 |
super(); |
|
458 |
this.rootFolder = rootFolder; |
|
459 |
this.targetFile = targetFile; |
|
460 |
this.rootFolderPath = rootFolder.getAbsolutePath() |
|
461 |
+ File.separatorChar; |
|
462 |
|
|
463 |
} |
|
464 |
|
|
465 |
/** |
|
466 |
* Execute compression process |
|
467 |
* |
|
468 |
* @throws IOException |
|
469 |
*/ |
|
470 |
public synchronized void zip() throws IOException { |
|
471 |
try { |
|
472 |
targetZip = new ZipOutputStream(openOutputStream(targetFile)); |
|
473 |
walk(rootFolder, null); |
|
474 |
} finally { |
|
475 |
IOUtils.closeQuietly(targetZip); |
|
476 |
targetZip = null; |
|
477 |
} |
|
478 |
} |
|
479 |
|
|
480 |
/** {@inheridDoc} */ |
|
481 |
@Override |
|
482 |
protected void handleFile(File file, int depth, |
|
483 |
@SuppressWarnings("rawtypes") Collection results) |
|
484 |
throws IOException { |
|
485 |
|
|
486 |
// Get path relative to root |
|
487 |
String relativePath = FilenameUtils.getRelativeTo( |
|
488 |
file.getAbsolutePath(), rootFolderPath); |
|
489 |
if (!UNIXLIKE_FILESYSTEM) { |
|
490 |
// Avoid problems about unzip in Linux when files was generated |
|
491 |
// on a windows |
|
492 |
relativePath = FilenameUtils.separatorsToUnix(relativePath); |
|
493 |
} |
|
494 |
|
|
495 |
InputStream in = null; |
|
496 |
try { |
|
497 |
if (LOG.isTraceEnabled()) { |
|
498 |
LOG.trace("adding file {} --> {}", file.getAbsolutePath(), |
|
499 |
relativePath); |
|
500 |
} |
|
501 |
targetZip.putNextEntry(new ZipEntry(relativePath)); |
|
502 |
|
|
503 |
in = openInputStream(file); |
|
504 |
IOUtils.copy(in, targetZip); |
|
505 |
targetZip.closeEntry(); |
|
506 |
} finally { |
|
507 |
IOUtils.closeQuietly(in); |
|
508 |
} |
|
509 |
} |
|
510 |
} |
|
511 |
} |
org.gvsig.educa.portableview/tags/org.gvsig.educa.portableview-1.0.210/org.gvsig.educa.portableview.lib/org.gvsig.educa.portableview.lib.impl/src/main/java/org/gvsig/educa/portableview/impl/util/FilenameUtils.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.educa.portableview.impl.util; |
|
23 |
|
|
24 |
/** |
|
25 |
* <p> |
|
26 |
* File name management utilities. Extends the provides by |
|
27 |
* <i>apache-common-IO</i> |
|
28 |
* </p> |
|
29 |
* |
|
30 |
* @author gvSIG Team |
|
31 |
* @version $Id$ |
|
32 |
* |
|
33 |
*/ |
|
34 |
public abstract class FilenameUtils extends org.apache.commons.io.FilenameUtils { |
|
35 |
|
|
36 |
/** |
|
37 |
* <p> |
|
38 |
* Return <code>path</code> as relative from <code>basePath</code> only if |
|
39 |
* <code>basePath</code> is an ancestor of <code>path</code>: |
|
40 |
* </p> |
|
41 |
* |
|
42 |
* <pre> |
|
43 |
* path basePath returns |
|
44 |
* ---------- ------------- ---------- |
|
45 |
* /home/user/x.txt /home/ user/x.txt |
|
46 |
* /home/user/x.txt /home/user/ x.txt |
|
47 |
* ~/x.txt /home/ user/x.t |
|
48 |
* /tmp/x.txt /home/ /tmp/x.txt |
|
49 |
* C:\foo\bar\xx.txt C: foo\bar\xx.txt |
|
50 |
* C:\foo\bar\xx.txt C:\foo\ bar\xx.txt |
|
51 |
* C:\foo\bar\xx.txt C:\foo\bar\ xx.txt |
|
52 |
* </pre> |
|
53 |
* |
|
54 |
* @param path |
|
55 |
* @param basePath |
|
56 |
* @return |
|
57 |
*/ |
|
58 |
public static String getRelativeTo(String path, String basePath) { |
|
59 |
if ((basePath.length() == 0) || (path.length() == 0)) { |
|
60 |
return path; |
|
61 |
} |
|
62 |
String absBasePath = getFullPath(basePath); |
|
63 |
String absPath = getFullPath(path); |
|
64 |
String name = getName(path); |
|
65 |
|
|
66 |
if (equals(absPath, absBasePath)) { |
|
67 |
return name; |
|
68 |
} |
|
69 |
if (absPath.startsWith(absBasePath)) { |
|
70 |
return getWithoutPrefix(concat( |
|
71 |
absPath.substring(absBasePath.length()), name)); |
|
72 |
} |
|
73 |
return path; |
|
74 |
} |
|
75 |
|
|
76 |
/** |
|
77 |
* <p> |
|
78 |
* Return <code>path</code> as relative from <code>basePath</code> only if |
|
79 |
* <code>basePath</code> is an ancestor of <code>path</code>: |
|
80 |
* </p> |
|
81 |
* |
|
82 |
* <pre> |
|
83 |
* path returns |
|
84 |
* ---------- ------------- |
|
85 |
* /home/user/x.txt home/user/x.txt |
|
86 |
* ~/x.txt x.txt |
|
87 |
* /tmp/x.txt tmp/x.txt |
|
88 |
* C:/foo/bar/xx.txt foo/bar/xx.txt |
|
89 |
* </pre> |
|
90 |
* |
|
91 |
* @param path |
|
92 |
* @return |
|
93 |
*/ |
|
94 |
public static String getWithoutPrefix(String path) { |
|
95 |
return path.substring(getPrefixLength(path)); |
|
96 |
} |
|
97 |
|
|
98 |
} |
org.gvsig.educa.portableview/tags/org.gvsig.educa.portableview-1.0.210/org.gvsig.educa.portableview.lib/org.gvsig.educa.portableview.lib.impl/src/main/java/org/gvsig/educa/portableview/impl/util/PersistenceUtils.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU General Public License |
|
8 |
* as published by the Free Software Foundation; either version 2 |
|
9 |
* of the License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
* GNU General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
|
19 |
* MA 02110-1301, USA. |
|
20 |
* |
|
21 |
*/ |
|
22 |
package org.gvsig.educa.portableview.impl.util; |
|
23 |
|
|
24 |
import java.io.File; |
|
25 |
import java.net.MalformedURLException; |
|
26 |
import java.net.URI; |
|
27 |
import java.net.URISyntaxException; |
|
28 |
import java.net.URL; |
|
29 |
import java.util.HashSet; |
|
30 |
import java.util.Iterator; |
|
31 |
import java.util.Map; |
|
32 |
import java.util.Set; |
|
33 |
|
|
34 |
import org.apache.commons.lang3.StringUtils; |
|
35 |
import org.gvsig.tools.dynobject.DynField; |
|
36 |
import org.gvsig.tools.dynobject.DynStruct; |
|
37 |
import org.gvsig.tools.persistence.PersistentContext; |
Also available in: Unified diff