Revision 1178
org.gvsig.jexcel/tags/org.gvsig.jexcel-1.0.147/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.147/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.147/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.147/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.147/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.147/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.147/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; |
|
32 |
import org.gvsig.tools.dynobject.DynObject; |
|
33 |
|
|
34 |
public class JExcelStoreProviderFactory extends AbstractFeatureStoreProviderFactory implements FeatureStoreProviderFactory{ |
|
35 |
|
|
36 |
public JExcelStoreProviderFactory(String name, String description) { |
|
37 |
super(name, description); |
|
38 |
} |
|
39 |
|
|
40 |
public DataStoreProvider createProvider(DataParameters parameters, |
|
41 |
DataStoreProviderServices providerServices) |
|
42 |
throws InitializeException { |
|
43 |
return new JExcelStoreProvider((JExcelStoreParameters) parameters, providerServices); |
|
44 |
} |
|
45 |
|
|
46 |
public DynObject createParameters() { |
|
47 |
return new JExcelStoreParameters(); |
|
48 |
} |
|
49 |
|
|
50 |
public int allowCreate() { |
|
51 |
return NO; |
|
52 |
} |
|
53 |
|
|
54 |
public int allowWrite() { |
|
55 |
return NO; |
|
56 |
} |
|
57 |
|
|
58 |
public int allowRead() { |
|
59 |
return YES; |
|
60 |
} |
|
61 |
|
|
62 |
public int hasRasterSupport() { |
|
63 |
return NO; |
|
64 |
} |
|
65 |
|
|
66 |
public int hasTabularSupport() { |
|
67 |
return YES; |
|
68 |
} |
|
69 |
|
|
70 |
public int hasVectorialSupport() { |
|
71 |
return YES; |
|
72 |
} |
|
73 |
|
|
74 |
public int allowMultipleGeometryTypes() { |
|
75 |
return YES; |
|
76 |
} |
|
77 |
|
|
78 |
public int allowEditableFeatureType() { |
|
79 |
return NO; |
|
80 |
} |
|
81 |
|
|
82 |
} |
org.gvsig.jexcel/tags/org.gvsig.jexcel-1.0.147/org.gvsig.jexcel.provider/src/main/java/org/gvsig/jexcel/dal/provider/JExcelStoreParameters.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.util.Locale; |
|
28 |
import org.apache.commons.lang3.StringEscapeUtils; |
|
29 |
import org.apache.commons.lang3.StringUtils; |
|
30 |
import org.cresques.cts.IProjection; |
|
31 |
import org.gvsig.fmap.dal.FileHelper; |
|
32 |
import org.gvsig.fmap.dal.OpenDataStoreParameters; |
|
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.gvsig.tools.dynobject.DynObject; |
|
41 |
import org.slf4j.Logger; |
|
42 |
import org.slf4j.LoggerFactory; |
|
43 |
|
|
44 |
public class JExcelStoreParameters extends AbstractDataParameters implements |
|
45 |
OpenFeatureStoreParameters, FilesystemStoreParameters { |
|
46 |
|
|
47 |
private static final Logger logger = LoggerFactory.getLogger(JExcelStoreParameters.class); |
|
48 |
|
|
49 |
public static final String PARAMETERS_DEFINITION_NAME = "JExcel"; |
|
50 |
|
|
51 |
private static final String FILE = "file"; |
|
52 |
private static final String IGNOREERRORS = "ignoreErrors"; |
|
53 |
private static final String CRS = "CRS"; |
|
54 |
private static final String FIELDTYPES = "fieldtypes"; |
|
55 |
private static final String LOCALE = "locale"; |
|
56 |
private static final String SHEET = "sheet"; |
|
57 |
private static final String ISFIRSTROWHEADER = "isFirstRowHeadeer"; |
|
58 |
private static final String AUTOMATICTYPESDETECTION = "automaticTypesDetection"; |
|
59 |
private static final String HEADER = "header"; |
|
60 |
|
|
61 |
private DelegatedDynObject parameters; |
|
62 |
|
|
63 |
public JExcelStoreParameters() { |
|
64 |
this(PARAMETERS_DEFINITION_NAME); |
|
65 |
} |
|
66 |
|
|
67 |
protected JExcelStoreParameters(String parametersDefinitionName) { |
|
68 |
this(parametersDefinitionName, JExcelStoreProvider.NAME); |
|
69 |
} |
|
70 |
|
|
71 |
public JExcelStoreParameters(String parametersDefinitionName, String name) { |
|
72 |
super(); |
|
73 |
this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName); |
|
74 |
this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name); |
|
75 |
} |
|
76 |
|
|
77 |
public String getDataStoreName() { |
|
78 |
return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME); |
|
79 |
} |
|
80 |
|
|
81 |
public String getDescription() { |
|
82 |
return this.getDynClass().getDescription(); |
|
83 |
} |
|
84 |
|
|
85 |
protected DelegatedDynObject getDelegatedDynObject() { |
|
86 |
return parameters; |
|
87 |
} |
|
88 |
|
|
89 |
public boolean isValid() { |
|
90 |
if ( getFileName(this) == null ) { |
|
91 |
return false; |
|
92 |
} |
|
93 |
return true; |
|
94 |
} |
|
95 |
|
|
96 |
public File getFile() { |
|
97 |
return (File) this.getDynValue(FILE); |
|
98 |
} |
|
99 |
|
|
100 |
public void setFile(File file) { |
|
101 |
this.setDynValue(FILE, file); |
|
102 |
} |
|
103 |
|
|
104 |
static IProjection getCRS(DynObject dynobj) { |
|
105 |
return (IProjection) dynobj.getDynValue(CRS); |
|
106 |
} |
|
107 |
|
|
108 |
static String getFileName(DynObject dynobj) { |
|
109 |
File f = (File) dynobj.getDynValue(FILE); |
|
110 |
if ( f == null ) { |
|
111 |
return null; |
|
112 |
} |
|
113 |
return f.getPath(); |
|
114 |
} |
|
115 |
|
|
116 |
static File getFile(DynObject dynobj) { |
|
117 |
File f = (File) dynobj.getDynValue(FILE); |
|
118 |
return f; |
|
119 |
} |
|
120 |
|
|
121 |
static Locale getLocale(DynObject dynobj) { |
|
122 |
try { |
|
123 |
String s = (String) dynobj.getDynValue(LOCALE); |
|
124 |
if ( s.trim().length() == 0 ) { |
|
125 |
return null; |
|
126 |
} |
|
127 |
if ( "DEFAULT".equalsIgnoreCase(s.trim()) ) { |
|
128 |
return Locale.getDefault(); |
|
129 |
} |
|
130 |
Locale locale = null; |
|
131 |
// locale = Locale.forLanguageTag(s); // Since java 1.7 |
|
132 |
String[] ss = s.split("-"); |
|
133 |
switch (ss.length) { |
|
134 |
case 1: |
|
135 |
locale = new Locale(ss[0]); |
|
136 |
break; |
|
137 |
case 2: |
|
138 |
locale = new Locale(ss[0], ss[1]); |
|
139 |
break; |
|
140 |
case 3: |
|
141 |
default: |
|
142 |
locale = new Locale(ss[0], ss[1], ss[2]); |
|
143 |
break; |
|
144 |
} |
|
145 |
return locale; |
|
146 |
} catch (Exception ex) { |
|
147 |
logger.warn("Can't get locale from JExcel parameters.", ex); |
|
148 |
return null; |
|
149 |
} |
|
150 |
} |
|
151 |
|
|
152 |
private static String getDelimiter(String line) { |
|
153 |
String sep = null; |
|
154 |
// Cuiaddo con los ":", los he puesto al final a proposito |
|
155 |
// ya que podian estar en la cadena para separar el size |
|
156 |
// size de cada tipo. |
|
157 |
String seps = ",;-|@#/+$%&!:"; |
|
158 |
for ( int i = 0; i < seps.length(); i++ ) { |
|
159 |
sep = seps.substring(i, 1); |
|
160 |
if ( line.contains(seps.substring(i, 1)) ) { |
|
161 |
break; |
|
162 |
} |
|
163 |
sep = null; |
|
164 |
} |
|
165 |
return sep; |
|
166 |
} |
|
167 |
|
|
168 |
static String[] getPointDimensionNames(DynObject dynobj) { |
|
169 |
String s = (String) dynobj.getDynValue("point"); |
|
170 |
if ( StringUtils.isEmpty(s) ) { |
|
171 |
return null; |
|
172 |
} |
|
173 |
return s.split(","); |
|
174 |
} |
|
175 |
|
|
176 |
static boolean getIgnoreErrors(DynObject dynobj) { |
|
177 |
Boolean b = (Boolean) dynobj.getDynValue(IGNOREERRORS); |
|
178 |
if ( b == null ) { |
|
179 |
return false; |
|
180 |
} |
|
181 |
return b.booleanValue(); |
|
182 |
} |
|
183 |
|
|
184 |
static int[] getFieldTypes(DynObject dynobj) { |
|
185 |
String s = (String) dynobj.getDynValue(FIELDTYPES); |
|
186 |
if ( StringUtils.isEmpty(s) ) { |
|
187 |
return null; |
|
188 |
} |
|
189 |
String sep = getDelimiter(s); |
|
190 |
if ( sep == null ) { |
|
191 |
return null; |
|
192 |
} |
|
193 |
DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager(); |
|
194 |
String fieldTypeNames[] = s.split("[" + sep + "]"); |
|
195 |
int fieldTypes[] = new int[fieldTypeNames.length]; |
|
196 |
for ( int i = 0; i < fieldTypeNames.length; i++ ) { |
|
197 |
s = fieldTypeNames[i].trim(); |
|
198 |
if ( s.contains(":") ) { |
|
199 |
s = s.split(":")[0]; |
|
200 |
} |
|
201 |
fieldTypes[i] = dataTypeManager.getType(s); |
|
202 |
} |
|
203 |
return fieldTypes; |
|
204 |
} |
|
205 |
|
|
206 |
static int[] getFieldSizes(DynObject dynobj) { |
|
207 |
String s = (String) dynobj.getDynValue(FIELDTYPES); |
|
208 |
if ( StringUtils.isEmpty(s) ) { |
|
209 |
return null; |
|
210 |
} |
|
211 |
String sep = getDelimiter(s); |
|
212 |
if ( sep == null ) { |
|
213 |
return null; |
|
214 |
} |
|
215 |
String fieldTypeNames[] = s.split("[" + sep + "]"); |
|
216 |
int fieldSizes[] = new int[fieldTypeNames.length]; |
|
217 |
for ( int i = 0; i < fieldTypeNames.length; i++ ) { |
|
218 |
String fieldtype = fieldTypeNames[i].trim(); |
|
219 |
if ( fieldtype.contains(":") ) { |
|
220 |
try { |
|
221 |
s = fieldtype.split(":")[1]; |
|
222 |
fieldSizes[i] = Integer.parseInt(s); |
|
223 |
} catch (Exception ex) { |
|
224 |
logger.warn("Can't get size of field " + i + " (" + fieldtype + ").", ex); |
|
225 |
} |
|
226 |
} else { |
|
227 |
fieldSizes[i] = 0; |
|
228 |
} |
|
229 |
} |
|
230 |
return fieldSizes; |
|
231 |
} |
|
232 |
|
|
233 |
static int getSheetIndex(DynObject dynobj) { |
|
234 |
Integer sheet = (Integer) dynobj.getDynValue(SHEET); |
|
235 |
if ( sheet == null ) { |
|
236 |
return 0; |
|
237 |
} |
|
238 |
return sheet.intValue(); |
|
239 |
} |
|
240 |
|
|
241 |
static String getHeader(DynObject dynobj) { |
|
242 |
String s = (String) dynobj.getDynValue(HEADER); |
|
243 |
s = StringEscapeUtils.unescapeJava(s); |
|
244 |
if ( StringUtils.isBlank(s) ) { |
|
245 |
return null; |
|
246 |
} |
|
247 |
return s; |
|
248 |
} |
|
249 |
|
|
250 |
static String[] getHeaders(DynObject dynobj) { |
|
251 |
String s = getHeader(dynobj); |
|
252 |
if ( StringUtils.isBlank(s) ) { |
|
253 |
return null; |
|
254 |
} |
|
255 |
String sep = getDelimiter(s); |
|
256 |
if ( sep == null ) { |
|
257 |
// Chungo |
|
258 |
return null; |
|
259 |
} |
|
260 |
String[] ss = s.split("[" + sep + "]"); |
|
261 |
return ss; |
|
262 |
} |
|
263 |
|
|
264 |
static boolean getAutomaticTypesDetection(DynObject dynobj) { |
|
265 |
Boolean b = (Boolean) dynobj.getDynValue(AUTOMATICTYPESDETECTION); |
|
266 |
if ( b == null ) { |
|
267 |
return false; |
|
268 |
} |
|
269 |
return b.booleanValue(); |
|
270 |
} |
|
271 |
|
|
272 |
static boolean isFirstRowHeader(DynObject dynobj) { |
|
273 |
Boolean b = (Boolean) dynobj.getDynValue(ISFIRSTROWHEADER); |
|
274 |
if ( b == null ) { |
|
275 |
return false; |
|
276 |
} |
|
277 |
return b.booleanValue(); |
|
278 |
} |
|
279 |
|
|
280 |
} |
org.gvsig.jexcel/tags/org.gvsig.jexcel-1.0.147/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.147/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 javax.json.JsonObject; |
|
35 |
import jxl.Cell; |
|
36 |
import jxl.Sheet; |
|
37 |
import jxl.Workbook; |
|
38 |
import jxl.read.biff.BiffException; |
|
39 |
|
|
40 |
import org.apache.commons.io.FilenameUtils; |
|
41 |
import org.apache.commons.lang3.StringUtils; |
|
42 |
import org.cresques.cts.IProjection; |
|
43 |
import org.gvsig.fmap.dal.DALLocator; |
|
44 |
import org.gvsig.fmap.dal.DataManager; |
|
45 |
import org.gvsig.fmap.dal.DataServerExplorer; |
|
46 |
import org.gvsig.fmap.dal.DataStore; |
|
47 |
import org.gvsig.fmap.dal.DataStoreNotification; |
|
48 |
import org.gvsig.fmap.dal.DataTypes; |
|
49 |
import org.gvsig.fmap.dal.FileHelper; |
|
50 |
import org.gvsig.fmap.dal.exception.DataException; |
|
51 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
52 |
import org.gvsig.fmap.dal.exception.OpenException; |
|
53 |
import org.gvsig.fmap.dal.exception.ReadException; |
|
54 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
55 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
|
56 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
|
57 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
|
58 |
import org.gvsig.fmap.dal.feature.Feature; |
|
59 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
60 |
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
|
61 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
62 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
63 |
import org.gvsig.fmap.dal.feature.exception.PerformEditingException; |
|
64 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
|
65 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
|
66 |
import org.gvsig.fmap.dal.feature.spi.memory.AbstractMemoryStoreProvider; |
|
67 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
|
68 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
|
69 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
|
70 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
|
71 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
|
72 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
|
73 |
import org.gvsig.fmap.geom.Geometry; |
|
74 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
75 |
import org.gvsig.fmap.geom.GeometryManager; |
|
76 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
|
77 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
78 |
import org.gvsig.fmap.geom.primitive.Point; |
|
79 |
import org.gvsig.fmap.geom.type.GeometryType; |
|
80 |
import org.gvsig.json.Json; |
|
81 |
import org.gvsig.json.JsonObjectBuilder; |
|
82 |
import org.gvsig.tools.ToolsLocator; |
|
83 |
import org.gvsig.tools.dataTypes.Coercion; |
|
84 |
import org.gvsig.tools.dataTypes.CoercionException; |
|
85 |
import org.gvsig.tools.dataTypes.DataType; |
|
86 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
|
87 |
import org.gvsig.tools.dynobject.exception.DynFieldNotFoundException; |
|
88 |
import org.gvsig.tools.exception.BaseException; |
|
89 |
import org.gvsig.tools.exception.NotYetImplemented; |
|
90 |
import org.gvsig.tools.logger.FilteredLogger; |
|
91 |
import org.gvsig.tools.persistence.PersistentState; |
|
92 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
|
93 |
import org.gvsig.tools.task.SimpleTaskStatus; |
|
94 |
import org.gvsig.tools.task.TaskStatusManager; |
|
95 |
import org.gvsig.tools.visitor.VisitCanceledException; |
|
96 |
import org.gvsig.tools.visitor.Visitor; |
|
97 |
import org.slf4j.Logger; |
|
98 |
import org.slf4j.LoggerFactory; |
|
99 |
|
|
100 |
@SuppressWarnings("UseSpecificCatch") |
|
101 |
public class JExcelStoreProvider extends AbstractMemoryStoreProvider implements |
|
102 |
ResourceConsumer { |
|
103 |
|
|
104 |
private static final Logger logger = LoggerFactory.getLogger(JExcelStoreProvider.class); |
|
105 |
|
|
106 |
public static final String NAME = "JExcel"; |
|
107 |
public static final String DESCRIPTION = "JExcel file"; |
|
108 |
|
|
109 |
public static final String METADATA_DEFINITION_NAME = NAME; |
|
110 |
|
|
111 |
private ResourceProvider resource; |
|
112 |
|
|
113 |
private long counterNewsOIDs = 0; |
|
114 |
private Envelope envelope; |
|
115 |
private boolean need_calculate_envelope = false; |
|
116 |
private SimpleTaskStatus taskStatus; |
|
117 |
private String sheetName = ""; |
|
118 |
|
|
119 |
public JExcelStoreProvider(JExcelStoreParameters parameters, |
|
120 |
DataStoreProviderServices storeServices) throws InitializeException { |
|
121 |
super( |
|
122 |
parameters, |
|
123 |
storeServices, |
|
124 |
FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME) |
|
125 |
); |
|
126 |
|
|
127 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
|
128 |
this.taskStatus = manager.createDefaultSimpleTaskStatus("JExcel"); |
|
129 |
|
|
130 |
counterNewsOIDs = 0; |
|
131 |
|
|
132 |
File file = getJExcelParameters().getFile(); |
|
133 |
resource = this.createResource( |
|
134 |
FileResource.NAME, |
|
135 |
new Object[]{file.getAbsolutePath()} |
|
136 |
); |
|
137 |
|
|
138 |
resource.addConsumer(this); |
|
139 |
initializeFeatureTypes(); |
|
140 |
} |
|
141 |
|
|
142 |
private JExcelStoreParameters getJExcelParameters() { |
|
143 |
return (JExcelStoreParameters) this.getParameters(); |
|
144 |
} |
|
145 |
|
|
146 |
public String getProviderName() { |
|
147 |
return NAME; |
|
148 |
} |
|
149 |
|
|
150 |
public boolean allowWrite() { |
|
151 |
return false; |
|
152 |
} |
|
153 |
|
|
154 |
private String getFullFileName() { |
|
155 |
// Usar solo para mostrar mensajes en el logger. |
|
156 |
String s = "(unknow)"; |
|
157 |
try { |
|
158 |
s = getJExcelParameters().getFile().getAbsolutePath(); |
|
159 |
} catch (Exception e2) { |
|
160 |
s = "(unknow)"; |
|
161 |
} |
|
162 |
return s; |
|
163 |
} |
|
164 |
|
|
165 |
@Override |
|
166 |
public void open() throws OpenException { |
|
167 |
if ( this.data != null ) { |
|
168 |
return; |
|
169 |
} |
|
170 |
this.data = new ArrayList<>(); |
|
171 |
resource.setData(new HashMap()); |
|
172 |
counterNewsOIDs = 0; |
|
173 |
try { |
|
174 |
loadFeatures(); |
|
175 |
} catch (RuntimeException e) { |
|
176 |
logger.warn("Can't load features from JExcel '" + getFullFileName() + "'.", e); |
|
177 |
throw e; |
|
178 |
} catch (Exception e) { |
|
179 |
logger.warn("Can't load features from JExcel '" + getFullFileName() + "'.", e); |
|
180 |
throw new RuntimeException(e); |
|
181 |
} |
|
182 |
} |
|
183 |
|
|
184 |
public DataServerExplorer getExplorer() throws ReadException { |
|
185 |
DataManager manager = DALLocator.getDataManager(); |
|
186 |
FilesystemServerExplorerParameters params; |
|
187 |
try { |
|
188 |
params = (FilesystemServerExplorerParameters) manager |
|
189 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
|
190 |
params.setRoot(this.getJExcelParameters().getFile().getParent()); |
|
191 |
return manager.openServerExplorer(FilesystemServerExplorer.NAME, params); |
|
192 |
} catch (DataException e) { |
|
193 |
throw new ReadException(this.getProviderName(), e); |
|
194 |
} catch (ValidateDataParametersException e) { |
|
195 |
throw new ReadException(this.getProviderName(), e); |
|
196 |
} |
|
197 |
|
|
198 |
} |
|
199 |
|
|
200 |
public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds, Iterator originalFeatureTypesUpdated) throws PerformEditingException { |
|
201 |
throw new UnsupportedOperationException(); |
|
202 |
} |
|
203 |
|
|
204 |
public boolean closeResourceRequested(ResourceProvider resource) { |
|
205 |
return true; |
|
206 |
} |
|
207 |
|
|
208 |
public int getOIDType() { |
|
209 |
return DataTypes.LONG; |
|
210 |
} |
|
211 |
|
|
212 |
public boolean supportsAppendMode() { |
|
213 |
return false; |
|
214 |
} |
|
215 |
|
|
216 |
public void append(FeatureProvider featureProvider) { |
|
217 |
throw new UnsupportedOperationException(); |
|
218 |
} |
|
219 |
|
|
220 |
public void beginAppend() { |
|
221 |
throw new UnsupportedOperationException(); |
|
222 |
} |
|
223 |
|
|
224 |
public void endAppend() { |
|
225 |
throw new UnsupportedOperationException(); |
|
226 |
} |
|
227 |
|
|
228 |
public void saveToState(PersistentState state) throws PersistenceException { |
|
229 |
throw new NotYetImplemented(); |
|
230 |
} |
|
231 |
|
|
232 |
public void loadFromState(PersistentState state) throws PersistenceException { |
|
233 |
throw new NotYetImplemented(); |
|
234 |
} |
|
235 |
|
|
236 |
public Object createNewOID() { |
|
237 |
return new Long(counterNewsOIDs++); |
|
238 |
} |
|
239 |
|
|
240 |
protected void initializeFeatureTypes() throws InitializeException { |
|
241 |
try { |
|
242 |
this.open(); |
|
243 |
} catch (OpenException e) { |
|
244 |
throw new InitializeException(this.getProviderName(), e); |
|
245 |
} |
|
246 |
} |
|
247 |
|
|
248 |
public Envelope getEnvelope() throws DataException { |
|
249 |
this.open(); |
|
250 |
if ( this.envelope != null ) { |
|
251 |
return this.envelope; |
|
252 |
} |
|
253 |
if ( !this.need_calculate_envelope ) { |
|
254 |
return null; |
|
255 |
} |
|
256 |
FeatureStore fs = this.getFeatureStore(); |
|
257 |
FeatureType ft = fs.getDefaultFeatureType(); |
|
258 |
FeatureAttributeDescriptor fad = ft.getAttributeDescriptor(ft.getDefaultGeometryAttributeIndex()); |
|
259 |
|
|
260 |
try { |
|
261 |
this.envelope = GeometryLocator.getGeometryManager().createEnvelope(fad.getGeomType().getSubType()); |
|
262 |
fs.accept(new Visitor() { |
|
263 |
public void visit(Object obj) throws VisitCanceledException, BaseException { |
|
264 |
Feature f = (Feature) obj; |
|
265 |
Geometry geom = f.getDefaultGeometry(); |
|
266 |
if ( geom != null ) { |
|
267 |
envelope.add(geom.getEnvelope()); |
|
268 |
} |
|
269 |
} |
|
270 |
}); |
|
271 |
} catch (BaseException e) { |
|
272 |
logger.warn("Can't calculate the envelope of JExcel file '" + this.getFullName() + "'.", e); |
|
273 |
this.envelope = null; |
|
274 |
} |
|
275 |
|
|
276 |
this.need_calculate_envelope = false; |
|
277 |
return this.envelope; |
|
278 |
} |
|
279 |
|
|
280 |
public Object getDynValue(String name) throws DynFieldNotFoundException { |
|
281 |
if ( DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name) ) { |
|
282 |
try { |
|
283 |
return this.getEnvelope(); |
|
284 |
} catch (DataException e) { |
|
285 |
return null; |
|
286 |
} |
|
287 |
} else { |
|
288 |
if ( DataStore.METADATA_CRS.equalsIgnoreCase(name) ) { |
|
289 |
IProjection pro = JExcelStoreParameters.getCRS(this.getJExcelParameters()); |
|
290 |
if ( pro != null ) { |
|
291 |
return pro; |
|
292 |
} |
|
293 |
} |
|
294 |
} |
|
295 |
return super.getDynValue(name); |
|
296 |
} |
|
297 |
|
|
298 |
public void resourceChanged(ResourceProvider resource) { |
|
299 |
this.getStoreServices().notifyChange( |
|
300 |
DataStoreNotification.RESOURCE_CHANGED, |
|
301 |
resource); |
|
302 |
} |
|
303 |
|
|
304 |
public Object getSourceId() { |
|
305 |
return this.getJExcelParameters().getFile(); |
|
306 |
} |
|
307 |
|
|
308 |
public String getName() { |
|
309 |
String name = this.getJExcelParameters().getFile().getName(); |
|
310 |
if( StringUtils.isBlank(this.sheetName) ) { |
|
311 |
return FilenameUtils.getBaseName(name); |
|
312 |
} |
|
313 |
return FilenameUtils.getBaseName(name)+"."+this.sheetName; |
|
314 |
} |
|
315 |
|
|
316 |
public String getFullName() { |
|
317 |
return this.getJExcelParameters().getFile().getAbsolutePath(); |
|
318 |
} |
|
319 |
|
|
320 |
public ResourceProvider getResource() { |
|
321 |
return resource; |
|
322 |
} |
|
323 |
|
|
324 |
private class FieldTypeParser { |
|
325 |
|
|
326 |
public String name = null; |
|
327 |
public int type = DataTypes.STRING; |
|
328 |
public int size = 0; |
|
329 |
public boolean allowNulls = true; |
|
330 |
|
|
331 |
private String typename = "string"; |
|
332 |
|
|
333 |
FieldTypeParser() { |
|
334 |
} |
|
335 |
|
|
336 |
private int getType(String value) { |
|
337 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
|
338 |
return dataTypesManager.getType(typename); |
|
339 |
} |
|
340 |
|
|
341 |
// El formato seria: |
|
342 |
// name[:typename[:size[:notnull|null]]] |
|
343 |
// name[__typename[__size[__notnull|null]]] |
|
344 |
// |
|
345 |
public boolean parse(String value) { |
|
346 |
String typename = null; |
|
347 |
String[] ss = null; |
|
348 |
if ( value.contains(":") ) { |
|
349 |
ss = value.split(":"); |
|
350 |
} else if ( value.contains("__") ) { |
|
351 |
ss = value.split("__"); |
|
352 |
} |
|
353 |
if ( ss == null ) { |
|
354 |
this.name = value; |
|
355 |
return true; |
|
356 |
} |
|
357 |
switch (ss.length) { |
|
358 |
case 4: |
|
359 |
if ( ss[3].length() > 0 ) { |
|
360 |
if ( "notnull".equalsIgnoreCase(ss[3]) ) { |
|
361 |
this.allowNulls = false; |
|
362 |
} else { |
|
363 |
this.allowNulls = true; |
|
364 |
} |
|
365 |
} |
|
366 |
case 3: |
|
367 |
if ( ss[2].length() > 0 ) { |
|
368 |
try { |
|
369 |
this.size = Integer.parseInt(ss[2]); |
|
370 |
} catch (Exception ex) { |
|
371 |
logger.warn("Ignore incorrect field size for field " + value + " in JExcel header of '" + getFullFileName() + "'.", ex); |
|
372 |
} |
|
373 |
} |
|
374 |
case 2: |
|
375 |
if ( ss[1].length() > 0 ) { |
|
376 |
this.typename = ss[1]; |
|
377 |
this.type = this.getType(this.typename); |
|
378 |
if ( this.type == DataTypes.INVALID ) { |
|
379 |
this.type = DataTypes.STRING; |
|
380 |
logger.info("Type '" + typename + "' not valid for attribute '" + value + "' in JExcel file '" + getFullFileName() + "'."); |
|
381 |
} |
|
382 |
} |
|
383 |
case 1: |
|
384 |
this.name = ss[0]; |
|
385 |
break; |
|
386 |
} |
|
387 |
|
|
388 |
if ( this.type != DataTypes.STRING ) { |
|
389 |
this.size = 0; |
|
390 |
} |
|
391 |
return true; |
|
392 |
} |
|
393 |
|
|
394 |
} |
|
395 |
|
|
396 |
private EditableFeatureType getFeatureType(String headers[], int automaticTypes[]) { |
|
397 |
EditableFeatureType fType = getStoreServices().createFeatureType(this.getName()); |
|
398 |
fType.setHasOID(true); |
|
399 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
|
400 |
|
|
401 |
FieldTypeParser[] fieldTypes = new FieldTypeParser[headers.length]; |
|
402 |
// |
|
403 |
// Calculamos cuales pueden ser los tipos de datos |
|
404 |
// |
|
405 |
for ( int i = 0; i < fieldTypes.length; i++ ) { |
|
406 |
fieldTypes[i] = new FieldTypeParser(); |
|
407 |
} |
|
408 |
|
|
409 |
// Asuminos los tipos pasados por parametro, que se supone |
|
410 |
// son los detectados automaticamente. |
|
411 |
if ( automaticTypes != null ) { |
|
412 |
for ( int i = 0; i < fieldTypes.length && i < automaticTypes.length; i++ ) { |
|
413 |
fieldTypes[i].type = automaticTypes[i]; |
|
414 |
} |
|
415 |
} |
|
416 |
// Luego probamos con lo que diga las cabezeras del CVS, sobreescribiendo |
|
417 |
// los tipos anteriores en caso de definirse en la cabezara. |
|
418 |
for ( int i = 0; i < fieldTypes.length; i++ ) { |
|
419 |
if ( !fieldTypes[i].parse(headers[i]) ) { |
|
420 |
continue; |
|
421 |
} |
|
422 |
|
|
423 |
} |
|
424 |
|
|
425 |
// Y por ultimo hacemos caso a lo que se haya especificado en los parametros |
|
426 |
// de apertura del JExcel, teniendo esto prioridad sobre todo. |
|
427 |
int[] param_types = JExcelStoreParameters.getFieldTypes(this.getParameters()); |
|
428 |
if ( param_types != null ) { |
|
429 |
for ( int i = 0; i < fieldTypes.length && i < param_types.length; i++ ) { |
|
430 |
fieldTypes[i].type = param_types[i]; |
|
431 |
} |
|
432 |
} |
|
433 |
|
|
434 |
int[] param_sizes = JExcelStoreParameters.getFieldSizes(this.getParameters()); |
|
435 |
if ( param_sizes != null ) { |
|
436 |
for ( int i = 0; i < param_sizes.length; i++ ) { |
|
437 |
if ( param_sizes[i] > 0 ) { |
|
438 |
fieldTypes[i].size = param_sizes[i]; |
|
439 |
} |
|
440 |
} |
|
441 |
} |
|
442 |
// |
|
443 |
// Una vez ya sabemos los tipos de datos rellenamos el feature-type |
|
444 |
// |
|
445 |
for ( int i = 0; i < fieldTypes.length; i++ ) { |
|
446 |
EditableFeatureAttributeDescriptor fad = fType.add( |
|
447 |
fieldTypes[i].name, |
|
448 |
fieldTypes[i].type |
|
449 |
); |
|
450 |
fad.setSize(fieldTypes[i].size); |
|
451 |
fad.setAllowNull(fieldTypes[i].allowNulls); |
|
452 |
if ( fieldTypes[i].type == DataTypes.GEOMETRY |
|
453 |
&& fType.getDefaultGeometryAttributeName() == null ) { |
|
454 |
fType.setDefaultGeometryAttributeName(fieldTypes[i].name); |
|
455 |
} |
|
456 |
} |
|
457 |
String[] pointDimensionNames = JExcelStoreParameters.getPointDimensionNames(this.getParameters()); |
|
458 |
if ( pointDimensionNames != null ) { |
|
459 |
// ToPointEvaluaror evaluator = new ToPointEvaluaror(pointDimensionNames); |
|
460 |
PointAttributeEmulator emulator = new PointAttributeEmulator(pointDimensionNames); |
|
461 |
EditableFeatureAttributeDescriptor attr = fType.add("GEOM", DataTypes.GEOMETRY, emulator); |
|
462 |
GeometryManager geommgr = GeometryLocator.getGeometryManager(); |
|
463 |
GeometryType gt; |
|
464 |
try { |
|
465 |
if ( emulator.fieldNames != null && emulator.fieldNames.length <= 2 ) { |
|
466 |
gt = geommgr.getGeometryType(Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM2D); |
|
467 |
} else { |
|
468 |
gt = geommgr.getGeometryType(Geometry.TYPES.GEOMETRY, Geometry.SUBTYPES.GEOM3D); |
|
469 |
} |
|
470 |
attr.setGeometryType(gt); |
|
471 |
} catch (Exception e) { |
|
472 |
logger.warn("Can't set geometry type for the calculated field in JExcel file '" + getFullFileName() + "'.", e); |
|
473 |
} |
|
474 |
} |
|
475 |
return fType; |
|
476 |
} |
|
477 |
|
|
478 |
static class PointAttributeEmulator implements FeatureAttributeEmulator { |
|
479 |
|
|
480 |
private static final Logger LOGGER = LoggerFactory.getLogger(PointAttributeEmulator.class); |
|
481 |
|
|
482 |
private static final int XNAME = 0; |
|
483 |
private static final int YNAME = 1; |
|
484 |
private static final int ZNAME = 2; |
|
485 |
|
|
486 |
private final GeometryManager geommgr; |
|
487 |
private final Coercion toDouble; |
|
488 |
private final DataType dataType; |
|
489 |
private FilteredLogger logger; |
|
490 |
|
|
491 |
private String[] fieldNames; |
|
492 |
|
|
493 |
public PointAttributeEmulator() { |
|
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 |
public PointAttributeEmulator(String[] pointDimensionNames) { |
|
502 |
this(); |
|
503 |
if ( pointDimensionNames.length > 2 ) { |
|
504 |
this.fieldNames = new String[3]; |
|
505 |
this.fieldNames[ZNAME] = pointDimensionNames[2]; |
|
506 |
} else { |
|
507 |
this.fieldNames = new String[2]; |
|
508 |
} |
|
509 |
this.fieldNames[XNAME] = pointDimensionNames[0]; |
|
510 |
this.fieldNames[YNAME] = pointDimensionNames[1]; |
|
511 |
} |
|
512 |
|
|
513 |
private FilteredLogger getLogger() { |
|
514 |
if( this.logger == null ) { |
|
515 |
this.logger = new FilteredLogger(LOGGER, "JExcelPointAttributeEmulator", 10); |
|
516 |
this.logger.setInterval(60000); |
|
517 |
} |
|
518 |
return this.logger; |
|
519 |
} |
|
520 |
|
|
521 |
@Override |
|
522 |
public Object get(Feature feature) { |
|
523 |
try { |
|
524 |
Object valueX = feature.get(this.fieldNames[XNAME]); |
|
525 |
valueX = toDouble.coerce(valueX); |
|
526 |
if ( valueX == null ) { |
|
527 |
return null; |
|
528 |
} |
|
529 |
Object valueY = feature.get(this.fieldNames[YNAME]); |
|
530 |
valueY = toDouble.coerce(valueY); |
|
531 |
if ( valueY == null ) { |
|
532 |
return null; |
|
533 |
} |
|
534 |
Object valueZ = null; |
|
535 |
if ( this.fieldNames.length > 2 ) { |
|
536 |
valueZ = toDouble.coerce(feature.get(this.fieldNames[ZNAME])); |
|
537 |
if ( valueZ == null ) { |
|
538 |
return null; |
|
539 |
} |
|
540 |
} |
|
541 |
|
|
542 |
double x = ((Double) valueX); |
|
543 |
double y = ((Double) valueY); |
|
544 |
Point point; |
|
545 |
if ( this.fieldNames.length > 2 ) { |
|
546 |
point = geommgr.createPoint(x, y, Geometry.SUBTYPES.GEOM3D); |
|
547 |
double z = ((Double) valueZ); |
|
548 |
point.setCoordinateAt(2, z); |
|
549 |
} else { |
|
550 |
point = geommgr.createPoint(x, y, Geometry.SUBTYPES.GEOM2D); |
|
551 |
} |
|
552 |
return point; |
|
553 |
} catch (Exception ex) { |
|
554 |
this.getLogger().warn("Can't create point in JExcel provider. XNAME='" |
|
555 |
+ this.fieldNames[XNAME] + "', YNAME='" + this.fieldNames[YNAME] + "' feature=" + feature.toString(), ex); |
|
556 |
return null; |
|
557 |
} |
|
558 |
} |
|
559 |
|
|
560 |
@Override |
|
561 |
public void set(EditableFeature feature, Object value) { |
|
562 |
if ( value == null ) { |
|
563 |
return; |
|
564 |
} |
|
565 |
Point point; |
|
566 |
if ( value instanceof MultiPoint ) { |
|
567 |
point = (Point) ((MultiPoint) value).getPrimitiveAt(0); |
|
568 |
} else { |
|
569 |
point = (Point) value; |
|
570 |
} |
|
571 |
feature.set(this.fieldNames[XNAME], point.getX()); |
|
572 |
feature.set(this.fieldNames[YNAME], point.getY()); |
|
573 |
if ( this.fieldNames.length > 2 ) { |
|
574 |
feature.set(this.fieldNames[ZNAME], point.getCoordinateAt(2)); |
|
575 |
} |
|
576 |
} |
|
577 |
|
|
578 |
@Override |
|
579 |
public boolean allowSetting() { |
|
580 |
return true; |
|
581 |
} |
|
582 |
|
|
583 |
@Override |
|
584 |
public String[] getRequiredFieldNames() { |
|
585 |
return this.fieldNames; |
|
586 |
} |
|
587 |
|
|
588 |
@Override |
Also available in: Unified diff