Revision 44242

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.lib/src/main/java/org/gvsig/fmap/dal/feature/spi/simpleprovider/BaseSimpleFilesystemServerProviderFactory.java
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.feature.spi.simpleprovider;
7

  
8
import java.io.File;
9
import org.gvsig.fmap.dal.DataServerExplorer;
10
import org.gvsig.fmap.dal.exception.InitializeException;
11
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider;
12
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderFactory;
13

  
14
/**
15
 *
16
 * @author jjdelcerro
17
 */
18
public class BaseSimpleFilesystemServerProviderFactory
19
        implements FilesystemServerExplorerProviderFactory {
20

  
21
    private final SimpleReaderFactory readerFactory;
22

  
23
    public BaseSimpleFilesystemServerProviderFactory(SimpleReaderFactory readerFactory) {
24
        this.readerFactory = readerFactory;
25
    }
26

  
27
    @Override
28
    public FilesystemServerExplorerProvider createProvider() throws InitializeException {
29
        return new BaseSimpleFilesystemServerProvider(this.readerFactory);
30
    }
31

  
32
    @Override
33
    public boolean accept(File pathname) {
34
        return this.readerFactory.accept(pathname);
35
    }
36

  
37
    @Override
38
    public String getName() {
39
        return this.readerFactory.getName();
40
    }
41

  
42
    @Override
43
    public String getDescription() {
44
        return this.readerFactory.getDescription();
45
    }
46

  
47
    @Override
48
    public int allowCreate() {
49
        return NO;
50
    }
51

  
52
    @Override
53
    public int getMode() {
54
        return DataServerExplorer.MODE_FEATURE | DataServerExplorer.MODE_GEOMETRY;
55
    }
56

  
57
    @Override
58
    public boolean isMode(int mode) {
59
        return this.getMode()==mode;
60
    }
61

  
62
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.lib/src/main/java/org/gvsig/fmap/dal/feature/spi/simpleprovider/BaseSimpleStoreProviderFactory.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.feature.spi.simpleprovider;
25

  
26
import org.gvsig.fmap.dal.DataParameters;
27
import org.gvsig.fmap.dal.DataStoreProvider;
28
import org.gvsig.fmap.dal.exception.InitializeException;
29
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProviderFactory;
30
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
31
import org.gvsig.tools.dynobject.DynObject;
32

  
33
public class BaseSimpleStoreProviderFactory 
34
        extends AbstractFeatureStoreProviderFactory 
35
    {
36

  
37
    private final SimpleReaderFactory readerFactory;
38
    
39
    protected BaseSimpleStoreProviderFactory(SimpleReaderFactory readerFactory) {
40
        super(readerFactory.getName(), readerFactory.getDescription());
41
        this.readerFactory = readerFactory;
42
    }
43

  
44
    @Override
45
    public DynObject createParameters() {
46
        return new BaseSimpleStoreParameters(this.readerFactory);
47
    }
48

  
49
    @Override
50
    public DataStoreProvider createProvider(
51
            DataParameters parameters, 
52
            DataStoreProviderServices providerServices) throws InitializeException {
53
        return new BaseSimpleStoreProvider(
54
                this.readerFactory,
55
                (BaseSimpleStoreParameters) parameters,
56
                providerServices
57
        );
58
    }
59

  
60
    @Override
61
    public int allowCreate() {
62
        return NO;
63
    }
64

  
65
    @Override
66
    public int allowWrite() {
67
        return NO;
68
    }
69

  
70
    @Override
71
    public int allowRead() {
72
        return YES;
73
    }
74

  
75
    @Override
76
    public int hasRasterSupport() {
77
        return NO;
78
    }
79

  
80
    @Override
81
    public int hasTabularSupport() {
82
        return YES;
83
    }
84

  
85
    @Override
86
    public int hasVectorialSupport() {
87
        return YES;
88
    }
89

  
90
    @Override
91
    public int allowMultipleGeometryTypes() {
92
        return YES;
93
    }
94

  
95
    @Override
96
    public int allowEditableFeatureType() {
97
        return NO;
98
    }
99

  
100
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.lib/src/main/java/org/gvsig/fmap/dal/feature/spi/simpleprovider/SimpleReaderFactory.java
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.feature.spi.simpleprovider;
7

  
8
import java.io.FileFilter;
9
import java.net.URL;
10
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory;
11
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters;
12
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderFactory;
13

  
14
/**
15
 *
16
 * @author jjdelcerro
17
 */
18
public interface SimpleReaderFactory extends FileFilter {
19
    
20
    public String getName();
21
    
22
    public String getDescription();
23
    
24
    public String[] getExtensions();
25
    
26
    public SimpleReader createReader(OpenFeatureStoreParameters params);
27
    
28
    public FeatureStoreProviderFactory createStoreProviderFactory();
29
    
30
    public FilesystemServerExplorerProviderFactory createFilesystemServerProviderFactory();
31
    
32
    public void selfRegister(URL parameters_xml, URL metadata_xml);
33
    
34
    public void selfRegister(Class loader);
35
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.lib/src/main/java/org/gvsig/fmap/dal/feature/spi/simpleprovider/BaseSimpleStoreParameters.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA 02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.feature.spi.simpleprovider;
25

  
26
import java.io.File;
27
import java.util.Arrays;
28
import java.util.List;
29
import java.util.Locale;
30
import org.apache.commons.lang3.StringUtils;
31
import org.cresques.cts.IProjection;
32
import org.gvsig.fmap.dal.FileHelper;
33
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters;
34
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters;
35
import org.gvsig.fmap.dal.spi.AbstractDataParameters;
36
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
37
import org.gvsig.tools.ToolsLocator;
38
import org.gvsig.tools.dataTypes.DataTypesManager;
39
import org.gvsig.tools.dynobject.DelegatedDynObject;
40
import org.slf4j.Logger;
41
import org.slf4j.LoggerFactory;
42

  
43
public class BaseSimpleStoreParameters extends AbstractDataParameters implements
44
        OpenFeatureStoreParameters, FilesystemStoreParameters {
45

  
46
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseSimpleStoreParameters.class);
47

  
48

  
49
    private static final String FILE = "file";
50
    private static final String IGNOREERRORS = "ignoreErrors";
51
    private static final String CRS = "CRS";
52
    private static final String FIELDTYPES = "fieldtypes";
53
    private static final String FIELDNAMES = "fieldnames";
54
    private static final String LOCALE = "locale";
55
    private static final String POINT = "pointFields";
56
    private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection";
57

  
58
    private final DelegatedDynObject parameters;
59
    private final SimpleReaderFactory readerFactory;
60
    
61
    public BaseSimpleStoreParameters(SimpleReaderFactory readerFactory) {
62
        super();
63
        this.readerFactory = readerFactory;
64
        this.parameters = (DelegatedDynObject) FileHelper.newParameters(readerFactory.getName());
65
        this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, readerFactory.getName());
66
    }
67

  
68
    @Override
69
    public String getDataStoreName() {
70
        return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME);
71
    }
72

  
73
    @Override
74
    public String getDescription() {
75
        return this.getDynClass().getDescription();
76
    }
77

  
78
    @Override
79
    protected DelegatedDynObject getDelegatedDynObject() {
80
        return parameters;
81
    }
82

  
83
    @Override
84
    public boolean isValid() {
85
        if ( this.getPathName() == null ) {
86
            return false;
87
        }
88
        return true;
89
    }
90

  
91
    @Override
92
    public File getFile() {
93
        return (File) this.getDynValue(FILE);
94
    }
95

  
96
    @Override
97
    public void setFile(File file) {
98
        this.setDynValue(FILE, file);
99
    }
100

  
101
    public IProjection getCRS() {
102
        return (IProjection) this.parameters.getDynValue(CRS);
103
    }
104

  
105
    public String getPathName() {
106
        File f = (File) this.parameters.getDynValue(FILE);
107
        if ( f == null ) {
108
            return null;
109
        }
110
        return f.getPath();
111
    }
112

  
113
    @SuppressWarnings("UseSpecificCatch")
114
    public Locale getLocale() {
115
        try {
116
            String s = (String) this.parameters.getDynValue(LOCALE);
117
            if ( s.trim().length() == 0 ) {
118
                return null;
119
            }
120
            if ( "DEFAULT".equalsIgnoreCase(s.trim()) ) {
121
                return Locale.getDefault();
122
            }
123
            Locale locale;
124
            // locale = Locale.forLanguageTag(s); // Since java 1.7
125
            String[] ss = s.split("-");
126
            switch (ss.length) {
127
            case 1:
128
                locale = new Locale(ss[0]);
129
                break;
130
            case 2:
131
                locale = new Locale(ss[0], ss[1]);
132
                break;
133
            case 3:
134
            default:
135
                locale = new Locale(ss[0], ss[1], ss[2]);
136
                break;
137
            }
138
            return locale;
139
        } catch (Exception ex) {
140
            LOGGER.warn("Can't get locale from ODS parameters.", ex);
141
            return null;
142
        }
143
    }
144

  
145
    public String[] getPointDimensionNames() {
146
        String s = (String) this.parameters.getDynValue(POINT);
147
        if ( StringUtils.isEmpty(s) ) {
148
            return null;
149
        }
150
        String sep = this.getDelimiter(s);
151
        if ( sep == null ) {
152
            return new String[] { s };
153
        }
154
        return s.split(sep);
155
    }
156

  
157
    public boolean getIgnoreErrors() {
158
        Boolean b = (Boolean) this.parameters.getDynValue(IGNOREERRORS);
159
        if ( b == null ) {
160
            return false;
161
        }
162
        return b;
163
    }
164

  
165
    private String getDelimiter(String line) {
166
        String sep = null;
167
        // Cuiaddo con los ":", los he puesto al final a proposito
168
        // ya que podian estar en la cadena para separar el size
169
        // de cada tipo.
170
        String seps = ",;-|@#/+$%&!:";
171
        for ( int i = 0; i < seps.length(); i++ ) {
172
            sep = seps.substring(i, 1);
173
            if ( line.contains(seps.substring(i, 1)) ) {
174
                break;
175
            }
176
            sep = null;
177
        }
178
        return sep;
179
    }
180

  
181
    public List<String> getFieldNames() {
182
        String s = (String) this.parameters.getDynValue(FIELDNAMES);
183
        if ( StringUtils.isEmpty(s) ) {
184
            return null;
185
        }
186
        String sep = this.getDelimiter(s);
187
        if ( sep == null ) {
188
            return null;
189
        }
190
        String fieldNames[] = s.split("[" + sep + "]");
191
        for (int i = 0; i < fieldNames.length; i++) {
192
            fieldNames[i] = fieldNames[i].trim();
193
        }
194
        return Arrays.asList(fieldNames);
195
    }
196
    
197
    public int[] getFieldTypes() {
198
        String s = (String) this.parameters.getDynValue(FIELDTYPES);
199
        if ( StringUtils.isEmpty(s) ) {
200
            return null;
201
        }
202
        String sep = this.getDelimiter(s);
203
        if ( sep == null ) {
204
            return null;
205
        }
206
        DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
207
        String fieldTypeNames[] = s.split("[" + sep + "]");
208
        int fieldTypes[] = new int[fieldTypeNames.length];
209
        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
210
            s = fieldTypeNames[i].trim();
211
            if ( s.contains(":") ) {
212
                s = s.split(":")[0];
213
            }
214
            fieldTypes[i] = dataTypeManager.getType(s);
215
        }
216
        return fieldTypes;
217
    }
