Revision 21427

View differences:

branches/v2_0_0_prep/libraries/libFMap_mapcontext/src/org/gvsig/fmap/mapcontext/rendering/legend/driver/gvl/FMapGVLDriver.java
1
package org.gvsig.fmap.mapcontext.rendering.legend.driver.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.Hashtable;
9

  
10
import org.exolab.castor.xml.MarshalException;
11
import org.exolab.castor.xml.Marshaller;
12
import org.exolab.castor.xml.ValidationException;
13
import org.gvsig.data.ReadException;
14
import org.gvsig.data.vectorial.FeatureAttributeDescriptor;
15
import org.gvsig.fmap.mapcontext.layers.FLayer;
16
import org.gvsig.fmap.mapcontext.layers.FLayers;
17
import org.gvsig.fmap.mapcontext.layers.XMLException;
18
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
19
import org.gvsig.fmap.mapcontext.rendering.legend.IClassifiedVectorLegend;
20
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
21
import org.gvsig.fmap.mapcontext.rendering.legend.IVectorLegend;
22
import org.gvsig.fmap.mapcontext.rendering.legend.LegendFactory;
23
import org.gvsig.fmap.mapcontext.rendering.legend.driver.IFMapLegendDriver;
24
import org.gvsig.fmap.mapcontext.rendering.legend.driver.LegendDriverException;
25

  
26
import com.iver.utiles.XMLEntity;
27
import com.iver.utiles.xmlEntity.generate.XmlTag;
28

  
29

  
30
public class FMapGVLDriver implements IFMapLegendDriver{
31

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

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

  
45

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

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

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

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

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

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

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

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

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

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

  
87

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

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

  
94

  
95
						try {
96
							for (int i = 0; i < fNames.length; i++) {
97

  
98
								int fieldIndex = m.getFeatureStore().getDefaultFeatureType().getFieldIndex(fNames[i]);
99
								if (fieldIndex != -1) {
100
									if(fTypes != null)
101
										if(!fTypes[i].equals(((FeatureAttributeDescriptor)m.getFeatureStore().getDefaultFeatureType().get(fieldIndex)).getDataType())) {
102
											errors |= LegendDriverException.CLASSIFICATION_FIELDS_TYPE_MISMATCH;
103
										}
104
								} else {
105
									errors |= LegendDriverException.CLASSIFICATION_FIELDS_NOT_FOUND;
106
								}
107
							}
108
						} catch (ReadException 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 (org.exolab.castor.xml.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) 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

  
164

  
165
}
0 166

  

Also available in: Unified diff