Revision 41483 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
DBFStoreParameters.java | ||
---|---|---|
25 | 25 |
|
26 | 26 |
import java.io.File; |
27 | 27 |
import java.nio.charset.Charset; |
28 |
import java.text.SimpleDateFormat; |
|
29 |
import java.util.Locale; |
|
30 |
import org.apache.commons.lang3.BooleanUtils; |
|
31 |
import org.apache.commons.lang3.StringUtils; |
|
28 | 32 |
|
29 | 33 |
import org.gvsig.fmap.dal.DataStoreParameters; |
30 | 34 |
import org.gvsig.fmap.dal.FileHelper; |
35 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
31 | 36 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemStoreParameters; |
32 | 37 |
import org.gvsig.fmap.dal.spi.AbstractDataParameters; |
33 | 38 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
34 | 39 |
import org.gvsig.tools.dynobject.DelegatedDynObject; |
40 |
import org.gvsig.tools.dynobject.DynObject; |
|
41 |
import org.slf4j.Logger; |
|
42 |
import org.slf4j.LoggerFactory; |
|
35 | 43 |
|
36 | 44 |
|
37 | 45 |
public class DBFStoreParameters extends AbstractDataParameters implements |
38 | 46 |
DataStoreParameters, FilesystemStoreParameters { |
39 | 47 |
|
48 |
private static final Logger logger = LoggerFactory.getLogger(DBFStoreParameters.class); |
|
49 |
|
|
40 | 50 |
public static final String PARAMETERS_DEFINITION_NAME = "DBFStoreParameters"; |
41 | 51 |
|
42 | 52 |
public static final String DBFFILE_PARAMTER_NAME = "dbfFile"; |
43 | 53 |
public static final String ENCODING_PARAMTER_NAME = "encoding"; |
54 |
public static final String HANDLE_DATES_AS_STRINGS = "handleDatesAsStrings"; |
|
55 |
public static final String DATE_FORMAT = "dateFormat"; |
|
56 |
public static final String LOCALE = "locale"; |
|
44 | 57 |
|
45 | 58 |
private DelegatedDynObject parameters; |
46 | 59 |
|
... | ... | |
117 | 130 |
this.setEncoding(Charset.forName(encoding)); |
118 | 131 |
} |
119 | 132 |
|
133 |
public boolean handleDatesAsStrings() { |
|
134 |
Boolean x = (Boolean) getDynValue(HANDLE_DATES_AS_STRINGS); |
|
135 |
return BooleanUtils.isTrue(x); |
|
136 |
} |
|
137 |
|
|
120 | 138 |
public void setEncoding(Charset charset) { |
121 | 139 |
this.setDynValue(ENCODING_PARAMTER_NAME, charset.name()); |
122 | 140 |
} |
... | ... | |
124 | 142 |
protected DelegatedDynObject getDelegatedDynObject() { |
125 | 143 |
return parameters; |
126 | 144 |
} |
145 |
|
|
146 |
public Locale getLocale() { |
|
147 |
try { |
|
148 |
String s = (String) this.getDynValue(LOCALE); |
|
149 |
if( s.trim().length()==0 ) { |
|
150 |
return null; |
|
151 |
} |
|
152 |
if( "DEFAULT".equalsIgnoreCase(s.trim()) ) { |
|
153 |
return Locale.getDefault(); |
|
154 |
} |
|
155 |
Locale locale = null; |
|
156 |
// locale = Locale.forLanguageTag(s); // Since java 1.7 |
|
157 |
String[] ss = s.split("-"); |
|
158 |
switch(ss.length) { |
|
159 |
case 1: |
|
160 |
locale = new Locale(ss[0]); |
|
161 |
break; |
|
162 |
case 2: |
|
163 |
locale = new Locale(ss[0],ss[1]); |
|
164 |
break; |
|
165 |
case 3: |
|
166 |
default: |
|
167 |
locale = new Locale(ss[0],ss[1],ss[2]); |
|
168 |
break; |
|
169 |
} |
|
170 |
return locale; |
|
171 |
} catch( Exception ex) { |
|
172 |
logger.warn("Can't get locale from DBF parameters.", ex); |
|
173 |
return Locale.getDefault(); |
|
174 |
} |
|
175 |
} |
|
176 |
|
|
177 |
public String getDateFormat() { |
|
178 |
return (String) getDynValue(DATE_FORMAT); |
|
179 |
} |
|
180 |
|
|
181 |
public void validate() throws ValidateDataParametersException { |
|
182 |
super.validate(); |
|
183 |
String sfmt = getDateFormat(); |
|
184 |
if( !StringUtils.isBlank(sfmt) ) { |
|
185 |
try { |
|
186 |
SimpleDateFormat datefmt = new SimpleDateFormat(sfmt, getLocale()); |
|
187 |
} catch(Exception ex) { |
|
188 |
throw new InvalidDateFormatException(sfmt,ex); |
|
189 |
} |
|
190 |
} |
|
191 |
} |
|
192 |
|
|
193 |
private static class InvalidDateFormatException extends ValidateDataParametersException { |
|
194 |
|
|
195 |
public InvalidDateFormatException(String sfmt, Throwable cause) { |
|
196 |
super( |
|
197 |
"Date format specified '%(dataformat)' is not valid.", |
|
198 |
cause, |
|
199 |
"Date_format_specified_XdataformatX_is_not_valid", |
|
200 |
0 |
|
201 |
); |
|
202 |
setValue("dataformat",sfmt); |
|
203 |
|
|
204 |
} |
|
205 |
} |
|
206 |
|
|
127 | 207 |
} |
Also available in: Unified diff