Revision 44309

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.csv/src/main/resources/org/gvsig/fmap/dal/store/csv/CSVParameters.xml
1 1
<?xml version= "1.0 "?>
2 2
<definitions>
3
  <version>1.0.0</version>
4
  <classes>
5
    <class name="CSVStoreParameters">
6
       <extends>
7
      	<class namespace="dal" name="ProviderParameters"/>
3
    <version>1.0.0</version>
4
    <classes>
5
        <class name="CSVStoreParameters">
6
            <extends>
7
                <class namespace="dal" name="ProviderParameters"/>
8
            </extends>
9
            <tags>
10
                <value name="defaultGroup" value="Basic"/>
11
            </tags>      
12
            <description>Store the parameters need to open a CSV file</description>
13
            <fields>
14
                <field name="File" type="file" mandatory="true" group="Basic">
15
                    <description>CSV file</description>
16
                </field>
17
                <field name="profile" type="string" mandatory="true" defaultValue="EXCEL_NORTH_EUROPE_PREFERENCE" group="Basic">
18
                    <description>Predefined formats</description>
19
                    <availableValues>
20
                        <value label="Standard (double quote, comma and crlf)">STANDARD_PREFERENCE</value>
21
                        <value label="Excel (double quote, comma and lf)">EXCEL_PREFERENCE</value>
22
                        <value label="Excel North Europe (double quote, semicolon and lf)">EXCEL_NORTH_EUROPE_PREFERENCE</value>
23
                        <value label="Tabs (double quote, tabs and lf)">TAB_PREFERENCE</value>
24
                    </availableValues>          
25
                </field>
26
                <field name="quotePolicy" type="string" mandatory="false" defaultValue="NormalQuoteMode" group="Basic">
27
                    <description>Sets the output quote policy of the format to the specified value. </description>
28
                    <availableValues>
29
                        <value label="Quote all">AlwaysQuoteMode</value>
30
                        <value label="Quotes fields which contain special characters">NormalQuoteMode</value>
31
                    </availableValues>          
32
                </field>
33
                <field name="locale" type="string" mandatory="true" defaultValue="DEFAULT" group="Basic">
34
                    <description>Locale used to parse double and float values (BCP47 language tag representing the locale)</description>
35
                    <availableValues>
36
                        <value label="Default (use system locale)">DEFAULT</value>
37
                        <value label="Canada (english)">en-CA</value>
38
                        <value label="Canada (french)">fr-CA</value>
39
                        <value label="Chinese">zh</value>
40
                        <value label="English">en</value>
41
                        <value label="French">fr</value>
42
                        <value label="German">de</value>
43
                        <value label="Italian">it</value>
44
                        <value label="Japanese">ja</value>
45
                        <value label="Korea">ko</value>
46
                        <value label="Spain">es</value>
47
                        <value label="Uk">en-GB</value>
48
                        <value label="Us">en-US</value>
49
                    </availableValues>          
50
                </field>
51
                <field name="firstLineHeader" type="boolean" mandatory="false" defaultValue="true" group="Basic">
52
                    <description>Set to true to use the first line of file as the header.</description>
53
                </field>
54
                <field name="recordSeparator" type="string" mandatory="false" defaultValue="" group="Advanced">
55
                    <description>Sets the record separator of the format to the specified character.</description>
56
                </field>
57
                <field name="delimiter" type="string" mandatory="false" defaultValue="" group="Advanced">
58
                    <description>Sets the field delimiter of the format to the specified character.</description>
59
                </field>
60
                <field name="quoteCharacter" type="string" mandatory="false" defaultValue="" group="Advanced">
61
                    <description>Sets the quoteChar of the format to the specified character.</description>
62
                </field>
63
                <field name="commentStartMarker" type="string" mandatory="false" defaultValue="" group="Advanced">
64
                    <description>Sets the comment start marker of the format to the specified character.</description>
65
                </field>
66
                <field name="automaticTypesDetection" type="boolean" mandatory="false" defaultValue="true" group="Advanced">
67
                    <description>This flag activate the autodetection of types in the CSV. Can be slow.</description>
68
                </field>
69
                <field name="escapeCharacter" type="string" mandatory="false" defaultValue="" group="Advanced">
70
                    <description>Sets the escape character of the format to the specified character</description>
71
                </field>
72
                <field name="header" type="string" mandatory="false" defaultValue="" group="Advanced">
73
                    <description>The header or empty if parsed automatically</description>
74
                </field>
75
                <field name="skipLines" label="Number of lines to skip" type="integer" mandatory="false" defaultValue="0" group="Advanced">
76
                    <description>The number of lines to skip at begin of file</description>
77
                </field>
78
                <field name="fieldsDefinition" label="Fields definition" type="string" mandatory="false" defaultValue="" group="Advanced">
79
                    <description>If set use this to define fields of fixed length instead of use a separator.\nThe format is a list of values separateds by spaces of the form N:M\nwhere N is the position of the first character of field, and M the last.\nCan accept N: to use form the N character to the end of line.</description>
80
                </field>
81
                <field name="surroundingSpacesNeedQuotes" type="boolean" mandatory="false" defaultValue="false" group="Advanced">
82
                    <description>the surroundingSpacesNeedQuotes flag.</description>
83
                </field>
84
                <field name="CRS" type="crs" mandatory="false" group="Advanced">
85
                    <description>The coordinate reference system used in this CSV file if contains geometries</description>
86
                </field>
87
                <field name="fieldtypes" type="string" mandatory="false" defaultValue="" group="Advanced">
88
                    <description>Comma separated list of field types. If empty strings are assumed (Eg: integer,string,double,boolean,float,geometry).</description>
89
                </field>
90
                <field name="charset" type="string" mandatory="false" defaultValue="UTF-8" group="Advanced">
91
                    <description>Sets the String to use for null values for output.</description>
92
                </field>
93
                <field name="point" type="string" mandatory="false" defaultValue="" group="Advanced">
94
                    <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>
95
                </field>
96
                <field name="pointColumnName" type="string" mandatory="false" defaultValue="geom" group="Advanced">
97
                    <description>Name to set to the column of the Point geometry.</description>
98
                </field>
99
                <field name="geometry_column" type="string" mandatory="false" defaultValue="" group="Advanced">
100
                    <description>Name of the geometry column (the geometry must be in WKT format).</description>
101
                </field>        
102
                <field name="ignoreErrors" type="boolean" mandatory="false" defaultValue="false" group="Advanced">
103
                    <description>Set to true to ignore errors during load of data.</description>
104
                </field>
105
                <field name="limit" type="integer" mandatory="false" defaultValue="-1" group="Advanced">
106
                    <description>Limit the number of record to load of CSV file.</description>
107
                </field>
108
            </fields>
109
        </class>
110
        <class name="CSVNewStoreParameters">
111
            <description>Store the parameters need to create a csv file</description>
112
      <extends>
113
        <class>CSVStoreParameters</class>
114
        <class namespace="dal" name="NewProviderParameters"/>
8 115
      </extends>
9
      <tags>
10
          <value name="defaultGroup" value="Basic"/>
11
      </tags>      
12
      <description>Store the parameters need to open a CSV file</description>
13
      <fields>
14
        <field name="File" type="file" mandatory="true" group="Basic">
15
          <description>CSV file</description>
16
        </field>
17
        <field name="profile" type="string" mandatory="true" defaultValue="EXCEL_NORTH_EUROPE_PREFERENCE" group="Basic">
18
          <description>Predefined formats</description>
19
  		  <availableValues>
20
            <value label="Standard (double quote, comma and crlf)">STANDARD_PREFERENCE</value>
