Revision 729

View differences:

org.gvsig.jexcel/tags/org.gvsig.jexcel-1.0.102/org.gvsig.jexcel.provider/src/main/resources/org/gvsig/jexcel/dal/provider/JExcelMetadata.xml
1
<?xml version="1.0"?>
2
<!--
3
Definitions of metadata fields of a shp file.  
4
 -->
5
<definitions>
6
  <version>1.0.0</version>
7
  <classes>
8
    
9
    <class name="JExcel" namespace="Metadata">
10
      <extends>
11
      	<class namespace="Metadata" name="SpatialProvider"/>
12
      </extends>
13
      <description>Metadata of a JExcel store</description>
14
      <fields>
15
      </fields>
16
    </class>
17

  
18
  </classes>
19
</definitions>  
org.gvsig.jexcel/tags/org.gvsig.jexcel-1.0.102/org.gvsig.jexcel.provider/src/main/resources/org/gvsig/jexcel/dal/provider/JExcelParameters.xml
1
<?xml version= "1.0 "?>
2
<definitions>
3
    <version>1.0.0</version>
4
    <classes>
5
        <class name="JExcel">
6
            <extends>
7
                <class namespace="dal" name="ProviderParameters"/>
8
            </extends>
9
            <description>Store the parameters need to open a Excel file</description>
10
            <fields>
11
                <field name="file" label="File" type="file" mandatory="true">
12
                    <description>Excel file</description>
13
                </field>
14
                <field name="locale" label="Locale" type="string" mandatory="true" defaultValue="DEFAULT">
15
                    <description>Locale used to parse double and float values (BCP47 language tag representing the locale)</description>
16
                    <availableValues>
17
                        <value label="Default (use system locale)">DEFAULT</value>
18
                        <value label="Canada (english)">en-CA</value>
19
                        <value label="Canada (french)">fr-CA</value>
20
                        <value label="Chinese">zh</value>
21
                        <value label="English">en</value>
22
                        <value label="French">fr</value>
23
                        <value label="German">de</value>
24
                        <value label="Italian">it</value>
25
                        <value label="Japanese">ja</value>
26
                        <value label="Korea">ko</value>
27
                        <value label="Spain">es</value>
28
                        <value label="Uk">en-GB</value>
29
                        <value label="Us">en-US</value>
30
                    </availableValues>          
31
                </field>
32
                <field name="sheet" label="Sheet to load" type="integer" subtype="jexcel.sheet.selector" mandatory="false" defaultValue="0">
33
                    <description>Sheet to use.</description>
34
                </field>
35
                <field name="isFirstRowHeadeer" label="Use first row as header" type="boolean" mandatory="false" defaultValue="false">
36
                    <description>Check for use first row as header.</description>
37
                </field>
38
                <field name="CRS" type="crs" mandatory="false">
39
                    <description>The coordinate reference system used when use the point parameter to generate a layer</description>
40
                </field>
41
                <field name="point" label="Point (X,Y,Z)" type="string" mandatory="false" defaultValue="" >
42
                    <description>This field allow to add a Geometry column to the table. It is a list of field names separated by commas used as dimensions for the attribute GEOM of type point to add to the table.</description>
43
                </field>
44

  
45
                <field name="header" label="Header" type="string" mandatory="false" defaultValue="" group="Advanced">
46
                    <description>The header or empty if parsed automatically</description>
47
                </field>
48
                <field name="fieldtypes" label="Field types" type="string" mandatory="false" defaultValue="" group="Advanced">
49
                    <description>Comma separated list of field types. If empty strings are assumed (Eg: integer,string,double,boolean,float,geometry).</description>
50
                </field>
51
                <field name="automaticTypesDetection" label="Automatic types detection" type="boolean" mandatory="false" defaultValue="true" group="Advanced">
52
                    <description>This flag activate the autodetection of types in the CSV. Can be slow.</description>
53
                </field>
54
                <field name="ignoreErrors" label="Ignore errors" type="boolean" mandatory="false" defaultValue="false" group="Advanced">
55
                    <description>Set to true to ignore errors during load of data.</description>
56
                </field>
57
            </fields>
58
      
59
        </class>
60
    </classes>
61
</definitions>  
org.gvsig.jexcel/tags/org.gvsig.jexcel-1.0.102/org.gvsig.jexcel.provider/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.jexcel.JExcelLibrary
org.gvsig.jexcel/tags/org.gvsig.jexcel-1.0.102/org.gvsig.jexcel.provider/src/main/java/org/gvsig/jexcel/JExcelSpread.java
1
package org.gvsig.jexcel;
2

  
3
import java.io.File;
4
import java.io.IOException;
5
import java.util.ArrayList;
6
import java.util.Collections;
7
import java.util.List;
8
import java.util.Locale;
9
import java.util.Objects;
10
import org.apache.commons.lang3.StringUtils;
11
import java.awt.Rectangle;
12
import java.util.Arrays;
13
import jxl.Cell;
14
import jxl.Workbook;
15
import org.slf4j.Logger;
16
import org.slf4j.LoggerFactory;
17

  
18
/**
19
 *
20
 * @author jjdelcerro
21
 */
