gvsig-attributeeditor / org.gvsig.attributeeditor / trunk / org.gvsig.attributeeditor / org.gvsig.attributeeditor.app / org.gvsig.attributeeditor.app.mainplugin / src / main / java / org / gvsig / app / mainplugin / extension / DefaultFeatureFormDefinitionsProvider.java @ 43
History | View | Annotate | Download (4.05 KB)
1 | 43 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.app.mainplugin.extension; |
||
3 | |||
4 | import java.io.File; |
||
5 | import java.io.FileInputStream; |
||
6 | import java.io.FileNotFoundException; |
||
7 | import java.io.IOException; |
||
8 | import java.util.Map; |
||
9 | import java.util.zip.CRC32; |
||
10 | import org.apache.commons.io.FileUtils; |
||
11 | import org.gvsig.andami.PluginServices; |
||
12 | import org.gvsig.andami.PluginsLocator; |
||
13 | import org.gvsig.andami.PluginsManager; |
||
14 | import org.gvsig.featureform.swing.FeatureFormDefinitionsProvider; |
||
15 | import org.gvsig.fmap.dal.exception.DataException; |
||
16 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
17 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
18 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
19 | import org.gvsig.tools.ToolsLocator; |
||
20 | import org.gvsig.tools.dynobject.DynClass; |
||
21 | import org.gvsig.tools.dynobject.DynObjectManager; |
||
22 | import org.slf4j.Logger; |
||
23 | import org.slf4j.LoggerFactory; |
||
24 | import org.xmlpull.v1.XmlPullParserException; |
||
25 | |||
26 | |||
27 | public class DefaultFeatureFormDefinitionsProvider implements FeatureFormDefinitionsProvider { |
||
28 | |||
29 | private static final Logger logger = LoggerFactory.getLogger(DefaultFeatureFormDefinitionsProvider.class); |
||
30 | |||
31 | private File definitionsFolder = null; |
||
32 | |||
33 | private File getDefinitionsFolder() throws IOException { |
||
34 | if( this.definitionsFolder == null ) { |
||
35 | PluginsManager pluginManager = PluginsLocator.getManager(); |
||
36 | PluginServices plugin = pluginManager.getPlugin(AttributeEditorExtension.class); |
||
37 | File homeFolder = plugin.getPluginHomeFolder();
|
||
38 | File definitionsFolder = new File(homeFolder,"definitions"); |
||
39 | if( !definitionsFolder.exists() ) {
|
||
40 | FileUtils.forceMkdir(definitionsFolder); |
||
41 | } |
||
42 | this.definitionsFolder = definitionsFolder;
|
||
43 | } |
||
44 | return this.definitionsFolder; |
||
45 | } |
||
46 | |||
47 | private long getCRC(FeatureType type) { |
||
48 | StringBuffer buffer = new StringBuffer(); |
||
49 | for( int i=0; i< type.size(); i++) { |
||
50 | FeatureAttributeDescriptor x = type.getAttributeDescriptor(i); |
||
51 | buffer.append(x.getName()); |
||
52 | buffer.append(x.getDataTypeName()); |
||
53 | buffer.append(x.getSize()); |
||
54 | } |
||
55 | CRC32 crc = new CRC32(); |
||
56 | byte[] data = buffer.toString().getBytes(); |
||
57 | crc.update(data); |
||
58 | return crc.getValue();
|
||
59 | } |
||
60 | |||
61 | private String getFilename(FeatureStore store) throws DataException { |
||
62 | FeatureType type = store.getDefaultFeatureType(); |
||
63 | |||
64 | return store.getName()+"-"+Long.toHexString(getCRC(type))+".xml"; |
||
65 | } |
||
66 | |||
67 | public void put(FeatureStore store) throws DataException, IOException { |
||
68 | DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
||
69 | FeatureType type = store.getDefaultFeatureType(); |
||
70 | if( !getDefinitionsFolder().exists() ) {
|
||
71 | FileUtils.forceMkdir(getDefinitionsFolder()); |
||
72 | } |
||
73 | File f = new File(getDefinitionsFolder(), getFilename(store)); |
||
74 | dynObjectManager.exportSimpleDynClassDefinitions(f,type); |
||
75 | } |
||
76 | |||
77 | public DynClass get(FeatureStore store) throws DataException { |
||
78 | DynObjectManager dynObjectManager = ToolsLocator.getDynObjectManager(); |
||
79 | FeatureType type = store.getDefaultFeatureType(); |
||
80 | |||
81 | File f;
|
||
82 | try {
|
||
83 | f = new File(getDefinitionsFolder(), getFilename(store)); |
||
84 | } catch (IOException ex) { |
||
85 | return type;
|
||
86 | } |
||
87 | if( !f.exists() ) {
|
||
88 | return type;
|
||
89 | } |
||
90 | FileInputStream fis;
|
||
91 | try {
|
||
92 | fis = new FileInputStream(f); |
||
93 | } catch (FileNotFoundException ex) { |
||
94 | return type;
|
||
95 | } |
||
96 | Map classes = null; |
||
97 | try {
|
||
98 | classes = dynObjectManager.importDynClassDefinitions(fis, this.getClass().getClassLoader());
|
||
99 | } catch (XmlPullParserException ex) {
|
||
100 | logger.warn("Can't load xml definition.", ex);
|
||
101 | return type;
|
||
102 | } catch (IOException ex) { |
||
103 | return type;
|
||
104 | } |
||
105 | DynClass dynClass = (DynClass) classes.get(type.getName()); |
||
106 | if( dynClass == null ) { |
||
107 | return type;
|
||
108 | } |
||
109 | return dynClass;
|
||
110 | } |
||
111 | |||
112 | } |