gvsig-raster / org.gvsig.raster.mosaic / trunk / org.gvsig.raster.mosaic / org.gvsig.raster.mosaic.io / src / main / java / org / gvsig / raster / mosaic / io / MosaicRasterFormatSerializer.java @ 723
History | View | Annotate | Download (6.55 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.mosaic.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 MosaicRasterFormatSerializer 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 DESCRIPTION = "Description"; |
45 |
public static final String NAME = "Name"; |
46 |
private MosaicRasterFormat mosaicFormat = null; |
47 |
|
48 |
public MosaicRasterFormatSerializer(MosaicRasterFormat mosaicFormat) {
|
49 |
this.mosaicFormat = mosaicFormat;
|
50 |
} |
51 |
|
52 |
/**
|
53 |
* Parsers a date list
|
54 |
* @param tInfo
|
55 |
* @param parser
|
56 |
* @param xml
|
57 |
* @param tag
|
58 |
* @throws XmlPullParserException
|
59 |
* @throws IOException
|
60 |
* @throws ParsingException
|
61 |
* @throws NumberFormatException
|
62 |
* @throws ParseException
|
63 |
*/
|
64 |
public void parseURIs(KXmlParser parser, String xml, String parseableTag) throws XmlPullParserException, IOException, NumberFormatException, ParsingException, ParseException { |
65 |
boolean end = false; |
66 |
boolean init = false; |
67 |
boolean readFile = false; |
68 |
int tag = parser.next();
|
69 |
while (!end) {
|
70 |
switch (tag) {
|
71 |
case KXmlParser.END_DOCUMENT:
|
72 |
return;
|
73 |
case KXmlParser.START_TAG:
|
74 |
if (parser.getName().compareTo(parseableTag) == 0) { |
75 |
init = true;
|
76 |
} |
77 |
if(init) {
|
78 |
if (parser.getName().compareTo(URI) == 0) { |
79 |
readFile = true;
|
80 |
} |
81 |
} |
82 |
break;
|
83 |
case KXmlParser.END_TAG:
|
84 |
if (parser.getName().compareTo(parseableTag) == 0) |
85 |
end = true;
|
86 |
break;
|
87 |
case KXmlParser.TEXT:
|
88 |
if(readFile) {
|
89 |
String file = parser.getText();
|
90 |
mosaicFormat.addFile(new File(file)); |
91 |
readFile = false;
|
92 |
} |
93 |
break;
|
94 |
} |
95 |
if (!end)
|
96 |
tag = parser.next(); |
97 |
} |
98 |
} |
99 |
|
100 |
/*
|
101 |
* (non-Javadoc)
|
102 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
|
103 |
*/
|
104 |
public void read(String xml) throws ParsingException { |
105 |
if(mosaicFormat == null) |
106 |
mosaicFormat = new MosaicRasterFormat();
|
107 |
else {
|
108 |
mosaicFormat.clean(); |
109 |
} |
110 |
boolean init = false; |
111 |
boolean end = false; |
112 |
|
113 |
KXmlParser parser = new KXmlParser();
|
114 |
Reader reader = new StringReader(xml); |
115 |
try {
|
116 |
parser.setInput(reader); |
117 |
} catch (XmlPullParserException e) {
|
118 |
throw new ParsingException(xml); |
119 |
} |
120 |
try {
|
121 |
int tag = parser.nextTag();
|
122 |
|
123 |
if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
|
124 |
parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
|
125 |
while(tag != KXmlParser.END_DOCUMENT) {
|
126 |
switch(tag) {
|
127 |
case KXmlParser.START_TAG:
|
128 |
if (parser.getName().compareTo(MAIN_TAG) == 0) |
129 |
init = true;
|
130 |
break;
|
131 |
case KXmlParser.END_TAG:
|
132 |
if(parser.getName().compareTo(MAIN_TAG) == 0) |
133 |
end = true;
|
134 |
break;
|
135 |
case KXmlParser.TEXT:
|
136 |
if(init) {
|
137 |
String name = parserString(parser, NAME, null); |
138 |
mosaicFormat.setName(name); |
139 |
String desc = parserString(parser, DESCRIPTION, null); |
140 |
mosaicFormat.setDescription(desc); |
141 |
parseURIs(parser, xml, URIS); |
142 |
init = false;
|
143 |
} |
144 |
break;
|
145 |
} |
146 |
if(end)
|
147 |
break;
|
148 |
tag = parser.next(); |
149 |
} |
150 |
} |
151 |
|
152 |
} catch (XmlPullParserException e) {
|
153 |
throw new ParsingException(xml); |
154 |
} catch (IOException e) { |
155 |
throw new ParsingException(xml); |
156 |
} catch (NumberFormatException e) { |
157 |
throw new ParsingException(xml); |
158 |
} catch (ParseException e) { |
159 |
throw new ParsingException(xml); |
160 |
} |
161 |
} |
162 |
|
163 |
/**
|
164 |
* Writes a MultiFileFormat to disk
|
165 |
* @param mosaicFormat
|
166 |
* @param file
|
167 |
* @throws IOException
|
168 |
*/
|
169 |
public static void write(MosaicRasterFormat mosaicFormat, String fileName) throws IOException { |
170 |
File file = new File(fileName); |
171 |
file.createNewFile(); |
172 |
FileWriter writer = new FileWriter(file); |
173 |
MosaicRasterFormatSerializer serializer = new MosaicRasterFormatSerializer(mosaicFormat);
|
174 |
writer.write(serializer.write()); |
175 |
writer.close(); |
176 |
} |
177 |
|
178 |
/**
|
179 |
* Reads a TimeSeriesFormat from disk
|
180 |
* @param timeSeriesFormat
|
181 |
* @param file
|
182 |
* @throws IOException
|
183 |
* @throws ParsingException
|
184 |
*/
|
185 |
public static MosaicRasterFormat read(MosaicRasterFormat timeSeriesFormat, String file) throws IOException, ParsingException { |
186 |
RmfBlocksManager manager = new RmfBlocksManager(file);
|
187 |
MosaicRasterFormatSerializer serializer = new MosaicRasterFormatSerializer(timeSeriesFormat);
|
188 |
manager.addClient(serializer); |
189 |
manager.read(null);
|
190 |
return timeSeriesFormat;
|
191 |
} |
192 |
|
193 |
/*
|
194 |
* (non-Javadoc)
|
195 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
|
196 |
*/
|
197 |
public String write() { |
198 |
StringBuffer b = new StringBuffer(); |
199 |
b.append("<?xml version=\"1.0\" encoding=\"ISO-8859-15\"?>\n");
|
200 |
b.append("<" + MAIN_TAG + ">\n"); |
201 |
putProperty(b, NAME, mosaicFormat.getName(), 2);
|
202 |
putProperty(b, DESCRIPTION, mosaicFormat.getDescription(), 2);
|
203 |
|
204 |
b.append("\t\t<" + URIS + ">\n"); |
205 |
for (int i = 0; i < mosaicFormat.getNumberOfFiles(); i++) { |
206 |
putProperty(b, URI, mosaicFormat.getPathToFile(i), 3); |
207 |
} |
208 |
b.append("\t\t</" + URIS + ">\n"); |
209 |
b.append("</" + MAIN_TAG + ">\n"); |
210 |
return b.toString();
|
211 |
} |
212 |
|
213 |
/*
|
214 |
* (non-Javadoc)
|
215 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
|
216 |
*/
|
217 |
public Object getResult() { |
218 |
return mosaicFormat;
|
219 |
} |
220 |
|
221 |
/*
|
222 |
* (non-Javadoc)
|
223 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
|
224 |
*/
|
225 |
public String getMainTag() { |
226 |
return MAIN_TAG;
|
227 |
} |
228 |
} |