Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / datastruct / serializer / ColorTableRmfSerializer.java @ 20866

History | View | Annotate | Download (8.31 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
 * &lt;ColorTable name="nombre" version="1.1"><BR>
45
 * &nbsp;&lt;Color value="0.0" name="rojo" rgb="#FF34FF" interpolated="50"/><BR>
46
 * &nbsp;&lt;Color value="1.0" name="rojo" rgb="#FF34FF" interpolated="50"/><BR>
47
 * &nbsp;&lt;Alpha value="0.0" number="255" interpolated="50"/><BR>
48
 * &nbsp;&lt;NoData rgba="#"><BR>
49
 * &lt;/ColorTable><BR>
50
 *
51
 * @version 23/04/2007
52
 * @author Nacho Brodin (nachobrodin@gmail.com)
53
 */
54
public class ColorTableRmfSerializer extends ClassSerializer {
55
        private final String MAIN_TAG = "ColorTable";
56

    
57
        private ColorTable  colorTable = null;
58

    
59
        /**
60
         * Constructor. Asigna la tabla a serializar.
61
         * @param ColorTable tabla a convertir en XML
62
         */
63
        public ColorTableRmfSerializer(ColorTable colorTable) {
64
                this.colorTable = colorTable;
65
        }
66

    
67
        /**
68
         * Constructor.
69
         */
70
        public ColorTableRmfSerializer() {
71
        }
72

    
73
        /**
74
         * Devuelve el color si lo encuentra en el arraylist y lo elimina, en caso
75
         * contrario devuelve null
76
         * @param list
77
         * @param value
78
         * @return
79
         */
80
        private static ColorItem getColorItem(ArrayList list, double value) {
81
                for (int i = 0; i < list.size(); i++) {
82
                        if (((ColorItem) list.get(i)).getValue() == value) {
83
                                return (ColorItem) list.remove(i);
84
                        }
85
                }
86
                return null;
87
        }
88

    
89
        /*
90
         * (non-Javadoc)
91
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
92
         */
93
        public void read(String xml) throws ParsingException {
94
                String paletteName = "";
95
                boolean interpolated = true;
96

    
97
                KXmlParser parser = new KXmlParser();
98
                Reader reader = new StringReader(xml);
99
                try {
100
                        parser.setInput(reader);
101
                } catch (XmlPullParserException e) {
102
                        throw new ParsingException(xml);
103
                }
104

    
105
                try {
106
                        ArrayList rows = new ArrayList();
107

    
108
                        int tag = parser.nextTag();
109

    
110
                        if (parser.getEventType() != KXmlParser.END_DOCUMENT) {
111
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
112

    
113
                                for (int i = 0; i < parser.getAttributeCount(); i++) {
114
                                        if (parser.getAttributeName(i).equals("name"))
115
                                                paletteName = parser.getAttributeValue(i);
116
                                        if (parser.getAttributeName(i).equals("interpolated"))
117
                                                interpolated = parser.getAttributeValue(i).equals("1");
118
                                }
119

    
120
                                while (tag != KXmlParser.END_DOCUMENT) {
121
                                        switch (tag) {
122
                                                case KXmlParser.START_TAG:
123
                                                        if (parser.getName().equals("Color")) {
124
                                                                ColorItem colorItem = new ColorItem();
125
                                                                int a = 255;
126
                                                                for (int i = 0; i < parser.getAttributeCount(); i++) {
127
                                                                        if (parser.getAttributeName(i).equals("value")) {
128
                                                                                colorItem.setValue(Double.parseDouble((String) parser.getAttributeValue(i)));
129
                                                                                ColorItem aux = getColorItem(rows, Double.parseDouble((String) parser.getAttributeValue(i)));
130
                                                                                if (aux != null)
131
                                                                                        a = aux.getColor().getAlpha();
132
                                                                        }
133
                                                                        if (parser.getAttributeName(i).equals("name")) {
134
                                                                                colorItem.setNameClass((String) parser.getAttributeValue(i));
135
                                                                        }
136
                                                                        if (parser.getAttributeName(i).equals("rgb")) {
137
                                                                                String rgb = parser.getAttributeValue(i);
138
                                                                                int r = Integer.valueOf(rgb.substring(0, rgb.indexOf(","))).intValue();
139
                                                                                int g = Integer.valueOf(rgb.substring(rgb.indexOf(",") + 1, rgb.lastIndexOf(","))).intValue();
140
                                                                                int b = Integer.valueOf(rgb.substring(rgb.lastIndexOf(",") + 1, rgb.length())).intValue();
141

    
142
                                                                                colorItem.setColor(new Color(r, g, b, a));
143
                                                                        }
144
                                                                        if (parser.getAttributeName(i).equals("interpolated")) {
145
                                                                                colorItem.setInterpolated(Double.parseDouble((String) parser.getAttributeValue(i)));
146
                                                                        }
147
                                                                }
148

    
149
                                                                rows.add(colorItem);
150
                                                                break;
151
                                                        }
152
                                                        if (parser.getName().equals("Alpha")) {
153
                                                                ColorItem colorItem = new ColorItem();
154
                                                                for (int i = 0; i < parser.getAttributeCount(); i++) {
155
                                                                        if (parser.getAttributeName(i).equals("value")) {
156
                                                                                colorItem.setValue(Double.parseDouble((String) parser.getAttributeValue(i)));
157
                                                                                ColorItem aux = getColorItem(rows, Double.parseDouble((String) parser.getAttributeValue(i)));
158
                                                                                if (aux != null) {
159
                                                                                        colorItem.setNameClass(aux.getNameClass());
160
                                                                                        colorItem.setInterpolated(aux.getInterpolated());
161
                                                                                        colorItem.setColor(new Color(aux.getColor().getRed(), aux.getColor().getGreen(), aux.getColor().getBlue(), colorItem.getColor().getAlpha()));
162
                                                                                }
163
                                                                        }
164
                                                                        if (parser.getAttributeName(i).equals("alpha")) {
165
                                                                                int a = Integer.parseInt(parser.getAttributeValue(i));
166

    
167
                                                                                colorItem.setColor(new Color(colorItem.getColor().getRed(), colorItem.getColor().getGreen(), colorItem.getColor().getBlue(), a));
168
                                                                        }
169
                                                                        if (parser.getAttributeName(i).equals("interpolated")) {
170
                                                                                colorItem.setInterpolated(Double.parseDouble((String) parser.getAttributeValue(i)));
171
                                                                        }
172
                                                                }
173

    
174
                                                                rows.add(colorItem);
175
                                                                break;
176
                                                        }
177
                                                        break;
178
                                        }
179

    
180
                                        tag = parser.next();
181
                                }
182
                                parser.require(KXmlParser.END_DOCUMENT, null, null);
183
                        }
184

    
185
                        colorTable = new ColorTable();
186
                        colorTable.setName(paletteName);
187
                        colorTable.createPaletteFromColorItems(rows, false);
188
                        colorTable.setInterpolated(interpolated);
189
                } catch (XmlPullParserException e) {
190
                        throw new ParsingException(xml);
191
                } catch (IOException e) {
192
                        throw new ParsingException(xml);
193
                }
194
        }
195

    
196
        /*
197
         * (non-Javadoc)
198
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
199
         */
200
        public String write() {
201

    
202
                StringBuffer b = new StringBuffer();
203

    
204
                if (colorTable == null)
205
                        return "";
206

    
207
                b.append("<" + MAIN_TAG + " name=\"" + colorTable.getName() + "\" interpolated=\"" + (colorTable.isInterpolated()?"1":"0") + "\" version=\"1.1\">\n");
208

    
209
                for (int i = 0; i < colorTable.getColorItems().size(); i++) {
210
                        b.append("\t<Color");
211
                        ColorItem colorItem = (ColorItem) colorTable.getColorItems().get(i);
212
                        b.append(" value=\"" + colorItem.getValue() + "\"");
213
                        b.append(" name=\"" + colorItem.getNameClass() + "\"");
214
                        Color c = colorItem.getColor();
215
                        b.append(" rgb=\"" + c.getRed() + "," + c.getGreen() + "," + c.getBlue() + "\"");
216
                        b.append(" interpolated=\"" + colorItem.getInterpolated() + "\"");
217
                        b.append("/>\n");
218
                }
219
                for (int i = 0; i < colorTable.getColorItems().size(); i++) {
220
                        b.append("\t<Alpha");
221
                        ColorItem colorItem = (ColorItem) colorTable.getColorItems().get(i);
222
                        b.append(" value=\"" + colorItem.getValue() + "\"");
223
                        Color c = colorItem.getColor();
224
                        b.append(" alpha=\"" + c.getAlpha() + "\"");
225
                        b.append(" interpolated=\"" + colorItem.getInterpolated() + "\"");
226
                        b.append("/>\n");
227
                }
228

    
229
                b.append("</" + MAIN_TAG + ">\n");
230

    
231
                return b.toString();
232
        }
233

    
234
        /*
235
         * (non-Javadoc)
236
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
237
         */
238
        public Object getResult() {
239
                return colorTable;
240
        }
241

    
242
        /*
243
         *  (non-Javadoc)
244
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
245
         */
246
        public String getMainTag() {
247
                return MAIN_TAG;
248
        }
249
}