218

  
219
    @SuppressWarnings("UseSpecificCatch")
220
    public int[] getFieldSizes() {
221
        String s = (String) this.parameters.getDynValue(FIELDTYPES);
222
        if ( StringUtils.isEmpty(s) ) {
223
            return null;
224
        }
225
        String sep = this.getDelimiter(s);
226
        if ( sep == null ) {
227
            return null;
228
        }
229
        String fieldTypeNames[] = s.split("[" + sep + "]");
230
        int fieldSizes[] = new int[fieldTypeNames.length];
231
        for ( int i = 0; i < fieldTypeNames.length; i++ ) {
232
            String fieldtype = fieldTypeNames[i].trim();
233
            if ( fieldtype.contains(":") ) {
234
                try {
235
                    s = fieldtype.split(":")[1];
236
                    fieldSizes[i] = Integer.parseInt(s);
237
                } catch (Exception ex) {
238
                    LOGGER.warn("Can't get size of field " + i + " (" + fieldtype + ").", ex);
239
                }
240
            } else {
241
                fieldSizes[i] = 0;
242
            }
243
        }
244
        return fieldSizes;
245
    }
246

  
247
    public boolean getAutomaticTypesDetection() {
248
        Boolean b = (Boolean) this.parameters.getDynValue(AUTOMATICTYPESDETECTION);
249
        if ( b == null ) {
250
            return false;
251
        }
252
        return b;
253
    }
