gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / store / rmf / ClassSerializer.java @ 2443
History | View | Annotate | Download (10.3 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.impl.store.rmf; |
23 |
|
24 |
import java.awt.geom.AffineTransform; |
25 |
import java.awt.geom.Point2D; |
26 |
import java.io.IOException; |
27 |
|
28 |
import org.gvsig.fmap.crs.CRSFactory; |
29 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
30 |
import org.gvsig.fmap.dal.coverage.exception.ParsingException; |
31 |
import org.gvsig.raster.impl.datastruct.ExtentImpl; |
32 |
import org.gvsig.raster.impl.datastruct.DefaultViewPortData; |
33 |
import org.kxml2.io.KXmlParser; |
34 |
import org.xmlpull.v1.XmlPullParserException; |
35 |
|
36 |
/**
|
37 |
* Clase de la que debe heredar cualquier clase que convierta objetos a XML
|
38 |
* para ser guardados en ficheros rmf.
|
39 |
*
|
40 |
* 23-abr-2007
|
41 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
42 |
*/
|
43 |
public abstract class ClassSerializer implements IRmfBlock { |
44 |
|
45 |
/**
|
46 |
* Parsea un tag con un String como texto de la forma \<TAG\>String\</TAG\>
|
47 |
* @param parser KXmlParser
|
48 |
* @param parseableTag cadena de texto con el nombre del tag
|
49 |
* @param errorTags Lista de tags que interrumpiran el proceso si son encontrados
|
50 |
* @return valor double recuperado
|
51 |
* @throws XmlPullParserException
|
52 |
* @throws IOException
|
53 |
* @throws ParsingException
|
54 |
*/
|
55 |
protected String parserString(KXmlParser parser, String parseableTag, String[] errorTags) throws XmlPullParserException, IOException, ParsingException { |
56 |
String value = null; |
57 |
boolean end = false; |
58 |
boolean tagOk = false; |
59 |
int tag = parser.next();
|
60 |
while (!end) {
|
61 |
switch (tag) {
|
62 |
case KXmlParser.END_DOCUMENT:
|
63 |
if (value != null) |
64 |
return value;
|
65 |
throw new ParsingException("No se ha encontrado el parametro " + parseableTag + " en el RMF"); |
66 |
case KXmlParser.START_TAG:
|
67 |
if (parser.getName() != null) { |
68 |
if (errorTags != null) |
69 |
for (int i = 0; i < errorTags.length; i++) |
70 |
if (parser.getName().compareTo(errorTags[i]) == 0) |
71 |
return null; |
72 |
if (parser.getName().compareTo(parseableTag) == 0) |
73 |
tagOk = true;
|
74 |
} |
75 |
break;
|
76 |
case KXmlParser.END_TAG:
|
77 |
if (parser.getName().compareTo(parseableTag) == 0) |
78 |
end = true;
|
79 |
break;
|
80 |
case KXmlParser.TEXT:
|
81 |
if (tagOk) {
|
82 |
value = parser.getText(); |
83 |
tagOk = false;
|
84 |
} |
85 |
break;
|
86 |
} |
87 |
if (!end)
|
88 |
tag = parser.next(); |
89 |
} |
90 |
return value;
|
91 |
} |
92 |
|
93 |
/**
|
94 |
* Parsea un bloque que representa un Extent con la forma \<Bbox\><BR> \<Px\>-245.41842857142848\</Px\><BR> \<Py\>1783.3830816326529\</Py\><BR> \<WCWidth\>500.09399999999994\</WCWidth\><BR> \<WCHeight\>324.72771428571446\</WCHeight\><BR> \</Bbox\><BR>
|
95 |
* @param gp
|
96 |
* @param parser
|
97 |
* @param parseableTag Tag principal del Extent
|
98 |
* @param bbox lista de 4 elementos que representa los 4 tags minx, maxy,
|
99 |
* ancho y alto
|
100 |
* @throws NumberFormatException
|
101 |
* @throws XmlPullParserException
|
102 |
* @throws IOException
|
103 |
* @throws ParsingException
|
104 |
*/
|
105 |
public Extent parseBoundingBox(KXmlParser parser, String parseableTag, String[] bbox) throws NumberFormatException, XmlPullParserException, IOException, ParsingException { |
106 |
boolean end = false; |
107 |
boolean tagOk = false; |
108 |
int tag = parser.next();
|
109 |
Extent ext = null;
|
110 |
while (!end) {
|
111 |
switch (tag) {
|
112 |
case KXmlParser.START_TAG:
|
113 |
if (parser.getName() != null) |
114 |
if (parser.getName().compareTo(parseableTag) == 0) |
115 |
tagOk = true;
|
116 |
break;
|
117 |
case KXmlParser.END_TAG:
|
118 |
if (parser.getName().compareTo(parseableTag) == 0) |
119 |
end = true;
|
120 |
break;
|
121 |
case KXmlParser.TEXT:
|
122 |
if (tagOk) {
|
123 |
double x = Double.valueOf(parserString(parser, bbox[0], null)).doubleValue(); |
124 |
double y = Double.valueOf(parserString(parser, bbox[1], null)).doubleValue(); |
125 |
double w = Double.valueOf(parserString(parser, bbox[2], null)).doubleValue(); |
126 |
double h = Double.valueOf(parserString(parser, bbox[3], null)).doubleValue(); |
127 |
ext = new ExtentImpl(x, y, x + w, y - h);
|
128 |
} |
129 |
tagOk = false;
|
130 |
break;
|
131 |
} |
132 |
if (!end)
|
133 |
tag = parser.next(); |
134 |
} |
135 |
return ext;
|
136 |
} |
137 |
|
138 |
/**
|
139 |
* Parsea un bloque correspondiente a un punto con la forma
|
140 |
* \<Pdim\><BR>
|
141 |
* \<Width\>140.0\</Width\><BR>
|
142 |
* \<Height\>140.0\</Height\><BR>
|
143 |
* \</Pdim\><BR>
|
144 |
* @param gp
|
145 |
* @param parser
|
146 |
* @param parseableTag
|
147 |
* @throws NumberFormatException
|
148 |
* @throws XmlPullParserException
|
149 |
* @throws IOException
|
150 |
* @throws ParsingException
|
151 |
*/
|
152 |
public Point2D parsePoint2D(KXmlParser parser, String parseableTag, String[] points) throws NumberFormatException, XmlPullParserException, IOException, ParsingException { |
153 |
boolean end = false; |
154 |
boolean tagOk = false; |
155 |
Point2D dim = null; |
156 |
int tag = parser.next();
|
157 |
while (!end) {
|
158 |
switch (tag) {
|
159 |
case KXmlParser.START_TAG:
|
160 |
if (parser.getName() != null) |
161 |
if (parser.getName().compareTo(parseableTag) == 0) |
162 |
tagOk = true;
|
163 |
break;
|
164 |
case KXmlParser.END_TAG:
|
165 |
if (parser.getName().compareTo(parseableTag) == 0) |
166 |
end = true;
|
167 |
break;
|
168 |
case KXmlParser.TEXT:
|
169 |
if (tagOk) {
|
170 |
double x = Double.valueOf(parserString(parser, points[0], null)).doubleValue(); |
171 |
double y = Double.valueOf(parserString(parser, points[1], null)).doubleValue(); |
172 |
dim = new Point2D.Double(x, y); |
173 |
tagOk = false;
|
174 |
} |
175 |
break;
|
176 |
} |
177 |
if (!end)
|
178 |
tag = parser.next(); |
179 |
} |
180 |
return dim;
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* Parsea un viewport contenido en un punto de control
|
185 |
* @param gp
|
186 |
* @param parser
|
187 |
* @param parseableTag Tag correspondiente a la cabecera del viewport
|
188 |
* @param bbox lista de tags correspondientes al extent. El primero ser? la cabecera y los siguientes cuatro minx, maxy, ancho y alto
|
189 |
* @param size tags del tama?o en pixeles. El primero ser? la cabecera y los siguientes dos ancho y alto
|
190 |
* @param proj tag de la proyecci?n
|
191 |
* @throws XmlPullParserException
|
192 |
* @throws IOException
|
193 |
* @throws ParsingException
|
194 |
*/
|
195 |
public DefaultViewPortData parseViewPort(KXmlParser parser, String parseableTag, String[] bbox, String[] size, String proj) throws XmlPullParserException, IOException, ParsingException { |
196 |
boolean end = false; |
197 |
boolean begin = true; |
198 |
DefaultViewPortData vp = new DefaultViewPortData();
|
199 |
vp.mat = new AffineTransform(); |
200 |
|
201 |
int tag = parser.next();
|
202 |
while (!end) {
|
203 |
switch (tag) {
|
204 |
case KXmlParser.START_TAG:
|
205 |
if (parser.getName() != null) |
206 |
if (parser.getName().compareTo(parseableTag) == 0) { |
207 |
String project = parserString(parser, proj, null); |
208 |
Extent ext = parseBoundingBox(parser, bbox[0], new String[] { bbox[1], bbox[2], bbox[3], bbox[4] }); |
209 |
Point2D dim = parsePoint2D(parser, size[0], new String[] { size[1], size[2] }); |
210 |
vp.setExtent(ext); |
211 |
vp.pxSize = new Point2D.Double(dim.getX(), dim.getY()); |
212 |
if (project != null && project != "") |
213 |
vp.setProjection(CRSFactory.getCRS(project)); |
214 |
begin = false;
|
215 |
} else if (begin) |
216 |
return null; |
217 |
break;
|
218 |
case KXmlParser.END_TAG:
|
219 |
if (parser.getName().compareTo(parseableTag) == 0) |
220 |
end = true;
|
221 |
break;
|
222 |
case KXmlParser.TEXT:
|
223 |
break;
|
224 |
} |
225 |
if (!end)
|
226 |
tag = parser.next(); |
227 |
} |
228 |
return vp;
|
229 |
} |
230 |
|
231 |
/**
|
232 |
* Convierte una lista de valores en un solo String en un array de enteros. El
|
233 |
* String debe representar una lista de numeros separados por espacios.
|
234 |
* <P>
|
235 |
* String v = "4 5 65 2 1"
|
236 |
* </P>
|
237 |
* <P>
|
238 |
* Se converir?a en:
|
239 |
* </P>
|
240 |
* <P>
|
241 |
* int[] i = new int[]{4, 5, 65, 2, 1};
|
242 |
* </P>
|
243 |
* @param list Cadena con la lista de valores
|
244 |
* @return Array de valores long
|
245 |
* @throws XmlPullParserException
|
246 |
* @throws IOException
|
247 |
*/
|
248 |
protected int[] convertStringInIntArray(String list) throws XmlPullParserException, IOException { |
249 |
String[] sValues = list.split(" "); |
250 |
int[] iValues = new int[sValues.length]; |
251 |
for (int i = 0; i < sValues.length; i++) |
252 |
try {
|
253 |
iValues[i] = Integer.parseInt(sValues[i]);
|
254 |
} catch (NumberFormatException e) { |
255 |
// iValues[i] vale cero
|
256 |
} |
257 |
return iValues;
|
258 |
} |
259 |
|
260 |
/**
|
261 |
* Convierte una lista de valores en un solo String en un array de double. El
|
262 |
* String debe representar una lista de numeros separados por espacios.
|
263 |
* <P>
|
264 |
* String v = "4.2 5 65.5 2 1"
|
265 |
* </P>
|
266 |
* <P>
|
267 |
* Se converir?a en:
|
268 |
* </P>
|
269 |
* <P>
|
270 |
* double[] i = new double[]{4.2, 5, 65.5, 2, 1};
|
271 |
* </P>
|
272 |
* @param list Cadena con la lista de valores
|
273 |
* @return Array de valores double
|
274 |
* @throws XmlPullParserException
|
275 |
* @throws IOException
|
276 |
*/
|
277 |
protected double[] convertStringInDoubleArray(String list) throws XmlPullParserException, IOException { |
278 |
String[] sValues = list.split(" "); |
279 |
double[] dValues = new double[sValues.length]; |
280 |
for (int i = 0; i < sValues.length; i++) |
281 |
try {
|
282 |
dValues[i] = Double.parseDouble(sValues[i]);
|
283 |
} catch (NumberFormatException e) { |
284 |
// dValues[i] vale cero
|
285 |
} |
286 |
return dValues;
|
287 |
} |
288 |
|
289 |
/**
|
290 |
* A?ade al buffer la propiedad (String) especificada en los par?metros
|
291 |
* @param b Buffer
|
292 |
* @param prop Nombre de la propiedad (Tag)
|
293 |
* @param value Valor de la propiedad
|
294 |
*/
|
295 |
public void putProperty(StringBuffer b, String prop, String value, int level) { |
296 |
for (int i = 0; i < level; i++) |
297 |
b.append("\t");
|
298 |
b.append("<" + prop + ">"); |
299 |
b.append(value); |
300 |
b.append("</" + prop + ">\n"); |
301 |
} |
302 |
|
303 |
/**
|
304 |
* A?ade al buffer la propiedad (int) especificada en los par?metros
|
305 |
* @param b Buffer
|
306 |
* @param prop Nombre de la propiedad (Tag)
|
307 |
* @param value Valor de la propiedad
|
308 |
*/
|
309 |
public void putProperty(StringBuffer b, String prop, int value, int level) { |
310 |
putProperty(b, prop, String.valueOf(value), level);
|
311 |
} |
312 |
|
313 |
/**
|
314 |
* A?ade al buffer la propiedad (double) especificada en los par?metros
|
315 |
* @param b Buffer
|
316 |
* @param prop Nombre de la propiedad (Tag)
|
317 |
* @param value Valor de la propiedad
|
318 |
*/
|
319 |
public void putProperty(StringBuffer b, String prop, double value, int level) { |
320 |
putProperty(b, prop, String.valueOf(value), level);
|
321 |
} |
322 |
} |