22
@SuppressWarnings("UseSpecificCatch")
23
public class JExcelSpread implements Spread {
24

  
25
    private static final Logger LOGGER = LoggerFactory.getLogger(JExcelSpread.class);
26
    
27
    private static class SheetJExcel implements Sheet {
28

  
29
        private final Workbook workbook;        
30
        private final File spreadFile;
31
        private final int sheetIndex;
32
        private final jxl.Sheet sheetExcel;
33

  
34
        public SheetJExcel(File spreadFile, int sheetIndex) throws IOException {
35
            try {
36
                this.spreadFile = spreadFile;
37
                this.sheetIndex = sheetIndex;
38
                this.workbook = Workbook.getWorkbook(spreadFile);
39
                this.sheetExcel = this.workbook.getSheet(sheetIndex);
40
            } catch (Exception ex) {
41
                throw new IOException("Can't open sheet "+sheetIndex+" from '"+Objects.toString(spreadFile)+"'.", ex);
42
            }
43
        }
44

  
45
        @Override
46
        public int getColumnCount() {
47
            return this.sheetExcel.getColumns();
48
        }
49

  
50
        @Override
51
        public Rectangle getUsedRange() {
52
            return null;
53
        }
54
        
55
        @Override
56
        public List<String> getColumnNames(boolean isFirstLineHedaer) {
57
            int columns = this.getColumnCount();
58
            List<String> columnNames = new ArrayList<>(columns);
59

  
60
            String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
61
            if (columns > abc.length()) {
62
                for (int i = 0; i < columns; i++) {
63
                    columnNames.add("C" + i);
64
                }
65
            } else {
66
                for (int i = 0; i < columns; i++) {
67
                    columnNames.add(abc.substring(i, i + 1));
68
                }
69
            }
70
            if (isFirstLineHedaer) {
71
                for (int col = 0; col < columns; col++) {
72
                    Object value = this.getValueAt(col, 0);
73
                    String s = Objects.toString(value);
74
                    if (!StringUtils.isBlank(s)) {
75
                        columnNames.set(col, s);
76
                    }
77
                }
78
            }
79
            return columnNames;
80
        }
81

  
82
        @Override
83
        public File getFile() {
84
            return this.spreadFile;
85
        }
86

  
87
        @Override
88
        public String getName() {
89
            return this.sheetExcel.getName();
90
        }
91

  
92
        @Override
93
        public int getRowCount() {
94
            return this.sheetExcel.getRows();
95
        }
96

  
97
        @Override
98
        public int getSheetIndex() {
99
            return this.sheetIndex;
100
        }
101

  
102
        @Override
103
        public Object getValueAt(int col, int row) {
104
            Cell cell = this.sheetExcel.getCell(col, row);
105
            Object value = cell.getContents();
106
            return value;
107
        }
108

  
109
    }
110

  
111
    private File spreadFile;
112

  
113
    public JExcelSpread() {
114

  
115
    }
116

  
117
    @Override
118
    public void open(File spreadFile) {
119
        this.spreadFile = spreadFile;
120
    }
121

  
122
    @Override
123
    public File getFile() {
124
        return this.spreadFile;
125
    }
126

  
127
    @Override
128
    public Sheet getSheet(int sheetIndex) {
129
        try {
130
            if (spreadFile == null || sheetIndex < 0) {
131
                return null;
132
            }
133
            return new SheetJExcel(spreadFile, sheetIndex);
134
        } catch (IOException ex) {
135
            return null;
136
        }
137
    }
138

  
139
    @Override
140
    public List<String> getSheetNames() {
141
        try {
142
            List<String> r = new ArrayList<>();
143
            Workbook workbook = Workbook.getWorkbook(spreadFile);
144
            r.addAll(Arrays.asList(workbook.getSheetNames()));
145
            return r;
146
        } catch (Exception ex) {
147
            return Collections.EMPTY_LIST;
148
        }
149
    }
150

  
151
}
org.gvsig.jexcel/tags/org.gvsig.jexcel-1.0.102/org.gvsig.jexcel.provider/src/main/java/org/gvsig/jexcel/Spread.java
1
package org.gvsig.jexcel;
2

  
3
import java.awt.Rectangle;
4
import java.io.File;
5
import java.util.List;
6

  
7
/**
8
 *
9
 * @author jjdelcerro
10
 */
