Statistics
| Revision:

gvsig-raster / org.gvsig.raster.multifile / trunk / org.gvsig.raster.multifile / org.gvsig.raster.multifile.io / src / main / java / org / gvsig / raster / multifile / io / MultiFileFormatSerializer.java @ 5988

History | View | Annotate | Download (6.43 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.multifile.io;
23

    
24
import java.io.File;
25
import java.io.FileWriter;
26
import java.io.IOException;
27
import java.io.Reader;
28
import java.io.StringReader;
29
import java.text.ParseException;
30

    
31
import org.gvsig.fmap.dal.coverage.exception.ParsingException;
32
import org.gvsig.raster.impl.store.rmf.ClassSerializer;
33
import org.gvsig.raster.impl.store.rmf.RmfBlocksManager;
34
import org.kxml2.io.KXmlParser;
35
import org.xmlpull.v1.XmlPullParserException;
36
/**
37
 * @author Nacho Brodin (nachobrodin@gmail.com)
38
 */
39
public class MultiFileFormatSerializer extends ClassSerializer {
40
        //TAGS
41
        public static final String MAIN_TAG          = "MultiFileFormat";
42
        public static final String URIS              = "URIs";
43
        public static final String URI               = "URI";
44
        public static final String NAME              = "Name";
45
        private MultiFileFormat   multiFileFormat    = null;
46

    
47
        /**
48
         * @param timeSeriesFormat
49
         */
50
        public MultiFileFormatSerializer(MultiFileFormat timeSeriesFormat) {
51
                this.multiFileFormat = timeSeriesFormat;
52
        }
53

    
54
         /**
55
         * Parsers a date list
56
         * @param tInfo
57
         * @param parser
58
         * @param xml
59
          * @param parseableTag
60
         * @param tag
61
         * @throws XmlPullParserException
62
         * @throws IOException
63
         * @throws ParsingException
64
         * @throws NumberFormatException
65
         * @throws ParseException
66
         */
67
        public void parseURIs(KXmlParser parser, String xml, String parseableTag) throws XmlPullParserException, IOException, NumberFormatException, ParsingException, ParseException  {
68
                boolean end = false;
69
                boolean init = false;
70
                boolean readFile = false;
71
                int tag = parser.next();
72
                while (!end) {
73
                        switch (tag) {
74
                                case KXmlParser.END_DOCUMENT:
75
                                        return;
76
                                case KXmlParser.START_TAG:
77
                                        if (parser.getName().compareTo(parseableTag) == 0) {
78
                                                init = true;
79
                                        }
80
                                        if(init) {
81
                                                if (parser.getName().compareTo(URI) == 0) {
82
                                                        readFile = true;
83
                                                }
84
                                        }
85
                                        break;
86
                                case KXmlParser.END_TAG:
87
                                        if (parser.getName().compareTo(parseableTag) == 0)
88
                                                end = true;
89
                                        break;
90
                                case KXmlParser.TEXT:
91
                                        if(readFile) {
92
                                                String file = parser.getText();
93
                                                multiFileFormat.addFile(new File(file));
94
                                                readFile = false;
95
                                        }
96
                                        break;
97
                        }
98
                        if (!end)
99
                                tag = parser.next();
100
                }
101
        }
102

    
103
        /*
104
         * (non-Javadoc)
105
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
106
         */
107
        public void read(String xml) throws ParsingException {
108
                if(multiFileFormat == null)
109
                        multiFileFormat = new MultiFileFormat();
110
                else {
111
                        multiFileFormat.clean();
112
                }
113
                boolean init = false;
114
                boolean end = false;
115

    
116
                KXmlParser parser = new KXmlParser();
117
                Reader reader = new StringReader(xml);
118
                try {
119
                        parser.setInput(reader);
120
                } catch (XmlPullParserException e) {
121
                        throw new ParsingException(xml);
122
                }
123
                try {
124
                        int tag = parser.nextTag();
125

    
126
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
127
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
128
                                while(tag != KXmlParser.END_DOCUMENT) {
129
                                        switch(tag) {
130
                                                case KXmlParser.START_TAG:
131
                                                        if (parser.getName().compareTo(MAIN_TAG) == 0)
132
                                                                init = true;
133
                                                        break;
134
                                                case KXmlParser.END_TAG:
135
                                                        if(parser.getName().compareTo(MAIN_TAG) == 0)
136
                                                                end = true;
137
                                                        break;
138
                                                case KXmlParser.TEXT:
139
                                                        if(init) {
140
                                                                String name = parserString(parser, NAME, null);
141
                                                                multiFileFormat.setName(name);
142
                                                                parseURIs(parser, xml, URIS);
143
                                                                init = false;
144
                                                        }
145
                                                        break;
146
                                        }
147
                                        if(end)
148
                                                break;
149
                                        tag = parser.next();
150
                                }
151
                        }
152

    
153
                } catch (XmlPullParserException e) {
154
                        throw new ParsingException(xml);
155
                } catch (IOException e) {
156
                        throw new ParsingException(xml);
157
                } catch (NumberFormatException e) {
158
                        throw new ParsingException(xml);
159
                } catch (ParseException e) {
160
                        throw new ParsingException(xml);
161
                }
162
        }
163

    
164
        /**
165
         * Writes a MultiFileFormat to disk
166
         * @param multiFileFormat
167
         * @param fileName
168
         * @param file
169
         * @throws IOException
170
         */
171
        public static void write(MultiFileFormat multiFileFormat, String fileName) throws IOException {
172
                File file = new File(fileName);
173
                file.createNewFile();
174
                FileWriter writer = new FileWriter(file);
175
                MultiFileFormatSerializer serializer = new MultiFileFormatSerializer(multiFileFormat);
176
                writer.write(serializer.write());
177
                writer.close();
178
        }
179

    
180
        /**
181
         * Reads a TimeSeriesFormat from disk
182
         * @param timeSeriesFormat
183
         * @param file
184
         * @return MultiFileFormat
185
         * @throws IOException
186
         * @throws ParsingException
187
         */
188
        public static MultiFileFormat read(MultiFileFormat timeSeriesFormat, String file) throws IOException, ParsingException {
189
                RmfBlocksManager manager = new RmfBlocksManager(file);
190
                MultiFileFormatSerializer serializer = new MultiFileFormatSerializer(timeSeriesFormat);
191
                manager.addClient(serializer);
192
                manager.read(null);
193
                return timeSeriesFormat;
194
        }
195

    
196
        /*
197
         * (non-Javadoc)
198
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
199
         */
200
        public String write() {
201
                StringBuffer b = new StringBuffer();
202
                b.append("<?xml version=\"1.0\" encoding=\"ISO-8859-15\"?>\n");
203
                b.append("<" + MAIN_TAG + ">\n");
204
                putProperty(b, NAME, multiFileFormat.getName(), 2);
205

    
206
                b.append("\t\t<" + URIS + ">\n");
207
                for (int i = 0; i < multiFileFormat.getNumberOfFiles(); i++) {
208
                        putProperty(b, URI, multiFileFormat.getPathToFile(i), 3);
209
                }
210
                b.append("\t\t</" + URIS + ">\n");
211
                b.append("</" + MAIN_TAG + ">\n");
212
                return b.toString();
213
        }
214

    
215
        /*
216
         * (non-Javadoc)
217
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
218
         */
219
        public Object getResult() {
220
                return multiFileFormat;
221
        }
222

    
223
        /*
224
         *  (non-Javadoc)
225
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
226
         */
227
        public String getMainTag() {
228
                return MAIN_TAG;
229
        }
230
}