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 / SerialRmfSerializer.java @ 162

History | View | Annotate | Download (7.66 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.SerialInfo;
32
import org.gvsig.raster.impl.datastruct.TimeInfo;
33
import org.gvsig.raster.impl.store.properties.DataStoreSerialInfo;
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
 * &nbsp;\<Serial\><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;\</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 TYPE       = "Type";
72
        public static final String DATES      = "Dates";
73
        public static final String DATE       = "Date";
74
        public static final String NAME       = "Name";
75
        private SerialInfo         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", "", SerialRmfSerializer.class);
84
        }
85

    
86
        /**
87
         * Constructor. Asigna la lista de puntos a serializar.
88
         * @param ColorTable tabla a convertir en XML
89
         */
90
        public SerialRmfSerializer(SerialInfo serialInfo) {
91
                this.serialInfo = serialInfo;
92
        }
93
        
94
        /**
95
         * Parsers a date list
96
         * @param tInfo
97
         * @param parser
98
         * @param xml
99
         * @param tag
100
         * @throws XmlPullParserException
101
         * @throws IOException
102
         * @throws ParsingException
103
         * @throws NumberFormatException
104
         */
105
        public void parserDates(TimeInfo tInfo, KXmlParser parser, String parseableTag, String[] errorTags) throws XmlPullParserException, IOException, NumberFormatException, ParsingException  {
106
                boolean end = false;
107
                boolean init = false;
108
                int tag = parser.next();
109
                while (!end) {
110
                        switch (tag) {
111
                                case KXmlParser.END_DOCUMENT:
112
                                        return;
113
                                case KXmlParser.START_TAG:
114
                                        if (parser.getName().compareTo(parseableTag) == 0)
115
                                                init = true;
116
                                        if(init) {
117
                                                String d = parserString(parser, DATE, errorTags);
118
                                                try {
119
                                                        tInfo.addDate(d);
120
                                                } catch (ParseException e) {
121
                                                }
122
                                        }
123
                                        break;
124
                                case KXmlParser.END_TAG:
125
                                        if (parser.getName().compareTo(parseableTag) == 0)
126
                                                end = true;
127
                                        break;
128
                                case KXmlParser.TEXT:
129
                                        break;
130
                        }
131
                        if (!end)
132
                                tag = parser.next();
133
                }
134
        }
135

    
136
        /**
137
         * Parsers a serial structure
138
         * @param tInfo
139
         * @param parser
140
         * @param xml
141
         * @param tag
142
         * @throws XmlPullParserException
143
         * @throws IOException
144
         * @throws ParsingException
145
         * @throws NumberFormatException
146
         */
147
        public void parserSerial(TimeInfo tInfo, KXmlParser parser, String xml, int tag) throws XmlPullParserException, IOException, NumberFormatException, ParsingException  {
148
                String name = parserString(parser, NAME, null);
149
                String desc = parserString(parser, DESC, null);
150
                String type = parserString(parser, TYPE, null);
151
                tInfo.setSerialName(name);
152
                tInfo.setDescription(desc);
153
                tInfo.setTimeType(type);
154
                parserDates(tInfo, parser, DATES, null);
155
        }
156

    
157
        /*
158
         * (non-Javadoc)
159
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
160
         */
161
        public void read(String xml) throws ParsingException {
162
                ArrayList<TimeInfo> list = new ArrayList<TimeInfo>();
163
                TimeInfo tInfo = null;
164
                boolean init = false;
165
                int nPoint = -1;
166
                boolean tagOk = false;
167

    
168
                KXmlParser parser = new KXmlParser();
169
                Reader reader = new StringReader(xml);
170
                try {
171
                        parser.setInput(reader);
172
                } catch (XmlPullParserException e) {
173
                        throw new ParsingException(xml);
174
                }
175
                try {
176
                        int tag = parser.nextTag();
177

    
178
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
179
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
180
                                while(tag != KXmlParser.END_DOCUMENT) {
181
                                        switch(tag) {
182
                                                case KXmlParser.START_TAG:
183
                                                        if (parser.getName().compareTo(MAIN_TAG) == 0)
184
                                                                init = true;
185
                                                        if(init) {
186
                                                                if (parser.getName().compareTo(SERIAL) == 0) {
187
                                                                        tInfo = new TimeInfo();
188
                                                                        tagOk = true;
189
                                                                        nPoint++;
190
                                                                }
191
                                                        }
192
                                                        break;
193
                                                case KXmlParser.END_TAG:
194
                                                        break;
195
                                                case KXmlParser.TEXT:
196
                                                        if(tagOk) {
197
                                                                parserSerial(tInfo, parser, xml, tag);
198
                                                                tagOk = false;
199
                                                                list.add(tInfo);
200
                                                        }
201
                                                        break;
202
                                        }
203
                                        tag = parser.next();
204
                                }
205
                                parser.require(KXmlParser.END_DOCUMENT, null, null);
206
                        }
207

    
208
                } catch (XmlPullParserException e) {
209
                        throw new ParsingException(xml);
210
                } catch (IOException e) {
211
                        throw new ParsingException(xml);
212
                }
213

    
214
                serialInfo = new DataStoreSerialInfo();
215
                ((DataStoreSerialInfo)serialInfo).setSerial(list);
216
        }
217

    
218
        /*
219
         * (non-Javadoc)
220
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
221
         */
222
        public String write() {
223
                StringBuffer b = new StringBuffer();
224
                DataStoreSerialInfo si = (DataStoreSerialInfo)serialInfo;
225
                
226
                b.append("<" + MAIN_TAG + ">\n");
227
                for (int i = 0; i < si.getNumberOfSerials(); i++) {
228
                        TimeInfo ti = si.getSerial(i);
229
                        b.append("\t<" + SERIAL + ">\n");
230
                        putProperty(b, NAME, ti.getSerialName(), 2);
231
                        putProperty(b, DESC, ti.getDescription(), 2);
232
                        putProperty(b, TYPE, ti.getTimeType() + "", 2);
233
                        b.append("\t<" + DATES + ">\n");
234
                        for (int j = 0; j < ti.getTime().size(); j++) {
235
                                putProperty(b, DATE, ti.getTimeInfo(j), 3);
236
                        }
237
                        b.append("\t<" + DATES + " n=\"" + i + "\">\n");
238
                        b.append("\t</" + SERIAL + ">\n");
239
                }
240
                b.append("</" + MAIN_TAG + ">\n");
241
                return b.toString();
242
        }
243

    
244
        /*
245
         * (non-Javadoc)
246
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
247
         */
248
        public Object getResult() {
249
                return serialInfo;
250
        }
251

    
252
        /*
253
         *  (non-Javadoc)
254
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
255
         */
256
        public String getMainTag() {
257
                return MAIN_TAG;
258
        }
259
}