11
public interface Spread {
12

  
13
    public static String NAME = "Excel";
14

  
15
    public static Spread create(File file, Object... args) {
16
        JExcelSpread spread = new JExcelSpread();
17
        spread.open(file);
18
        return spread;
19
    }
20

  
21
    public interface Sheet {
22

  
23
        int getColumnCount();
24

  
25
        List<String> getColumnNames(boolean isFirstLineHedaer);
26

  
27
        File getFile();
28

  
29
        String getName();
30

  
31
        int getRowCount();
32

  
33
        int getSheetIndex();
34

  
35
        Object getValueAt(int col, int row);
36

  
37
        public Rectangle getUsedRange();
38
    }
39

  
40
    public void open(File spreadFile);
41

  
42
    public File getFile();
43

  
44
    public Sheet getSheet(int sheetIndex);
45

  
46
    public List<String> getSheetNames();
47
}
org.gvsig.jexcel/tags/org.gvsig.jexcel-1.0.102/org.gvsig.jexcel.provider/src/main/java/org/gvsig/jexcel/JExcelLibrary.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.jexcel;
25

  
26
import java.util.ArrayList;
27
import java.util.List;
28

  
29
import org.gvsig.fmap.dal.DALFileLibrary;
30
import org.gvsig.fmap.dal.DALFileLocator;
31
import org.gvsig.fmap.dal.DALLibrary;
32
import org.gvsig.fmap.dal.DALLocator;
33
import org.gvsig.fmap.dal.FileHelper;
34
import org.gvsig.fmap.dal.spi.DataManagerProviderServices;
35
import org.gvsig.jexcel.dal.provider.JExcelFilesystemServerProvider;
36
import org.gvsig.jexcel.dal.provider.JExcelStoreParameters;
37
import org.gvsig.jexcel.dal.provider.JExcelStoreProvider;
38
import org.gvsig.jexcel.dal.provider.JExcelStoreProviderFactory;
39
import org.gvsig.jexcel.dynformfield.point.JDynFormFieldExcelPointFieldsFactory;
40
import org.gvsig.jexcel.dynformfield.sheet.JDynFormFieldExcelSheetFactory;
41
import org.gvsig.metadata.exceptions.MetadataException;
42
import org.gvsig.tools.dynform.spi.DynFormSPILocator;
43
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
44
import org.gvsig.tools.library.AbstractLibrary;
45
import org.gvsig.tools.library.LibraryException;
46

  
47
public class JExcelLibrary extends AbstractLibrary {
48

  
49
    @Override
50
    public void doRegistration() {
51
        registerAsServiceOf(DALLibrary.class);
52
        require(DALFileLibrary.class);
53
    }
54

  
55
    @Override
56
    protected void doInitialize() throws LibraryException {
57
    }
58

  
59
    @Override
60
    protected void doPostInitialize() throws LibraryException {
61
        List<Throwable> exs = new ArrayList<Throwable>();
62

  
63
        FileHelper.registerParametersDefinition(
64
                JExcelStoreParameters.PARAMETERS_DEFINITION_NAME,
65
                JExcelStoreParameters.class, "JExcelParameters.xml");
66
        try {
67
            FileHelper.registerMetadataDefinition(
68
                    JExcelStoreProvider.METADATA_DEFINITION_NAME,
69
                    JExcelStoreProvider.class, "JExcelMetadata.xml");
70
        } catch (MetadataException e) {
71
            exs.add(e);
72
        }
73

  
74
        DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator
75
                .getDataManager();
76

  
77
        try {
78
            if (!dataman.getStoreProviders().contains(JExcelStoreProvider.NAME)) {
79
                dataman.registerStoreProviderFactory(new JExcelStoreProviderFactory(JExcelStoreProvider.NAME, JExcelStoreProvider.DESCRIPTION));
80

  
81
            }
82
        } catch (RuntimeException e) {
83
            exs.add(e);
84
        }
85

  
86
        try {
87
            DALFileLocator.getFilesystemServerExplorerManager()
88
                    .registerProvider(JExcelStoreProvider.NAME,
89
                            JExcelStoreProvider.DESCRIPTION,
90
                            JExcelFilesystemServerProvider.class);
91
        } catch (RuntimeException e) {
92
            exs.add(e);
93
        }
94

  
95
        DynFormSPIManager manager = DynFormSPILocator.getDynFormSPIManager();
96
        manager.registerDynFieldFactory(new JDynFormFieldExcelSheetFactory());
97
        manager.registerDynFieldFactory(new JDynFormFieldExcelPointFieldsFactory());
98

  
99
        if (exs.size() > 0) {
100
            throw new LibraryException(this.getClass(), exs);
101
        }
102
    }
103
}
org.gvsig.jexcel/tags/org.gvsig.jexcel-1.0.102/org.gvsig.jexcel.provider/src/main/java/org/gvsig/jexcel/dal/provider/JExcelFilesystemServerProvider.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.jexcel.dal.provider;
25

  
26
import java.io.File;
27

  
28
import org.gvsig.fmap.dal.DALLocator;
29
import org.gvsig.fmap.dal.DataManager;
30
import org.gvsig.fmap.dal.DataServerExplorer;
31
import org.gvsig.fmap.dal.DataStoreParameters;
32
import org.gvsig.fmap.dal.NewDataStoreParameters;
33
import org.gvsig.fmap.dal.exception.CreateException;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.exception.FileNotFoundException;
36
import org.gvsig.fmap.dal.exception.RemoveException;
37
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
38
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
39
import org.gvsig.fmap.dal.serverexplorer.filesystem.impl.AbstractFilesystemServerExplorerProvider;
40
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider;
41
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices;
42

  
43
public class JExcelFilesystemServerProvider extends AbstractFilesystemServerExplorerProvider 
44
	implements FilesystemServerExplorerProvider, ResourceConsumer {
45

  
46
	private FilesystemServerExplorerProviderServices serverExplorer;
47

  
48
	public String getDataStoreProviderName() {
49
		return JExcelStoreProvider.NAME;
50
	}
51

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

  
56
	public boolean accept(File pathname) {
57
		return (pathname.getName().toLowerCase().endsWith(".xls"));
58
	}
59

  
60
	public String getDescription() {
61
		return JExcelStoreProvider.DESCRIPTION;
62
	}
63

  
64
	public DataStoreParameters getParameters(File file) throws DataException {
65
		DataManager manager = DALLocator.getDataManager();
66
		JExcelStoreParameters params = (JExcelStoreParameters) manager
67
				.createStoreParameters(this
68
				.getDataStoreProviderName());
69
		params.setFile(file);
70
		return params;
71
	}
72

  
73
	public boolean canCreate() {
74
		return false;
75
	}
76

  
77
	public boolean canCreate(NewDataStoreParameters parameters) {
78
			throw new UnsupportedOperationException(); 
79
	}
80

  
81
	public void create(NewDataStoreParameters parameters, boolean overwrite)
82
			throws CreateException {
83
		throw new UnsupportedOperationException(); 
84
	}
85

  
86
	public NewDataStoreParameters getCreateParameters() throws DataException {
87
		throw new UnsupportedOperationException(); 
88
	}
89

  
90
	public void initialize(
91
			FilesystemServerExplorerProviderServices serverExplorer) {
92
		this.serverExplorer = serverExplorer;
93
	}
94

  
95
	public void remove(DataStoreParameters parameters) throws RemoveException {
96
		File file = ((JExcelStoreParameters) parameters).getFile();
97
		if (!file.exists()) {
98
			throw new RemoveException(this.getDataStoreProviderName(),
99
					new FileNotFoundException(file));
100
		}
101
		if (!file.delete()) {
102
			// FIXME throws ???
103
		}
104

  
105
	}
106

  
107
	public boolean closeResourceRequested(ResourceProvider resource) {
108
		// while it is using a resource anyone can't close it
109
		return false;
110
	}
111

  
112
	public void resourceChanged(ResourceProvider resource) {
113
		//Do nothing
114

  
115
	}
116

  
117

  
118
}
org.gvsig.jexcel/tags/org.gvsig.jexcel-1.0.102/org.gvsig.jexcel.provider/src/main/java/org/gvsig/jexcel/dal/provider/JExcelStoreProvider.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.jexcel.dal.provider;
25

  
26
import java.io.File;
27
import java.io.IOException;
28
import java.text.SimpleDateFormat;
29
import java.util.ArrayList;
30
import java.util.HashMap;
31
import java.util.Iterator;
32
import java.util.List;
33
import java.util.Locale;
34
import jxl.Cell;
35
import jxl.Sheet;
36
import jxl.Workbook;
37
import jxl.read.biff.BiffException;
38

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

  
96
public class JExcelStoreProvider extends AbstractMemoryStoreProvider implements
97
        ResourceConsumer {
98

  
99
    private static final Logger logger = LoggerFactory.getLogger(JExcelStoreProvider.class);
100

  
101
    public static final String NAME = "JExcel";
102
    public static final String DESCRIPTION = "JExcel file";
103

  
104
    public static final String METADATA_DEFINITION_NAME = NAME;
105

  
106
    private ResourceProvider resource;
107

  
108
    private long counterNewsOIDs = 0;
109
    private Envelope envelope;
110
    private boolean need_calculate_envelope = false;
111
    private SimpleTaskStatus taskStatus;
112
    private String sheetName = "";
113

  
114
    public JExcelStoreProvider(JExcelStoreParameters parameters,
115
            DataStoreProviderServices storeServices) throws InitializeException {
116
        super(
117
                parameters,
118
                storeServices,
119
                FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME)
120
        );
121

  
122
        TaskStatusManager manager = ToolsLocator.getTaskStatusManager();
123
        this.taskStatus = manager.createDefaultSimpleTaskStatus("JExcel");
124

  
125
        counterNewsOIDs = 0;
126

  
127
        File file = getJExcelParameters().getFile();
128
        resource = this.createResource(
129
                FileResource.NAME,
130
                new Object[]{file.getAbsolutePath()}
131
        );
132

  
133
        resource.addConsumer(this);
134
        initializeFeatureTypes();
135
    }
