gvsig-raster / org.gvsig.raster.wms / branches / org.gvsig.raster.wms_dataaccess_refactoring / org.gvsig.raster.wms.io / src / main / java / org / gvsig / raster / wms / io / time / DefaultDimension.java @ 2378
History | View | Annotate | Download (8.08 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
package org.gvsig.raster.wms.io.time; |
23 |
|
24 |
import org.gvsig.tools.ToolsLocator; |
25 |
import org.gvsig.tools.dynobject.DynStruct; |
26 |
import org.gvsig.tools.persistence.PersistenceManager; |
27 |
import org.gvsig.tools.persistence.PersistentState; |
28 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
29 |
|
30 |
|
31 |
/**
|
32 |
* This class instances a regular WMS dimension. It handles single, multiple and
|
33 |
* interval values and uses them as they were a point, a list or a regularly
|
34 |
* split line, respectivelly.<br>
|
35 |
* <p>
|
36 |
* As far as it implements RemoteTimeDimension it uses the same interface and
|
37 |
* documentation.
|
38 |
* </p>
|
39 |
* @author jaume dominguez faus (jaume.dominguez@iver.es)
|
40 |
*
|
41 |
*/
|
42 |
public class DefaultDimension implements RemoteTimeDimension { |
43 |
static private final String digit = "[0-9]"; |
44 |
static private final String nonZeroDigit = "[1-9]"; |
45 |
static private final String letter = "[_$%a-zA-Z]"; |
46 |
static private final String word = letter+"("+letter+"|"+digit+")+"; // TODO Should be * instead of + ?? |
47 |
static private final String floatingPointNumber = "("+digit+"+(\\."+digit+"+)?)"; |
48 |
static private final String integerNumber = nonZeroDigit+digit+"+"; |
49 |
static private final String dimensionItem = "("+floatingPointNumber+"|"+word+")"; |
50 |
/**
|
51 |
* regular expression for matching dimensions.
|
52 |
*/
|
53 |
static private final String regexpDefaultDimensionExpression = |
54 |
"("+floatingPointNumber+"/"+floatingPointNumber+"/"+floatingPointNumber+"|"+ |
55 |
dimensionItem+"(,"+dimensionItem+")*)"; |
56 |
|
57 |
private String name; |
58 |
private String unit; |
59 |
private String unitSymbol; |
60 |
private String expression; |
61 |
private String period; |
62 |
private Object minValue; |
63 |
private Object maxValue; |
64 |
private int type; |
65 |
private boolean compiled = false; |
66 |
|
67 |
public void loadFromState(PersistentState state) throws PersistenceException { |
68 |
this.name = state.getString("name"); |
69 |
this.unit = state.getString("unit"); |
70 |
this.unitSymbol = state.getString("unitSymbol"); |
71 |
this.expression = state.getString("expression"); |
72 |
this.period = state.getString("period"); |
73 |
this.minValue = state.getString("minValue"); |
74 |
this.maxValue = state.getString("maxValue"); |
75 |
this.type = state.getInt("type"); |
76 |
this.compiled = state.getBoolean("compiled"); |
77 |
} |
78 |
|
79 |
public void saveToState(PersistentState state) throws PersistenceException { |
80 |
state.set("name", name);
|
81 |
state.set("unit", unit);
|
82 |
state.set("unitSymbol", unitSymbol);
|
83 |
state.set("expression", expression);
|
84 |
state.set("period", period);
|
85 |
state.set("minValue", minValue);
|
86 |
state.set("maxValue", maxValue);
|
87 |
state.set("type", type);
|
88 |
state.set("compiled", compiled);
|
89 |
} |
90 |
|
91 |
public static void registerPersistent() { |
92 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
93 |
DynStruct definition = manager.getDefinition("DefaultDimension_Persistent");
|
94 |
if( definition == null ) { |
95 |
definition = manager.addDefinition( |
96 |
DefaultDimension.class, |
97 |
"DefaultDimension_Persistent",
|
98 |
"DefaultDimension Persistence",
|
99 |
null,
|
100 |
null
|
101 |
); |
102 |
} |
103 |
|
104 |
definition.addDynFieldString("name").setMandatory(false); |
105 |
definition.addDynFieldString("unit").setMandatory(false); |
106 |
definition.addDynFieldString("unitSymbol").setMandatory(false); |
107 |
definition.addDynFieldString("expression").setMandatory(false); |
108 |
definition.addDynFieldString("period").setMandatory(false); |
109 |
definition.addDynFieldString("minValue").setMandatory(false); |
110 |
definition.addDynFieldString("maxValue").setMandatory(false); |
111 |
definition.addDynFieldInt("type").setMandatory(false); |
112 |
definition.addDynFieldBoolean("compiled").setMandatory(false); |
113 |
} |
114 |
|
115 |
public DefaultDimension() {}
|
116 |
|
117 |
/**
|
118 |
* Creates a new instance of DefaultDimension.
|
119 |
* @param _name
|
120 |
* @param _units
|
121 |
* @param _unitSymbol
|
122 |
* @param _dimensionExpression
|
123 |
*/
|
124 |
public DefaultDimension(String _name, String _units, String _unitSymbol, String _dimensionExpression) { |
125 |
this.name = _name;
|
126 |
this.unit = _units;
|
127 |
this.unitSymbol = _unitSymbol;
|
128 |
setExpression(_dimensionExpression); |
129 |
} |
130 |
|
131 |
public String getName() { |
132 |
return name;
|
133 |
} |
134 |
|
135 |
public String getUnit() { |
136 |
return unit;
|
137 |
} |
138 |
|
139 |
public String getUnitSymbol() { |
140 |
return unitSymbol;
|
141 |
} |
142 |
|
143 |
public String getLowLimit() { |
144 |
return (String) minValue; |
145 |
} |
146 |
|
147 |
public String getHighLimit() { |
148 |
return (String) maxValue; |
149 |
} |
150 |
|
151 |
public String getResolution() { |
152 |
if (type == INTERVAL) {
|
153 |
String[] s = expression.split("/"); |
154 |
return (s.length == 1) ? s[3] : null; |
155 |
} else return null; |
156 |
} |
157 |
|
158 |
public boolean isValidValue(String value) { |
159 |
return value.matches(word) || value.matches(floatingPointNumber);
|
160 |
} |
161 |
|
162 |
public Object valueOf(String value) throws IllegalArgumentException { |
163 |
if (compiled) {
|
164 |
if (value.matches(word)) {
|
165 |
return (String) value; |
166 |
} else if (value.matches(integerNumber)){ |
167 |
return new Integer(value); |
168 |
} |
169 |
else if (value.matches(floatingPointNumber)) { |
170 |
return new Float(value); |
171 |
} |
172 |
} |
173 |
return null; |
174 |
} |
175 |
|
176 |
public String valueAt(int pos) throws ArrayIndexOutOfBoundsException { |
177 |
if (compiled) {
|
178 |
if (pos<0 || pos>valueCount()) |
179 |
throw new ArrayIndexOutOfBoundsException(pos+"(must be >= 0 and <="+valueCount()+")"); |
180 |
|
181 |
if (type == SINGLE_VALUE)
|
182 |
return expression;
|
183 |
|
184 |
if (type == MULTIPLE_VALUE)
|
185 |
return expression.split(",")[pos]; |
186 |
|
187 |
if (type == INTERVAL) {
|
188 |
double minPos = Double.parseDouble((String) minValue); |
189 |
double maxPos = Double.parseDouble((String) maxValue); |
190 |
double step = Double.parseDouble(period); |
191 |
double newPos = minPos + (step*pos);
|
192 |
if (newPos < minPos)
|
193 |
return minPos+""; |
194 |
|
195 |
if (newPos > maxPos)
|
196 |
return maxPos+""; |
197 |
return newPos+""; |
198 |
} |
199 |
} |
200 |
return null; |
201 |
} |
202 |
|
203 |
public int valueCount() { |
204 |
if (compiled) {
|
205 |
if (type == MULTIPLE_VALUE) {
|
206 |
return expression.split(",").length; |
207 |
} else if (type == INTERVAL) { |
208 |
int count;
|
209 |
double min = Double.parseDouble((String) minValue); |
210 |
double max = Double.parseDouble((String) maxValue); |
211 |
double step = Double.parseDouble(period); |
212 |
double distance = max - min;
|
213 |
count = (int) (distance/step);
|
214 |
return count;
|
215 |
} else {
|
216 |
return 1; |
217 |
} |
218 |
} |
219 |
return -1; |
220 |
} |
221 |
|
222 |
public String getExpression() { |
223 |
return expression;
|
224 |
} |
225 |
|
226 |
public void setExpression(String expr) { |
227 |
expression = expr.toUpperCase(); |
228 |
|
229 |
} |
230 |
|
231 |
public int getType() { |
232 |
return type;
|
233 |
} |
234 |
|
235 |
public void compile() throws IllegalArgumentException { |
236 |
if (expression.matches(regexpDefaultDimensionExpression)){
|
237 |
|
238 |
} else {
|
239 |
//System.err.println("Invalid dimension expression: "+expr+" (for "+this.getName()+")");
|
240 |
throw new IllegalArgumentException(); |
241 |
} |
242 |
|
243 |
|
244 |
String separator;
|
245 |
if (expression.indexOf("/")!=-1) { |
246 |
separator = "/";
|
247 |
type = INTERVAL; |
248 |
} else if (expression.indexOf(",")!=-1) { |
249 |
separator = ",";
|
250 |
type = MULTIPLE_VALUE; |
251 |
} else {
|
252 |
separator = ",";
|
253 |
type = SINGLE_VALUE; |
254 |
} |
255 |
compiled = true;
|
256 |
String[] s = expression.split(separator); |
257 |
minValue = valueOf(s[0]);
|
258 |
if (type == INTERVAL) {
|
259 |
maxValue = (s.length>1) ? valueOf(s[1]) : valueOf(s[0]); |
260 |
period = (s.length>2) ? s[2] : null; |
261 |
} else if (type == MULTIPLE_VALUE) { |
262 |
maxValue = valueOf(s[s.length-1]);
|
263 |
} else {
|
264 |
maxValue = valueOf(s[0]);
|
265 |
} |
266 |
|
267 |
} |
268 |
|
269 |
} |