Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / dbf / DBFFeature.java @ 23088

History | View | Annotate | Download (4.06 KB)

1
package org.gvsig.fmap.data.feature.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.fmap.data.DataException;
11
import org.gvsig.fmap.data.ReadException;
12
import org.gvsig.fmap.data.feature.AbstractFeature;
13
import org.gvsig.fmap.data.feature.AttributeDescriptor;
14
import org.gvsig.fmap.data.feature.Feature;
15
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor;
16
import org.gvsig.fmap.data.feature.FeatureID;
17
import org.gvsig.fmap.data.feature.FeatureType;
18
import org.gvsig.fmap.data.feature.IsNotFeatureSettingException;
19
import org.gvsig.fmap.geom.primitive.Envelope;
20

    
21

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

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

    
34

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

    
45
        }
46

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

    
58

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

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

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

    
119
        public Envelope getExtent(){
120
                return null;
121
        }
122

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

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

    
131

    
132
}