svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.dbf / src / main / java / org / gvsig / fmap / dal / store / dbf / DBFStoreParameters.java @ 47436
History | View | Annotate | Download (9.3 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | package org.gvsig.fmap.dal.store.dbf; |
25 | |||
26 | import java.io.File; |
||
27 | import java.nio.charset.Charset; |
||
28 | 41483 | jjdelcerro | import java.text.SimpleDateFormat; |
29 | import java.util.Locale; |
||
30 | import org.apache.commons.lang3.BooleanUtils; |
||
31 | import org.apache.commons.lang3.StringUtils; |
||
32 | 43245 | jjdelcerro | import org.gvsig.fmap.dal.DataParameters; |
33 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.FileHelper; |
34 | 41483 | jjdelcerro | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
35 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.feature.OpenFeatureStoreParameters; |
36 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
37 | 46338 | fdiaz | import org.gvsig.fmap.dal.spi.AbstractDataStoreParameters; |
38 | 40435 | jjdelcerro | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
39 | 43245 | jjdelcerro | import org.gvsig.fmap.dal.store.dbf.utils.DbaseCodepage; |
40 | 40435 | jjdelcerro | import org.gvsig.tools.dynobject.DelegatedDynObject; |
41 | 41483 | jjdelcerro | import org.slf4j.Logger; |
42 | import org.slf4j.LoggerFactory; |
||
43 | 40435 | jjdelcerro | |
44 | |||
45 | 46338 | fdiaz | public class DBFStoreParameters extends AbstractDataStoreParameters implements |
46 | 43020 | jjdelcerro | OpenFeatureStoreParameters, FilesystemStoreParameters { |
47 | 40435 | jjdelcerro | |
48 | 41483 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(DBFStoreParameters.class); |
49 | 42814 | fdiaz | |
50 | 40435 | jjdelcerro | public static final String PARAMETERS_DEFINITION_NAME = "DBFStoreParameters"; |
51 | |||
52 | public static final String DBFFILE_PARAMTER_NAME = "dbfFile"; |
||
53 | public static final String ENCODING_PARAMTER_NAME = "encoding"; |
||
54 | 41483 | jjdelcerro | public static final String HANDLE_DATES_AS_STRINGS = "handleDatesAsStrings"; |
55 | public static final String DATE_FORMAT = "dateFormat"; |
||
56 | public static final String LOCALE = "locale"; |
||
57 | 47436 | fdiaz | public static final String ALLOW_DECIMAL_INCONSISTENCIES_PARAMTER_NAME = "allowInconsistenciesInDecimals"; |
58 | 42814 | fdiaz | public static final String ALLOW_DUPLICATED_FIELD_NAMES = "allowDuplicatedFieldNames"; |
59 | 40435 | jjdelcerro | |
60 | 43245 | jjdelcerro | // We don't want to persist or show effective encoding in the store parameters dialog
|
61 | // But we still need it to properly encode layers when editing!!
|
||
62 | private String effectiveEncoding = null; |
||
63 | 40435 | jjdelcerro | private DelegatedDynObject parameters;
|
64 | |||
65 | public DBFStoreParameters() {
|
||
66 | this(PARAMETERS_DEFINITION_NAME);
|
||
67 | } |
||
68 | |||
69 | protected DBFStoreParameters(String parametersDefinitionName) { |
||
70 | this(parametersDefinitionName, DBFStoreProvider.NAME);
|
||
71 | } |
||
72 | |||
73 | public DBFStoreParameters(String parametersDefinitionName, String name) { |
||
74 | super();
|
||
75 | this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
|
||
76 | this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
|
||
77 | } |
||
78 | |||
79 | public String getDataStoreName() { |
||
80 | return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME); |
||
81 | } |
||
82 | |||
83 | public String getDescription() { |
||
84 | return this.getDynClass().getDescription(); |
||
85 | } |
||
86 | |||
87 | public boolean isValid() { |
||
88 | return (this.getDBFFileName() != null); |
||
89 | } |
||
90 | |||
91 | public File getFile() { |
||
92 | return (File) this.getDynValue(DBFFILE_PARAMTER_NAME); |
||
93 | } |
||
94 | |||
95 | public void setFile(File file) { |
||
96 | this.setDynValue(DBFFILE_PARAMTER_NAME, file);
|
||
97 | } |
||
98 | |||
99 | public void setFile(String fileName) { |
||
100 | this.setDynValue(DBFFILE_PARAMTER_NAME, fileName);
|
||
101 | } |
||
102 | |||
103 | public String getDBFFileName() { |
||
104 | if( this.getDBFFile() == null ) { |
||
105 | return null; |
||
106 | } |
||
107 | return this.getDBFFile().getAbsolutePath(); |
||
108 | } |
||
109 | |||
110 | public File getDBFFile() { |
||
111 | return (File) this.getDynValue(DBFFILE_PARAMTER_NAME); |
||
112 | } |
||
113 | |||
114 | public void setDBFFile(File file) { |
||
115 | this.setDynValue(DBFFILE_PARAMTER_NAME, file);
|
||
116 | } |
||
117 | |||
118 | public void setDBFFile(String fileName) { |
||
119 | this.setDynValue(DBFFILE_PARAMTER_NAME, fileName);
|
||
120 | } |
||
121 | 43245 | jjdelcerro | |
122 | public String getCPGFileName() { |
||
123 | return DbaseCodepage.getCpgFileName(getDBFFileName());
|
||
124 | } |
||
125 | |||
126 | public File getCPGFile() { |
||
127 | return new File(DbaseCodepage.getCpgFileName(getDBFFileName())); |
||
128 | } |
||
129 | 40435 | jjdelcerro | |
130 | public String getEncodingName() { |
||
131 | 41917 | jjdelcerro | String s = (String) getDynValue(ENCODING_PARAMTER_NAME); |
132 | if( StringUtils.isBlank(s) ) {
|
||
133 | return null; |
||
134 | } |
||
135 | if( "DEFAULT".equalsIgnoreCase(s.trim()) ) { |
||
136 | return null; |
||
137 | } |
||
138 | return s.trim();
|
||
139 | } |
||
140 | 42814 | fdiaz | |
141 | 40435 | jjdelcerro | public Charset getEncoding() { |
142 | String name = getEncodingName();
|
||
143 | 41917 | jjdelcerro | if( StringUtils.isBlank(name) ) {
|
144 | return null; |
||
145 | } |
||
146 | 40435 | jjdelcerro | return Charset.forName(name); |
147 | } |
||
148 | |||
149 | 43245 | jjdelcerro | /**
|
150 | * The encoding actually used to read/write the dbf
|
||
151 | */
|
||
152 | public String getEffectiveEncodingName() { |
||
153 | if (effectiveEncoding==null) { |
||
154 | return getEncodingName();
|
||
155 | } |
||
156 | return effectiveEncoding.trim();
|
||
157 | } |
||
158 | |||
159 | /**
|
||
160 | * The encoding actually used to read/write the dbf
|
||
161 | */
|
||
162 | public Charset getEffectiveEncoding() { |
||
163 | String name = getEffectiveEncodingName();
|
||
164 | if (name==null) { |
||
165 | return null; |
||
166 | } |
||
167 | return Charset.forName(name); |
||
168 | } |
||
169 | |||
170 | 40435 | jjdelcerro | public void setEncoding(String encoding) { |
171 | this.setEncoding(Charset.forName(encoding)); |
||
172 | 42814 | fdiaz | } |
173 | 40435 | jjdelcerro | |
174 | 43245 | jjdelcerro | /**
|
175 | * The encoding actually used to read/write the dbf
|
||
176 | */
|
||
177 | public void setEffectiveEncoding(String encoding) { |
||
178 | this.effectiveEncoding = encoding;
|
||
179 | } |
||
180 | |||
181 | 42814 | fdiaz | public boolean handleDatesAsStrings() { |
182 | Boolean x = (Boolean) getDynValue(HANDLE_DATES_AS_STRINGS); |
||
183 | return BooleanUtils.isTrue(x);
|
||
184 | } |
||
185 | |||
186 | public boolean allowDuplicatedFieldNames() { |
||
187 | Boolean x = (Boolean) getDynValue(ALLOW_DUPLICATED_FIELD_NAMES); |
||
188 | return BooleanUtils.isTrue(x);
|
||
189 | } |
||
190 | |||
191 | public void setEncoding(Charset charset) { |
||
192 | this.setDynValue(ENCODING_PARAMTER_NAME, charset.name());
|
||
193 | 40435 | jjdelcerro | } |
194 | 43245 | jjdelcerro | |
195 | /**
|
||
196 | * The encoding actually used to read/write the dbf
|
||
197 | */
|
||
198 | public void setEffectiveEncoding(Charset charset) { |
||
199 | this.effectiveEncoding = charset.name();
|
||
200 | } |
||
201 | |||
202 | @Override
|
||
203 | public DataParameters getCopy() {
|
||
204 | DataParameters copy = super.getCopy();
|
||
205 | if (copy instanceof DBFStoreParameters) { |
||
206 | DBFStoreParameters dbfParams = (DBFStoreParameters) copy; |
||
207 | dbfParams.setEffectiveEncoding(this.effectiveEncoding);
|
||
208 | } |
||
209 | return copy;
|
||
210 | } |
||
211 | 40435 | jjdelcerro | |
212 | protected DelegatedDynObject getDelegatedDynObject() {
|
||
213 | return parameters;
|
||
214 | } |
||
215 | 42814 | fdiaz | |
216 | 41483 | jjdelcerro | public Locale getLocale() { |
217 | try {
|
||
218 | String s = (String) this.getDynValue(LOCALE); |
||
219 | if( s.trim().length()==0 ) { |
||
220 | return null; |
||
221 | } |
||
222 | if( "DEFAULT".equalsIgnoreCase(s.trim()) ) { |
||
223 | return Locale.getDefault(); |
||
224 | } |
||
225 | Locale locale = null; |
||
226 | // locale = Locale.forLanguageTag(s); // Since java 1.7
|
||
227 | String[] ss = s.split("-"); |
||
228 | switch(ss.length) {
|
||
229 | case 1: |
||
230 | locale = new Locale(ss[0]); |
||
231 | break;
|
||
232 | case 2: |
||
233 | locale = new Locale(ss[0],ss[1]); |
||
234 | break;
|
||
235 | case 3: |
||
236 | default:
|
||
237 | locale = new Locale(ss[0],ss[1],ss[2]); |
||
238 | break;
|
||
239 | } |
||
240 | return locale;
|
||
241 | } catch( Exception ex) { |
||
242 | logger.warn("Can't get locale from DBF parameters.", ex);
|
||
243 | return Locale.getDefault(); |
||
244 | } |
||
245 | } |
||
246 | |||
247 | public String getDateFormat() { |
||
248 | return (String) getDynValue(DATE_FORMAT); |
||
249 | } |
||
250 | |||
251 | public void validate() throws ValidateDataParametersException { |
||
252 | super.validate();
|
||
253 | String sfmt = getDateFormat();
|
||
254 | if( !StringUtils.isBlank(sfmt) ) {
|
||
255 | try {
|
||
256 | SimpleDateFormat datefmt = new SimpleDateFormat(sfmt, getLocale()); |
||
257 | } catch(Exception ex) { |
||
258 | throw new InvalidDateFormatException(sfmt,ex); |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 | 47436 | fdiaz | |
263 | public boolean allowInconsistenciesInDecimals() { |
||
264 | Boolean x = (Boolean) this.getDynValue(ALLOW_DECIMAL_INCONSISTENCIES_PARAMTER_NAME); |
||
265 | if(x == null){ |
||
266 | return false; |
||
267 | } |
||
268 | return x;
|
||
269 | } |
||
270 | 42814 | fdiaz | |
271 | 41483 | jjdelcerro | private static class InvalidDateFormatException extends ValidateDataParametersException { |
272 | 42814 | fdiaz | |
273 | 41483 | jjdelcerro | public InvalidDateFormatException(String sfmt, Throwable cause) { |
274 | super(
|
||
275 | "Date format specified '%(dataformat)' is not valid.",
|
||
276 | cause, |
||
277 | "Date_format_specified_XdataformatX_is_not_valid",
|
||
278 | 0
|
||
279 | ); |
||
280 | setValue("dataformat",sfmt);
|
||
281 | 42814 | fdiaz | |
282 | 41483 | jjdelcerro | } |
283 | } |
||
284 | 42814 | fdiaz | |
285 | 40435 | jjdelcerro | } |