254

  
255
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.lib/src/main/java/org/gvsig/fmap/dal/feature/spi/simpleprovider/AbstractSimpleReaderFactory.java
1
package org.gvsig.fmap.dal.feature.spi.simpleprovider;
2

  
3
import java.io.File;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.net.URL;
7
import org.apache.commons.io.FilenameUtils;
8
import org.gvsig.fmap.dal.DALFileLocator;
9
import org.gvsig.fmap.dal.DALLocator;
10
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory;
11
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters;
12
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderFactory;
13
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
14
import org.gvsig.metadata.MetadataLocator;
15
import org.gvsig.metadata.MetadataManager;
16
import org.gvsig.tools.ToolsLocator;
17
import org.gvsig.tools.dynobject.DynStruct;
18
import org.gvsig.tools.persistence.PersistenceManager;
19
import org.slf4j.Logger;
20
import org.slf4j.LoggerFactory;
21

  
22
/**
23
 *
24
 * @author jjdelcerro
25
 */
26
public abstract class AbstractSimpleReaderFactory implements SimpleReaderFactory {
27

  
28
    protected Logger LOGGER = LoggerFactory.getLogger(AbstractSimpleReaderFactory.class);
29

  
30
    private final String name;
31
    private final String description;
32
    private final String[] extensions;
33

  
34
    protected AbstractSimpleReaderFactory(
35
            String name,
36
            String description,
37
            String[] extensions
38
    ) {
39
        this.name = name;
40
        this.description = description;
41
        this.extensions = extensions;
42
    }
43

  
44
    @Override
45
    public String getName() {
46
        return name;
47
    }
48

  
49
    @Override
50
    public String getDescription() {
51
        return description;
52
    }
53

  
54
    @Override
55
    public String[] getExtensions() {
56
        return extensions;
57
    }
58

  
59
    @Override
60
    public boolean accept(File pathname) {
61
        return FilenameUtils.isExtension(pathname.getName(), this.getExtensions());
62
    }
63

  
64
    @Override
65
    public FeatureStoreProviderFactory createStoreProviderFactory() {
66
        return new BaseSimpleStoreProviderFactory(this);
67
    }
68

  
69
    @Override
70
    public FilesystemServerExplorerProviderFactory createFilesystemServerProviderFactory() {
71
        return new BaseSimpleFilesystemServerProviderFactory(this);
72
    }
73

  
74
    private InputStream openStream(URL url) {
75
        try {
76
            return url.openStream();
77
        } catch (IOException ex) {
78
            return null;
79
        }
80
    }
81

  
82
    @Override
83
    public void selfRegister(Class loader) {
84
        this.selfRegister(
85
                loader.getResource(this.getName() + "Parameters.xml"),
86
                loader.getResource(this.getName() + "Metadata.xml")
87
        );    
88
    }
89
    
90
    @SuppressWarnings("UseSpecificCatch")
91
    @Override
92
    public void selfRegister(URL parameters_xml, URL metadata_xml) {
93
        DynStruct definition;
94
        PersistenceManager persistenceManager = ToolsLocator.getPersistenceManager();
95

  
96
        definition = persistenceManager.getDefinition(this.getName());
97
        if (definition == null) {
98
            InputStream stream = this.openStream(parameters_xml);
99
            if (stream == null) {
100
                throw new RuntimeException("Can't access parameters definition in resource '" + parameters_xml.toString() + "'.");
101
            }
102
            persistenceManager.updateDefinition(
103
                    BaseSimpleStoreParameters.class,
104
                    this.getName(),
105
                    stream,
106
                    BaseSimpleStoreParameters.class.getClassLoader(),
107
                    null,
108
                    null
109
            );
110
        }
111

  
112
        if( metadata_xml!=null ) {
113
            MetadataManager metadataManager = MetadataLocator.getMetadataManager();
114
            try {
115
                definition = metadataManager.getDefinition(this.getName());
116
                if (definition == null) {
117
                    InputStream stream = this.openStream(metadata_xml);
118
                    metadataManager.addDefinition(
119
                            this.getName(),
120
                            stream,
121
                            BaseSimpleStoreParameters.class.getClassLoader()
122
                    );
123
                }
124
            } catch (Exception e) {
125
                LOGGER.warn(
126
                        "Can't access metadata definition in resource '" + metadata_xml.toString() + "'.",
127
                        e
128
                );
129
            }
130
        }
131

  
132
        DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator
133
                .getDataManager();
134

  
135
        if (!dataman.getStoreProviders().contains(this.getName())) {
136
            dataman.registerStoreProviderFactory(
137
                    new BaseSimpleStoreProviderFactory(this)
138
            );
139
        }
140

  
141
        try {
142
            DALFileLocator.getFilesystemServerExplorerManager()
143
                    .registerProviderFactory(
144
                            new BaseSimpleFilesystemServerProviderFactory(this)
145
                    );
146
        } catch (Exception e) {
147
            LOGGER.warn(
148
                "Can't register filesystem server provider factory ("+this.getName()+").",
149
                e
150
            );
151

  
152
        }
153

  
154
    }
155

  
156
    @Override
157
    public abstract SimpleReader createReader(OpenFeatureStoreParameters params);
158

  
159
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.lib/src/main/java/org/gvsig/fmap/dal/feature/spi/simpleprovider/BaseSimpleFilesystemServerProvider.java
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.feature.spi.simpleprovider;
7

  
8
import java.io.File;
9
import org.gvsig.fmap.dal.DataServerExplorer;
10
import org.gvsig.fmap.dal.DataStoreParameters;
11
import org.gvsig.fmap.dal.exception.DataException;
12
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
13
import org.gvsig.fmap.dal.serverexplorer.filesystem.impl.AbstractFilesystemServerExplorerProvider;
14
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider;
15
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices;
16

  
17
/**
18
 *
19
 * @author jjdelcerro
20
 */
21
public class BaseSimpleFilesystemServerProvider 
22
        extends AbstractFilesystemServerExplorerProvider 
23
        implements FilesystemServerExplorerProvider, ResourceConsumer 
24
    {
25
    
26
    private FilesystemServerExplorerProviderServices serverExplorer;
27
    private final SimpleReaderFactory readerFactory;
28

  
29
    public BaseSimpleFilesystemServerProvider(SimpleReaderFactory readerFactory) {
30
        this.readerFactory = readerFactory;
31
    }
32

  
33
    @Override
34
    public String getDataStoreProviderName() {
35
        return this.readerFactory.getName();
36
    }
37

  
38
    @Override
39
    public int getMode() {
40
        return DataServerExplorer.MODE_FEATURE | DataServerExplorer.MODE_GEOMETRY;
41
    }
42

  
43
    @Override
44
    public boolean accept(File file) {
45
        return this.readerFactory.accept(file);
46
    }
47

  
48
    @Override
49
    public DataStoreParameters getParameters(File file) throws DataException {
50
        BaseSimpleStoreParameters params = new BaseSimpleStoreParameters(this.readerFactory);
51
        params.setFile(file);
52
        return params;
53
    }
54

  
55
    @Override
56
    public void initialize(FilesystemServerExplorerProviderServices serverExplorer) {
57
        this.serverExplorer = serverExplorer;
58
    }
59

  
60
    @Override
61
    public String getDescription() {
62
        return this.readerFactory.getDescription();
63
    }
64
    
65
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.lib/src/main/java/org/gvsig/fmap/dal/feature/spi/simpleprovider/BaseSimpleStoreProvider.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA 02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.feature.spi.simpleprovider;
25

  
26
import java.io.File;
27
import java.io.IOException;
28
import java.util.ArrayList;
29
import java.util.HashMap;
30
import java.util.Iterator;
31
import java.util.List;
32
import java.util.Locale;
33

  
34
import org.apache.commons.lang3.StringUtils;
35
import org.cresques.cts.IProjection;
36
import org.gvsig.fmap.dal.DALLocator;
37
import org.gvsig.fmap.dal.DataManager;
38
import org.gvsig.fmap.dal.DataServerExplorer;
39
import org.gvsig.fmap.dal.DataStore;
40
import org.gvsig.fmap.dal.DataStoreNotification;
41
import org.gvsig.fmap.dal.DataTypes;
42
import org.gvsig.fmap.dal.FileHelper;
43
import org.gvsig.fmap.dal.exception.DataException;
44
import org.gvsig.fmap.dal.exception.InitializeException;
45
import org.gvsig.fmap.dal.exception.OpenException;
46
import org.gvsig.fmap.dal.exception.ReadException;
47
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
48
import org.gvsig.fmap.dal.feature.EditableFeature;
49
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
50
import org.gvsig.fmap.dal.feature.EditableFeatureType;
51
import org.gvsig.fmap.dal.feature.Feature;
52
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
53
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator;
54
import org.gvsig.fmap.dal.feature.FeatureStore;
55
import org.gvsig.fmap.dal.feature.FeatureType;
56
import org.gvsig.fmap.dal.feature.exception.PerformEditingException;
57
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
58
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
59
import org.gvsig.fmap.dal.feature.spi.memory.AbstractMemoryStoreProvider;
60
import org.gvsig.fmap.dal.resource.file.FileResource;
61
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
62
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
63
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
64
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
65
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
66
import org.gvsig.fmap.geom.Geometry;
67
import org.gvsig.fmap.geom.GeometryLocator;
68
import org.gvsig.fmap.geom.GeometryManager;
69
import org.gvsig.fmap.geom.aggregate.MultiPoint;
70
import org.gvsig.fmap.geom.primitive.Envelope;
71
import org.gvsig.fmap.geom.primitive.Point;
72
import org.gvsig.fmap.geom.type.GeometryType;
73
import org.gvsig.tools.ToolsLocator;
74
import org.gvsig.tools.dataTypes.CoercionException;
75
import org.gvsig.tools.dataTypes.DataType;
76
import org.gvsig.tools.dataTypes.DataTypesManager;
77
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
78
import org.gvsig.tools.dataTypes.DataTypesManager.CoercionWithLocale;
79
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException;
80
import org.gvsig.tools.exception.BaseException;
81
import org.gvsig.tools.task.SimpleTaskStatus;
82
import org.gvsig.tools.task.TaskStatusManager;
83
import org.gvsig.tools.visitor.VisitCanceledException;
84
import org.gvsig.tools.visitor.Visitor;
85
import org.slf4j.Logger;
86
import org.slf4j.LoggerFactory;
87

  
88
public class BaseSimpleStoreProvider extends AbstractMemoryStoreProvider implements
89
        ResourceConsumer {
90

  
91
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseSimpleStoreProvider.class);
92

  
93

  
94
    private final ResourceProvider resource;
95

  
96
    private long counterNewsOIDs = 0;
97
    private Envelope envelope;
98
    private boolean need_calculate_envelope = false;
99
    private final SimpleTaskStatus taskStatus;
100
    private String name = "";
101
    private final SimpleReaderFactory readerFactory;
102

  
103
    @SuppressWarnings("OverridableMethodCallInConstructor")
104
    public BaseSimpleStoreProvider(
105
            SimpleReaderFactory readerFactory,
106
            BaseSimpleStoreParameters parameters,
107
            DataStoreProviderServices storeServices
108
        ) throws InitializeException {
109
        super(
110
                parameters,
111
                storeServices,
112
                FileHelper.newMetadataContainer(readerFactory.getName())
113
        );
114
        this.readerFactory = readerFactory;
115
        TaskStatusManager manager = ToolsLocator.getTaskStatusManager();
116
        this.taskStatus = manager.createDefaultSimpleTaskStatus(readerFactory.getName());
117

  
118
        counterNewsOIDs = 0;
119

  
120
        File file = getParameters().getFile();
121
        resource = this.createResource(
122
                FileResource.NAME,
123
                new Object[]{file.getAbsolutePath()}
124
        );
125

  
126
        resource.addConsumer(this);
127
        initializeFeatureTypes();
128
    }
129

  
130
    @Override
131
    public BaseSimpleStoreParameters getParameters() {
132
        return (BaseSimpleStoreParameters) super.getParameters();
133
    }
134

  
135
    @Override
136
    public String getProviderName() {
137
        return readerFactory.getName();
138
    }
139

  
140
    @Override
141
    public boolean allowWrite() {
142
        return false;
143
    }
144

  
145
    private String getFullFileName() {
146
        // Usar solo para mostrar mensajes en el logger.
147
        String s;
148
        try {
149
            s = getParameters().getFile().getAbsolutePath();
150
        } catch (Exception e2) {
151
            s = "(unknow)";
152
        }
153
        return s;
154
    }
155

  
156
    @Override
157
    @SuppressWarnings("UseSpecificCatch")
158
    public void open() throws OpenException {
159
        if ( this.data != null ) {
160
            return;
161
        }
162
        this.data = new ArrayList<>();
163
        resource.setData(new HashMap());
164
        counterNewsOIDs = 0;
165
        try {
166
            loadFeatures();
167
        } catch (RuntimeException e) {
168
            LOGGER.warn("Can't load features from JExcel '" + getFullFileName() + "'.", e);
169
            throw e;
170
        } catch (Exception e) {
171
            LOGGER.warn("Can't load features from JExcel '" + getFullFileName() + "'.", e);
172
            throw new RuntimeException(e);
173
        }
174
    }
175

  
176
    @Override
177
    public DataServerExplorer getExplorer() throws ReadException {
178
        DataManager manager = DALLocator.getDataManager();
179
        FilesystemServerExplorerParameters params;
180
        try {
181
            params = (FilesystemServerExplorerParameters) manager
182
                    .createServerExplorerParameters(FilesystemServerExplorer.NAME);
183
            params.setRoot(this.getParameters().getFile().getParent());
184
            return manager.openServerExplorer(FilesystemServerExplorer.NAME, params);
185
        } catch (DataException | ValidateDataParametersException e) {
186
            throw new ReadException(this.getProviderName(), e);
187
        }
188

  
189
    }
190

  
191
    @Override
192
    public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds, Iterator originalFeatureTypesUpdated) throws PerformEditingException {
193
        throw new UnsupportedOperationException();
194
    }
195

  
196
    @Override
197
    public boolean closeResourceRequested(ResourceProvider resource) {
198
        return true;
199
    }
200

  
201
    @Override
202
    public int getOIDType() {
203
        return DataTypes.LONG;
204
    }
205

  
206
    @Override
207
    public boolean supportsAppendMode() {
208
        return false;
209
    }
210

  
211
    @Override
212
    public void append(FeatureProvider featureProvider) {
213
        throw new UnsupportedOperationException();
214
    }
215

  
216
    @Override
217
    public void beginAppend() {
218
        throw new UnsupportedOperationException();
219
    }
220

  
221
    @Override
222
    public void endAppend() {
223
        throw new UnsupportedOperationException();
224
    }
225

  
226
    @Override
227
    public Object createNewOID() {
228
        return counterNewsOIDs++;
229
    }
230

  
231
    protected void initializeFeatureTypes() throws InitializeException {
232
        try {
233
            this.open();
234
        } catch (OpenException e) {
235
            throw new InitializeException(this.getProviderName(), e);
236
        }
237
    }
238

  
239
    @Override
240
    public Envelope getEnvelope() throws DataException {
241
        this.open();
242
        if ( this.envelope != null ) {
243
            return this.envelope;
244
        }
245
        if ( !this.need_calculate_envelope ) {
246
            return null;
247
        }
248
        FeatureStore fs = this.getFeatureStore();
249
        FeatureType ft = fs.getDefaultFeatureType();
250
        FeatureAttributeDescriptor fad = ft.getAttributeDescriptor(ft.getDefaultGeometryAttributeIndex());
251

  
252
        try {
253
            this.envelope = GeometryLocator.getGeometryManager().createEnvelope(fad.getGeomType().getSubType());
254
            fs.accept(new Visitor() {
255
                @Override
256
                public void visit(Object obj) throws VisitCanceledException, BaseException {
257
                    Feature f = (Feature) obj;
258
                    Geometry geom = f.getDefaultGeometry();
259
                    if ( geom != null ) {
260
                        envelope.add(geom.getEnvelope());
261
                    }
262
                }
263
            });
264
        } catch (BaseException e) {
265
            LOGGER.warn("Can't calculate the envelope of JExcel file '" + this.getFullName() + "'.", e);
266
            this.envelope = null;
267
        }
268

  
269
        this.need_calculate_envelope = false;
270
        return this.envelope;
271
    }
272

  
273
    @Override
274
    public Object getDynValue(String name) throws DynFieldNotFoundException {
275
        if ( DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name) ) {
276
            try {
277
                return this.getEnvelope();
278
            } catch (DataException e) {
279
                return null;
280
            }
281
        } else {
282
            if ( DataStore.METADATA_CRS.equalsIgnoreCase(name) ) {
283
                IProjection pro = this.getParameters().getCRS();
284
                if ( pro != null ) {
285
                    return pro;
286
                }
287
            }
288
        }
289
        return super.getDynValue(name);
290
    }
