svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / DefaultFeatureFormDefinitionsProvider.java @ 42511
History | View | Annotate | Download (7.83 KB)
1 |
package org.gvsig.app.extension; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileInputStream; |
5 |
import java.io.FileNotFoundException; |
6 |
import java.io.IOException; |
7 |
import java.io.InputStream; |
8 |
import java.nio.charset.Charset; |
9 |
import java.util.ArrayList; |
10 |
import java.util.Collections; |
11 |
import java.util.Iterator; |
12 |
import java.util.List; |
13 |
import java.util.Map; |
14 |
import java.util.zip.CRC32; |
15 |
import org.apache.commons.io.FileUtils; |
16 |
import org.apache.commons.io.IOUtils; |
17 |
import org.gvsig.andami.PluginServices; |
18 |
import org.gvsig.andami.PluginsLocator; |
19 |
import org.gvsig.andami.PluginsManager; |
20 |
import org.gvsig.featureform.swing.FeatureFormDefinitionsProvider; |
21 |
import org.gvsig.featureform.swing.FeatureFormDefinitionsProvider.Entry; |
22 |
import org.gvsig.fmap.dal.exception.DataException; |
23 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
24 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
25 |
import org.gvsig.fmap.dal.feature.FeatureType; |
26 |
import org.gvsig.tools.ToolsLocator; |
27 |
import org.gvsig.tools.dynobject.DynClass; |
28 |
import org.gvsig.tools.dynobject.DynObjectManager; |
29 |
import org.slf4j.Logger; |
30 |
import org.slf4j.LoggerFactory; |
31 |
|
32 |
public class DefaultFeatureFormDefinitionsProvider implements FeatureFormDefinitionsProvider { |
33 |
|
34 |
private class DefaultEntry implements Entry { |
35 |
|
36 |
private String key; |
37 |
private DynClass dynClass;
|
38 |
private String xml; |
39 |
|
40 |
public DefaultEntry(String key, DynClass dynClass, String xml) { |
41 |
this.key = key;
|
42 |
this.dynClass = dynClass;
|
43 |
this.xml = xml;
|
44 |
} |
45 |
|
46 |
public DefaultEntry(String key, DynClass dynClass) { |
47 |
this(key, dynClass, null); |
48 |
} |
49 |
|
50 |
public DefaultEntry(FeatureStore store) throws DataException { |
51 |
this(null, store.getDefaultFeatureType(), null); |
52 |
this.key = getFilename(store);
|
53 |
} |
54 |
|
55 |
@Override
|
56 |
public DynClass getDynClass() {
|
57 |
return dynClass;
|
58 |
} |
59 |
|
60 |
@Override
|
61 |
public String getKey() { |
62 |
return key;
|
63 |
} |
64 |
|
65 |
@Override
|
66 |
public String getXml() { |
67 |
if (this.xml == null) { |
68 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
69 |
this.xml = dynObjectManager.exportSimpleDynClassDefinitions(this.getDynClass()); |
70 |
} |
71 |
return this.xml; |
72 |
} |
73 |
|
74 |
public boolean setXml(String xml) { |
75 |
if (xml == null) { |
76 |
this.xml = null; |
77 |
return true; |
78 |
} |
79 |
Map classes;
|
80 |
DynClass dynClass; |
81 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
82 |
try {
|
83 |
InputStream is = IOUtils.toInputStream(xml, Charset.forName("UTF-8")); |
84 |
classes = dynObjectManager.importDynClassDefinitions(is, this.getClass().getClassLoader());
|
85 |
} catch (Exception ex) { |
86 |
logger.warn("Can't parse xml definition.", ex);
|
87 |
return false; |
88 |
} |
89 |
if (!classes.containsKey(this.getDynClass().getName())) { |
90 |
logger.warn("Can't load definition of '" + this.getDynClass().getName() + "' from xml."); |
91 |
return false; |
92 |
} |
93 |
this.xml = xml;
|
94 |
return true; |
95 |
} |
96 |
|
97 |
public void setDynClass(DynClass dynClass) { |
98 |
this.dynClass = dynClass;
|
99 |
} |
100 |
|
101 |
@Override
|
102 |
public int compareTo(Entry t) { |
103 |
return this.getKey().compareTo(t.getKey()); |
104 |
} |
105 |
|
106 |
} |
107 |
|
108 |
private static final Logger logger = LoggerFactory.getLogger(DefaultFeatureFormDefinitionsProvider.class); |
109 |
|
110 |
private File definitionsFolder = null; |
111 |
|
112 |
public File getDefinitionsFolder() throws IOException { |
113 |
if (this.definitionsFolder == null) { |
114 |
PluginsManager pluginManager = PluginsLocator.getManager(); |
115 |
PluginServices plugin = pluginManager.getPlugin(CreateDefaultFormDefinitionExtension.class); |
116 |
File homeFolder = plugin.getPluginHomeFolder();
|
117 |
File definitionsFolder = new File(homeFolder, "forms"); |
118 |
if (!definitionsFolder.exists()) {
|
119 |
FileUtils.forceMkdir(definitionsFolder); |
120 |
} |
121 |
this.definitionsFolder = definitionsFolder;
|
122 |
} |
123 |
return this.definitionsFolder; |
124 |
} |
125 |
|
126 |
public long getCRC(FeatureType type) { |
127 |
StringBuilder buffer = new StringBuilder(); |
128 |
for (int i = 0; i < type.size(); i++) { |
129 |
FeatureAttributeDescriptor x = type.getAttributeDescriptor(i); |
130 |
buffer.append(x.getName()); |
131 |
buffer.append(x.getDataTypeName()); |
132 |
buffer.append(x.getSize()); |
133 |
} |
134 |
CRC32 crc = new CRC32(); |
135 |
byte[] data = buffer.toString().getBytes(); |
136 |
crc.update(data); |
137 |
return crc.getValue();
|
138 |
} |
139 |
|
140 |
public String getFilename(FeatureStore store) throws DataException { |
141 |
FeatureType type = store.getDefaultFeatureType(); |
142 |
|
143 |
return store.getName() + "-" + Long.toHexString(getCRC(type)) + ".xml"; |
144 |
} |
145 |
|
146 |
public Entry get(File definitionFile) { |
147 |
String xml;
|
148 |
Map classes;
|
149 |
DynClass dynClass; |
150 |
|
151 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
152 |
if (definitionFile == null || !definitionFile.exists()) { |
153 |
return null; |
154 |
} |
155 |
try {
|
156 |
xml = FileUtils.readFileToString(definitionFile); |
157 |
} catch (Exception ex) { |
158 |
return null; |
159 |
} |
160 |
try {
|
161 |
InputStream is = IOUtils.toInputStream(xml, Charset.forName("UTF-8")); |
162 |
classes = dynObjectManager.importDynClassDefinitions(is, this.getClass().getClassLoader());
|
163 |
} catch (Exception ex) { |
164 |
logger.warn("Can't parse xml definition.", ex);
|
165 |
return null; |
166 |
} |
167 |
try {
|
168 |
dynClass = (DynClass) classes.values().iterator().next(); |
169 |
} catch (Exception ex) { |
170 |
return null; |
171 |
} |
172 |
if (dynClass == null) { |
173 |
return null; |
174 |
} |
175 |
|
176 |
InputStream is;
|
177 |
try {
|
178 |
is = new FileInputStream(definitionFile); |
179 |
} catch (FileNotFoundException ex) { |
180 |
return null; |
181 |
} |
182 |
return new DefaultEntry(definitionFile.getName(), dynClass, xml); |
183 |
} |
184 |
|
185 |
@Override
|
186 |
public Entry get(FeatureStore store) throws DataException { |
187 |
FeatureType type = store.getDefaultFeatureType(); |
188 |
|
189 |
File f;
|
190 |
try {
|
191 |
f = new File(getDefinitionsFolder(), getFilename(store)); |
192 |
} catch (IOException ex) { |
193 |
return new DefaultEntry(store); |
194 |
} |
195 |
Entry entry = this.get(f);
|
196 |
return entry;
|
197 |
} |
198 |
|
199 |
@Override
|
200 |
public void put(Entry entry) throws DataException, IOException { |
201 |
if (!getDefinitionsFolder().exists()) {
|
202 |
FileUtils.forceMkdir(getDefinitionsFolder()); |
203 |
} |
204 |
File f = new File(getDefinitionsFolder(), entry.getKey()); |
205 |
FileUtils.write(f, entry.getXml()); |
206 |
} |
207 |
|
208 |
@Override
|
209 |
public Iterator<Entry> iterator() { |
210 |
try {
|
211 |
List<Entry> definitions = new ArrayList(); |
212 |
File folder;
|
213 |
folder = this.getDefinitionsFolder();
|
214 |
for (File f : folder.listFiles()) { |
215 |
Entry entry = this.get(f);
|
216 |
if (entry != null) { |
217 |
definitions.add(entry); |
218 |
} |
219 |
} |
220 |
Collections.sort(definitions);
|
221 |
return definitions.iterator();
|
222 |
} catch (IOException ex) { |
223 |
logger.warn("Can't retrieve list of forms definitions.", ex);
|
224 |
return null; |
225 |
} |
226 |
} |
227 |
|
228 |
@Override
|
229 |
public void put(FeatureStore store) throws DataException, IOException { |
230 |
DefaultEntry entry = new DefaultEntry(store);
|
231 |
this.put(entry);
|
232 |
} |
233 |
|
234 |
@Override
|
235 |
public DynClass getDynClass(FeatureStore store) throws DataException { |
236 |
Entry entry = this.get(store);
|
237 |
if (entry == null) { |
238 |
return null; |
239 |
} |
240 |
return entry.getDynClass();
|
241 |
} |
242 |
|
243 |
} |