svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRaster / src / org / gvsig / raster / dataset / io / rmf / ClassSerializer.java @ 27512
History | View | Annotate | Download (10.1 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.raster.dataset.io.rmf; |
20 |
|
21 |
import java.awt.geom.AffineTransform; |
22 |
import java.awt.geom.Point2D; |
23 |
import java.io.IOException; |
24 |
|
25 |
import org.cresques.impl.cts.ProjectionPool; |
26 |
import org.gvsig.raster.datastruct.Extent; |
27 |
import org.gvsig.raster.datastruct.ViewPortData; |
28 |
import org.kxml2.io.KXmlParser; |
29 |
import org.xmlpull.v1.XmlPullParserException; |
30 |
|
31 |
/**
|
32 |
* Clase de la que debe heredar cualquier clase que convierta objetos a XML
|
33 |
* para ser guardados en ficheros rmf.
|
34 |
*
|
35 |
* 23-abr-2007
|
36 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
37 |
*/
|
38 |
public abstract class ClassSerializer implements IRmfBlock { |
39 |
|
40 |
/**
|
41 |
* Parsea un tag con un String como texto de la forma \<TAG\>String\</TAG\>
|
42 |
* @param parser KXmlParser
|
43 |
* @param parseableTag cadena de texto con el nombre del tag
|
44 |
* @param errorTags Lista de tags que interrumpiran el proceso si son encontrados
|
45 |
* @return valor double recuperado
|
46 |
* @throws XmlPullParserException
|
47 |
* @throws IOException
|
48 |
* @throws ParsingException
|
49 |
*/
|
50 |
protected String parserString(KXmlParser parser, String parseableTag, String[] errorTags) throws XmlPullParserException, IOException, ParsingException { |
51 |
String value = null; |
52 |
boolean end = false; |
53 |
boolean tagOk = false; |
54 |
int tag = parser.next();
|
55 |
while (!end) {
|
56 |
switch (tag) {
|
57 |
case KXmlParser.END_DOCUMENT:
|
58 |
if (value != null) |
59 |
return value;
|
60 |
throw new ParsingException("No se ha encontrado el parametro " + parseableTag + " en el RMF"); |
61 |
case KXmlParser.START_TAG:
|
62 |
if (parser.getName() != null) { |
63 |
if (errorTags != null) |
64 |
for (int i = 0; i < errorTags.length; i++) |
65 |
if (parser.getName().compareTo(errorTags[i]) == 0) |
66 |
return null; |
67 |
if (parser.getName().compareTo(parseableTag) == 0) |
68 |
tagOk = true;
|
69 |
} |
70 |
break;
|
71 |
case KXmlParser.END_TAG:
|
72 |
if (parser.getName().compareTo(parseableTag) == 0) |
73 |
end = true;
|
74 |
break;
|
75 |
case KXmlParser.TEXT:
|
76 |
if (tagOk) {
|
77 |
value = parser.getText(); |
78 |
tagOk = false;
|
79 |
} |
80 |
break;
|
81 |
} |
82 |
if (!end)
|
83 |
tag = parser.next(); |
84 |
} |
85 |
return value;
|
86 |
} |
87 |
|
88 |
/**
|
89 |
* 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>
|
90 |
* @param gp
|
91 |
* @param parser
|
92 |
* @param parseableTag Tag principal del Extent
|
93 |
* @param bbox lista de 4 elementos que representa los 4 tags minx, maxy,
|
94 |
* ancho y alto
|
95 |
* @throws NumberFormatException
|
96 |
* @throws XmlPullParserException
|
97 |
* @throws IOException
|
98 |
* @throws ParsingException
|
99 |
*/
|
100 |
public Extent parseBoundingBox(KXmlParser parser, String parseableTag, String[] bbox) throws NumberFormatException, XmlPullParserException, IOException, ParsingException { |
101 |
boolean end = false; |
102 |
boolean tagOk = false; |
103 |
int tag = parser.next();
|
104 |
Extent ext = null;
|
105 |
while (!end) {
|
106 |
switch (tag) {
|
107 |
case KXmlParser.START_TAG:
|
108 |
if (parser.getName() != null) |
109 |
if (parser.getName().compareTo(parseableTag) == 0) |
110 |
tagOk = true;
|
111 |
break;
|
112 |
case KXmlParser.END_TAG:
|
113 |
if (parser.getName().compareTo(parseableTag) == 0) |
114 |
end = true;
|
115 |
break;
|
116 |
case KXmlParser.TEXT:
|
117 |
if (tagOk) {
|
118 |
double x = Double.valueOf(parserString(parser, bbox[0], null)).doubleValue(); |
119 |
double y = Double.valueOf(parserString(parser, bbox[1], null)).doubleValue(); |
120 |
double w = Double.valueOf(parserString(parser, bbox[2], null)).doubleValue(); |
121 |
double h = Double.valueOf(parserString(parser, bbox[3], null)).doubleValue(); |
122 |
ext = new Extent(x, y, x + w, y - h);
|
123 |
} |
124 |
tagOk = false;
|
125 |
break;
|
126 |
} |
127 |
if (!end)
|
128 |
tag = parser.next(); |
129 |
} |
130 |
return ext;
|
131 |
} |
132 |
|
133 |
/**
|
134 |
* Parsea un bloque correspondiente a un punto con la forma
|
135 |
* \<Pdim\><BR>
|
136 |
* \<Width\>140.0\</Width\><BR>
|
137 |
* \<Height\>140.0\</Height\><BR>
|
138 |
* \</Pdim\><BR>
|
139 |
* @param gp
|
140 |
* @param parser
|
141 |
* @param parseableTag
|
142 |
* @throws NumberFormatException
|
143 |
* @throws XmlPullParserException
|
144 |
* @throws IOException
|
145 |
* @throws ParsingException
|
146 |
*/
|
147 |
public Point2D parsePoint2D(KXmlParser parser, String parseableTag, String[] points) throws NumberFormatException, XmlPullParserException, IOException, ParsingException { |
148 |
boolean end = false; |
149 |
boolean tagOk = false; |
150 |
Point2D dim = null; |
151 |
int tag = parser.next();
|
152 |
while (!end) {
|
153 |
switch (tag) {
|
154 |
case KXmlParser.START_TAG:
|
155 |
if (parser.getName() != null) |
156 |
if (parser.getName().compareTo(parseableTag) == 0) |
157 |
tagOk = true;
|
158 |
break;
|
159 |
case KXmlParser.END_TAG:
|
160 |
if (parser.getName().compareTo(parseableTag) == 0) |
161 |
end = true;
|
162 |
break;
|
163 |
case KXmlParser.TEXT:
|
164 |
if (tagOk) {
|
165 |
double x = Double.valueOf(parserString(parser, points[0], null)).doubleValue(); |
166 |
double y = Double.valueOf(parserString(parser, points[1], null)).doubleValue(); |
167 |
dim = new Point2D.Double(x, y); |
168 |
tagOk = false;
|
169 |
} |
170 |
break;
|
171 |
} |
172 |
if (!end)
|
173 |
tag = parser.next(); |
174 |
} |
175 |
return dim;
|
176 |
} |
177 |
|
178 |
/**
|
179 |
* Parsea un viewport contenido en un punto de control
|
180 |
* @param gp
|
181 |
* @param parser
|
182 |
* @param parseableTag Tag correspondiente a la cabecera del viewport
|
183 |
* @param bbox lista de tags correspondientes al extent. El primero ser? la cabecera y los siguientes cuatro minx, maxy, ancho y alto
|
184 |
* @param size tags del tama?o en pixeles. El primero ser? la cabecera y los siguientes dos ancho y alto
|
185 |
* @param proj tag de la proyecci?n
|
186 |
* @throws XmlPullParserException
|
187 |
* @throws IOException
|
188 |
* @throws ParsingException
|
189 |
*/
|
190 |
public ViewPortData parseViewPort(KXmlParser parser, String parseableTag, String[] bbox, String[] size, String proj) throws XmlPullParserException, IOException, ParsingException { |
191 |
boolean end = false; |
192 |
boolean begin = true; |
193 |
ViewPortData vp = new ViewPortData();
|
194 |
vp.mat = new AffineTransform(); |
195 |
|
196 |
int tag = parser.next();
|
197 |
while (!end) {
|
198 |
switch (tag) {
|
199 |
case KXmlParser.START_TAG:
|
200 |
if (parser.getName() != null) |
201 |
if (parser.getName().compareTo(parseableTag) == 0) { |
202 |
String project = parserString(parser, proj, null); |
203 |
Extent ext = parseBoundingBox(parser, bbox[0], new String[] { bbox[1], bbox[2], bbox[3], bbox[4] }); |
204 |
Point2D dim = parsePoint2D(parser, size[0], new String[] { size[1], size[2] }); |
205 |
vp.setExtent(ext); |
206 |
vp.pxSize = new Point2D.Double(dim.getX(), dim.getY()); |
207 |
if (project != null && project != "") |
208 |
vp.setProjection(new ProjectionPool().get(project));
|
209 |
begin = false;
|
210 |
} else if (begin) |
211 |
return null; |
212 |
break;
|
213 |
case KXmlParser.END_TAG:
|
214 |
if (parser.getName().compareTo(parseableTag) == 0) |
215 |
end = true;
|
216 |
break;
|
217 |
case KXmlParser.TEXT:
|
218 |
break;
|
219 |
} |
220 |
if (!end)
|
221 |
tag = parser.next(); |
222 |
} |
223 |
return vp;
|
224 |
} |
225 |
|
226 |
/**
|
227 |
* Convierte una lista de valores en un solo String en un array de enteros. El
|
228 |
* String debe representar una lista de numeros separados por espacios.
|
229 |
* <P>
|
230 |
* String v = "4 5 65 2 1"
|
231 |
* </P>
|
232 |
* <P>
|
233 |
* Se converir?a en:
|
234 |
* </P>
|
235 |
* <P>
|
236 |
* int[] i = new int[]{4, 5, 65, 2, 1};
|
237 |
* </P>
|
238 |
* @param list Cadena con la lista de valores
|
239 |
* @return Array de valores long
|
240 |
* @throws XmlPullParserException
|
241 |
* @throws IOException
|
242 |
*/
|
243 |
protected int[] convertStringInIntArray(String list) throws XmlPullParserException, IOException { |
244 |
String[] sValues = list.split(" "); |
245 |
int[] iValues = new int[sValues.length]; |
246 |
for (int i = 0; i < sValues.length; i++) |
247 |
try {
|
248 |
iValues[i] = Integer.parseInt(sValues[i]);
|
249 |
} catch (NumberFormatException e) { |
250 |
// iValues[i] vale cero
|
251 |
} |
252 |
return iValues;
|
253 |
} |
254 |
|
255 |
/**
|
256 |
* Convierte una lista de valores en un solo String en un array de double. El
|
257 |
* String debe representar una lista de numeros separados por espacios.
|
258 |
* <P>
|
259 |
* String v = "4.2 5 65.5 2 1"
|
260 |
* </P>
|
261 |
* <P>
|
262 |
* Se converir?a en:
|
263 |
* </P>
|
264 |
* <P>
|
265 |
* double[] i = new double[]{4.2, 5, 65.5, 2, 1};
|
266 |
* </P>
|
267 |
* @param list Cadena con la lista de valores
|
268 |
* @return Array de valores double
|
269 |
* @throws XmlPullParserException
|
270 |
* @throws IOException
|
271 |
*/
|
272 |
protected double[] convertStringInDoubleArray(String list) throws XmlPullParserException, IOException { |
273 |
String[] sValues = list.split(" "); |
274 |
double[] dValues = new double[sValues.length]; |
275 |
for (int i = 0; i < sValues.length; i++) |
276 |
try {
|
277 |
dValues[i] = Double.parseDouble(sValues[i]);
|
278 |
} catch (NumberFormatException e) { |
279 |
// dValues[i] vale cero
|
280 |
} |
281 |
return dValues;
|
282 |
} |
283 |
|
284 |
/**
|
285 |
* A?ade al buffer la propiedad (String) especificada en los par?metros
|
286 |
* @param b Buffer
|
287 |
* @param prop Nombre de la propiedad (Tag)
|
288 |
* @param value Valor de la propiedad
|
289 |
*/
|
290 |
public void putProperty(StringBuffer b, String prop, String value, int level) { |
291 |
for (int i = 0; i < level; i++) |
292 |
b.append("\t");
|
293 |
b.append("<" + prop + ">"); |
294 |
b.append(value); |
295 |
b.append("</" + prop + ">\n"); |
296 |
} |
297 |
|
298 |
/**
|
299 |
* A?ade al buffer la propiedad (int) especificada en los par?metros
|
300 |
* @param b Buffer
|
301 |
* @param prop Nombre de la propiedad (Tag)
|
302 |
* @param value Valor de la propiedad
|
303 |
*/
|
304 |
public void putProperty(StringBuffer b, String prop, int value, int level) { |
305 |
putProperty(b, prop, String.valueOf(value), level);
|
306 |
} |
307 |
|
308 |
/**
|
309 |
* A?ade al buffer la propiedad (double) especificada en los par?metros
|
310 |
* @param b Buffer
|
311 |
* @param prop Nombre de la propiedad (Tag)
|
312 |
* @param value Valor de la propiedad
|
313 |
*/
|
314 |
public void putProperty(StringBuffer b, String prop, double value, int level) { |
315 |
putProperty(b, prop, String.valueOf(value), level);
|
316 |
} |
317 |
} |