136

  
137
    private JExcelStoreParameters getJExcelParameters() {
138
        return (JExcelStoreParameters) this.getParameters();
139
    }
140

  
141
    public String getProviderName() {
142
        return NAME;
143
    }
144

  
145
    public boolean allowWrite() {
146
        return false;
147
    }
148

  
149
    private String getFullFileName() {
150
        // Usar solo para mostrar mensajes en el logger.
151
        String s = "(unknow)";
152
        try {
153
            s = getJExcelParameters().getFile().getAbsolutePath();
154
        } catch (Exception e2) {
155
            s = "(unknow)";
156
        }
157
        return s;
158
    }
159

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

  
179
    public DataServerExplorer getExplorer() throws ReadException {
180
        DataManager manager = DALLocator.getDataManager();
181
        FilesystemServerExplorerParameters params;
182
        try {
183
            params = (FilesystemServerExplorerParameters) manager
184
                    .createServerExplorerParameters(FilesystemServerExplorer.NAME);
185
            params.setRoot(this.getJExcelParameters().getFile().getParent());
186
            return manager.openServerExplorer(FilesystemServerExplorer.NAME, params);
187
        } catch (DataException e) {
188
            throw new ReadException(this.getProviderName(), e);
189
        } catch (ValidateDataParametersException e) {
190
            throw new ReadException(this.getProviderName(), e);
191
        }
192

  
193
    }