291

  
292
    @Override
293
    public void resourceChanged(ResourceProvider resource) {
294
        this.getStoreServices().notifyChange(
295
                DataStoreNotification.RESOURCE_CHANGED,
296
                resource);
297
    }
298

  
299
    @Override
300
    public Object getSourceId() {
301
        return this.getParameters().getFile();
302
    }
303

  
304
    @Override
305
    public String getName() {
306
        return this.name;
307
    }
308

  
309
    @Override
310
    public String getFullName() {
311
        return this.getParameters().getFile().getAbsolutePath();
312
    }
313

  
314
    @Override
315
    public ResourceProvider getResource() {
316
        return resource;
317
    }
318

  
319
    private class FieldTypeParser {
320

  
321
        public String name = null;
322
        public int type = DataTypes.STRING;
323
        public int size = 0;
324
        public boolean allowNulls = true;
325

  
326
        private String typename = "string";
327

  
328
        FieldTypeParser() {
329
        }
330

  
331
        private int getType(String value) {
332
            DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager();
333
            return dataTypesManager.getType(typename);
334
        }
335

  
336
        // El formato seria:
337
        //   name[:typename[:size[:notnull|null]]]
338
        //   name[__typename[__size[__notnull|null]]]
339
        //
340
        @SuppressWarnings("UseSpecificCatch")
341
        public boolean parse(String value) {
342
            String theTypename = null;
343
            String[] ss = null;
344
            if ( value.contains(":") ) {
345
                ss = value.split(":");
346
            } else if ( value.contains("__") ) {
347
                ss = value.split("__");
348
            }
349
            if ( ss == null ) {
350
                this.name = value;
351
                return true;
352
            }
353
            switch (ss.length) {
354
            case 4:
355
                if ( ss[3].length() > 0 ) {
356
                    if ( "notnull".equalsIgnoreCase(ss[3]) ) {
357
                        this.allowNulls = false;
358
                    } else {
359
                        this.allowNulls = true;
360
                    }
361
                }
362
            case 3:
363
                if ( ss[2].length() > 0 ) {
364
                    try {
365
                        this.size = Integer.parseInt(ss[2]);
366
                    } catch (Exception ex) {
367
                        LOGGER.warn("Ignore incorrect field size for field " + value + " in JExcel header of '" + getFullFileName() + "'.", ex);
368
                    }
369
                }
370
            case 2:
371
                if ( ss[1].length() > 0 ) {
372
                    this.typename = ss[1];
373
                    this.type = this.getType(this.typename);
374
                    if ( this.type == DataTypes.INVALID ) {
375
                        this.type = DataTypes.STRING;
376
                        LOGGER.info("Type '" + theTypename + "' not valid for attribute '" + value + "' in JExcel file '" + getFullFileName() + "'.");
377
                    }
378
                }
379
            case 1:
380
                this.name = ss[0];
381
                break;
382
            }
383

  
384
            if ( this.type != DataTypes.STRING ) {
385
                this.size = 0;
386
            }
387
            return true;
388
        }
389

  
390
    }
