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 @ 41483
History | View | Annotate | Download (6.84 KB)
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.dbf; |
25 |
|
26 |
import java.io.File; |
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; |
32 |
|
33 |
import org.gvsig.fmap.dal.DataStoreParameters; |
34 |
import org.gvsig.fmap.dal.FileHelper; |
35 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
36 |
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 |
import org.gvsig.tools.dynobject.DynObject; |
41 |
import org.slf4j.Logger; |
42 |
import org.slf4j.LoggerFactory; |
43 |
|
44 |
|
45 |
public class DBFStoreParameters extends AbstractDataParameters implements |
46 |
DataStoreParameters, FilesystemStoreParameters { |
47 |
|
48 |
private static final Logger logger = LoggerFactory.getLogger(DBFStoreParameters.class); |
49 |
|
50 |
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 |
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 |
|
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 |
return (String) getDynValue(ENCODING_PARAMTER_NAME); |
119 |
} |
120 |
|
121 |
public Charset getEncoding() { |
122 |
String name = getEncodingName();
|
123 |
if (name == null) { |
124 |
return null; |
125 |
} |
126 |
return Charset.forName(name); |
127 |
} |
128 |
|
129 |
public void setEncoding(String encoding) { |
130 |
this.setEncoding(Charset.forName(encoding)); |
131 |
} |
132 |
|
133 |
public boolean handleDatesAsStrings() { |
134 |
Boolean x = (Boolean) getDynValue(HANDLE_DATES_AS_STRINGS); |
135 |
return BooleanUtils.isTrue(x);
|
136 |
} |
137 |
|
138 |
public void setEncoding(Charset charset) { |
139 |
this.setDynValue(ENCODING_PARAMTER_NAME, charset.name());
|
140 |
} |
141 |
|
142 |
protected DelegatedDynObject getDelegatedDynObject() {
|
143 |
return parameters;
|
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 |
|
207 |
} |