21
            <value label="Excel (double quote, comma and lf)">EXCEL_PREFERENCE</value>
22
            <value label="Excel North Europe (double quote, semicolon and lf)">EXCEL_NORTH_EUROPE_PREFERENCE</value>
23
            <value label="Tabs (double quote, tabs and lf)">TAB_PREFERENCE</value>
24
          </availableValues>          
25
        </field>
26
        <field name="quotePolicy" type="string" mandatory="false" defaultValue="NormalQuoteMode" group="Basic">
27
          <description>Sets the output quote policy of the format to the specified value. </description>
28
  		  <availableValues>
29
          	<value label="Quote all">AlwaysQuoteMode</value>
30
            <value label="Quotes fields which contain special characters">NormalQuoteMode</value>
31
          </availableValues>          
32
        </field>
33
        <field name="locale" type="string" mandatory="true" defaultValue="DEFAULT" group="Basic">
34
          <description>Locale used to parse double and float values (BCP47 language tag representing the locale)</description>
35
  	  <availableValues>
36
            <value label="Default (use system locale)">DEFAULT</value>
37
            <value label="Canada (english)">en-CA</value>
38
            <value label="Canada (french)">fr-CA</value>
39
            <value label="Chinese">zh</value>
40
            <value label="English">en</value>
41
            <value label="French">fr</value>
42
            <value label="German">de</value>
43
            <value label="Italian">it</value>
44
            <value label="Japanese">ja</value>
45
            <value label="Korea">ko</value>
46
            <value label="Spain">es</value>
47
            <value label="Uk">en-GB</value>
48
            <value label="Us">en-US</value>
49
          </availableValues>          
50
        </field>
51
        <field name="firstLineHeader" type="boolean" mandatory="false" defaultValue="true" group="Basic">
52
          <description>Set to true to use the first line of file as the header.</description>
53
        </field>
54
        <field name="recordSeparator" type="string" mandatory="false" defaultValue="" group="Advanced">
55
          <description>Sets the record separator of the format to the specified character.</description>
56
        </field>
57
        <field name="delimiter" type="string" mandatory="false" defaultValue="" group="Advanced">
58
          <description>Sets the field delimiter of the format to the specified character.</description>
59
        </field>
60
        <field name="quoteCharacter" type="string" mandatory="false" defaultValue="" group="Advanced">
61
          <description>Sets the quoteChar of the format to the specified character.</description>
62
        </field>
63
        <field name="commentStartMarker" type="string" mandatory="false" defaultValue="" group="Advanced">
64
          <description>Sets the comment start marker of the format to the specified character.</description>
65
        </field>
66
        <field name="automaticTypesDetection" type="boolean" mandatory="false" defaultValue="true" group="Advanced">
67
          <description>This flag activate the autodetection of types in the CSV. Can be slow.</description>
68
        </field>
69
        <field name="escapeCharacter" type="string" mandatory="false" defaultValue="" group="Advanced">
70
          <description>Sets the escape character of the format to the specified character</description>
71
        </field>
72
        <field name="header" type="string" mandatory="false" defaultValue="" group="Advanced">
73
          <description>The header or empty if parsed automatically</description>
74
        </field>
75
        <field name="skipLines" label="Number of lines to skip" type="integer" mandatory="false" defaultValue="0" group="Advanced">
76
          <description>The number of lines to skip at begin of file</description>
77
        </field>
78
        <field name="fieldsDefinition" label="Fields definition" type="string" mandatory="false" defaultValue="" group="Advanced">
79
          <description>If set use this to define fields of fixed length instead of use a separator.\nThe format is a list of values separateds by spaces of the form N:M\nwhere N is the position of the first character of field, and M the last.\nCan accept N: to use form the N character to the end of line.</description>
80
        </field>
81
        <field name="surroundingSpacesNeedQuotes" type="boolean" mandatory="false" defaultValue="false" group="Advanced">
82
          <description>the surroundingSpacesNeedQuotes flag.</description>
83
        </field>
84
        <field name="CRS" type="crs" mandatory="false" group="Advanced">
85
          <description>The coordinate reference system used in this CSV file if contains geometries</description>
86
        </field>
87
        <field name="fieldtypes" type="string" mandatory="false" defaultValue="" group="Advanced">
88
          <description>Comma separated list of field types. If empty strings are assumed (Eg: integer,string,double,boolean,float,geometry).</description>
89
        </field>
90
        <field name="charset" type="string" mandatory="false" defaultValue="UTF-8" group="Advanced">
91
          <description>Sets the String to use for null values for output.</description>
92
        </field>
93
        <field name="point" type="string" mandatory="false" defaultValue="" group="Advanced">
94
          <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>
95
        </field>
96
        <field name="pointColumnName" type="string" mandatory="false" defaultValue="geom" group="Advanced">
97
          <description>Name to set to the column of the Point geometry.</description>
98
        </field>
99
        <field name="geometry_column" type="string" mandatory="false" defaultValue="" group="Advanced">
100
          <description>Name of the geometry column (the geometry must be in WKT format).</description>
101
        </field>        
102
        <field name="ignoreErrors" type="boolean" mandatory="false" defaultValue="false" group="Advanced">
103
          <description>Set to true to ignore errors during load of data.</description>
104
        </field>
105
        <field name="limit" type="integer" mandatory="false" defaultValue="-1" group="Advanced">
106
          <description>Limit the number of record to load of CSV file.</description>
107
        </field>
108
      </fields>
109
    </class>
110
  </classes>
116
            <fields>
117
<!--                <field name="File" type="file" mandatory="true" group="Basic">
118
                    <description>CSV file</description>
119
                </field>
120
                <field name="profile" type="string" mandatory="true" defaultValue="EXCEL_NORTH_EUROPE_PREFERENCE" group="Basic">
121
                    <description>Predefined formats</description>
122
                    <availableValues>
123
                        <value label="Standard (double quote, comma and crlf)">STANDARD_PREFERENCE</value>
124
                        <value label="Excel (double quote, comma and lf)">EXCEL_PREFERENCE</value>
125
                        <value label="Excel North Europe (double quote, semicolon and lf)">EXCEL_NORTH_EUROPE_PREFERENCE</value>
126
                        <value label="Tabs (double quote, tabs and lf)">TAB_PREFERENCE</value>
127
                    </availableValues>          
128
                </field>
129
                <field name="quotePolicy" type="string" mandatory="false" defaultValue="NormalQuoteMode" group="Basic">
130
                    <description>Sets the output quote policy of the format to the specified value. </description>
131
                    <availableValues>
132
                        <value label="Quote all">AlwaysQuoteMode</value>
133
                        <value label="Quotes fields which contain special characters">NormalQuoteMode</value>
134
                    </availableValues>          
135
                </field>
136
                <field name="locale" type="string" mandatory="true" defaultValue="DEFAULT" group="Basic">
137
                    <description>Locale used to parse double and float values (BCP47 language tag representing the locale)</description>
138
                    <availableValues>
139
                        <value label="Default (use system locale)">DEFAULT</value>
140
                        <value label="Canada (english)">en-CA</value>
141
                        <value label="Canada (french)">fr-CA</value>
142
                        <value label="Chinese">zh</value>
143
                        <value label="English">en</value>
144
                        <value label="French">fr</value>
145
                        <value label="German">de</value>
146
                        <value label="Italian">it</value>
147
                        <value label="Japanese">ja</value>
148
                        <value label="Korea">ko</value>
149
                        <value label="Spain">es</value>
150
                        <value label="Uk">en-GB</value>
151
                        <value label="Us">en-US</value>
152
                    </availableValues>          
153
                </field>
