Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / dbf / DBFFeature.java @ 20660

History | View | Annotate | Download (4.12 KB)

1
package org.gvsig.data.datastores.vectorial.file.dbf;
2

    
3
import java.text.DateFormat;
4
import java.text.ParseException;
5
import java.util.Date;
6
import java.util.Iterator;
7
import java.util.List;
8
import java.util.Locale;
9

    
10
import org.gvsig.data.datastores.vectorial.file.dbf.utils.DbaseFile;
11
import org.gvsig.data.exception.DataException;
12
import org.gvsig.data.exception.ReadException;
13
import org.gvsig.data.spatialprovisional.IExtent;
14
import org.gvsig.data.vectorial.AttributeDescriptor;
15
import org.gvsig.data.vectorial.Feature;
16
import org.gvsig.data.vectorial.IFeature;
17
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
18
import org.gvsig.data.vectorial.IFeatureID;
19
import org.gvsig.data.vectorial.IFeatureType;
20
import org.gvsig.data.vectorial.IsNotFeatureSettingException;
21

    
22

    
23
public class DBFFeature extends Feature{
24
        protected static Locale ukLocale = new Locale("en", "UK");
25
        protected DBFStore store;
26
        protected long featureIndex;
27

    
28
        protected DBFFeature(IFeatureType featureType, DBFStore store, long featureIndex) throws ReadException {
29
                super(featureType);
30
                this.store=store;
31
                this.featureIndex=featureIndex;
32
                load();
33
        }
34

    
35

    
36
        /* (non-Javadoc)
37
         * @see org.gvsig.data.vectorial.Feature#cloneFeature()
38
         */
39
        protected IFeature cloneFeature() throws DataException {
40
                try {
41
                        return new DBFFeature(this.featureType,this.store,this.featureIndex);
42
                } catch (ReadException e) {
43
                        throw new IsNotFeatureSettingException("Clone");
44
                }
45

    
46
        }
47

    
48
        protected void load() throws ReadException{
49
                Iterator iterator=featureType.iterator();
50
                this.loading();
51
                while (iterator.hasNext()) {
52
                        IFeatureAttributeDescriptor descriptor = (IFeatureAttributeDescriptor) iterator.next();
53
                        try {
54
                                this.loadValue(descriptor);
55
                        } catch (IsNotFeatureSettingException e) {
56
                                throw new ReadException(store.getName(),e);
57
                        }
58

    
59

    
60
                }
61
                this.stopLoading();
62
        }
63

    
64
        protected void loadValue(IFeatureAttributeDescriptor descriptor)  throws ReadException, IsNotFeatureSettingException{
65
                int i=((AttributeDescriptor)descriptor).originalPosition();
66
                String value = null;
67
                try{
68
                        value=store.getStringFieldValue((int) this.featureIndex, i);
69
                } catch (DataException e){
70
                        throw new ReadException(this.store.getName(),e);
71
                }
72
                value=value.trim();
73
                String fieldType=descriptor.getDataType();
74
                if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_STRING)){
75
                        this.set(i,value);
76
                }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_DOUBLE)){
77
                        try{
78
                                Double.parseDouble(value);
79
                        }catch (NumberFormatException e) {
80
                                value="0";
81
                        }
82
                        this.set(i,Double.parseDouble(value));
83
                }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_INT)){
84
                        this.set(i,Integer.parseInt(value));
85
                }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_FLOAT)){
86
                        this.set(i,Float.parseFloat(value));
87
                }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_LONG)){
88
                        this.set(i,Long.parseLong(value));
89
                }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_BOOLEAN)){
90
                        this.set(i,Boolean.parseBoolean(value));
91
                }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_BYTE)){
92
                        this.set(i,Byte.parseByte(value));
93
                }else if (fieldType.equals(IFeatureAttributeDescriptor.TYPE_DATE)){
94
                        String year = value.substring(0, 4);
95
                        String month = value.substring(4, 6);
96
                        String day = value.substring(6, 8);
97
                        DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, ukLocale);
98
                        /* Calendar c = Calendar.getInstance();
99
                         c.clear();
100
                         c.set(Integer.parseInt(year), Integer.parseInt(month),
101
                         Integer.parseInt(day));
102
                         c.set(Calendar.MILLISECOND, 0); */
103
                        String strAux = month + "/" + day + "/" + year;
104
                        Date dat=null;
105
                        try {
106
                                dat = df.parse(strAux);
107
                        } catch (ParseException e) {
108
                                throw new ReadException(this.store.getName(),e);
109
                        }
110
                        this.set(i,dat);
111
                }else{
112
                        this.set(i,descriptor.getDefaultValue());
113
                }
114
        }
115

    
116
        public IFeatureID getID() {
117
                return new DBFFeatureID(this.store,featureIndex);
118
        }
119

    
120
        public IExtent getExtent(){
121
                return null;
122
        }
123

    
124
        public List getAllSRS() {
125
                return null;
126
        }
127

    
128
        public String getDefaultSRS() {
129
                return null;
130
        }
131

    
132

    
133
}