194

  
195
    public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds, Iterator originalFeatureTypesUpdated) throws PerformEditingException {
196
        throw new UnsupportedOperationException();
197
    }
198

  
199
    public boolean closeResourceRequested(ResourceProvider resource) {
200
        return true;
201
    }
202

  
203
    public int getOIDType() {
204
        return DataTypes.LONG;
205
    }
206

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

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

  
215
    public void beginAppend() {
216
        throw new UnsupportedOperationException();
217
    }
218

  
219
    public void endAppend() {
220
        throw new UnsupportedOperationException();
221
    }
222

  
223
    public void saveToState(PersistentState state) throws PersistenceException {
224
        throw new NotYetImplemented();
225
    }
226

  
227
    public void loadFromState(PersistentState state) throws PersistenceException {
228
        throw new NotYetImplemented();
229
    }
230

  
231
    public Object createNewOID() {
232
        return new Long(counterNewsOIDs++);
233
    }
234

  
235
    protected void initializeFeatureTypes() throws InitializeException {
236
        try {
237
            this.open();
238
        } catch (OpenException e) {
239
            throw new InitializeException(this.getProviderName(), e);
240
        }
241
    }
242

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

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

  
271
        this.need_calculate_envelope = false;
272
        return this.envelope;
273
    }
274

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

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

  
299
    public Object getSourceId() {
300
        return this.getJExcelParameters().getFile();
301
    }
302

  
303
    public String getName() {
304
        String name = this.getJExcelParameters().getFile().getName();
305
        if( StringUtils.isBlank(this.sheetName) ) {
306
            return FilenameUtils.getBaseName(name);
307
        }
308
        return FilenameUtils.getBaseName(name)+"."+this.sheetName;
309
    }
310

  
311
    public String getFullName() {
312
        return this.getJExcelParameters().getFile().getAbsolutePath();
313
    }
314

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

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

  
389
    }
390

  
391
    private EditableFeatureType getFeatureType(String headers[], int automaticTypes[]) {
392
        EditableFeatureType fType = getStoreServices().createFeatureType(this.getName());
393
        fType.setHasOID(true);
394
        DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager();
395

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

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

  
418
        }
419

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

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

  
473
    static class PointAttributeEmulator implements FeatureAttributeEmulator {
474

  
475
        private static final Logger logger = LoggerFactory.getLogger(PointAttributeEmulator.class);
476

  
477
        private static final int XNAME = 0;
478
        private static final int YNAME = 1;
479
        private static final int ZNAME = 2;
480

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

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

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

  
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).doubleValue();
524
                double y = ((Double) valueY).doubleValue();
525
                Point point = null;
526
                if ( this.fieldNames.length > 2 ) {
527
                    point = geommgr.createPoint(x, y, Geometry.SUBTYPES.GEOM3D);
528
                    double z = ((Double) valueZ).doubleValue();
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 JExcel provider. XNAME='"
537
                            + this.fieldNames[XNAME] + "', YNAME='" + this.fieldNames[YNAME] + "' feature=" + feature.toString(), ex);
538
                }
539
                return null;
540
            }
541
        }
542

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

  
560
        public boolean allowSetting() {
561
            return true;
562
        }
563

  
564
        public String[] getRequiredFieldNames() {
565
            return this.fieldNames;
566
        }
567

  
568
    }