154
                <field name="recordSeparator" type="string" mandatory="false" defaultValue="" group="Advanced">
155
                    <description>Sets the record separator of the format to the specified character.</description>
156
                </field>
157
                <field name="delimiter" type="string" mandatory="false" defaultValue="" group="Advanced">
158
                    <description>Sets the field delimiter of the format to the specified character.</description>
159
                </field>
160
                <field name="quoteCharacter" type="string" mandatory="false" defaultValue="" group="Advanced">
161
                    <description>Sets the quoteChar of the format to the specified character.</description>
162
                </field>
163
                <field name="commentStartMarker" type="string" mandatory="false" defaultValue="" group="Advanced">
164
                    <description>Sets the comment start marker of the format to the specified character.</description>
165
                </field>
166
                <field name="escapeCharacter" type="string" mandatory="false" defaultValue="" group="Advanced">
167
                    <description>Sets the escape character of the format to the specified character</description>
168
                </field>
169
                <field name="charset" type="string" mandatory="false" defaultValue="UTF-8" group="Advanced">
170
                    <description>Sets the String to use for null values for output.</description>
171
                </field>-->
172
            </fields>
173
        </class>
174
    </classes>
111 175
</definitions>  
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.csv/src/main/java/org/gvsig/fmap/dal/store/csv/CSVNewStoreParameters.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.dal.store.csv;
25

  
26
import org.gvsig.fmap.dal.feature.EditableFeatureType;
27
import org.gvsig.fmap.dal.feature.FeatureType;
28
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
29

  
30
public class CSVNewStoreParameters extends CSVStoreParameters implements
31
        NewFeatureStoreParameters {
32

  
33
    public static final String PARAMETERS_DEFINITION_NAME = "CSVNewStoreParameters";
34

  
35
    public static final String FEATURETYPE_PARAMTER_NAME = "FeatureType";
36

  
37
    public static final String CODEPAGE_PARAMTER_NAME = "codePage";
38

  
39
    public CSVNewStoreParameters() {
40
        super(PARAMETERS_DEFINITION_NAME);
41
    }
42

  
43
    public byte getCodePage() {
44
        return ((Byte) this.getDynValue(CODEPAGE_PARAMTER_NAME));
45
    }
46

  
47
    public void setCodePage(byte value) {
48
        setDynValue(CODEPAGE_PARAMTER_NAME, value);
49
    }
50

  
51
    @Override
52
    public EditableFeatureType getDefaultFeatureType() {
53
        return (EditableFeatureType) this.getDynValue(FEATURETYPE_PARAMTER_NAME);
54
    }
55

  
56
    @Override
57
    public void setDefaultFeatureType(FeatureType featureType) {
58
        this.setDynValue(FEATURETYPE_PARAMTER_NAME, featureType);
59
    }
60

  
61
}
0 62

  
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.csv/src/main/java/org/gvsig/fmap/dal/store/csv/CSVStoreProviderFactory.java
48 48
	}
49 49
	
50 50
	public int allowCreate() {
51
		return NO;
51
		return YES;
52 52
	}
53 53
	
54 54
	public int allowWrite() {
55
		return NO;
55
		return YES;
56 56
	}
57 57

  
58 58
	public int allowRead() {
......
76 76
	}
77 77
	
78 78
	public int allowEditableFeatureType() {
79
		return NO;
79
		return YES;
80 80
	}
81 81

  
82 82
        public int useLocalIndexesCanImprovePerformance() {
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.csv/src/main/java/org/gvsig/fmap/dal/store/csv/CSVLibrary.java
44 44
        require(DALFileLibrary.class);
45 45
    }
46 46

  
47
	@Override
48
	protected void doInitialize() throws LibraryException {
49
	}
47
    @Override
48
    protected void doInitialize() throws LibraryException {
49
    }
50 50

  
51
	@Override
52
	protected void doPostInitialize() throws LibraryException {
53
		List<Throwable> exs = new ArrayList<Throwable>();
51
    @Override
52
    protected void doPostInitialize() throws LibraryException {
53
        List<Throwable> exs = new ArrayList<Throwable>();
54 54

  
55
		FileHelper.registerParametersDefinition(
56
				CSVStoreParameters.PARAMETERS_DEFINITION_NAME,
57
				CSVStoreParameters.class, "CSVParameters.xml");
58
		try {
59
			FileHelper.registerMetadataDefinition(
60
					CSVStoreProvider.METADATA_DEFINITION_NAME,
61
					CSVStoreProvider.class, "CSVMetadata.xml");
62
		} catch (MetadataException e) {
63
			exs.add(e);
64
		}
55
        FileHelper.registerParametersDefinition(
56
                CSVStoreParameters.PARAMETERS_DEFINITION_NAME,
57
                CSVStoreParameters.class, "CSVParameters.xml");
58
        FileHelper.registerParametersDefinition(
59
                CSVNewStoreParameters.PARAMETERS_DEFINITION_NAME,
60
                CSVNewStoreParameters.class, "CSVParameters.xml");
61
        try {
65 62

  
66
		DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator
67
				.getDataManager();
63
        } catch (Exception e) {
64
            exs.add(e);
65
        }
66
        try {
67
            FileHelper.registerMetadataDefinition(
68
                    CSVStoreProvider.METADATA_DEFINITION_NAME,
69
                    CSVStoreProvider.class, "CSVMetadata.xml");
70
        } catch (MetadataException e) {
71
            exs.add(e);
72
        }
68 73

  
69
		try {
70
			if (!dataman.getStoreProviders().contains(CSVStoreProvider.NAME)) {
71
				dataman.registerStoreProviderFactory(new CSVStoreProviderFactory(CSVStoreProvider.NAME, CSVStoreProvider.DESCRIPTION));
74
        DataManagerProviderServices dataman = (DataManagerProviderServices) DALLocator
75
                .getDataManager();
72 76

  
73
			}
74
		} catch (RuntimeException e) {
75
			exs.add(e);
76
		}
77
        try {
78
            if (!dataman.getStoreProviders().contains(CSVStoreProvider.NAME)) {
79
                dataman.registerStoreProviderFactory(new CSVStoreProviderFactory(CSVStoreProvider.NAME, CSVStoreProvider.DESCRIPTION));
77 80

  
78
		try {
79
			DALFileLocator.getFilesystemServerExplorerManager()
80
					.registerProvider(CSVStoreProvider.NAME,
81
							CSVStoreProvider.DESCRIPTION,
82
							CSVFilesystemServerProvider.class);
83
		} catch (RuntimeException e) {
84
			exs.add(e);
85
		}
81
            }
82
        } catch (RuntimeException e) {
83
            exs.add(e);
84
        }
86 85

  
87
		if (exs.size() > 0) {
88
			throw new LibraryException(this.getClass(), exs);
89
		}
90
	}
86
        try {
87
            DALFileLocator.getFilesystemServerExplorerManager()
88
                    .registerProvider(CSVStoreProvider.NAME,
89
                            CSVStoreProvider.DESCRIPTION,
90
                            CSVFilesystemServerProvider.class);
91
        } catch (RuntimeException e) {
92
            exs.add(e);
93
        }
94

  
95
        if (exs.size() > 0) {
96
            throw new LibraryException(this.getClass(), exs);
97
        }
98
    }
