Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / gvl / FMapGVLDriver.java @ 22729

History | View | Annotate | Download (5.09 KB)

1
package com.iver.cit.gvsig.fmap.drivers.gvl;
2

    
3
import java.io.File;
4
import java.io.FileNotFoundException;
5
import java.io.FileReader;
6
import java.io.FileWriter;
7
import java.io.IOException;
8
import java.util.ArrayList;
9
import java.util.Hashtable;
10

    
11
import org.exolab.castor.xml.MarshalException;
12
import org.exolab.castor.xml.Marshaller;
13
import org.exolab.castor.xml.ValidationException;
14

    
15

    
16
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
17
import com.iver.cit.gvsig.fmap.drivers.legend.IFMapLegendDriver;
18
import com.iver.cit.gvsig.fmap.drivers.legend.LegendDriverException;
19
import com.iver.cit.gvsig.fmap.layers.FLayer;
20
import com.iver.cit.gvsig.fmap.layers.FLayers;
21
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
22
import com.iver.cit.gvsig.fmap.layers.XMLException;
23
import com.iver.cit.gvsig.fmap.rendering.IClassifiedVectorLegend;
24
import com.iver.cit.gvsig.fmap.rendering.ILegend;
25
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
26
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
27
import com.iver.utiles.XMLEntity;
28
import com.iver.utiles.xmlEntity.generate.XmlTag;
29

    
30

    
31
public class FMapGVLDriver implements IFMapLegendDriver{
32

    
33
        private static final String DESCRIPTION = "gvSIG legend";
34
        private static final String FILE_EXTENSION = "gvl";
35

    
36
        public boolean accept(File f) {
37
                if (f.isDirectory()) return true;
38
                String fName = f.getAbsolutePath();
39
                if (fName!=null) {
40
                        fName = fName.toLowerCase();
41
                        return fName.endsWith(".gvl");
42
                }
43
                return false;
44
        }
45

    
46

    
47
        public String getDescription() {
48
                return DESCRIPTION;
49
        }
50

    
51
        public String getFileExtension() {
52
                return FILE_EXTENSION;
53
        }
54

    
55
        public Hashtable read(FLayers layers,FLayer layer, File file) throws LegendDriverException {
56

    
57
                Hashtable table = new Hashtable();
58
                File xmlFile = new File(file.getAbsolutePath());
59
                FileReader reader = null;
60

    
61
                try {
62
                        reader = new FileReader(xmlFile);
63

    
64
                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
65
                        ILegend myLegend = LegendFactory.createFromXML(new XMLEntity(tag));
66

    
67
                        if(myLegend != null ) {
68
                                //CAPA DE LINEAS
69
                                if (layer instanceof FLyrVect) {
70
                                        FLyrVect m = (FLyrVect) layer;
71
                                        IVectorLegend l = (IVectorLegend) myLegend;
72
                                        int errors = 0;
73

    
74
                                        // check general conditions for IVectorLegend
75
                                        try {
76

    
77
                                                // check shape type
78
//                                                if (l.getShapeType() != 0 && m.getShapeType() != l.getShapeType()) {
79
                                                if((l.getDefaultSymbol().getSymbolType() != m.getShapeType())
80
                                                                && !l.isSuitableForShapeType(m.getShapeType())){
81
                                                        errors |= LegendDriverException.LAYER_SHAPETYPE_MISMATCH;
82
                                                }
83

    
84
                                        } catch (ReadDriverException e) {
85
                                                errors |= LegendDriverException.READ_DRIVER_EXCEPTION;
86
                                        }
87

    
88

    
89
                                        if(myLegend instanceof IClassifiedVectorLegend) {
90
                                                IClassifiedVectorLegend cl = (IClassifiedVectorLegend) myLegend;
91

    
92
                                                String[] fNames = cl.getClassifyingFieldNames();
93
                                                int[] fTypes = cl.getClassifyingFieldTypes();
94

    
95

    
96
                                                try {
97
                                                        for (int i = 0; i < fNames.length; i++) {
98
                                                                int fieldIndex = m.getSource().getRecordset().getFieldIndexByName(fNames[i]);
99
                                                                if (fieldIndex != -1) {
100
                                                                        if(fTypes != null)
101
                                                                                if(fTypes[i] !=  m.getSource().getRecordset().getFieldType(fieldIndex)) {
102
                                                                                        errors |= LegendDriverException.CLASSIFICATION_FIELDS_TYPE_MISMATCH;
103
                                                                                }
104
                                                                } else {
105
                                                                        errors |= LegendDriverException.CLASSIFICATION_FIELDS_NOT_FOUND;
106
                                                                }
107
                                                        }
108
                                                } catch (ReadDriverException e) {
109
                                                        errors |= LegendDriverException.READ_DRIVER_EXCEPTION;
110
                                                }
111
                                        }
112
                                        if (errors == 0) {
113
                                                table.put(layer, myLegend);
114
                                                //return myLegend;
115
                                                return table;
116
                                        }
117
                                        else throw new LegendDriverException(errors);
118
                                }
119
                        }
120
                } catch (FileNotFoundException e) {
121
                        // should be unreachable code
122
                        throw new Error ("file_not_found");
123
                } catch (MarshalException e) {
124
                        throw new Error ("file_corrupt");
125
                } catch (ValidationException e) {
126
                        // should be unreachable code
127
                        throw new Error ("ValidationException");
128
                } catch (XMLException e) {
129
                        throw new Error ("unsupported_legend");
130
                } finally {
131
                        try {
132
                                if (reader!=null) {
133
                                        reader.close();
134
                                }
135
                        } catch (IOException e) {
136

    
137
                                throw new Error ("file_closing_failed");
138
                        }
139
                }
140
                return null;
141

    
142
        }
143

    
144
        public void write(FLayers layers, FLayer layer, ILegend legend, File file, String version) throws LegendDriverException  {
145
                FileWriter writer;
146
                int errors = 0;
147
                try {
148
                        writer = new FileWriter(file.getAbsolutePath());
149
                        Marshaller m = new Marshaller(writer);
150
                        m.setEncoding("ISO-8859-1");
151
                        m.marshal(legend.getXMLEntity().getXmlTag());
152
                } catch (MarshalException e) {
153
                        errors |= LegendDriverException.SAVE_LEGEND_ERROR;
154
                } catch (ValidationException e) {
155
                        errors |= LegendDriverException.SAVE_LEGEND_ERROR;
156
                } catch (IOException e) {
157
                        errors |= LegendDriverException.SYSTEM_ERROR;
158
                }
159
                if (errors != 0) throw new LegendDriverException(errors);
160

    
161
        }
162

    
163
        public ArrayList<String> getSupportedVersions() {
164
                ArrayList<String> versions = new ArrayList<String>();
165
                versions.add("gvSIG 1.0.0");
166
                return versions;
167
        }
168

    
169
}