391

  
392
    @SuppressWarnings("UseSpecificCatch")
393
    private EditableFeatureType getFeatureType(List<String> headers, int automaticTypes[]) {
394
        EditableFeatureType fType = getStoreServices().createFeatureType(this.getName());
395
        fType.setHasOID(true);
396
        DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager();
397

  
398
        FieldTypeParser[] fieldTypes = new FieldTypeParser[headers.size()];
399
        //
400
        // Calculamos cuales pueden ser los tipos de datos
401
        //
402
        for ( int i = 0; i < fieldTypes.length; i++ ) {
403
            fieldTypes[i] = new FieldTypeParser();
404
        }
405

  
406
        // Asuminos los tipos pasados por parametro, que se supone
407
        // son los detectados automaticamente.
408
        if ( automaticTypes != null ) {
409
            for ( int i = 0; i < fieldTypes.length && i < automaticTypes.length; i++ ) {
410
                fieldTypes[i].type = automaticTypes[i];
411
            }
412
        }
413
        // Luego probamos con lo que diga las cabezeras del CVS, sobreescribiendo
414
        // los tipos anteriores en caso de definirse en la cabezara.
415
        for ( int i = 0; i < fieldTypes.length; i++ ) {
416
            if ( !fieldTypes[i].parse(headers.get(i)) ) {
417
                continue;
418
            }
419

  
420
        }
421

  
422
        // Y por ultimo hacemos caso a lo que se haya especificado en los parametros
423
        // de apertura del reader, teniendo esto prioridad sobre todo.
424
        int[] param_types = this.getParameters().getFieldTypes();
425
        if ( param_types != null ) {
426
            for ( int i = 0; i < fieldTypes.length && i < param_types.length; i++ ) {
427
                fieldTypes[i].type = param_types[i];
428
            }
429
        }
430

  
431
        int[] param_sizes = this.getParameters().getFieldSizes();
432
        if ( param_sizes != null ) {
433
            for ( int i = 0; i < param_sizes.length; i++ ) {
434
                if ( param_sizes[i] > 0 ) {
435
                    fieldTypes[i].size = param_sizes[i];
436
                }
437
            }
438
        }
439
        //
440
        // Una vez ya sabemos los tipos de datos rellenamos el feature-type
441
        //
442
        for (FieldTypeParser fieldType : fieldTypes) {
443
            EditableFeatureAttributeDescriptor fad = fType.add(fieldType.name, fieldType.type);
444
            fad.setSize(fieldType.size);
445
            fad.setAllowNull(fieldType.allowNulls);
446
            if (fieldType.type == DataTypes.GEOMETRY && fType.getDefaultGeometryAttributeName() == null) {
447
                fType.setDefaultGeometryAttributeName(fieldType.name);
448
            }
449
        }
450
        String[] pointDimensionNames = this.getParameters().getPointDimensionNames();
451
        if ( pointDimensionNames != null ) {
452
//            ToPointEvaluaror evaluator = new ToPointEvaluaror(pointDimensionNames);
453
            PointAttributeEmulator emulator = new PointAttributeEmulator(pointDimensionNames);
454
            EditableFeatureAttributeDescriptor attr = fType.add("the_geom", DataTypes.GEOMETRY, emulator);
455
            GeometryManager geommgr = GeometryLocator.getGeometryManager();
456
            GeometryType gt;
457
            try {
458
                if ( emulator.fieldNames != null && emulator.fieldNames.length <= 2 ) {
459
                	gt = geommgr.getGeometryType(Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM2D);
460
                } else {
461
                	gt = geommgr.getGeometryType(Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM3D);
462
                }
463
                attr.setGeometryType(gt);
464
            } catch (Exception e) {
465
                LOGGER.warn("Can't set geometry type for the calculated field in JExcel file '" + getFullFileName() + "'.", e);
466
            }
467
        }
468
        return fType;
469
    }