91 99
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.csv/src/main/java/org/gvsig/fmap/dal/store/csv/CSVFilesystemServerProvider.java
24 24
package org.gvsig.fmap.dal.store.csv;
25 25

  
26 26
import java.io.File;
27
import java.io.IOException;
28
import org.gvsig.fmap.dal.DALLocator;
29
import org.gvsig.fmap.dal.DataManager;
27 30

  
28 31
import org.gvsig.fmap.dal.DataServerExplorer;
32
import org.gvsig.fmap.dal.DataStoreParameters;
33
import org.gvsig.fmap.dal.NewDataStoreParameters;
34
import org.gvsig.fmap.dal.exception.CreateException;
35
import org.gvsig.fmap.dal.exception.DataException;
36
import org.gvsig.fmap.dal.exception.FileNotFoundException;
37
import org.gvsig.fmap.dal.exception.InitializeException;
38
import org.gvsig.fmap.dal.exception.RemoveException;
39
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
40
import org.gvsig.fmap.dal.feature.EditableFeatureType;
41
import org.gvsig.fmap.dal.feature.FeatureType;
42
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters;
43
import org.gvsig.fmap.dal.resource.ResourceAction;
44
import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException;
45
import org.gvsig.fmap.dal.resource.file.FileResource;
29 46
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
47
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
48
import org.gvsig.fmap.dal.serverexplorer.filesystem.AbsolutePathRequiredException;
30 49
import org.gvsig.fmap.dal.serverexplorer.filesystem.impl.AbstractFilesystemServerExplorerProvider;
31 50
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProvider;
51
import org.gvsig.fmap.dal.serverexplorer.filesystem.spi.FilesystemServerExplorerProviderServices;
52
import org.gvsig.tools.dataTypes.DataTypes;
53
import org.supercsv.prefs.CsvPreference;
32 54

  
33
public class CSVFilesystemServerProvider extends AbstractFilesystemServerExplorerProvider 
34
	implements FilesystemServerExplorerProvider, ResourceConsumer {
55
public class CSVFilesystemServerProvider extends AbstractFilesystemServerExplorerProvider
56
        implements FilesystemServerExplorerProvider, ResourceConsumer {
35 57

  
58
    protected FilesystemServerExplorerProviderServices serverExplorer;
59

  
36 60
    @Override
37
	public String getDataStoreProviderName() {
38
		return CSVStoreProvider.NAME;
39
	}
61
    public String getDataStoreProviderName() {
62
        return CSVStoreProvider.NAME;
63
    }
40 64

  
41 65
    @Override
42
	public int getMode() {
43
		return DataServerExplorer.MODE_FEATURE | DataServerExplorer.MODE_GEOMETRY;
44
	}
66
    public int getMode() {
67
        return DataServerExplorer.MODE_FEATURE | DataServerExplorer.MODE_GEOMETRY;
68
    }
45 69

  
46 70
    @Override
47
	public boolean accept(File pathname) {
48
		return (pathname.getName().toLowerCase().endsWith(".csv"));
49
	}
71
    public boolean accept(File pathname) {
72
        return (pathname.getName().toLowerCase().endsWith(".csv"));
73
    }
50 74

  
51 75
    @Override
52
	public String getDescription() {
53
		return CSVStoreProvider.DESCRIPTION;
54
	}
76
    public String getDescription() {
77
        return CSVStoreProvider.DESCRIPTION;
78
    }
55 79

  
80
    @Override
81
    public DataStoreParameters getParameters(File file) throws DataException {
82
        DataManager manager = DALLocator.getDataManager();
83
        CSVStoreParameters params = (CSVStoreParameters) manager
84
                .createStoreParameters(this.getDataStoreProviderName());
85
        params.setFile(file);
86
        return params;
87
    }
56 88

  
89
    @Override
90
    public boolean canCreate() {
91
        return true;
92
    }
93

  
94
    @Override
95
    public boolean canCreate(NewDataStoreParameters parameters) {
96
        CSVNewStoreParameters params = (CSVNewStoreParameters) parameters;
97
        if (params.getFile().getParentFile().canWrite()) {
98
            return false;
99
        }
100
        if (params.getFile().exists()) {
101
            if (!params.getFile().canWrite()) {
102
                return false;
103
            }
104
        }
105
        if (params.getDefaultFeatureType() == null) {
106
            return false;
107
        }
108
        return true;
109
    }
110

  
111
    public void create(NewDataStoreParameters parameters, boolean overwrite)
112
            throws CreateException {
113

  
114
        final CSVNewStoreParameters params = (CSVNewStoreParameters) parameters;
115
        final File file = params.getFile();
116

  
117
        if (!file.isAbsolute()) {
118
            throw new AbsolutePathRequiredException(file.getPath());
119
        }
120

  
121
        if (file.exists()) {
122
            if (overwrite) {
123
//				if (!file.delete()) {
124
//					throw new CreateException(this.getDataStoreProviderName(),
125
//							new IOException(
126
//							"cannot delete file"));
127
//				}
128
            } else {
129
                throw new CreateException(this.getDataStoreProviderName(),
130
                        new IOException(
131
                                "file already exist"));
132
            }
133
        }
134

  
135
        final FileResource resource;
136
        try {
137

  
138
            resource = (FileResource) this.serverExplorer
139
                    .getServerExplorerProviderServices().createResource(
140
                            FileResource.NAME,
141
                            new Object[]{file.getAbsolutePath()});
142
        } catch (InitializeException e1) {
143
            throw new CreateException(params.getFile().getAbsolutePath(), e1);
144
        }
145
        resource.addConsumer(this);
146

  
147
        try {
148
            resource.execute(new ResourceAction() {
149
                @Override
150
                public Object run() throws Exception {
151
                    CSVFeatureWriter writer = new CSVFeatureWriter();
152
                    //TODO correct values to the writer from params
153
                    writer.initialize(params, file.getAbsoluteFile(), params.getDefaultFeatureType(), null);
154
                    writer.begin();
155

  
156
//                    writer.begin(
157
//                            params,
158
//                            fixFeatureType(params.getDefaultFeatureType()),
159
//                            0
160
//                    );
161
                    writer.end();
162
                    //writer.dispose();
163
                    resource.notifyChanges();
164
                    return null;
165
                }
166
            });
167
        } catch (ResourceExecuteException e) {
168
            throw new CreateException(this.getDataStoreProviderName(), e);
169
        } finally {
170
            resource.removeConsumer(this);
171
        }
172

  
173
    }
174

  
175
    protected NewDataStoreParameters createInstanceNewDataStoreParameters() {
176
        return new CSVNewStoreParameters();
177
    }
178

  
179
    @Override
180
    public NewDataStoreParameters getCreateParameters() {
181
        NewFeatureStoreParameters params = (NewFeatureStoreParameters) this
182
                .createInstanceNewDataStoreParameters();
183
        params.setDefaultFeatureType(this.serverExplorer
184
                .getServerExplorerProviderServices().createNewFeatureType());
185
        return params;
186
    }
187

  
188
    @Override
189
    public void initialize(FilesystemServerExplorerProviderServices serverExplorer) {
190
        this.serverExplorer = serverExplorer;
191

  
192
    }
193

  
194
    @Override
195
    public void remove(DataStoreParameters parameters) throws RemoveException {
196
        CSVStoreParameters params = (CSVStoreParameters) parameters;
197
        File file = params.getFile();
198
        if (!file.exists()) {
199
            throw new RemoveException(this.getDataStoreProviderName(),
200
                    new FileNotFoundException(params.getFile()));
201
        }
202
        if (!file.delete()) {
203
            throw new RemoveException(this.getDataStoreProviderName(),
204
                    new IOException()); // FIXME Exception
205
        }
206
    }
207

  
208
    @Override
209
    public boolean closeResourceRequested(ResourceProvider resource) {
210
        // while it is using a resource anyone can't close it
211
        return !(this.equals(resource));
212
    }
213

  
214
    /*
215
	 * (non-Javadoc)
216
	 *
217
	 * @see
218
	 * org.gvsig.fmap.dal.resource.spi.ResourceConsumer#resourceChanged(org.
219
	 * gvsig.fmap.dal.resource.spi.ResourceProvider)
220
     */
221
    @Override
222
    public void resourceChanged(ResourceProvider resource) {
223
        // Do nothing
224
    }
225

  
226
    protected EditableFeatureType fixFeatureType(FeatureType featureType) {
227
        EditableFeatureType newFeatureType;
228

  
229
        if (featureType instanceof EditableFeatureType) {
230
            newFeatureType = (EditableFeatureType) featureType.getCopy();
231
        } else {
232
            newFeatureType = featureType.getEditable();
233
        }
234

  
235
        for (int i = 0; i < newFeatureType.size(); i++) {
236
            EditableFeatureAttributeDescriptor attr = (EditableFeatureAttributeDescriptor) newFeatureType.getAttributeDescriptor(i);
237
            String s;
238
            switch (attr.getType()) {
239
                case DataTypes.INT:
240
                    s = String.valueOf(Integer.MAX_VALUE);
241
                    attr.setSize(18);
242
                    attr.setPrecision(0);
243
                    break;
244
                case DataTypes.LONG:
245
                    s = String.valueOf(Long.MAX_VALUE);
246
                    attr.setSize(18);
247
                    attr.setPrecision(0);
248
                    break;
249
                case DataTypes.FLOAT:
250
                    attr.setSize(18);
251
                    attr.setPrecision(6);
252
                    break;
253
                case DataTypes.DOUBLE:
254
                    attr.setSize(18);
255
                    attr.setPrecision(6);
256
                    break;
257
            }
258
        }
259
        return newFeatureType;
260
    }
57 261
}
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.csv/src/main/java/org/gvsig/fmap/dal/store/csv/CSVStoreProvider.java
37 37
import java.util.Iterator;
38 38
import java.util.List;
39 39
import java.util.Locale;
40
import java.util.logging.Level;
40 41

  
41 42
import org.apache.commons.io.FilenameUtils;
42 43
import org.apache.commons.io.IOUtils;
......
49 50
import org.gvsig.fmap.dal.DataStoreNotification;
50 51
import org.gvsig.fmap.dal.DataTypes;
51 52
import org.gvsig.fmap.dal.FileHelper;
53
import org.gvsig.fmap.dal.exception.CloseException;
52 54
import org.gvsig.fmap.dal.exception.DataException;
53 55
import org.gvsig.fmap.dal.exception.InitializeException;
54 56
import org.gvsig.fmap.dal.exception.OpenException;
......
108 110
import org.supercsv.io.CsvListWriter;
109 111
import org.supercsv.prefs.CsvPreference;
110 112

  
113
@SuppressWarnings("UseSpecificCatch")
111 114
public class CSVStoreProvider extends AbstractMemoryStoreProvider implements
112 115
        ResourceConsumer {
113 116

  
......
123 126
    private long counterNewsOIDs = 0;
124 127
    private Envelope envelope;
125 128
    private boolean need_calculate_envelope = false;
126
    private SimpleTaskStatus taskStatus;
129
    private final SimpleTaskStatus taskStatus;
130
    private final CSVFeatureWriter writer;
127 131

  
128 132
    public CSVStoreProvider(CSVStoreParameters parameters,
129 133
            DataStoreProviderServices storeServices) throws InitializeException {
......
132 136
                storeServices,
133 137
                FileHelper.newMetadataContainer(METADATA_DEFINITION_NAME)
134 138
        );
135

  
139
        this.writer = new CSVFeatureWriter();
136 140
        TaskStatusManager manager = ToolsLocator.getTaskStatusManager();
137 141
        this.taskStatus = manager.createDefaultSimpleTaskStatus("CSV");
138 142

  
......
145 149
        );
146 150

  
147 151
        resource.addConsumer(this);
152

  
148 153
        initializeFeatureTypes();
149 154
    }
