svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / datastruct / serializer / ColorTableRmfSerializer.java @ 13396
History | View | Annotate | Download (8.4 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.raster.datastruct.serializer; |
20 |
|
21 |
import java.awt.Color; |
22 |
import java.io.IOException; |
23 |
import java.io.Reader; |
24 |
import java.io.StringReader; |
25 |
import java.util.ArrayList; |
26 |
|
27 |
import org.gvsig.raster.dataset.io.rmf.ClassSerializer; |
28 |
import org.gvsig.raster.dataset.io.rmf.ParsingException; |
29 |
import org.gvsig.raster.datastruct.ColorItem; |
30 |
import org.gvsig.raster.datastruct.ColorTable; |
31 |
import org.kxml2.io.KXmlParser; |
32 |
import org.xmlpull.v1.XmlPullParserException; |
33 |
/**
|
34 |
* <P>
|
35 |
* Clase para convertir a XML una tabla de color y obtener la tabla desde XML.
|
36 |
* Esta clase implementa el interfaz IRmfBlock con los m?todos de escritura y
|
37 |
* lectura. Estos ser?n utilizados por el gestor de ficheros RMF para escribir y
|
38 |
* leer datos.
|
39 |
* </P>
|
40 |
* <P>
|
41 |
* La estructura XML de una tabla de color es la siguiente:
|
42 |
* </P>
|
43 |
* <P>
|
44 |
* <ColorTable name="nombre" version="1.1"><BR>
|
45 |
* <Color value="0.0" name="rojo" rgb="#FF34FF" interpolated="50"/><BR>
|
46 |
* <Color value="1.0" name="rojo" rgb="#FF34FF" interpolated="50"/><BR>
|
47 |
* <Alpha value="0.0" number="255" interpolated="50"/><BR>
|
48 |
* <NoData rgba="#"><BR>
|
49 |
* </ColorTable><BR>
|
50 |
*
|
51 |
* @version 23/04/2007
|
52 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
53 |
*/
|
54 |
public class ColorTableRmfSerializer extends ClassSerializer { |
55 |
|
56 |
//TAGS
|
57 |
public static final String MAIN_TAG = "ColorTable"; |
58 |
|
59 |
private ColorTable colorTable = null; |
60 |
|
61 |
/**
|
62 |
* Constructor. Asigna la tabla a serializar.
|
63 |
* @param ColorTable tabla a convertir en XML
|
64 |
*/
|
65 |
public ColorTableRmfSerializer(ColorTable colorTable) {
|
66 |
this.colorTable = colorTable;
|
67 |
} |
68 |
|
69 |
/**
|
70 |
* Constructor.
|
71 |
*/
|
72 |
public ColorTableRmfSerializer() {
|
73 |
} |
74 |
|
75 |
/**
|
76 |
* Devuelve el color si lo encuentra en el arraylist y lo elimina, en caso
|
77 |
* contrario devuelve null
|
78 |
* @param list
|
79 |
* @param value
|
80 |
* @return
|
81 |
*/
|
82 |
private static ColorItem getColorItem(ArrayList list, double value) { |
83 |
for (int i = 0; i < list.size(); i++) { |
84 |
if (((ColorItem) list.get(i)).getValue() == value) {
|
85 |
return (ColorItem) list.remove(i);
|
86 |
} |
87 |
} |
88 |
return null; |
89 |
} |
90 |
|
91 |
/*
|
92 |
* (non-Javadoc)
|
93 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
|
94 |
*/
|
95 |
public void read(String xml) throws ParsingException { |
96 |
String paletteName = ""; |
97 |
boolean interpolated = true; |
98 |
|
99 |
KXmlParser parser = new KXmlParser();
|
100 |
Reader reader = new StringReader(xml); |
101 |
try {
|
102 |
parser.setInput(reader); |
103 |
} catch (XmlPullParserException e) {
|
104 |
throw new ParsingException(xml); |
105 |
} |
106 |
|
107 |
try {
|
108 |
ArrayList items = new ArrayList(); |
109 |
|
110 |
ArrayList rows = new ArrayList(); |
111 |
|
112 |
int tag = parser.nextTag();
|
113 |
|
114 |
if ( parser.getEventType() != KXmlParser.END_DOCUMENT ) {
|
115 |
parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
|
116 |
|
117 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
118 |
if (parser.getAttributeName(i).equals("name")) |
119 |
paletteName = parser.getAttributeValue(i); |
120 |
if (parser.getAttributeName(i).equals("interpolated")) |
121 |
interpolated = parser.getAttributeValue(i).equals("1");
|
122 |
} |
123 |
|
124 |
while (tag != KXmlParser.END_DOCUMENT) {
|
125 |
switch (tag) {
|
126 |
case KXmlParser.START_TAG:
|
127 |
if (parser.getName().equals("Color")) { |
128 |
ColorItem colorItem = new ColorItem();
|
129 |
int a = 255; |
130 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
131 |
if (parser.getAttributeName(i).equals("value")) { |
132 |
colorItem.setValue(Double.parseDouble((String) parser.getAttributeValue(i))); |
133 |
ColorItem aux = getColorItem(rows, Double.parseDouble((String) parser.getAttributeValue(i))); |
134 |
if (aux != null) |
135 |
a = aux.getColor().getAlpha(); |
136 |
} |
137 |
if (parser.getAttributeName(i).equals("name")) { |
138 |
colorItem.setNameClass((String) parser.getAttributeValue(i));
|
139 |
} |
140 |
if (parser.getAttributeName(i).equals("rgb")) { |
141 |
String rgb = parser.getAttributeValue(i);
|
142 |
int r = Integer.valueOf(rgb.substring(0, rgb.indexOf(","))).intValue(); |
143 |
int g = Integer.valueOf(rgb.substring(rgb.indexOf(",") + 1, rgb.lastIndexOf(","))).intValue(); |
144 |
int b = Integer.valueOf(rgb.substring(rgb.lastIndexOf(",") + 1, rgb.length())).intValue(); |
145 |
|
146 |
colorItem.setColor(new Color(r, g, b, a)); |
147 |
} |
148 |
if (parser.getAttributeName(i).equals("interpolated")) { |
149 |
colorItem.setInterpolated(Double.parseDouble((String) parser.getAttributeValue(i))); |
150 |
} |
151 |
} |
152 |
|
153 |
rows.add(colorItem); |
154 |
break;
|
155 |
} |
156 |
if (parser.getName().equals("Alpha")) { |
157 |
ColorItem colorItem = new ColorItem();
|
158 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
159 |
if (parser.getAttributeName(i).equals("value")) { |
160 |
colorItem.setValue(Double.parseDouble((String) parser.getAttributeValue(i))); |
161 |
ColorItem aux = getColorItem(rows, Double.parseDouble((String) parser.getAttributeValue(i))); |
162 |
if (aux != null) { |
163 |
colorItem.setNameClass(aux.getNameClass()); |
164 |
colorItem.setInterpolated(aux.getInterpolated()); |
165 |
colorItem.setColor(new Color(aux.getColor().getRed(), aux.getColor().getGreen(), aux.getColor().getBlue(), colorItem.getColor().getAlpha())); |
166 |
} |
167 |
} |
168 |
if (parser.getAttributeName(i).equals("alpha")) { |
169 |
int a = Integer.parseInt(parser.getAttributeValue(i)); |
170 |
|
171 |
colorItem.setColor(new Color(colorItem.getColor().getRed(), colorItem.getColor().getGreen(), colorItem.getColor().getBlue(), a)); |
172 |
} |
173 |
if (parser.getAttributeName(i).equals("interpolated")) { |
174 |
colorItem.setInterpolated(Double.parseDouble((String) parser.getAttributeValue(i))); |
175 |
} |
176 |
} |
177 |
|
178 |
rows.add(colorItem); |
179 |
break;
|
180 |
} |
181 |
break;
|
182 |
} |
183 |
|
184 |
tag = parser.next(); |
185 |
} |
186 |
parser.require(KXmlParser.END_DOCUMENT, null, null); |
187 |
} |
188 |
|
189 |
for (int i = 0; i < rows.size(); i++) |
190 |
items.add(rows.get(i)); |
191 |
|
192 |
colorTable = new ColorTable();
|
193 |
colorTable.setName(paletteName); |
194 |
colorTable.createPaletteFromColorItems(items, false);
|
195 |
colorTable.setInterpolated(interpolated); |
196 |
|
197 |
} catch (XmlPullParserException e) {
|
198 |
throw new ParsingException(xml); |
199 |
} catch (IOException e) { |
200 |
throw new ParsingException(xml); |
201 |
} |
202 |
} |
203 |
|
204 |
/*
|
205 |
* (non-Javadoc)
|
206 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
|
207 |
*/
|
208 |
public String write() { |
209 |
|
210 |
StringBuffer b = new StringBuffer(); |
211 |
|
212 |
b.append("<" + MAIN_TAG + " name=\"" + colorTable.getName() + "\" interpolated=\"" + (colorTable.isInterpolated()?"1":"0") + "\" version=\"1.1\">\n"); |
213 |
|
214 |
for (int i = 0; i < colorTable.getColorItems().size(); i++) { |
215 |
b.append("\t<Color");
|
216 |
ColorItem colorItem = (ColorItem) colorTable.getColorItems().get(i); |
217 |
b.append(" value=\"" + colorItem.getValue() + "\""); |
218 |
b.append(" name=\"" + colorItem.getNameClass() + "\""); |
219 |
Color c = colorItem.getColor();
|
220 |
b.append(" rgb=\"" + c.getRed() + "," + c.getGreen() + "," + c.getBlue() + "\""); |
221 |
b.append(" interpolated=\"" + colorItem.getInterpolated() + "\""); |
222 |
b.append("/>\n");
|
223 |
} |
224 |
for (int i = 0; i < colorTable.getColorItems().size(); i++) { |
225 |
b.append("\t<Alpha");
|
226 |
ColorItem colorItem = (ColorItem) colorTable.getColorItems().get(i); |
227 |
b.append(" value=\"" + colorItem.getValue() + "\""); |
228 |
Color c = colorItem.getColor();
|
229 |
b.append(" alpha=\"" + c.getAlpha() + "\""); |
230 |
b.append(" interpolated=\"" + colorItem.getInterpolated() + "\""); |
231 |
b.append("/>\n");
|
232 |
} |
233 |
|
234 |
b.append("</" + MAIN_TAG + ">\n"); |
235 |
|
236 |
return b.toString();
|
237 |
} |
238 |
|
239 |
/*
|
240 |
* (non-Javadoc)
|
241 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
|
242 |
*/
|
243 |
public Object getResult() { |
244 |
return colorTable;
|
245 |
} |
246 |
|
247 |
/*
|
248 |
* (non-Javadoc)
|
249 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
|
250 |
*/
|
251 |
public String getMainTag() { |
252 |
return MAIN_TAG;
|
253 |
} |
254 |
} |