Statistics
| Revision:

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 / TimeSeriesRmfSerializer.java @ 2616

History | View | Annotate | Download (8.02 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
 * \<TimeSeries\><BR>
52
 * &nbsp;\<SerialElement\><BR>
53
 * &nbsp;&nbsp;\<Name\>katrina\</Name\><BR>
54
 * &nbsp;&nbsp;\<Description\>Evoluci?n del huracan katrina\</Description\><BR>
55
 * &nbsp;&nbsp;\<Type\>Single value\</Type\><BR>
56
 * &nbsp;&nbsp;\<Dates\><BR>
57
 * &nbsp;&nbsp;&nbsp;\<Date\>28//8//2005-6:10:00\</Date\><BR>
58
 * &nbsp;&nbsp;\</Dates\><BR>
59
 * &nbsp;\</SerialElement\><BR>
60
 * \</TimeSeries\><BR>
61
 * </P>
62
 *
63
 * @author Nacho Brodin (nachobrodin@gmail.com)
64
 */
65
public class TimeSeriesRmfSerializer extends ClassSerializer {
66
        //TAGS
67
        public static final String MAIN_TAG   = "TimeSeries";
68
        public static final String SERIAL     = "SerialElement";
69
        public static final String DESC       = "Description";
70
        public static final String TIMETYPE   = "SourceType";
71
        public static final String DATES      = "Dates";
72
        public static final String DATE       = "Date";
73
        public static final String NAME       = "Name";
74
        private TimeSeries         serialInfo = null;
75

    
76
        /**
77
         * Registra GeoPointRmfSerializer en los puntos de extension de Serializer
78
         */
79
        public static void register() {
80
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
81
                ExtensionPoint point = extensionPoints.get("Serializer");
82
                point.append("Series", "", TimeSeriesRmfSerializer.class);
83
        }
84

    
85
        /**
86
         * Constructor. Asigna la lista de puntos a serializar.
87
         * @param ColorTable tabla a convertir en XML
88
         */
89
        public TimeSeriesRmfSerializer(TimeSeries serialInfo) {
90
                this.serialInfo = serialInfo;
91
        }
92
        
93
        
94
        private void readDate(DefaultSerialInfo tInfo, KXmlParser parser) throws ParseException {
95
                String d = parser.getText();
96
                try {
97
                        Double value = new Double(d);
98
                        tInfo.addValue(value.doubleValue());
99
                } catch (NumberFormatException e) {
100
                        tInfo.addValue(d);
101
                }
102
        }
103
        
104
        /**
105
         * Parsers a date list
106
         * @param tInfo
107
         * @param parser
108
         * @param xml
109
         * @param tag
110
         * @throws XmlPullParserException
111
         * @throws IOException
112
         * @throws ParsingException
113
         * @throws NumberFormatException
114
         * @throws ParseException 
115
         */
116
        public void parserDates(DefaultSerialInfo tInfo, KXmlParser parser, String parseableTag, String[] errorTags) throws XmlPullParserException, IOException, NumberFormatException, ParsingException, ParseException  {
117
                boolean end = false;
118
                boolean init = false;
119
                boolean readDate = false;
120
                int tag = parser.next();
121
                while (!end) {
122
                        switch (tag) {
123
                                case KXmlParser.END_DOCUMENT:
124
                                        return;
125
                                case KXmlParser.START_TAG:
126
                                        if (parser.getName().compareTo(parseableTag) == 0) {
127
                                                init = true;
128
                                        }
129
                                        if(init) {
130
                                                if (parser.getName().compareTo(DATE) == 0) {
131
                                                        readDate = true;
132
                                                }
133
                                        }
134
                                        break;
135
                                case KXmlParser.END_TAG:
136
                                        if (parser.getName().compareTo(parseableTag) == 0)
137
                                                end = true;
138
                                        break;
139
                                case KXmlParser.TEXT:
140
                                        if(readDate) {
141
                                                readDate(tInfo, parser);
142
                                                readDate = false;
143
                                        }
144
                                        break;
145
                        }
146
                        if (!end)
147
                                tag = parser.next();
148
                }
149
        }
150

    
151
        /**
152
         * Parsers a serial structure
153
         * @param tInfo
154
         * @param parser
155
         * @param xml
156
         * @param tag
157
         * @throws XmlPullParserException
158
         * @throws IOException
159
         * @throws ParsingException
160
         * @throws NumberFormatException
161
         * @throws ParseException 
162
         */
163
        public void parserSerial(DefaultSerialInfo tInfo, KXmlParser parser, String xml, int tag) throws XmlPullParserException, IOException, NumberFormatException, ParsingException, ParseException  {
164
                String name = parserString(parser, NAME, null);
165
                String desc = parserString(parser, DESC, null);
166
                String timeType = parserString(parser, TIMETYPE, null);
167
                
168
                tInfo.setSerialName(name);
169
                tInfo.setDescription(desc);
170
                try {
171
                        tInfo.setTimeType(new Integer(timeType));
172
                } catch (NumberFormatException e) {
173
                }
174
                parserDates(tInfo, parser, DATES, null);
175
        }
176

    
177
        public void read(String xml) throws ParsingException {
178
                ArrayList<DefaultSerialInfo> list = new ArrayList<DefaultSerialInfo>();
179
                DefaultSerialInfo tInfo = null;
180
                boolean init = false;
181
                boolean tagOk = false;
182
                boolean end = false;
183

    
184
                KXmlParser parser = new KXmlParser();
185
                Reader reader = new StringReader(xml);
186
                try {
187
                        parser.setInput(reader);
188
                } catch (XmlPullParserException e) {
189
                        throw new ParsingException(xml);
190
                }
191
                try {
192
                        int tag = parser.nextTag();
193

    
194
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
195
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
196
                                while(tag != KXmlParser.END_DOCUMENT) {
197
                                        switch(tag) {
198
                                                case KXmlParser.START_TAG:
199
                                                        if (parser.getName().compareTo(MAIN_TAG) == 0)
200
                                                                init = true;
201
                                                        if(init) {
202
                                                                if (parser.getName().compareTo(SERIAL) == 0) {
203
                                                                        tInfo = new DefaultSerialInfo();
204
                                                                        tagOk = true;
205
                                                                }
206
                                                        }
207
                                                        break;
208
                                                case KXmlParser.END_TAG:
209
                                                        if(parser.getName().compareTo(MAIN_TAG) == 0)
210
                                                                end = true;
211
                                                        break;
212
                                                case KXmlParser.TEXT:
213
                                                        if(tagOk) {
214
                                                                parserSerial(tInfo, parser, xml, tag);
215
                                                                //list.add(tInfo);
216
                                                                tagOk = false;
217
                                                        }
218
                                                        break;
219
                                        }
220
                                        if(end)
221
                                                break;
222
                                        tag = parser.next();
223
                                }
224
                        }
225

    
226
                } catch (XmlPullParserException e) {
227
                        throw new ParsingException(xml);
228
                } catch (IOException e) {
229
                        throw new ParsingException(xml);
230
                } catch (NumberFormatException e) {
231
                        throw new ParsingException(xml);
232
                } catch (ParseException e) {
233
                        throw new ParsingException(xml);
234
                }
235

    
236
                ((DefaultTimeSerials)serialInfo).addSerialInfo(tInfo);//.setSerial(list);
237
        }
238

    
239
        public String write() {
240
                StringBuffer b = new StringBuffer();
241
                DefaultTimeSerials si = (DefaultTimeSerials)serialInfo;
242
                
243
                b.append("<" + MAIN_TAG + ">\n");
244
                for (int i = 0; i < si.getNumberOfSerials(); i++) {
245
                        DefaultSerialInfo ti = si.getSerial(i);
246
                        b.append("\t<" + SERIAL + ">\n");
247
                        putProperty(b, NAME, ti.getSerialName(), 2);
248
                        putProperty(b, DESC, ti.getDescription(), 2);
249
                        putProperty(b, TIMETYPE, ti.getTimeType() + "", 2);
250
                        b.append("\t\t<" + DATES + ">\n");
251
                        for (int j = 0; j < ti.getTime().size(); j++) {
252
                                putProperty(b, DATE, ti.getTimeInfo(j), 3);
253
                        }
254
                        b.append("\t\t</" + DATES + ">\n");
255
                        b.append("\t</" + SERIAL + ">\n");
256
                }
257
                b.append("</" + MAIN_TAG + ">\n");
258
                return b.toString();
259
        }
260

    
261
        public Object getResult() {
262
                return serialInfo;
263
        }
264

    
265
        public String getMainTag() {
266
                return MAIN_TAG;
267
        }
268
}