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 @ 1849
History | View | Annotate | Download (4.25 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 |
/*
|
69 |
* (non-Javadoc)
|
70 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
|
71 |
*/
|
72 |
public String getMainTag() { |
73 |
return MAIN_TAG;
|
74 |
} |
75 |
|
76 |
/*
|
77 |
* (non-Javadoc)
|
78 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
|
79 |
*/
|
80 |
public Object getResult() { |
81 |
return fileList;
|
82 |
} |
83 |
|
84 |
/*
|
85 |
* (non-Javadoc)
|
86 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
|
87 |
*/
|
88 |
public void read(String xml) throws ParsingException { |
89 |
KXmlParser parser = new KXmlParser();
|
90 |
Reader reader = new StringReader(xml); |
91 |
try {
|
92 |
parser.setInput(reader); |
93 |
} catch (XmlPullParserException e) {
|
94 |
throw new ParsingException(xml); |
95 |
} |
96 |
|
97 |
try {
|
98 |
int tag = parser.nextTag();
|
99 |
|
100 |
if (parser.getEventType() != KXmlParser.END_DOCUMENT) {
|
101 |
parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
|
102 |
|
103 |
while (tag != KXmlParser.END_DOCUMENT) {
|
104 |
switch (tag) {
|
105 |
case KXmlParser.START_TAG:
|
106 |
if (parser.getName().equals("ROIFile")) { |
107 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
108 |
if (parser.getAttributeName(i).equals("value")) { |
109 |
if(fileList == null) |
110 |
fileList = new ArrayList<File>(); |
111 |
fileList.add(new File((String) parser.getAttributeValue(i))); |
112 |
} |
113 |
} |
114 |
} |
115 |
} |
116 |
tag = parser.next(); |
117 |
} |
118 |
} |
119 |
reader.close(); |
120 |
} catch (XmlPullParserException e) {
|
121 |
throw new ParsingException(xml); |
122 |
} catch (IOException e) { |
123 |
throw new ParsingException(xml); |
124 |
} |
125 |
} |
126 |
|
127 |
/*
|
128 |
* (non-Javadoc)
|
129 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
|
130 |
*/
|
131 |
public String write() throws IOException { |
132 |
if (fileList == null || fileList.size() == 0) |
133 |
return ""; |
134 |
|
135 |
StringBuffer b = new StringBuffer(); |
136 |
|
137 |
b.append("<" + MAIN_TAG + ">\n"); |
138 |
for (int i = 0; i < fileList.size(); i++) { |
139 |
if(fileList.get(i) != null) { |
140 |
b.append("\t<ROIFile");
|
141 |
b.append(" value=\"" + fileList.get(i).getAbsolutePath() + "\"/>\n"); |
142 |
b.append("</" + MAIN_TAG + ">\n"); |
143 |
} |
144 |
} |
145 |
|
146 |
return b.toString();
|
147 |
} |
148 |
|
149 |
} |