470

  
471
    static class PointAttributeEmulator implements FeatureAttributeEmulator {
472

  
473
        private static final Logger LOGGER = LoggerFactory.getLogger(PointAttributeEmulator.class);
474

  
475
        private static final int XNAME = 0;
476
        private static final int YNAME = 1;
477
        private static final int ZNAME = 2;
478

  
479
        private final GeometryManager geommgr;
480
        private final String[] fieldNames;
481
        private final Coercion toDouble;
482
        private final DataType dataType;
483
        private int errorcount = 0;
484

  
485
        public PointAttributeEmulator(String[] pointDimensionNames) {
486
            if ( pointDimensionNames.length > 2 ) {
487
                this.fieldNames = new String[3];
488
                this.fieldNames[ZNAME] = pointDimensionNames[2];
489
            } else {
490
                this.fieldNames = new String[2];
491
            }
492
            this.fieldNames[XNAME] = pointDimensionNames[0];
493
            this.fieldNames[YNAME] = pointDimensionNames[1];
494
            this.geommgr = GeometryLocator.getGeometryManager();
495
            DataTypesManager datatypeManager = ToolsLocator.getDataTypesManager();
496

  
497
            this.toDouble = datatypeManager.getCoercion(DataTypes.DOUBLE);
498
            this.dataType = datatypeManager.get(DataTypes.GEOMETRY);
499
        }
500

  
501
        @Override
502
        @SuppressWarnings("UseSpecificCatch")
503
        public Object get(Feature feature) {
504
            try {
505
                Object valueX = feature.get(this.fieldNames[XNAME]);
506
                valueX = toDouble.coerce(valueX);
507
                if ( valueX == null ) {
508
                    return null;
509
                }
510
                Object valueY = feature.get(this.fieldNames[YNAME]);
511
                valueY = toDouble.coerce(valueY);
512
                if ( valueY == null ) {
513
                    return null;
514
                }
515
                Object valueZ = null;
516
                if ( this.fieldNames.length > 2 ) {
517
                    valueZ = toDouble.coerce(feature.get(this.fieldNames[ZNAME]));
518
                    if ( valueZ == null ) {
519
                        return null;
520
                    }
521
                }
522

  
523
                double x = ((Double) valueX);
524
                double y = ((Double) valueY);
525
                Point point;
526
                if ( this.fieldNames.length > 2 ) {
527
                    point = geommgr.createPoint(x, y, Geometry.SUBTYPES.GEOM3D);
528
                    double z = ((Double) valueZ);
529
                    point.setCoordinateAt(2, z);
530
                } else {
531
                    point = geommgr.createPoint(x, y, Geometry.SUBTYPES.GEOM2D);
532
                }
533
                return point;
534
            } catch (Exception ex) {
535
                if ( ++errorcount < 5 ) {
536
                    LOGGER.warn("[" + errorcount + "] Can't create point in ODS provider."+
537
                            "XNAME='" + this.fieldNames[XNAME] + 
538
                            "', YNAME='" + this.fieldNames[YNAME] + 
539
                            "', ZNAME='" + ((this.fieldNames.length > 2)? this.fieldNames[ZNAME]: "(2D)") +
540
                            "' feature=" + feature.toString(), ex);
541
                }
542
                return null;
543
            }
544
        }
545

  
546
        @Override
547
        public void set(EditableFeature feature, Object value) {
548
            if ( value == null ) {
549
                return;
550
            }
551
            Point point;
552
            if ( value instanceof MultiPoint ) {
553
                point = (Point) ((MultiPoint) value).getPrimitiveAt(0);
554
            } else {
555
                point = (Point) value;
556
            }
557
            feature.set(this.fieldNames[XNAME], point.getX());
558
            feature.set(this.fieldNames[YNAME], point.getY());
559
            if ( this.fieldNames.length > 2 ) {
560
                feature.set(this.fieldNames[ZNAME], point.getCoordinateAt(2));
561
            }
562
        }
563

  
564
        @Override
565
        public boolean allowSetting() {
566
            return true;
567
        }
568

  
569
        @Override
570
        public String[] getRequiredFieldNames() {
571
            return this.fieldNames;
572
        }
573

  
574
    }
