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 @ 43020
History | View | Annotate | Download (7.32 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 | 40435 | jjdelcerro | |
33 | 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 | import org.gvsig.fmap.dal.spi.AbstractDataParameters; |
||
38 | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
||
39 | import org.gvsig.tools.dynobject.DelegatedDynObject; |
||
40 | 41483 | jjdelcerro | import org.slf4j.Logger; |
41 | import org.slf4j.LoggerFactory; |
||
42 | 40435 | jjdelcerro | |
43 | |||
44 | public class DBFStoreParameters extends AbstractDataParameters implements |
||
45 | 43020 | jjdelcerro | OpenFeatureStoreParameters, FilesystemStoreParameters { |
46 | 40435 | jjdelcerro | |
47 | 41483 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(DBFStoreParameters.class); |
48 | 42814 | fdiaz | |
49 | 40435 | jjdelcerro | public static final String PARAMETERS_DEFINITION_NAME = "DBFStoreParameters"; |
50 | |||
51 | public static final String DBFFILE_PARAMTER_NAME = "dbfFile"; |
||
52 | public static final String ENCODING_PARAMTER_NAME = "encoding"; |
||
53 | 41483 | jjdelcerro | public static final String HANDLE_DATES_AS_STRINGS = "handleDatesAsStrings"; |
54 | public static final String DATE_FORMAT = "dateFormat"; |
||
55 | public static final String LOCALE = "locale"; |
||
56 | 42814 | fdiaz | public static final String ALLOW_DUPLICATED_FIELD_NAMES = "allowDuplicatedFieldNames"; |
57 | 40435 | jjdelcerro | |
58 | private DelegatedDynObject parameters;
|
||
59 | |||
60 | public DBFStoreParameters() {
|
||
61 | this(PARAMETERS_DEFINITION_NAME);
|
||
62 | } |
||
63 | |||
64 | protected DBFStoreParameters(String parametersDefinitionName) { |
||
65 | this(parametersDefinitionName, DBFStoreProvider.NAME);
|
||
66 | } |
||
67 | |||
68 | public DBFStoreParameters(String parametersDefinitionName, String name) { |
||
69 | super();
|
||
70 | this.parameters = (DelegatedDynObject) FileHelper.newParameters(parametersDefinitionName);
|
||
71 | this.setDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME, name);
|
||
72 | } |
||
73 | |||
74 | public String getDataStoreName() { |
||
75 | return (String) this.getDynValue(DataStoreProviderServices.PROVIDER_PARAMTER_NAME); |
||
76 | } |
||
77 | |||
78 | public String getDescription() { |
||
79 | return this.getDynClass().getDescription(); |
||
80 | } |
||
81 | |||
82 | public boolean isValid() { |
||
83 | return (this.getDBFFileName() != null); |
||
84 | } |
||
85 | |||
86 | public File getFile() { |
||
87 | return (File) this.getDynValue(DBFFILE_PARAMTER_NAME); |
||
88 | } |
||
89 | |||
90 | public void setFile(File file) { |
||
91 | this.setDynValue(DBFFILE_PARAMTER_NAME, file);
|
||
92 | } |
||
93 | |||
94 | public void setFile(String fileName) { |
||
95 | this.setDynValue(DBFFILE_PARAMTER_NAME, fileName);
|
||
96 | } |
||
97 | |||
98 | public String getDBFFileName() { |
||
99 | if( this.getDBFFile() == null ) { |
||
100 | return null; |
||
101 | } |
||
102 | return this.getDBFFile().getAbsolutePath(); |
||
103 | } |
||
104 | |||
105 | public File getDBFFile() { |
||
106 | return (File) this.getDynValue(DBFFILE_PARAMTER_NAME); |
||
107 | } |
||
108 | |||
109 | public void setDBFFile(File file) { |
||
110 | this.setDynValue(DBFFILE_PARAMTER_NAME, file);
|
||
111 | } |
||
112 | |||
113 | public void setDBFFile(String fileName) { |
||
114 | this.setDynValue(DBFFILE_PARAMTER_NAME, fileName);
|
||
115 | } |
||
116 | |||
117 | public String getEncodingName() { |
||
118 | 41917 | jjdelcerro | String s = (String) getDynValue(ENCODING_PARAMTER_NAME); |
119 | if( StringUtils.isBlank(s) ) {
|
||
120 | return null; |
||
121 | } |
||
122 | if( "DEFAULT".equalsIgnoreCase(s.trim()) ) { |
||
123 | return null; |
||
124 | } |
||
125 | return s.trim();
|
||
126 | } |
||
127 | 42814 | fdiaz | |
128 | 40435 | jjdelcerro | public Charset getEncoding() { |
129 | String name = getEncodingName();
|
||
130 | 41917 | jjdelcerro | if( StringUtils.isBlank(name) ) {
|
131 | return null; |
||
132 | } |
||
133 | 40435 | jjdelcerro | return Charset.forName(name); |
134 | } |
||
135 | |||
136 | public void setEncoding(String encoding) { |
||
137 | this.setEncoding(Charset.forName(encoding)); |
||
138 | 42814 | fdiaz | } |
139 | 40435 | jjdelcerro | |
140 | 42814 | fdiaz | public boolean handleDatesAsStrings() { |
141 | Boolean x = (Boolean) getDynValue(HANDLE_DATES_AS_STRINGS); |
||
142 | return BooleanUtils.isTrue(x);
|
||
143 | } |
||
144 | |||
145 | public boolean allowDuplicatedFieldNames() { |
||
146 | Boolean x = (Boolean) getDynValue(ALLOW_DUPLICATED_FIELD_NAMES); |
||
147 | return BooleanUtils.isTrue(x);
|
||
148 | } |
||
149 | |||
150 | public void setEncoding(Charset charset) { |
||
151 | this.setDynValue(ENCODING_PARAMTER_NAME, charset.name());
|
||
152 | 40435 | jjdelcerro | } |
153 | |||
154 | protected DelegatedDynObject getDelegatedDynObject() {
|
||
155 | return parameters;
|
||
156 | } |
||
157 | 42814 | fdiaz | |
158 | 41483 | jjdelcerro | public Locale getLocale() { |
159 | try {
|
||
160 | String s = (String) this.getDynValue(LOCALE); |
||
161 | if( s.trim().length()==0 ) { |
||
162 | return null; |
||
163 | } |
||
164 | if( "DEFAULT".equalsIgnoreCase(s.trim()) ) { |
||
165 | return Locale.getDefault(); |
||
166 | } |
||
167 | Locale locale = null; |
||
168 | // locale = Locale.forLanguageTag(s); // Since java 1.7
|
||
169 | String[] ss = s.split("-"); |
||
170 | switch(ss.length) {
|
||
171 | case 1: |
||
172 | locale = new Locale(ss[0]); |
||
173 | break;
|
||
174 | case 2: |
||
175 | locale = new Locale(ss[0],ss[1]); |
||
176 | break;
|
||
177 | case 3: |
||
178 | default:
|
||
179 | locale = new Locale(ss[0],ss[1],ss[2]); |
||
180 | break;
|
||
181 | } |
||
182 | return locale;
|
||
183 | } catch( Exception ex) { |
||
184 | logger.warn("Can't get locale from DBF parameters.", ex);
|
||
185 | return Locale.getDefault(); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | public String getDateFormat() { |
||
190 | return (String) getDynValue(DATE_FORMAT); |
||
191 | } |
||
192 | |||
193 | public void validate() throws ValidateDataParametersException { |
||
194 | super.validate();
|
||
195 | String sfmt = getDateFormat();
|
||
196 | if( !StringUtils.isBlank(sfmt) ) {
|
||
197 | try {
|
||
198 | SimpleDateFormat datefmt = new SimpleDateFormat(sfmt, getLocale()); |
||
199 | } catch(Exception ex) { |
||
200 | throw new InvalidDateFormatException(sfmt,ex); |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | 42814 | fdiaz | |
205 | 41483 | jjdelcerro | private static class InvalidDateFormatException extends ValidateDataParametersException { |
206 | 42814 | fdiaz | |
207 | 41483 | jjdelcerro | public InvalidDateFormatException(String sfmt, Throwable cause) { |
208 | super(
|
||
209 | "Date format specified '%(dataformat)' is not valid.",
|
||
210 | cause, |
||
211 | "Date_format_specified_XdataformatX_is_not_valid",
|
||
212 | 0
|
||
213 | ); |
||
214 | setValue("dataformat",sfmt);
|
||
215 | 42814 | fdiaz | |
216 | 41483 | jjdelcerro | } |
217 | } |
||
218 | 42814 | fdiaz | |
219 | 40435 | jjdelcerro | } |