Statistics
| Revision:

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

History | View | Annotate | Download (6.88 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.io.IOException;
22
import java.io.Reader;
23
import java.io.StringReader;
24

    
25
import org.gvsig.raster.dataset.IBuffer;
26
import org.gvsig.raster.dataset.io.rmf.ClassSerializer;
27
import org.gvsig.raster.dataset.io.rmf.ParsingException;
28
import org.gvsig.raster.datastruct.ColorTable;
29
import org.kxml2.io.KXmlParser;
30
import org.xmlpull.v1.XmlPullParserException;
31

    
32
/**
33
 * <P>
34
 * Clase para convertir a XML una tabla de color y obtener la tabla desde XML.
35
 * Esta clase implementa el interfaz IRmfBlock con los m?todos de escritura y
36
 * lectura. Estos ser?n utilizados por el gestor de ficheros RMF para escribir y
37
 * leer datos.
38
 * </P>
39
 * <P>
40
 * La estructura XML de una tabla de color es la siguiente:
41
 * </P>
42
 * <P>
43
 *\<ColorTable\> <BR>
44
 *&nbsp;\<Type\>0\</Type\><BR>
45
 *&nbsp;\<Range\>5000:6000 3500:5000 .... 0:-2000\</Range\><BR>
46
 *&nbsp;\<RGB\>-16777216 -16777216 .... -13158401 -16763905\</RGB\><BR>
47
 *&nbsp;\<Names\>clase1 clase2 .... claseN \</Names\><BR>
48
 *\</ColorTable\><BR>
49
 *</P>
50
 *
51
 * @version 23/04/2007
52
 * @author Nacho Brodin (nachobrodin@gmail.com)
53
 *
54
 */
55
public class ColorTableRmfSerializer extends ClassSerializer {
56

    
57
        //TAGS
58
        public static final String MAIN_TAG = "ColorTable";
59
        public static final String TYPE = "Type";
60
        public static final String RGB = "RGB";
61
        public static final String NAMES = "Names";
62
        public static final String RANGE = "Range";
63
        public static final String NAME = "PaletteName";
64

    
65
        private ColorTable  colorTable = null;
66

    
67
        /**
68
         * Constructor. Asigna la tabla a serializar.
69
         * @param ColorTable tabla a convertir en XML
70
         */
71
        public ColorTableRmfSerializer(ColorTable colorTable) {
72
                this.colorTable = colorTable;
73
        }
74

    
75
        /**
76
         * Constructor.
77
         */
78
        public ColorTableRmfSerializer() {
79
        }
80

    
81
        /*
82
         * (non-Javadoc)
83
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
84
         */
85
        public void read(String xml) throws ParsingException {
86
                int type = 0;
87
                String paletteName = "";
88
                int[] rgb = null;
89
                double[] range = null;
90
                String[] names = null;
91

    
92
                KXmlParser parser = new KXmlParser();
93
                Reader reader = new StringReader(xml);
94
                try {
95
                        parser.setInput(reader);
96
                } catch (XmlPullParserException e) {
97
                        throw new ParsingException(xml);
98
                }
99
                try {
100
                        int tag = parser.nextTag();
101

    
102
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
103
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
104
                                while(tag != KXmlParser.END_DOCUMENT) {
105
                                        switch(tag) {
106
                                                case KXmlParser.START_TAG:
107
                                                        if(parser.getName() != null) {
108
                                                                if (parser.getName().compareTo(MAIN_TAG) == 0) {
109
                                                                        type = Integer.parseInt(parserString(parser, TYPE, null));
110
                                                                        paletteName = parserString(parser, NAME, null);
111
                                                                        String rgbList = parserString(parser, RGB, null);
112
                                                                        if(rgbList != null && rgb == null) {
113
                                                                                rgb = convertStringInIntArray(rgbList);
114
                                                                                rgbList = null;
115
                                                                        }
116
                                                                        String nameList = parserString(parser, NAMES, null);
117
                                                                        if(nameList != null && names == null) {
118
                                                                                names = nameList.split(" ");
119
                                                                                nameList = null;
120
                                                                        }
121
                                                                        String rangeList = parserString(parser, RANGE, null);
122
                                                                        if(rangeList != null && range == null) {
123
                                                                                range = convertStringInDoubleArray(rangeList);
124
                                                                                rangeList = null;
125
                                                                        }
126
                                                                }
127
                                                        }
128
                                                        break;
129
                                                case KXmlParser.END_TAG:
130
                                                        break;
131
                                                case KXmlParser.TEXT:
132
                                                        break;
133
                                        }
134
                                        tag = parser.next();
135
                                }
136
                                parser.require(KXmlParser.END_DOCUMENT, null, null);
137
                        }
138

    
139
                } catch (XmlPullParserException e) {
140
                        throw new ParsingException(xml);
141
                } catch (IOException e) {
142
                        throw new ParsingException(xml);
143
                }
144

    
145
                colorTable = new ColorTable(paletteName);
146
                colorTable.setNameClass(names);
147
                colorTable.setColorTable(rgb);
148
                /*
149
                colorTable.setType(type);
150
                if(colorTable.getType() == IBuffer.TYPE_FLOAT || colorTable.getType() == IBuffer.TYPE_DOUBLE)
151
                        colorTable.setDoubleRange(range);
152
                else if(colorTable.getType() == IBuffer.TYPE_BYTE || colorTable.getType() == IBuffer.TYPE_SHORT || colorTable.getType() == IBuffer.TYPE_INT) {
153
                        int[] r = new int[range.length];
154
                        for (int i = 0; i < range.length; i++)
155
                                r[i] = (int)range[i];
156
                        colorTable.setIntRange(r);
157
                }
158
                */
159
        }
160

    
161
        /*
162
         * (non-Javadoc)
163
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
164
         */
165
        public String write() {
166

    
167
                StringBuffer b = new StringBuffer();
168

    
169
                b.append("<" + MAIN_TAG + ">\n");
170

    
171
                /*
172
                putProperty(b, TYPE, colorTable.getType(), 1);
173
                putProperty(b, NAME, colorTable.getName(), 1);
174

175
                b.append("\t<" + RGB + ">");
176
                if(colorTable.getType() == IBuffer.TYPE_BYTE || colorTable.getType() == IBuffer.TYPE_SHORT || colorTable.getType() == IBuffer.TYPE_INT) {
177
                        for (int rgb = 0; rgb < colorTable.getColorTable().length; rgb++)
178
                                b.append(colorTable.getColorTable()[rgb] + " ");
179
                }
180
                if(colorTable.getType() == IBuffer.TYPE_FLOAT || colorTable.getType() == IBuffer.TYPE_DOUBLE) {
181
                        for (int rgb = 0; rgb < colorTable.getColorTable().length; rgb++)
182
                                b.append(colorTable.getColorTable()[rgb] + " ");
183
                }
184
                b.append("</" + RGB + ">\n");
185

186
                b.append("\t<" + NAMES + ">");
187
                for (int rgb = 0; rgb < colorTable.getNameClass().length; rgb++)
188
                        b.append(colorTable.getNameClass()[rgb] + " ");
189
                b.append("</" + NAMES + ">\n");
190

191
                b.append("\t<" + RANGE + ">");
192
                if(colorTable.getType() == IBuffer.TYPE_BYTE || colorTable.getType() == IBuffer.TYPE_SHORT || colorTable.getType() == IBuffer.TYPE_INT) {
193
                        for (int rgb = 0; rgb < colorTable.getIntRange().length; rgb++)
194
                                b.append(colorTable.getIntRange()[rgb] + " ");
195
                }
196
                if(colorTable.getType() == IBuffer.TYPE_FLOAT || colorTable.getType() == IBuffer.TYPE_DOUBLE) {
197
                        for (int rgb = 0; rgb < colorTable.getDoubleRange().length; rgb++)
198
                                b.append(colorTable.getDoubleRange()[rgb] + " ");
199
                }
200
                */
201
                b.append("</" + RANGE + ">\n");
202

    
203
                b.append("</" + MAIN_TAG + ">");
204

    
205
                return b.toString();
206
        }
207

    
208
        /*
209
         * (non-Javadoc)
210
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
211
         */
212
        public Object getResult() {
213
                return colorTable;
214
        }
215

    
216
        /*
217
         *  (non-Javadoc)
218
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
219
         */
220
        public String getMainTag() {
221
                return MAIN_TAG;
222
        }
223
}