575

  
576

  
577
    @SuppressWarnings("UseSpecificCatch")
578
    private void loadFeatures() throws IOException, DataException,
579
            CoercionException, CloneNotSupportedException {
580
        SimpleReader reader = null;
581

  
582
        try {
583
            List<String> headers;
584
            FeatureStoreProviderServices store = this.getStoreServices();
585

  
586
            boolean ignore_errors = getParameters().getIgnoreErrors();
587

  
588
            reader = this.readerFactory.createReader(this.getParameters());
589
            this.name = reader.getName();
590
            
591
            headers = getParameters().getFieldNames();
592
            if ( headers == null ) {
593
                headers = reader.getFieldNames();
594
            }
595

  
596
            // Initialize the feature types
597
            EditableFeatureType edftype = this.getFeatureType(headers, automaticDetectionOfTypes(reader));
598
            FeatureType ftype = edftype.getNotEditableCopy();
599
            List<FeatureType> ftypes = new ArrayList<>();
600
            ftypes.add(ftype);
601
            store.setFeatureTypes(ftypes, ftype);
602

  
603
            Coercion coercion[] = new Coercion[ftype.size()];
604
            int sizes[] = new int[ftype.size()];
605
            for ( int i = 0; i < ftype.size(); i++ ) {
606
                sizes[i] = -1;
607
                FeatureAttributeDescriptor ad = ftype.getAttributeDescriptor(i);
608
                coercion[i] = ad.getDataType().getCoercion();
609
                if ( ad.getDataType().getType() == DataTypes.STRING ) {
610
                    if ( ad.getSize() == 0 ) {
611
                        // Es un string y no tiene un size asignado.
612
                        // Lo ponemos a cero para calcularlo.
613
                        sizes[i] = 0;
614
                    }
615
                }
616
            }
617
            if ( ftype.getDefaultGeometryAttributeName() != null ) {
618
                this.need_calculate_envelope = true;
619
            }
620

  
621
            Locale locale = getParameters().getLocale();
622
            taskStatus.message("_loading");
623
            int count = 0;
624

  
625
            int count_errors = 0;
626
            
627
            reader.rewind();
628
            List<Object> row = reader.read();
629

  
630
            Object rawvalue;
631
            while ( row != null ) {
632
                taskStatus.setCurValue(++count);
633
                FeatureProvider feature = this.createFeatureProvider(ftype);
634
                for ( int i = 0; i < row.size(); i++ ) {
635
                    rawvalue = row.get(i);
636
                    if( rawvalue instanceof String && StringUtils.isBlank((String)rawvalue) ) {
637
                        rawvalue = null;
638
                    }
639
                    try {
640
                        Object value;
641
                        if ( locale != null && coercion[i] instanceof CoercionWithLocale ) {
642
                            value = ((CoercionWithLocale) (coercion[i])).coerce(rawvalue, locale);
643
                        } else {
644
                            value = coercion[i].coerce(rawvalue);
645
                        }
646
                        feature.set(i, value);
647
                        if ( sizes[i] >= 0 && value != null ) {
648
                            int x = ((String) value).length();
649
                            if ( sizes[i] < x ) {
650
                                sizes[i] = x;
651
                            }
652
                        }
653
                    } catch (RuntimeException ex) {
654
                        if ( !ignore_errors ) {
655
                            throw ex;
656
                        }
657
                        if ( count_errors++ < 10 ) {
658
                            LOGGER.warn("Can't load value of attribute " + i + " in row " + count + ".", ex);
659
                        }
660
                        if ( count_errors == 10 ) {
661
                            LOGGER.info("Too many errors, suppress messages.");
662
                        }
663
                    }
664
                }
665
                this.addFeatureProvider(feature);
666
                row = reader.read();
667
            }
668
            for ( int i = 0; i < ftype.size(); i++ ) {
669
                if ( sizes[i] > 0 ) {
670
                    EditableFeatureAttributeDescriptor efad = ((EditableFeatureAttributeDescriptor) edftype.getAttributeDescriptor(i));
671
                    efad.setSize(sizes[i]);
672
                }
673
            }
674
            // Volvemos a asignar al store el featuretype, ya que puede
675
            // haber cambiado.
676
            ftype = edftype.getNotEditableCopy();
677
            ftypes = new ArrayList<>();
678
            ftypes.add(ftype);
679
            store.setFeatureTypes(ftypes, ftype);
680

  
681
            taskStatus.terminate();
682
        } catch (Exception ex) {
683
            LOGGER.warn("Can't load features from '"+this.readerFactory.getName()+"' file '" + getFullFileName() + "'.", ex);
684
        } finally {
685
            if ( reader != null ) {
686
                try {
687
                    reader.close();
688
                } catch (Exception ex) {
689
                    // Do nothing
690
                }
691
            }
692

  
693
        }
694
    }