150 155

  
......
152 157
        return (CSVStoreParameters) this.getParameters();
153 158
    }
154 159

  
160
    @Override
155 161
    public String getProviderName() {
156 162
        return NAME;
157 163
    }
158 164

  
165
    @Override
159 166
    public boolean allowWrite() {
160
        return false;
167
        return true;
161 168
    }
162 169

  
163 170
    private String getFullFileName() {
164 171
        // Usar solo para mostrar mensajes en el logger.
165
        String s = "(unknow)";
172
        String s;
166 173
        try {
167 174
            s = getCSVParameters().getFile().getAbsolutePath();
168 175
        } catch (Exception e2) {
......
171 178
        return s;
172 179
    }
173 180

  
181
    @Override
174 182
    public void open() throws OpenException {
175 183
        if (this.data != null) {
176 184
            return;
......
189 197
        }
190 198
    }
191 199

  
200
    @Override
192 201
    public DataServerExplorer getExplorer() throws ReadException {
193 202
        DataManager manager = DALLocator.getDataManager();
194 203
        FilesystemServerExplorerParameters params;
......
205 214

  
206 215
    }
207 216

  
208
    class Writer {
209

  
210
        private Envelope envelope = null;
211
        private boolean calculate_envelope = false;
212
        private CsvListWriter listWriter = null;
213
        private CsvPreference csvpreferences = null;
214
        private FileWriter fwriter = null;
215
        private FeatureType ftype;
216
        private File file;
217
        private String[] values;
218
        private FeatureAttributeDescriptor[] descriptors;
219
        private Coercion convert = null;
220
        private int errorcounts = 0;
221
        private Throwable lasterror = null;
222
        private Locale locale = null;
223

  
224
        public void initialize(File file, FeatureType ftype, CsvPreference csvpreferences) {
225
            this.file = file;
226
            this.ftype = ftype;
227
            this.csvpreferences = csvpreferences;
228
            this.locale = CSVStoreParameters.getLocale(getCSVParameters());
229
            if (csvpreferences == null) {
230
                this.csvpreferences = CsvPreference.STANDARD_PREFERENCE;
231
            }
232
            if (ftype.getDefaultGeometryAttributeName() != null) {
233
                this.calculate_envelope = true;
234
            }
235
            this.descriptors = this.ftype.getAttributeDescriptors();
236
            this.convert = ToolsLocator.getDataTypesManager().getCoercion(org.gvsig.tools.dataTypes.DataTypes.STRING);
237
            this.errorcounts = 0;
238
        }
239

  
240
        public void begin() {
241
            try {
242
                this.fwriter = new FileWriter(file);
243
            } catch (IOException e) {
244
                logger.warn("Can't open file for write (" + file.getAbsolutePath() + ").", e);
245
                throw new RuntimeException(e);
246
            }
247
            this.listWriter = new CsvListWriter(this.fwriter, this.csvpreferences);
248
            int n = 0;
249
            for (int i = 0; i < descriptors.length; i++) {
250
                FeatureAttributeDescriptor descriptor = descriptors[i];
251
                if (descriptor.getEvaluator() == null) {
252
                    n++;
253
                }
254
            }
255

  
256
            String[] header = new String[n];
257
            this.values = new String[n];
258
            n = 0;
259
            for (int i = 0; i < descriptors.length; i++) {
260
                FeatureAttributeDescriptor descriptor = descriptors[i];
261
                if (descriptor.getEvaluator() == null) {
262
                    String name = descriptor.getName();
263
                    String typeName = descriptor.getDataTypeName();
264
                    if (descriptor.getDataType().getType() == DataTypes.STRING) {
265
                        header[n++] = name + "__" + typeName + "__" + descriptor.getSize();
266
                    } else {
267
                        header[n++] = name + "__" + typeName;
268
                    }
269
                }
270
            }
271
            try {
272
                listWriter.writeHeader(header);
273
            } catch (Exception e) {
274
                logger.warn("Can't write header '" + header.toString() + "' file for write (" + file.getAbsolutePath() + ").", e);
275
                throw new RuntimeException(e);
276
            }
277
        }
278

  
279
        public void add(FeatureProvider feature) {
280
            if (this.calculate_envelope) {
281
                Geometry geom = feature.getDefaultGeometry();
282
                if (geom != null) {
283
                    if (envelope == null) {
284
                        try {
285
                            envelope = (Envelope) geom.getEnvelope().clone();
286
                        } catch (CloneNotSupportedException e) {
287
                            logger.warn("Este error no deberia pasar, siempre se puede hacer un clone de un envelope.", e);
288
                        }
289
                    } else {
290
                        envelope.add(geom.getEnvelope());
291
                    }
292
                }
293
            }
294
            int n = 0;
295
            for (int i = 0; i < descriptors.length; i++) {
296
                FeatureAttributeDescriptor descriptor = descriptors[i];
297
                if (descriptor.getEvaluator() == null) {
298
                    Object value = feature.get(i);
299
                    try {
300
                        n++;
301
                        if (this.convert != null && this.convert instanceof CoercionWithLocale) {
302
                            values[n] = (String) ((CoercionWithLocale) this.convert).coerce(value, this.locale);
303
                        } else {
304
                            values[n] = (String) this.convert.coerce(value);
305
                        }
306
                    } catch (CoercionException e) {
307
                        try {
308
                            values[n] = value.toString();
309
                        } catch (Exception ex) {
310
                            values[n] = "";
311
                        }
312
                        if (errorcounts++ <= 10) {
313
                            this.lasterror = e;
314
                            logger.warn("Can't convert value of field " + i + " to string in CVS file '" + getFullFileName() + "'.", e);
315
                            if (errorcounts == 10) {
316
                                logger.warn("Too many error writing CVS file '" + getFullFileName() + "', don't output more.");
317
                            }
318
                        }
319
                    }
320
                }
321
            }
322
            try {
323
                this.listWriter.writeHeader(values);
324
            } catch (IOException e) {
325
                if (errorcounts++ <= 10) {
326
                    this.lasterror = e;
327
                    logger.warn("Can't write values to CVS file '" + getFullFileName() + "'.", e);
328
                    if (errorcounts == 10) {
329
                        logger.warn("Too many error writing CVS file '" + getFullFileName() + "', don't output more.");
330
                    }
331
                }
332
            }
333

  
334
        }
335

  
336
        public void end() throws PerformEditingException {
337
            if (this.errorcounts > 0) {
338
                throw new PerformEditingException(this.file.getAbsolutePath(), lasterror);
339
            }
340
            if (listWriter != null) {
341
                try {
342
                    listWriter.close();
343
                } catch (Exception ex) {
344
                    // Ignore error
345
                }
346
                listWriter = null;
347
            }
348
            if (fwriter != null) {
349
                try {
350
                    fwriter.close();
351
                } catch (Exception ex) {
352
                    // Ignore error
353
                }
354
                fwriter = null;
355
            }
356
        }
357

  
358
        public Envelope getEnvelope() {
359
            return this.envelope;
360
        }
361
    }
362

  
217
    @Override
363 218
    public void performChanges(Iterator deleteds, Iterator inserteds, Iterator updateds, Iterator originalFeatureTypesUpdated) throws PerformEditingException {
364 219

  
365 220
        try {
366 221
            this.taskStatus.add();
367 222
            taskStatus.message("_preparing");
223
            this.getResource().closeRequest();
368 224
            getResource().execute(new ResourceAction() {
225
                @Override
369 226
                public Object run() throws Exception {
370 227
                    FeatureSet features = null;
371 228
                    DisposableIterator it = null;
372 229
                    try {
373 230
                        File file = (File) resource.get();
374 231

  
375
                        Writer writer = new Writer();
376
                        writer.initialize(file, getStoreServices().getDefaultFeatureType(), getCSVPreferences());
377 232
                        features
378 233
                                = getStoreServices().getFeatureStore()
379
                                .getFeatureSet();
234
                                        .getFeatureSet();
380 235
                        List<FeatureProvider> newdata = new ArrayList<FeatureProvider>();
381
                        writer.begin();
236
                        FeatureType ftype = getStoreServices().getDefaultFeatureType();
237
                        writer.initialize(getCSVParameters(), file, ftype, getCSVPreferences());
238
                        writer.beginAppend();
382 239
                        it = features.fastIterator();
383 240
                        taskStatus.setRangeOfValues(0, 0);
384 241
                        long counter = 0;
......
410 267
                    return null;
411 268
                }
412 269

  
413
                private CsvPreference getCSVPreferences() {
414
                    CSVReader reader = new CSVReader(getCSVParameters());
415
                    return reader.getCSVPreferences();
416
                }
417

  
418 270
            });
419 271
            this.taskStatus.terminate();
420 272
        } catch (Exception e) {
......
425 277
        }
426 278
    }
427 279

  
280
    private CsvPreference getCSVPreferences() {
281
        CSVReader reader = new CSVReader(getCSVParameters());
282
        return reader.getCSVPreferences();
283
    }
284

  
285
    @Override
428 286
    public boolean closeResourceRequested(ResourceProvider resource) {
429 287
        return true;
430 288
    }
431 289

  
290
    @Override
432 291
    public int getOIDType() {
433 292
        return DataTypes.LONG;
434 293
    }
435 294

  
295
    @Override
436 296
    public boolean supportsAppendMode() {
437
        return false;
297
        return true;
438 298
    }
439 299

  
440
    public void append(FeatureProvider featureProvider) {
441
        throw new UnsupportedOperationException();
300
    @Override
301
    public void append(final FeatureProvider featureProvider) {
302
        //todo
303
        getResource().execute(new ResourceAction() {
304
            @Override
305
            public Object run() throws Exception {
306
                //writer.append(getStoreServices().createFeature(featureProvider));
307
                writer.add(featureProvider);
308
                return null;
309
            }
310
        });
442 311
    }
443 312

  
444
    public void beginAppend() {
445
        throw new UnsupportedOperationException();
313
    @Override
314
    public void beginAppend() throws DataException {
315
        this.close();
316
        getResource().execute(new ResourceAction() {
317
            public Object run() throws Exception {
318
                writer.initialize(
319
                        getCSVParameters(),
320
                        getCSVParameters().getFile(),
321
                        getFeatureStore().getDefaultFeatureType(),
322
                        null
323
                );
324
                writer.beginAppend();
325
                return null;
326
            }
327
        });
328

  
446 329
    }
447 330

  
331
    @Override
448 332
    public void endAppend() {
449
        throw new UnsupportedOperationException();
333
        try {
334
            getResource().execute(new ResourceAction() {
335
                public Object run() throws Exception {
336
                    writer.end();
337
                    resource.notifyChanges(); //resourcesNotifyChanges();
338
                    counterNewsOIDs = -1;
339
                    return null;
340
                }
341
            });
342
            writer.end();
343
        } catch (PerformEditingException ex) {
344
            logger.warn("Not been able to end append '" + this.getFullName() + "'.", ex);
345
        }
450 346
    }
451 347

  
452 348
    public void saveToState(PersistentState state) throws PersistenceException {
......
457 353
        throw new NotYetImplemented();
458 354
    }
459 355

  
356
    @Override
460 357
    public Object createNewOID() {
461
        return new Long(counterNewsOIDs++);
358
        return counterNewsOIDs++;
462 359
    }
463 360

  
464 361
    protected void initializeFeatureTypes() throws InitializeException {
......
469 366
        }
470 367
    }
471 368

  
369
    @Override
472 370
    public Envelope getEnvelope() throws DataException {
473 371
        this.open();
474 372
        if (this.envelope != null) {
......
484 382
        try {
485 383
            this.envelope = GeometryLocator.getGeometryManager().createEnvelope(fad.getGeomType().getSubType());
486 384
            fs.accept(new Visitor() {
385
                @Override
487 386
                public void visit(Object obj) throws VisitCanceledException, BaseException {
488 387
                    Feature f = (Feature) obj;
489 388
                    Geometry geom = f.getDefaultGeometry();
......
501 400
        return this.envelope;
502 401
    }
503 402

  
403
    @Override
504 404
    public Object getDynValue(String name) throws DynFieldNotFoundException {
505 405
        if (DataStore.METADATA_ENVELOPE.equalsIgnoreCase(name)) {
506 406
            try {
......
519 419
        return super.getDynValue(name);
520 420
    }
521 421

  
422
    @Override
522 423
    public void resourceChanged(ResourceProvider resource) {
523 424
        this.getStoreServices().notifyChange(
524 425
                DataStoreNotification.RESOURCE_CHANGED,
525 426
                resource);
526 427
    }
527 428

  
429
    @Override
528 430
    public Object getSourceId() {
529 431
        return this.getCSVParameters().getFile();
530 432
    }
531 433

  
434
    @Override
532 435
    public String getName() {
533 436
        String name = this.getCSVParameters().getFile().getName();
534 437
        return FilenameUtils.getBaseName(name);
535 438
    }
536 439

  
440
    @Override
537 441
    public String getFullName() {
538 442
        return this.getCSVParameters().getFile().getAbsolutePath();
539 443
    }
540 444

  
445
    @Override
541 446
    public ResourceProvider getResource() {
542 447
        return resource;
543 448
    }
......
549 454
        return s.trim().length() == 0;
550 455
    }
551 456

  
457
    private void init(CSVStoreParameters parameters, DataStoreProviderServices storeServices) {
458
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
459
    }
460

  
552 461
    private class FieldTypeParser {
553 462

  
554 463
        public String name = null;
......
589 498
        //   name[__typename[__size[__notnull|null]]]
590 499
        //
591 500
        public boolean parse(String value) {
592
            String typename = null;
501
            String theTypename = null;
593 502
            String[] ss = null;
594 503
            if (value.contains(":")) {
595 504
                ss = value.split(":");
......
615 524
                        this.type = this.getType(this.typename);
616 525
                        if (this.type == DataTypes.INVALID) {
617 526
                            this.type = DataTypes.STRING;
618
                            logger.info("Type '" + typename + "' not valid for attribute '" + value + "' in CSV file '" + getFullFileName() + "'.");
527
                            logger.info("Type '" + theTypename + "' not valid for attribute '" + value + "' in CSV file '" + getFullFileName() + "'.");
619 528
                        }
620 529
                    }
621 530
                    if (!StringUtils.isBlank(ss[2])) {
......
651 560
                        this.type = this.getType(this.typename);
652 561
                        if (this.type == DataTypes.INVALID) {
653 562
                            this.type = DataTypes.STRING;
654
                            logger.info("Type '" + typename + "' not valid for attribute '" + value + "' in CSV file '" + getFullFileName() + "'.");
563
                            logger.info("Type '" + theTypename + "' not valid for attribute '" + value + "' in CSV file '" + getFullFileName() + "'.");
655 564
                        }
656 565
                    }
657 566
                case 1:
......
736 645
        if (pointDimensionNames != null) {
737 646
            PointAttributeEmulator emulator = new PointAttributeEmulator(pointDimensionNames);
738 647
            EditableFeatureAttributeDescriptor attr = fType.add(
739
                    CSVStoreParameters.getPointColumnName(this.getParameters()), 
648
                    CSVStoreParameters.getPointColumnName(this.getParameters()),
740 649
                    DataTypes.GEOMETRY, emulator
741 650
            );
742 651
            GeometryManager geommgr = GeometryLocator.getGeometryManager();
......
750 659
                logger.warn("Can't set geometry type for the calculated field in CSV file '" + getFullFileName() + "'.", e);
751 660
            }
752 661
        }
753
        
662

  
754 663
        String geometry_column = CSVStoreParameters.getGeometryColumn(this.getParameters());
755
        if( !StringUtils.isEmpty(geometry_column) ) {
664
        if (!StringUtils.isEmpty(geometry_column)) {
756 665
            EditableFeatureAttributeDescriptor attr = (EditableFeatureAttributeDescriptor) fType.get(geometry_column);
757
            if( attr!=null && attr.getType()!=DataTypes.GEOMETRY ) {
666
            if (attr != null && attr.getType() != DataTypes.GEOMETRY) {
758 667
                attr.setDataType(DataTypes.GEOMETRY);
759 668
                GeometryManager geommgr = GeometryLocator.getGeometryManager();
760 669
                GeometryType gt;
......
800 709
            this.dataType = datatypeManager.get(DataTypes.GEOMETRY);
801 710
        }
802 711

  
712
        @Override
803 713
        public Object get(Feature feature) {
804 714
            try {
805 715
                Object valueX = feature.get(this.fieldNames[XNAME]);
......
820 730
                    }
821 731
                }
822 732

  
823
                double x = ((Double) valueX).doubleValue();
824
                double y = ((Double) valueY).doubleValue();
733
                double x = ((Double) valueX);
734
                double y = ((Double) valueY);
825 735
                Point point = geommgr.createPoint(x, y, Geometry.SUBTYPES.GEOM3D);
826 736
                if (this.fieldNames.length > 2) {
827
                    double z = ((Double) valueZ).doubleValue();
737
                    double z = ((Double) valueZ);
828 738
                    point.setCoordinateAt(2, z);
829 739
                }
830 740
                return point;
......
854 764
            }
855 765
        }
856 766

  
767
        @Override
857 768
        public boolean allowSetting() {
858 769
            return true;
859 770
        }
860 771

  
772
        @Override
861 773
        public String[] getRequiredFieldNames() {
862 774
            return this.fieldNames;
863 775
        }
......
885 797
            this.toDouble = ToolsLocator.getDataTypesManager().getCoercion(DataTypes.DOUBLE);
886 798
        }
887 799

  
800
        @Override
888 801
        public Object evaluate(EvaluatorData data) throws EvaluatorException {
889 802
            try {
890 803
                double x = ((Double) toDouble.coerce(data.getDataValue(xname))).doubleValue();
......
904 817
            }
905 818
        }
906 819

  
820
        @Override
907 821
        public String getName() {
908 822
            return "ToPointEvaluaror";
909 823
        }
......
941 855
    private InputStreamReader openFile(File f, String charsetName) throws FileNotFoundException {
942 856
        Charset charset = Charset.defaultCharset();
943 857
        FileInputStream fis = new FileInputStream(f);
944
        if( !StringUtils.isEmpty(charsetName) ) {
945
            if( Charset.isSupported(charsetName) )  {
858
        if (!StringUtils.isEmpty(charsetName)) {
859
            if (Charset.isSupported(charsetName)) {
946 860
                try {
947 861
                    charset = Charset.forName(charsetName);
948
                } catch(Throwable th) {
949
                    logger.warn("Can't use charset '"+charsetName+"' for read csv '"+this.getFullFileName()+"'.", th);
862
                } catch (Throwable th) {
863
                    logger.warn("Can't use charset '" + charsetName + "' for read csv '" + this.getFullFileName() + "'.", th);
950 864
                }
951 865
            } else {
952
                logger.warn("charset '"+charsetName+"' not supported for read csv '"+this.getFullFileName()+"'.");                
866
                logger.warn("charset '" + charsetName + "' not supported for read csv '" + this.getFullFileName() + "'.");
953 867
            }
954 868
        }
955 869
        InputStreamReader isr = new InputStreamReader(fis, charset);
956 870
        return isr;
957 871
    }
958
    
872

  
959 873
    private void loadFeatures() {
960 874
        InputStreamReader in = null;
961 875
        SimpleReader reader = null;
......
966 880
            boolean ignore_errors = CSVStoreParameters.getIgnoreErrors(getCSVParameters());
967 881

  
968 882
            in = openFile(
969
                this.getCSVParameters().getFile(), 
970
                CSVStoreParameters.getCharset(this.getCSVParameters())
883
                    this.getCSVParameters().getFile(),
884
                    CSVStoreParameters.getCharset(this.getCSVParameters())
971 885
            );
972 886

  
973 887
            reader = getSimpleReader(in);
......
982 896
                        } else {
983 897
                            String msg = "Can't retrieve header from csv file '"
984 898
                                    + this.getCSVParameters().getFile()
985
                                    .getAbsolutePath()
899
                                            .getAbsolutePath()
986 900
                                    + "' and not specified in the parameters.";
987 901
                            logger.warn(msg);
988 902
                            throw new RuntimeException(msg);
......
998 912
            }
999 913

  
1000 914
            int[] detectedTypes = automaticDetectionOfTypes(headers);
1001
            if( detectedTypes!=null && detectedTypes.length>headers.length ) {
915
            if (detectedTypes != null && detectedTypes.length > headers.length) {
1002 916
                // Se han detectado mas columnas que las que hay en la cabezera,
1003 917
                // a?adimos mas columnas a la cabezera.
1004 918
                String[] headers2 = new String[detectedTypes.length];
1005
                for( int i=0; i<headers2.length; i++ ) {
1006
                    if( i<headers.length ) {
919
                for (int i = 0; i < headers2.length; i++) {
920
                    if (i < headers.length) {
1007 921
                        headers2[i] = headers[i];
1008 922
                    } else {
1009 923
                        headers2[i] = getFixedHeader(i);
......
1011 925
                }
1012 926
                headers = headers2;
1013 927
            }
1014
            for( int i=0; i<headers.length; i++ ) {
1015
                if( StringUtils.isEmpty(headers[i]) ) {
928
            for (int i = 0; i < headers.length; i++) {
929
                if (StringUtils.isEmpty(headers[i])) {
1016 930
                    headers[i] = getFixedHeader(i);
1017 931
                }
1018 932
            }
......
1029 943
                sizes[i] = -1;
1030 944
                FeatureAttributeDescriptor ad = ftype.getAttributeDescriptor(i);
1031 945
                coercion[i] = ad.getDataType().getCoercion();
1032
                switch (ad.getDataType().getType() ) {
1033
                case DataTypes.INT:
1034
                    sizes[i] = 10;
1035
                    break;
1036
                case DataTypes.LONG:
1037
                    sizes[i] = 20;
1038
                    break;
1039
                case DataTypes.STRING:
1040
                    if (ad.getSize() == 0) {
1041
                        // Es un string y no tiene un size asignado.
1042
                        // Lo ponemos a cero para calcularlo.
946
                switch (ad.getDataType().getType()) {
947
                    case DataTypes.INT:
948
                        sizes[i] = 10;
949
                        break;
950
                    case DataTypes.LONG:
951
                        sizes[i] = 20;
952
                        break;
953
                    case DataTypes.STRING:
954
                        if (ad.getSize() == 0) {
955
                            // Es un string y no tiene un size asignado.
956
                            // Lo ponemos a cero para calcularlo.
957
                            sizes[i] = 0;
958
                        }
959
                        break;
960
                    case DataTypes.URL:
961
                    case DataTypes.URI:
962
                    case DataTypes.FILE:
1043 963
                        sizes[i] = 0;
1044
                    }
1045
                    break;
1046
                case DataTypes.URL:
1047
                case DataTypes.URI:
1048
                case DataTypes.FILE:
1049
                    sizes[i] = 0;
1050 964
                }
1051 965
            }
1052 966
            if (ftype.getDefaultGeometryAttributeName() != null) {
......
1079 993
                            value = coercion[i].coerce(rawvalue);
1080 994
                        }
1081 995
                        feature.set(i, value);
1082
                        if (sizes[i] >= 0 && 
1083
                            (value instanceof String || value instanceof URL || 
1084
                             value instanceof URI || value instanceof File) ) {
996
                        if (sizes[i] >= 0
997
                                && (value instanceof String || value instanceof URL
998
                                || value instanceof URI || value instanceof File)) {
1085 999
                            int x = value.toString().length();
1086 1000
                            if (sizes[i] < x) {
1087 1001
                                sizes[i] = x;
......
1100 1014
                    }
1101 1015
                }
1102 1016
                this.addFeatureProvider(feature);
1103
                if( limit>0 ) {
1104
                    if( limit < this.data.size() ) {
1017
                if (limit > 0) {
1018
                    if (limit < this.data.size()) {
1105 1019
                        break;
1106 1020
                    }
1107 1021
                }
......
1165 1079
                    this.getFullFileName()
1166 1080
            );
1167 1081
            types = x.detect(
1168
                    headers.length, 
1169
                    reader, 
1170
                    CSVStoreParameters.isFirstLineHeader(getCSVParameters()), 
1082
                    headers.length,
1083
                    reader,
1084
                    CSVStoreParameters.isFirstLineHeader(getCSVParameters()),
1171 1085
                    CSVStoreParameters.getLocale(getCSVParameters())
1172 1086
            );
1173 1087
        } catch (Exception ex) {
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.file/org.gvsig.fmap.dal.file.csv/src/main/java/org/gvsig/fmap/dal/store/csv/CSVFeatureWriter.java
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.store.csv;
7

  
8
import java.io.File;
9
import java.io.FileWriter;
10
import java.io.IOException;
11
import java.util.Locale;
12
import org.gvsig.fmap.dal.DataTypes;
13
import org.gvsig.fmap.dal.feature.Feature;
14
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
15
import org.gvsig.fmap.dal.feature.FeatureType;
16
import org.gvsig.fmap.dal.feature.exception.PerformEditingException;
17
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
18
import org.gvsig.fmap.geom.Geometry;
19
import org.gvsig.fmap.geom.primitive.Envelope;
20
import org.gvsig.tools.ToolsLocator;
21
import org.gvsig.tools.dataTypes.CoercionException;
22
import org.gvsig.tools.dataTypes.DataTypesManager;
23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25
import org.supercsv.io.CsvListWriter;
26
import org.supercsv.prefs.CsvPreference;
27

  
28
/**
29
 *
30
 * @author osc
31
 */
32
public class CSVFeatureWriter {
33

  
34
    private Envelope envelope = null;
35
    private boolean calculate_envelope = false;
36
    private CsvListWriter listWriter = null;
37
    private CsvPreference csvpreferences = null;
38
    private FileWriter fwriter = null;
39
    private FeatureType ftype;
40
    private File file;
41
    private String[] values;
42
    private FeatureAttributeDescriptor[] descriptors;
43
    private DataTypesManager.Coercion convert = null;
44
    private int errorcounts = 0;
45
    private Throwable lasterror = null;
46
    private Locale locale = null;
47
    private CSVStoreParameters storeParameters;
48
    private static final Logger logger = LoggerFactory.getLogger(CSVFeatureWriter.class);
49

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff