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 / DefaultFeatureTypeDefinitionsManager.java @ 42792
History | View | Annotate | Download (7.29 KB)
1 |
package org.gvsig.app.extension; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.FileInputStream; |
5 |
import java.io.FileOutputStream; |
6 |
import java.io.IOException; |
7 |
import java.io.InputStream; |
8 |
import java.nio.charset.Charset; |
9 |
import java.util.HashMap; |
10 |
import java.util.Map; |
11 |
import java.util.Properties; |
12 |
import java.util.zip.CRC32; |
13 |
|
14 |
import org.apache.commons.io.FileUtils; |
15 |
import org.apache.commons.io.IOUtils; |
16 |
import org.gvsig.andami.PluginServices; |
17 |
import org.gvsig.andami.PluginsLocator; |
18 |
import org.gvsig.andami.PluginsManager; |
19 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
20 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
21 |
import org.gvsig.fmap.dal.feature.FeatureType; |
22 |
import org.gvsig.fmap.dal.feature.FeatureTypeDefinitionsManager; |
23 |
import org.gvsig.tools.ToolsLocator; |
24 |
import org.gvsig.tools.dynobject.DynClass; |
25 |
import org.gvsig.tools.dynobject.DynObjectManager; |
26 |
import org.slf4j.Logger; |
27 |
import org.slf4j.LoggerFactory; |
28 |
|
29 |
public class DefaultFeatureTypeDefinitionsManager implements FeatureTypeDefinitionsManager { |
30 |
|
31 |
private static final Logger logger = LoggerFactory.getLogger(DefaultFeatureTypeDefinitionsManager.class); |
32 |
|
33 |
private File definitionsFolder = null; |
34 |
private final Map<String, DynClass> dynClasses; |
35 |
|
36 |
public DefaultFeatureTypeDefinitionsManager() {
|
37 |
this.dynClasses = new HashMap<>(); |
38 |
} |
39 |
|
40 |
private File getDefinitionsFolder() throws IOException { |
41 |
if (this.definitionsFolder == null) { |
42 |
PluginsManager pluginManager = PluginsLocator.getManager(); |
43 |
PluginServices plugin = pluginManager.getPlugin(CreateDefaultFormDefinitionExtension.class); |
44 |
File homeFolder = plugin.getPluginHomeFolder();
|
45 |
File definitionsFolder = new File(homeFolder, "schemas"); |
46 |
if (!definitionsFolder.exists()) {
|
47 |
FileUtils.forceMkdir(definitionsFolder); |
48 |
} |
49 |
this.definitionsFolder = definitionsFolder;
|
50 |
} |
51 |
return this.definitionsFolder; |
52 |
} |
53 |
|
54 |
private long getCRC(FeatureType type) { |
55 |
StringBuilder buffer = new StringBuilder(); |
56 |
for (int i = 0; i < type.size(); i++) { |
57 |
FeatureAttributeDescriptor x = type.getAttributeDescriptor(i); |
58 |
buffer.append(x.getName()); |
59 |
buffer.append(x.getDataTypeName()); |
60 |
buffer.append(x.getSize()); |
61 |
} |
62 |
CRC32 crc = new CRC32(); |
63 |
byte[] data = buffer.toString().getBytes(); |
64 |
crc.update(data); |
65 |
return crc.getValue();
|
66 |
} |
67 |
|
68 |
private String getKey(FeatureStore store, FeatureType featureType) { |
69 |
return store.getName() + "_" + Long.toHexString(getCRC(featureType)); |
70 |
} |
71 |
|
72 |
private File getDefinitionFile(String key) { |
73 |
File folder;
|
74 |
try {
|
75 |
folder = getDefinitionsFolder(); |
76 |
} catch (IOException ex) { |
77 |
return null; |
78 |
} |
79 |
File f = new File(folder, key + ".xml"); |
80 |
if (f.exists()) {
|
81 |
return f;
|
82 |
} |
83 |
Properties prop = new Properties(); |
84 |
FileInputStream fin = null; |
85 |
try {
|
86 |
fin = new FileInputStream(new File(folder, "index.properties")); |
87 |
prop.load(fin); |
88 |
} catch (IOException ex) { |
89 |
return null; |
90 |
} finally {
|
91 |
IOUtils.closeQuietly(fin); |
92 |
} |
93 |
String s = prop.getProperty(key, null); |
94 |
if (s == null) { |
95 |
return null; |
96 |
} |
97 |
f = new File(folder, s); |
98 |
if (f.exists()) {
|
99 |
return f;
|
100 |
} |
101 |
f = new File(s); |
102 |
if (f.exists()) {
|
103 |
return f;
|
104 |
} |
105 |
return null; |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public DynClass get(FeatureStore store, FeatureType featureType) {
|
110 |
String key = this.getKey(store, featureType); |
111 |
DynClass dynClass = this.dynClasses.get(key);
|
112 |
if (dynClass != null) { |
113 |
return dynClass;
|
114 |
} |
115 |
File definitionFile = getDefinitionFile(key);
|
116 |
if (definitionFile == null) { |
117 |
return featureType;
|
118 |
} |
119 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
120 |
try {
|
121 |
String xml = FileUtils.readFileToString(definitionFile);
|
122 |
xml = xml.replaceAll("@@@",definitionFile.getParentFile().getAbsolutePath());
|
123 |
|
124 |
InputStream is = IOUtils.toInputStream(xml, Charset.forName("UTF-8")); |
125 |
Map<String, DynClass> dynClasses = dynObjectManager.importDynClassDefinitions(is, this.getClass().getClassLoader()); |
126 |
for (DynClass aDynClass : dynClasses.values()) {
|
127 |
this.dynClasses.put(aDynClass.getName(), aDynClass);
|
128 |
} |
129 |
} catch (Exception ex) { |
130 |
logger.warn("Can't parse xml definition.", ex);
|
131 |
return null; |
132 |
} |
133 |
|
134 |
dynClass = this.dynClasses.get(key);
|
135 |
if (dynClass != null) { |
136 |
return dynClass;
|
137 |
} |
138 |
return featureType;
|
139 |
} |
140 |
|
141 |
@Override
|
142 |
public boolean contains(FeatureStore store, FeatureType featureType) { |
143 |
String key = this.getKey(store, featureType); |
144 |
return this.dynClasses.containsKey(key); |
145 |
} |
146 |
|
147 |
@Override
|
148 |
public void add(FeatureStore store, FeatureType featureType, DynClass dynClass) { |
149 |
try {
|
150 |
String key = this.getKey(store, featureType); |
151 |
File f = new File(getDefinitionsFolder(), key + ".xml"); |
152 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
153 |
String xml = dynObjectManager.exportSimpleDynClassDefinitions(dynClass);
|
154 |
FileUtils.write(f, xml); |
155 |
this.dynClasses.put(key, dynClass);
|
156 |
} catch (IOException ex) { |
157 |
throw new RuntimeException(ex); |
158 |
} |
159 |
} |
160 |
|
161 |
@Override
|
162 |
public void remove(FeatureStore store, FeatureType featureType) { |
163 |
String key = this.getKey(store, featureType); |
164 |
this.dynClasses.remove(key);
|
165 |
} |
166 |
|
167 |
public void addModel(File model) { |
168 |
DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
169 |
try {
|
170 |
String xml = FileUtils.readFileToString(model);
|
171 |
InputStream is = IOUtils.toInputStream(xml, Charset.forName("UTF-8")); |
172 |
Map<String, DynClass> dynClasses = dynObjectManager.importDynClassDefinitions(is, this.getClass().getClassLoader()); |
173 |
|
174 |
File folder;
|
175 |
try {
|
176 |
folder = getDefinitionsFolder(); |
177 |
} catch (IOException ex) { |
178 |
return ;
|
179 |
} |
180 |
Properties prop = new Properties(); |
181 |
FileInputStream fin = null; |
182 |
try {
|
183 |
fin = new FileInputStream(new File(folder, "index.properties")); |
184 |
prop.load(fin); |
185 |
} catch (IOException ex) { |
186 |
} finally {
|
187 |
IOUtils.closeQuietly(fin); |
188 |
} |
189 |
for (DynClass aDynClass : dynClasses.values()) {
|
190 |
String className = aDynClass.getName();
|
191 |
prop.setProperty(className, model.getAbsolutePath()); |
192 |
} |
193 |
FileOutputStream fout = null; |
194 |
try {
|
195 |
fout = new FileOutputStream(new File(folder, "index.properties")); |
196 |
prop.store(fout, "");
|
197 |
} catch (IOException ex) { |
198 |
} finally {
|
199 |
IOUtils.closeQuietly(fout); |
200 |
} |
201 |
|
202 |
} catch (Exception ex) { |
203 |
logger.warn("Can't parse xml definition.", ex);
|
204 |
} |
205 |
} |
206 |
|
207 |
} |