569

  
570
    static class JExcelReader {
571

  
572
        Workbook workbook = null;
573
        Sheet sheet = null;
574
        boolean isFirstLineHedaer = false;
575
        int currentRow = 0;
576
        String[] row = null;
577
        private int sheetIndex;
578

  
579
        public JExcelReader(File f, int sheet, boolean isFirstLineHedaer)
580
                throws IOException, BiffException {
581
            this.currentRow = 0;
582
            this.sheetIndex = sheet;
583
            this.workbook = Workbook.getWorkbook(f);
584
            this.sheet = this.workbook.getSheet(sheet);
585
            this.row = new String[this.sheet.getColumns()];
586
            this.isFirstLineHedaer = isFirstLineHedaer;
587
            if ( this.isFirstLineHedaer ) {
588
                this.currentRow++;
589
            }
590
        }
591

  
592
        public String[] getHeader() {
593
            int columns = this.sheet.getColumns();
594
            String header[] = new String[columns];
595
            String abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
596
            if ( columns > abc.length() ) {
597
                for ( int i = 0; i < columns; i++ ) {
598
                    header[i] = "C" + i;
599
                }
600
            } else {
601
                for ( int i = 0; i < columns; i++ ) {
602
                    header[i] = abc.substring(i, i+1);
603
                }
604
            }
605
            if ( this.isFirstLineHedaer ) {
606
                for ( int col = 0; col < columns; col++ ) {
607
                    Cell cell = this.sheet.getCell(col, 0);
608
                    String s = cell.getContents().trim();
609
                    if ( !StringUtils.isBlank(s) ) {
610
                        header[col] = s;
611
                    }
612
                }
613
            }
614
            return header;
615
        }
616

  
617
        public String[] read() {
618
            if( this.currentRow>=this.sheet.getRows() ) {
619
                return null;
620
            }
621
            int columns = this.sheet.getColumns();
622
            for ( int col = 0; col < columns; col++ ) {
623
                Cell cell = this.sheet.getCell(col, this.currentRow);
624
                this.row[col] = cell.getContents();
625
            }
626
            this.currentRow++;
627
            return row;
628
        }
629

  
630
        public void close() {
631
            this.workbook.close();
632
        }
633
        
634
        public String getSheetName() {
635
            String[] names = this.workbook.getSheetNames();
636
            return names[this.sheetIndex];
637
        }
638
    }
639

  
640
    private void loadFeatures() throws IOException, DataException,
641
            CoercionException, CloneNotSupportedException {
642
        JExcelReader reader = null;
643

  
644
        try {
645
            String headers[] = null;
646
            FeatureStoreProviderServices store = this.getStoreServices();
647

  
648
            boolean ignore_errors = JExcelStoreParameters.getIgnoreErrors(getJExcelParameters());
649

  
650
            reader = new JExcelReader(
651
                    this.getJExcelParameters().getFile(),
652
                    JExcelStoreParameters.getSheetIndex(getJExcelParameters()),
653
                    JExcelStoreParameters.isFirstRowHeader(getJExcelParameters())
654
            );
655
            this.sheetName = reader.getSheetName();
656
            
657
            headers = JExcelStoreParameters.getHeaders(getJExcelParameters());
658
            if ( headers == null ) {
659
                headers = reader.getHeader();
660
            }
661

  
662
            // Initialize the feature types
663
            EditableFeatureType edftype = this.getFeatureType(headers, automaticDetectionOfTypes());
664
            FeatureType ftype = edftype.getNotEditableCopy();
665
            List<FeatureType> ftypes = new ArrayList<FeatureType>();
666
            ftypes.add(ftype);
667
            store.setFeatureTypes(ftypes, ftype);
668

  
669
            Coercion coercion[] = new Coercion[ftype.size()];
670
            int sizes[] = new int[ftype.size()];
671
            for ( int i = 0; i < ftype.size(); i++ ) {
672
                sizes[i] = -1;
673
                FeatureAttributeDescriptor ad = ftype.getAttributeDescriptor(i);
674
                coercion[i] = ad.getDataType().getCoercion();
675
                if ( ad.getDataType().getType() == DataTypes.STRING ) {
676
                    if ( ad.getSize() == 0 ) {
677
                        // Es un string y no tiene un size asignado.
678
                        // Lo ponemos a cero para calcularlo.
679
                        sizes[i] = 0;
680
                    }
681
                }
682
            }
683
            if ( ftype.getDefaultGeometryAttributeName() != null ) {
684
                this.need_calculate_envelope = true;
685
            }
686

  
687
            Locale locale = JExcelStoreParameters.getLocale(getJExcelParameters());
688
            taskStatus.message("_loading");
689
            int count = 0;
690

  
691
            int count_errors = 0;
692
            String[] row = reader.read();
693

  
694
            Object rawvalue = null;
695
            while ( row != null ) {
696
                taskStatus.setCurValue(++count);
697
                FeatureProvider feature = this.createFeatureProvider(ftype);
698
                for ( int i = 0; i < row.length; i++ ) {
699
                    rawvalue = row [i];
700
                    if( rawvalue instanceof String && StringUtils.isBlank((String)rawvalue) ) {
701
                        rawvalue = null;
702
                    }
703
                    try {
704
                        Object value = null;
705
                        value = coercion[i].coerce(rawvalue);
706
                        feature.set(i, value);
707
                        if ( sizes[i] >= 0 && value != null ) {
708
                            int x = ((String) value).length();
709
                            if ( sizes[i] < x ) {
710
                                sizes[i] = x;
711
                            }
712
                        }
713
                    } catch (RuntimeException ex) {
714
                        if ( !ignore_errors ) {
715
                            throw ex;
716
                        }
717
                        if ( count_errors++ < 10 ) {
718
                            logger.warn("Can't load value of attribute " + i + " in row " + count + ".", ex);
719
                        }
720
                        if ( count_errors == 10 ) {
721
                            logger.info("Too many errors, suppress messages.");
722
                        }
723
                    }
724
                }
725
                this.addFeatureProvider(feature);
726
                row = reader.read();
727
            }
728
            for ( int i = 0; i < ftype.size(); i++ ) {
729
                if ( sizes[i] > 0 ) {
730
                    EditableFeatureAttributeDescriptor efad = ((EditableFeatureAttributeDescriptor) edftype.getAttributeDescriptor(i));
731
                    efad.setSize(sizes[i]);
732
                }
733
            }
734
            // Volvemos a asignar al store el featuretype, ya que puede
735
            // haber cambiado.
736
            ftype = edftype.getNotEditableCopy();
737
            ftypes = new ArrayList<FeatureType>();
738
            ftypes.add(ftype);
739
            store.setFeatureTypes(ftypes, ftype);
740

  
741
            taskStatus.terminate();
742
        } catch (Exception ex) {
743
            logger.warn("Can't load features from excel file '" + getFullFileName() + "'.", ex);
744
        } finally {
745
            if ( reader != null ) {
746
                try {
747
                    reader.close();
748
                } catch (Exception ex) {
749
                    // Do nothing
750
                }
751
                reader = null;
752
            }
753

  
754
        }
755
    }
