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 @ 2443

History | View | Annotate | Download (8.4 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
 * @version 23/04/2007
64
 * @author Nacho Brodin (nachobrodin@gmail.com)
65
 */
66
public class TimeSeriesRmfSerializer extends ClassSerializer {
67
        //TAGS
68
        public static final String MAIN_TAG   = "TimeSeries";
69
        public static final String SERIAL     = "SerialElement";
70
        public static final String DESC       = "Description";
71
        public static final String TIMETYPE   = "SourceType";
72
        public static final String DATES      = "Dates";
73
        public static final String DATE       = "Date";
74
        public static final String NAME       = "Name";
75
        private TimeSeries         serialInfo = null;
76

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

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

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

    
178
        /*
179
         * (non-Javadoc)
180
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
181
         */
182
        public void read(String xml) throws ParsingException {
183
                ArrayList<DefaultSerialInfo> list = new ArrayList<DefaultSerialInfo>();
184
                DefaultSerialInfo tInfo = null;
185
                boolean init = false;
186
                boolean tagOk = false;
187
                boolean end = false;
188

    
189
                KXmlParser parser = new KXmlParser();
190
                Reader reader = new StringReader(xml);
191
                try {
192
                        parser.setInput(reader);
193
                } catch (XmlPullParserException e) {
194
                        throw new ParsingException(xml);
195
                }
196
                try {
197
                        int tag = parser.nextTag();
198

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

    
231
                } catch (XmlPullParserException e) {
232
                        throw new ParsingException(xml);
233
                } catch (IOException e) {
234
                        throw new ParsingException(xml);
235
                } catch (NumberFormatException e) {
236
                        throw new ParsingException(xml);
237
                } catch (ParseException e) {
238
                        throw new ParsingException(xml);
239
                }
240

    
241
                ((DefaultTimeSerials)serialInfo).addSerialInfo(tInfo);//.setSerial(list);
242
        }
243

    
244
        /*
245
         * (non-Javadoc)
246
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
247
         */
248
        public String write() {
249
                StringBuffer b = new StringBuffer();
250
                DefaultTimeSerials si = (DefaultTimeSerials)serialInfo;
251
                
252
                b.append("<" + MAIN_TAG + ">\n");
253
                for (int i = 0; i < si.getNumberOfSerials(); i++) {
254
                        DefaultSerialInfo ti = si.getSerial(i);
255
                        b.append("\t<" + SERIAL + ">\n");
256
                        putProperty(b, NAME, ti.getSerialName(), 2);
257
                        putProperty(b, DESC, ti.getDescription(), 2);
258
                        putProperty(b, TIMETYPE, ti.getTimeType() + "", 2);
259
                        b.append("\t\t<" + DATES + ">\n");
260
                        for (int j = 0; j < ti.getTime().size(); j++) {
261
                                putProperty(b, DATE, ti.getTimeInfo(j), 3);
262
                        }
263
                        b.append("\t\t</" + DATES + ">\n");
264
                        b.append("\t</" + SERIAL + ">\n");
265
                }
266
                b.append("</" + MAIN_TAG + ">\n");
267
                return b.toString();
268
        }
269

    
270
        /*
271
         * (non-Javadoc)
272
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
273
         */
274
        public Object getResult() {
275
                return serialInfo;
276
        }
277

    
278
        /*
279
         *  (non-Javadoc)
280
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
281
         */
282
        public String getMainTag() {
283
                return MAIN_TAG;
284
        }
285
}