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 / ROIFileRmfSerializer.java @ 2579
History | View | Annotate | Download (3.88 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.File; |
25 |
import java.io.IOException; |
26 |
import java.io.Reader; |
27 |
import java.io.StringReader; |
28 |
import java.util.ArrayList; |
29 |
import java.util.List; |
30 |
|
31 |
import org.gvsig.fmap.dal.coverage.exception.ParsingException; |
32 |
import org.gvsig.raster.impl.store.rmf.ClassSerializer; |
33 |
import org.gvsig.tools.ToolsLocator; |
34 |
import org.gvsig.tools.extensionpoint.ExtensionPoint; |
35 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
36 |
import org.kxml2.io.KXmlParser; |
37 |
import org.xmlpull.v1.XmlPullParserException; |
38 |
/**
|
39 |
* <P>
|
40 |
* This class converts to XML the path of a file of Regions of interest.
|
41 |
* </P>
|
42 |
*
|
43 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
44 |
*/
|
45 |
public class ROIFileRmfSerializer extends ClassSerializer { |
46 |
private final String MAIN_TAG = "ROI"; |
47 |
private List<File> fileList = null; |
48 |
|
49 |
/**
|
50 |
* Registra ProjectionRmfSerializer en los puntos de extension de Serializer
|
51 |
*/
|
52 |
public static void register() { |
53 |
ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager(); |
54 |
ExtensionPoint point = extensionPoints.get("Serializer");
|
55 |
point.append("ROI", "", ROIFileRmfSerializer.class); |
56 |
} |
57 |
|
58 |
public ROIFileRmfSerializer(List<File> file) { |
59 |
this.fileList = file;
|
60 |
} |
61 |
|
62 |
/**
|
63 |
* Constructor.
|
64 |
*/
|
65 |
public ROIFileRmfSerializer() {
|
66 |
} |
67 |
|
68 |
public String getMainTag() { |
69 |
return MAIN_TAG;
|
70 |
} |
71 |
|
72 |
public Object getResult() { |
73 |
return fileList;
|
74 |
} |
75 |
|
76 |
public void read(String xml) throws ParsingException { |
77 |
KXmlParser parser = new KXmlParser();
|
78 |
Reader reader = new StringReader(xml); |
79 |
try {
|
80 |
parser.setInput(reader); |
81 |
} catch (XmlPullParserException e) {
|
82 |
throw new ParsingException(xml); |
83 |
} |
84 |
|
85 |
try {
|
86 |
int tag = parser.nextTag();
|
87 |
|
88 |
if (parser.getEventType() != KXmlParser.END_DOCUMENT) {
|
89 |
parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
|
90 |
|
91 |
while (tag != KXmlParser.END_DOCUMENT) {
|
92 |
switch (tag) {
|
93 |
case KXmlParser.START_TAG:
|
94 |
if (parser.getName().equals("ROIFile")) { |
95 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
96 |
if (parser.getAttributeName(i).equals("value")) { |
97 |
if(fileList == null) |
98 |
fileList = new ArrayList<File>(); |
99 |
fileList.add(new File((String) parser.getAttributeValue(i))); |
100 |
} |
101 |
} |
102 |
} |
103 |
} |
104 |
tag = parser.next(); |
105 |
} |
106 |
} |
107 |
reader.close(); |
108 |
} catch (XmlPullParserException e) {
|
109 |
throw new ParsingException(xml); |
110 |
} catch (IOException e) { |
111 |
throw new ParsingException(xml); |
112 |
} |
113 |
} |
114 |
|
115 |
public String write() throws IOException { |
116 |
if (fileList == null || fileList.size() == 0) |
117 |
return ""; |
118 |
|
119 |
StringBuffer b = new StringBuffer(); |
120 |
|
121 |
b.append("<" + MAIN_TAG + ">\n"); |
122 |
for (int i = 0; i < fileList.size(); i++) { |
123 |
if(fileList.get(i) != null) { |
124 |
b.append("\t<ROIFile");
|
125 |
b.append(" value=\"" + fileList.get(i).getAbsolutePath() + "\"/>\n"); |
126 |
b.append("</" + MAIN_TAG + ">\n"); |
127 |
} |
128 |
} |
129 |
|
130 |
return b.toString();
|
131 |
} |
132 |
|
133 |
} |