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 / serializer / SerialRmfSerializer.java @ 633
History | View | Annotate | Download (8.59 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.serializer; |
23 |
|
24 |
import java.io.IOException; |
25 |
import java.io.Reader; |
26 |
import java.io.StringReader; |
27 |
import java.text.ParseException; |
28 |
import java.util.ArrayList; |
29 |
|
30 |
import org.gvsig.fmap.dal.coverage.exception.ParsingException; |
31 |
import org.gvsig.fmap.dal.coverage.store.props.TimeSeries; |
32 |
import org.gvsig.raster.impl.datastruct.DefaultSerialInfo; |
33 |
import org.gvsig.raster.impl.provider.DefaultTimeSerials; |
34 |
import org.gvsig.raster.impl.store.rmf.ClassSerializer; |
35 |
import org.gvsig.tools.ToolsLocator; |
36 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
37 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
38 |
import org.kxml2.io.KXmlParser; |
39 |
import org.xmlpull.v1.XmlPullParserException; |
40 |
/**
|
41 |
* <P>
|
42 |
* Clase para convertir a XML la informaci?n de una serie y obtener esta informaci?n desde XML.
|
43 |
* Esta clase implementa el interfaz IRmfBlock con los m?todos de escritura y
|
44 |
* lectura. Estos ser?n utilizados por el gestor de ficheros RMF para escribir y
|
45 |
* leer datos.
|
46 |
* </P>
|
47 |
* <P>
|
48 |
* La estructura XML es la siguiente:
|
49 |
* </P>
|
50 |
* <P>
|
51 |
* \<Serials\><BR>
|
52 |
* \<Serial\><BR>
|
53 |
* \<Name\>katrina\</Name\><BR>
|
54 |
* \<Description\>Evoluci?n del huracan katrina\</Description\><BR>
|
55 |
* \<Type\>Single value\</Type\><BR>
|
56 |
* \<Dates\><BR>
|
57 |
* \<Date\>28//8//2005-6:10:00\</Date\><BR>
|
58 |
* \</Dates\><BR>
|
59 |
* \</Serial\><BR>
|
60 |
* \</Serials\><BR>
|
61 |
* </P>
|
62 |
*
|
63 |
* @version 23/04/2007
|
64 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
65 |
*/
|
66 |
public class SerialRmfSerializer extends ClassSerializer { |
67 |
//TAGS
|
68 |
public static final String MAIN_TAG = "Serials"; |
69 |
public static final String SERIAL = "Serial"; |
70 |
public static final String DESC = "Description"; |
71 |
public static final String TIMETYPE = "TimeType"; |
72 |
public static final String DATATYPE = "DataType"; |
73 |
public static final String DATES = "Dates"; |
74 |
public static final String DATE = "Date"; |
75 |
public static final String NAME = "Name"; |
76 |
private TimeSeries serialInfo = null; |
77 |
|
78 |
/**
|
79 |
* Registra GeoPointRmfSerializer en los puntos de extension de Serializer
|
80 |
*/
|
81 |
public static void register() { |
82 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
83 |
ExtensionPoint point = extensionPoints.get("Serializer");
|
84 |
point.append("Series", "", SerialRmfSerializer.class); |
85 |
} |
86 |
|
87 |
/**
|
88 |
* Constructor. Asigna la lista de puntos a serializar.
|
89 |
* @param ColorTable tabla a convertir en XML
|
90 |
*/
|
91 |
public SerialRmfSerializer(TimeSeries serialInfo) {
|
92 |
this.serialInfo = serialInfo;
|
93 |
} |
94 |
|
95 |
|
96 |
private void readDate(DefaultSerialInfo tInfo, KXmlParser parser) throws ParseException { |
97 |
String d = parser.getText();
|
98 |
try {
|
99 |
Double value = new Double(d); |
100 |
tInfo.addValue(value.doubleValue()); |
101 |
} catch (NumberFormatException e) { |
102 |
tInfo.addValue(d); |
103 |
} |
104 |
} |
105 |
|
106 |
/**
|
107 |
* Parsers a date list
|
108 |
* @param tInfo
|
109 |
* @param parser
|
110 |
* @param xml
|
111 |
* @param tag
|
112 |
* @throws XmlPullParserException
|
113 |
* @throws IOException
|
114 |
* @throws ParsingException
|
115 |
* @throws NumberFormatException
|
116 |
* @throws ParseException
|
117 |
*/
|
118 |
public void parserDates(DefaultSerialInfo tInfo, KXmlParser parser, String parseableTag, String[] errorTags) throws XmlPullParserException, IOException, NumberFormatException, ParsingException, ParseException { |
119 |
boolean end = false; |
120 |
boolean init = false; |
121 |
boolean readDate = false; |
122 |
int tag = parser.next();
|
123 |
while (!end) {
|
124 |
switch (tag) {
|
125 |
case KXmlParser.END_DOCUMENT:
|
126 |
return;
|
127 |
case KXmlParser.START_TAG:
|
128 |
if (parser.getName().compareTo(parseableTag) == 0) { |
129 |
init = true;
|
130 |
} |
131 |
if(init) {
|
132 |
if (parser.getName().compareTo(DATE) == 0) { |
133 |
readDate = true;
|
134 |
} |
135 |
} |
136 |
break;
|
137 |
case KXmlParser.END_TAG:
|
138 |
if (parser.getName().compareTo(parseableTag) == 0) |
139 |
end = true;
|
140 |
break;
|
141 |
case KXmlParser.TEXT:
|
142 |
if(readDate) {
|
143 |
readDate(tInfo, parser); |
144 |
readDate = false;
|
145 |
} |
146 |
break;
|
147 |
} |
148 |
if (!end)
|
149 |
tag = parser.next(); |
150 |
} |
151 |
} |
152 |
|
153 |
/**
|
154 |
* Parsers a serial structure
|
155 |
* @param tInfo
|
156 |
* @param parser
|
157 |
* @param xml
|
158 |
* @param tag
|
159 |
* @throws XmlPullParserException
|
160 |
* @throws IOException
|
161 |
* @throws ParsingException
|
162 |
* @throws NumberFormatException
|
163 |
* @throws ParseException
|
164 |
*/
|
165 |
public void parserSerial(DefaultSerialInfo tInfo, KXmlParser parser, String xml, int tag) throws XmlPullParserException, IOException, NumberFormatException, ParsingException, ParseException { |
166 |
String name = parserString(parser, NAME, null); |
167 |
String desc = parserString(parser, DESC, null); |
168 |
String timeType = parserString(parser, TIMETYPE, null); |
169 |
String dataType = parserString(parser, DATATYPE, null); |
170 |
|
171 |
tInfo.setSerialName(name); |
172 |
tInfo.setDescription(desc); |
173 |
try {
|
174 |
tInfo.setTimeType(new Integer(timeType)); |
175 |
} catch (NumberFormatException e) { |
176 |
} |
177 |
try {
|
178 |
tInfo.setDataType(new Integer(dataType)); |
179 |
} catch (NumberFormatException e) { |
180 |
} |
181 |
parserDates(tInfo, parser, DATES, null);
|
182 |
} |
183 |
|
184 |
/*
|
185 |
* (non-Javadoc)
|
186 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
|
187 |
*/
|
188 |
public void read(String xml) throws ParsingException { |
189 |
ArrayList<DefaultSerialInfo> list = new ArrayList<DefaultSerialInfo>(); |
190 |
DefaultSerialInfo tInfo = null;
|
191 |
boolean init = false; |
192 |
boolean tagOk = false; |
193 |
boolean end = false; |
194 |
|
195 |
KXmlParser parser = new KXmlParser();
|
196 |
Reader reader = new StringReader(xml); |
197 |
try {
|
198 |
parser.setInput(reader); |
199 |
} catch (XmlPullParserException e) {
|
200 |
throw new ParsingException(xml); |
201 |
} |
202 |
try {
|
203 |
int tag = parser.nextTag();
|
204 |
|
205 |
if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
|
206 |
parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
|
207 |
while(tag != KXmlParser.END_DOCUMENT) {
|
208 |
switch(tag) {
|
209 |
case KXmlParser.START_TAG:
|
210 |
if (parser.getName().compareTo(MAIN_TAG) == 0) |
211 |
init = true;
|
212 |
if(init) {
|
213 |
if (parser.getName().compareTo(SERIAL) == 0) { |
214 |
tInfo = new DefaultSerialInfo();
|
215 |
tagOk = true;
|
216 |
} |
217 |
} |
218 |
break;
|
219 |
case KXmlParser.END_TAG:
|
220 |
if(parser.getName().compareTo(MAIN_TAG) == 0) |
221 |
end = true;
|
222 |
break;
|
223 |
case KXmlParser.TEXT:
|
224 |
if(tagOk) {
|
225 |
parserSerial(tInfo, parser, xml, tag); |
226 |
list.add(tInfo); |
227 |
tagOk = false;
|
228 |
} |
229 |
break;
|
230 |
} |
231 |
if(end)
|
232 |
break;
|
233 |
tag = parser.next(); |
234 |
} |
235 |
} |
236 |
|
237 |
} catch (XmlPullParserException e) {
|
238 |
throw new ParsingException(xml); |
239 |
} catch (IOException e) { |
240 |
throw new ParsingException(xml); |
241 |
} catch (NumberFormatException e) { |
242 |
throw new ParsingException(xml); |
243 |
} catch (ParseException e) { |
244 |
throw new ParsingException(xml); |
245 |
} |
246 |
|
247 |
((DefaultTimeSerials)serialInfo).setSerial(list); |
248 |
} |
249 |
|
250 |
/*
|
251 |
* (non-Javadoc)
|
252 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
|
253 |
*/
|
254 |
public String write() { |
255 |
StringBuffer b = new StringBuffer(); |
256 |
DefaultTimeSerials si = (DefaultTimeSerials)serialInfo; |
257 |
|
258 |
b.append("<" + MAIN_TAG + ">\n"); |
259 |
for (int i = 0; i < si.getNumberOfSerials(); i++) { |
260 |
DefaultSerialInfo ti = si.getSerial(i); |
261 |
b.append("\t<" + SERIAL + ">\n"); |
262 |
putProperty(b, NAME, ti.getSerialName(), 2);
|
263 |
putProperty(b, DESC, ti.getDescription(), 2);
|
264 |
putProperty(b, TIMETYPE, ti.getTimeType() + "", 2); |
265 |
putProperty(b, DATATYPE, ti.getDataType() + "", 2); |
266 |
b.append("\t\t<" + DATES + ">\n"); |
267 |
for (int j = 0; j < ti.getTime().size(); j++) { |
268 |
putProperty(b, DATE, ti.getTimeInfo(j), 3);
|
269 |
} |
270 |
b.append("\t\t</" + DATES + ">\n"); |
271 |
b.append("\t</" + SERIAL + ">\n"); |
272 |
} |
273 |
b.append("</" + MAIN_TAG + ">\n"); |
274 |
return b.toString();
|
275 |
} |
276 |
|
277 |
/*
|
278 |
* (non-Javadoc)
|
279 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
|
280 |
*/
|
281 |
public Object getResult() { |
282 |
return serialInfo;
|
283 |
} |
284 |
|
285 |
/*
|
286 |
* (non-Javadoc)
|
287 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
|
288 |
*/
|
289 |
public String getMainTag() { |
290 |
return MAIN_TAG;
|
291 |
} |
292 |
} |