756

  
757
    private int[] automaticDetectionOfTypes() throws IOException {
758
        boolean automatic_types_detection = JExcelStoreParameters.getAutomaticTypesDetection(getJExcelParameters());
759
        if ( !automatic_types_detection ) {
760
            return null;
761
        }
762
        final int T_INT = 0;
763
        final int T_FLOAT = 1;
764
        final int T_DOUBLE = 2;
765
        final int T_LONG = 3;
766
        final int T_URL = 4;
767
        final int T_DATE = 5;
768
        boolean possibleDataTypes[][] = null;
769
        Locale locale = null;
770
        JExcelReader reader = null;
771
        int[] types = null;
772

  
773
        String headers[] = null;
774
        SimpleDateFormat dateFormat = new SimpleDateFormat();
775

  
776
        try {
777
            reader = new JExcelReader(
778
                    this.getJExcelParameters().getFile(),
779
                    JExcelStoreParameters.getSheetIndex(getJExcelParameters()),
780
                    JExcelStoreParameters.isFirstRowHeader(getJExcelParameters())
781
            );
782

  
783
            headers = reader.getHeader();
784
            if ( headers == null ) {
785
                headers = JExcelStoreParameters.getHeaders(getJExcelParameters());
786
            }
787
            types = new int[headers.length];
788

  
789
            
790
            
791
            possibleDataTypes = new boolean[headers.length][6];
792
            for ( int i = 0; i < possibleDataTypes.length; i++ ) {
793
                for ( int j = 0; j < 4; j++ ) {
794
                    possibleDataTypes[i][j] = true;
795
                }
796
            }
797
            locale = JExcelStoreParameters.getLocale(getJExcelParameters());
798
            if ( locale == null ) {
799
                locale = Locale.getDefault();
800
            }
801

  
802
            Coercion coercion[] = new Coercion[6];
803
            DataTypesManager typeManager = ToolsLocator.getDataTypesManager();
804
            coercion[T_DOUBLE] = typeManager.getCoercion(DataTypes.DOUBLE);
805
            coercion[T_FLOAT] = typeManager.getCoercion(DataTypes.FLOAT);
806
            coercion[T_INT] = typeManager.getCoercion(DataTypes.INT);
807
            coercion[T_LONG] = typeManager.getCoercion(DataTypes.LONG);
808
            coercion[T_DATE] = typeManager.getCoercion(DataTypes.DATE);
809
            coercion[T_URL] = typeManager.getCoercion(DataTypes.URL);
810
            
811
//            CoercionWithLocale toDouble = (CoercionWithLocale) typeManager.getCoercion(DataTypes.DOUBLE);
812
//            CoercionWithLocale toFloat = (CoercionWithLocale) typeManager.getCoercion(DataTypes.FLOAT);
813

  
814
            String[] row = reader.read();
815

  
816
            while ( row != null ) {
817
                for ( int i = 0; i < row.length; i++ ) {
818
                    Object rawvalue = row[i];
819
                    if( rawvalue instanceof String && StringUtils.isBlank((String)rawvalue) ) {
820
                        rawvalue = null;
821
                    }
822
                    Object value = null;
823
                    if ( possibleDataTypes[i][T_DOUBLE] ) {
824
                        try {
825
//                            value = toDouble.coerce(rawvalue, locale);
826
                            value = coercion[T_DOUBLE].coerce(rawvalue);
827
                            possibleDataTypes[i][T_DOUBLE] = true;
828
                        } catch (Exception ex) {
829
                            possibleDataTypes[i][T_DOUBLE] = false;
830
                        }
831
                    }
832
                    if ( possibleDataTypes[i][T_FLOAT] ) {
833
                        try {
834
//                            value = toFloat.coerce(rawvalue, locale);
835
                            value = coercion[T_FLOAT].coerce(rawvalue);
836
                            possibleDataTypes[i][T_FLOAT] = true;
837
                        } catch (Exception ex) {
838
                            possibleDataTypes[i][T_FLOAT] = false;
839
                        }
840
                    }
841
                    if ( possibleDataTypes[i][T_LONG] ) {
842
                        try {
843
//                            value = Long.parseLong((String) rawvalue);
844
                            value = coercion[T_LONG].coerce(rawvalue);
845
                            possibleDataTypes[i][T_LONG] = true;
846
                        } catch (Exception ex) {
847
                            possibleDataTypes[i][T_LONG] = false;
848
                        }
849
                    }
850
                    if ( possibleDataTypes[i][T_INT] ) {
851
                        try {
852
//                            value = Integer.parseInt((String) rawvalue);
853
                            value = coercion[T_INT].coerce(rawvalue);
854
                            possibleDataTypes[i][T_INT] = true;
855
                        } catch (Exception ex) {
856
                            possibleDataTypes[i][T_INT] = false;
857
                        }
858
                    }
859
                    if ( possibleDataTypes[i][T_DATE] ) {
860
                        try {
861
//                            value = dateFormat.parse((String) rawvalue);
862
                            value = coercion[T_DATE].coerce(rawvalue);
863
                            possibleDataTypes[i][T_DATE] = true;
864
                        } catch (Exception ex) {
865
                            possibleDataTypes[i][T_DATE] = false;
866
                        }
867
                    }
868
                    if ( possibleDataTypes[i][T_URL] ) {
869
                        try {
870
//                            value = new URL((String) rawvalue);
871
                            value = coercion[T_URL].coerce(rawvalue);
872
                            possibleDataTypes[i][T_URL] = true;
873
                        } catch (Exception ex) {
874
                            possibleDataTypes[i][T_URL] = false;
875
                        }
876
                    }
877
                }
878
                row = reader.read();
879
            }
880
        } catch (Exception ex) {
881
            logger.warn("Can't detect automatic values.", ex);
882
        } finally {
883
            if ( reader != null ) {
884
                try {
885
                    reader.close();
886
                } catch (Exception ex) {
887
                    // Do nothing
888
                }
889
                reader = null;
890
            }
891
        }
892
        if ( types != null && possibleDataTypes != null ) {
893
            for ( int i = 0; i < possibleDataTypes.length; i++ ) {
894
                if ( possibleDataTypes[i][T_INT] ) {
895
                    types[i] = DataTypes.INT;
896
                    continue;
897
                }
898
                if ( possibleDataTypes[i][T_LONG] ) {
899
                    types[i] = DataTypes.LONG;
900
                    continue;
901
                }
902
                if ( possibleDataTypes[i][T_FLOAT] ) {
903
                    types[i] = DataTypes.FLOAT;
904
                    continue;
905
                }
906
                if ( possibleDataTypes[i][T_DOUBLE] ) {
907
                    types[i] = DataTypes.DOUBLE;
908
                    continue;
909
                }
910
                if ( possibleDataTypes[i][T_URL] ) {
911
                    types[i] = DataTypes.URL;
912
                    continue;
913
                }
914
                if ( possibleDataTypes[i][T_DATE] ) {
915
                    types[i] = DataTypes.DATE;
916
                    continue;
917
                }
918
                types[i] = DataTypes.STRING;
919
            }
920
        }
921
        return types;
922
    }
923

  
924
}
org.gvsig.jexcel/tags/org.gvsig.jexcel-1.0.102/org.gvsig.jexcel.provider/src/main/java/org/gvsig/jexcel/dal/provider/JExcelStoreProviderFactory.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.jexcel.dal.provider;
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.FeatureStoreProviderFactory;
30
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProviderFactory;
31
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff