Statistics
| Revision:

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

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

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

    
122
        public IExtent getExtent() {
123
                return null;
124
        }
125

    
126
        public List getAllSRS() {
127
                return null;
128
        }
129

    
130
        public String getDefaultSRS() {
131
                return null;
132
        }
133

    
134

    
135
}