695
    
696
    private int[] automaticDetectionOfTypes(SimpleReader reader) throws IOException {
697
        boolean automatic_types_detection = getParameters().getAutomaticTypesDetection();
698
        if ( !automatic_types_detection ) {
699
            return null;
700
        }
701
        int[] types = null;
702
        try {
703
            reader.rewind();
704
            List<String> fieldNames = reader.getFieldNames();
705
            if ( fieldNames == null ) {
706
                fieldNames = getParameters().getFieldNames();
707
            }
708

  
709
            AutomaticDetectionOfTypes x = new AutomaticDetectionOfTypes(
710
                    this.getFullFileName()
711
            );
712
            types = x.detect(
713
                    fieldNames.size(), 
714
                    reader, 
715
                    false, 
716
                    getParameters().getLocale()
717
            );
718
        } catch (Exception ex) {
719
            throw new RuntimeException("Problems reading file '" + getFullFileName() + "'.", ex);
720
        }
721
        return types;
722
    }
723

  
724
    @Override
725
    public boolean hasDynValue(String name) {
726
        if ( DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name) ) {
727
            return true;
728
        } else if ( DataStore.METADATA_CRS.equalsIgnoreCase(name) ) {
729
            return true;
730
        }
731
        return super.hasDynValue(name);
732
    }
733

  
734
    
735
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.lib/src/main/java/org/gvsig/fmap/dal/feature/spi/simpleprovider/SimpleReader.java
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.feature.spi.simpleprovider;
7

  
8
import java.io.File;
9
import java.util.List;
10
import org.gvsig.fmap.dal.feature.spi.simpleprovider.AutomaticDetectionOfTypes.Rows;
11

  
12
/**
13
 *
14
 * @author jjdelcerro
15
 */
16
public interface SimpleReader extends Rows {
17

  
18
    public SimpleReaderFactory getFactory();
19
    
20
    public File getFile();
21
    
22
    public List<String> getFieldNames();
23

  
24
    public List<Object> read();
25
    
26
    public void rewind();
27
    
28
    public void close();
29

  
30
    public String getName();
31
    
32
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.lib/src/main/java/org/gvsig/fmap/dal/feature/spi/simpleprovider/AbstractSimpleSequentialReaderFactory.java
1
package org.gvsig.fmap.dal.feature.spi.simpleprovider;
2

  
3
import java.io.File;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.net.URL;
7
import org.apache.commons.io.FilenameUtils;
8
import org.gvsig.fmap.dal.DALFileLocator;
9
import org.gvsig.fmap.dal.DALLocator;
10
import org.gvsig.fmap.dal.DataParameters;
11
import org.gvsig.fmap.dal.feature.FeatureStoreProviderFactory;
12
import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters;
13
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderFactory;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff