Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / datastruct / serializer / ColorTableLibraryPersistence.java @ 12838

History | View | Annotate | Download (11.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 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.File;
23
import java.io.FileInputStream;
24
import java.io.FileNotFoundException;
25
import java.io.FileOutputStream;
26
import java.io.IOException;
27
import java.util.ArrayList;
28

    
29
import org.gvsig.raster.datastruct.ColorItem;
30
import org.gvsig.raster.datastruct.ColorTable;
31
import org.kxml2.io.KXmlParser;
32
import org.kxml2.io.KXmlSerializer;
33
import org.xmlpull.v1.XmlPullParserException;
34
/**
35
 * @version 02/07/2007
36
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
37
 */
38
public class ColorTableLibraryPersistence {
39

    
40
        /**
41
         * Devuelve la lista de ficheros de paletas
42
         * @param palettesBasePath
43
         * @return
44
         */
45
        public static ArrayList getPaletteFileList(String palettesBasePath) {
46
                updateVersion(palettesBasePath);
47

    
48
                File paletteFiles = new File(palettesBasePath);
49

    
50
                if (!paletteFiles.exists())
51
                        return null;
52

    
53
                ArrayList fileList = new ArrayList();
54

    
55
                File[] list = paletteFiles.listFiles();
56
                for (int i = 0; i < list.length; i++)
57
                        fileList.add(list[i].getName());
58

    
59
                return fileList;
60
        }
61

    
62
        /**
63
         * Si existe la version de paleta 1.0, la actualizara a la 1.1 y borrara la
64
         * antigua version.
65
         * @param palettesPath
66
         */
67
        public static void updateVersion_1_0_to_1_1(String palettesBasePath) {
68
                File palettesFile = new File(new File(palettesBasePath).getParent().toString() + File.separator + "palettes.xml");
69
                if (!palettesFile.exists())
70
                        return;
71
                new File(palettesBasePath).mkdir();
72

    
73
                try {
74
                        KXmlParser parser = new KXmlParser();
75
                        FileInputStream fileInputStream = new FileInputStream(palettesFile);
76
                        parser.setInput(fileInputStream, null);
77
                        int tag = parser.nextTag();
78

    
79
                        parser.require(KXmlParser.START_TAG, null, "palettes");
80
                        tag = parser.nextTag();
81
                        parser.require(KXmlParser.START_TAG, null, "palette_list");
82
                        parser.skipSubTree();
83
                        parser.require(KXmlParser.END_TAG, null, "palette_list");
84
                        tag = parser.nextTag();
85

    
86
                        while (tag == KXmlParser.START_TAG) {
87
                                parser.require(KXmlParser.START_TAG, null, "palette");
88
                                if (parser.getAttributeCount() == 2) {
89
                                        // Generar nuevo fichero
90
                                        KXmlSerializer parserOutput = new KXmlSerializer();
91
                                        FileOutputStream fileOutputStream = new FileOutputStream(palettesBasePath + File.separator + parser.getAttributeValue(0) + ".xml");
92

    
93
                                        parserOutput.setOutput(fileOutputStream, null);
94
                                        parserOutput.startDocument("UTF-8", null);
95
                                        parserOutput.startTag(null, "ColorTable");
96
                                        parserOutput.attribute(null, "name", parser.getAttributeValue(0));
97
                                        parserOutput.attribute(null, "version", "1.1");
98

    
99
                                        tag = parser.nextTag();
100
                                        parser.require(KXmlParser.START_TAG, null, "table");
101
                                        tag = parser.nextTag();
102

    
103
                                        parserOutput.text("\n");
104
                                        ArrayList items = new ArrayList();
105
                                        while (tag == KXmlParser.START_TAG) {
106
                                                parser.require(KXmlParser.START_TAG, null, "entry");
107
                                                if (parser.getAttributeCount() == 3) {
108
                                                        String rgb = parser.getAttributeValue(1).substring(parser.getAttributeValue(1).indexOf(",") + 1, parser.getAttributeValue(1).length());
109

    
110
                                                        int a = Integer.valueOf(parser.getAttributeValue(1).substring(0, parser.getAttributeValue(1).indexOf(","))).intValue();
111
                                                        int r = Integer.valueOf(rgb.substring(0, rgb.indexOf(","))).intValue();
112
                                                        int g = Integer.valueOf(rgb.substring(rgb.indexOf(",") + 1, rgb.lastIndexOf(","))).intValue();
113
                                                        int b = Integer.valueOf(rgb.substring(rgb.lastIndexOf(",") + 1, rgb.length())).intValue();
114

    
115
                                                        ColorItem colorItem = new ColorItem();
116
                                                        colorItem.setColor(new Color(r, g, b, a));
117
                                                        colorItem.setInterpolated(50);
118
                                                        colorItem.setNameClass(parser.getAttributeValue(0));
119
                                                        colorItem.setValue(Double.parseDouble(parser.getAttributeValue(2)));
120
                                                        items.add(colorItem);
121
                                                }
122
                                                tag = parser.nextTag();
123
                                                parser.require(KXmlParser.END_TAG, null, "entry");
124
                                                tag = parser.nextTag();
125
                                        }
126
                                        parser.require(KXmlParser.END_TAG, null, "table");
127
                                        tag = parser.nextTag();
128

    
129
                                        ColorTable colorTable = new ColorTable();
130
                                        colorTable.createPaletteFromColorItems(items, true);
131
                                        items = colorTable.getColorItems();
132
                                        for (int i = 0; i < items.size(); i++) {
133
                                                ColorItem colorItem = (ColorItem) items.get(i);
134
                                                parserOutput.startTag(null, "Color");
135
                                                parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue()));
136
                                                parserOutput.attribute(null, "name", String.valueOf(colorItem.getNameClass()));
137
                                                Color color = colorItem.getColor();
138
                                                parserOutput.attribute(null, "rgb", String.valueOf(color.getRed() + "," + color.getGreen() + "," + color.getBlue()));
139
                                                parserOutput.attribute(null, "interpolated", String.valueOf(colorItem.getInterpolated()));
140
                                                parserOutput.endTag(null, "Color");
141
                                                parserOutput.text("\n");
142
                                        }
143

    
144
                                        for (int i = 0; i < items.size(); i++) {
145
                                                ColorItem colorItem = (ColorItem) items.get(i);
146
                                                parserOutput.startTag(null, "Alpha");
147
                                                parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue()));
148
                                                parserOutput.attribute(null, "alpha", String.valueOf(colorItem.getColor().getAlpha()));
149
                                                parserOutput.attribute(null, "interpolated", String.valueOf(colorItem.getInterpolated()));
150
                                                parserOutput.endTag(null, "Alpha");
151
                                                parserOutput.text("\n");
152
                                        }
153

    
154
                                        parserOutput.endTag(null, "ColorTable");
155
                                        parserOutput.text("\n");
156
                                        parserOutput.endDocument();
157
                                        // Cerrar nuevo fichero
158
                                        fileOutputStream.close();
159
                                }
160
                                parser.require(KXmlParser.END_TAG, null, "palette");
161
                                tag = parser.nextTag();
162
                        }
163
                        parser.require(KXmlParser.END_TAG, null, "palettes");
164

    
165
                        fileInputStream.close();
166

    
167
                        // Cambiar nombre a antiguo fichero
168
                        palettesFile.renameTo(new File(palettesBasePath + File.separator + "palettes.xml~"));
169
                        return;
170

    
171
                } catch (FileNotFoundException fnfEx) {
172
                        fnfEx.printStackTrace();
173
                } catch (XmlPullParserException xmlEx) {
174
                        System.out.println("El fichero de paletas predeterminadas no tiene la estructura correcta:\n        " + xmlEx.getMessage());
175
                } catch (IOException e) {
176
                }
177
        }
178

    
179
        /**
180
         * Invocar? todos los metodos de actualizaciones de version
181
         * @param palettesBasePath
182
         */
183
        public static void updateVersion(String palettesBasePath) {
184
                updateVersion_1_0_to_1_1(palettesBasePath);
185
                //updateVersion_1_1_to_1_2(palettesBasePath);
186
        }
187

    
188
        /**
189
         * Devuelve el color si lo encuentra en el arraylist y lo elimina, en caso
190
         * contrario devuelve null
191
         * @param list
192
         * @param value
193
         * @return
194
         */
195
        private static ColorItem getColorItem(ArrayList list, double value) {
196
                for (int i = 0; i < list.size(); i++) {
197
                        if (((ColorItem) list.get(i)).getValue() == value) {
198
                                return (ColorItem) list.remove(i);
199
                        }
200
                }
201
                return null;
202
        }
203

    
204
        /**
205
         * Lee una paleta del fichero xml de paletas y la carga en la tabla del panel.
206
         * @param palettesPath Camino al fichero de paletas predefinidas.
207
         * @param paletteName Nombre de paleta a cargar desde el fichero xml.
208
         */
209
        public static String loadPalette(String palettesBasePath, String paletteFileName, ArrayList items) {
210
                updateVersion(palettesBasePath);
211

    
212
                items.clear();
213

    
214
                File palettesFile = new File(palettesBasePath + File.separator + paletteFileName);
215
                if (!palettesFile.exists())
216
                        return null;
217

    
218
                try {
219
                        String paletteName = "";
220
                        ArrayList rows = new ArrayList();
221

    
222
                        KXmlParser parser = new KXmlParser();
223
                        FileInputStream fileInputStream = new FileInputStream(palettesBasePath + File.separator + paletteFileName);
224
                        parser.setInput(fileInputStream, null);
225
                        int tag = parser.nextTag();
226

    
227
                        parser.require(KXmlParser.START_TAG, null, "ColorTable");
228
                        for (int i = 0; i < parser.getAttributeCount(); i++) {
229
                                if (parser.getAttributeName(i).equals("name")) {
230
                                        paletteName = parser.getAttributeValue(i);
231
                                }
232
                        }
233
                        tag = parser.nextTag();
234

    
235
                        while (!((tag == KXmlParser.END_TAG) && (parser.getName().equals("ColorTable")))) {
236
                                try {
237
                                        if (tag == KXmlParser.START_TAG) {
238
                                                if (parser.getName().equals("Color")) {
239
                                                        ColorItem colorItem = new ColorItem();
240
                                                        int a = 255;
241
                                                        for (int i = 0; i < parser.getAttributeCount(); i++) {
242
                                                                if (parser.getAttributeName(i).equals("value")) {
243
                                                                        colorItem.setValue(Double.parseDouble((String) parser.getAttributeValue(i)));
244
                                                                        ColorItem aux = getColorItem(rows, Double.parseDouble((String) parser.getAttributeValue(i)));
245
                                                                        if (aux != null)
246
                                                                                a = aux.getColor().getAlpha();
247
                                                                }
248
                                                                if (parser.getAttributeName(i).equals("name")) {
249
                                                                        colorItem.setNameClass((String) parser.getAttributeValue(i));
250
                                                                }
251
                                                                if (parser.getAttributeName(i).equals("rgb")) {
252
                                                                        String rgb = parser.getAttributeValue(i);
253
                                                                        int r = Integer.valueOf(rgb.substring(0, rgb.indexOf(","))).intValue();
254
                                                                        int g = Integer.valueOf(rgb.substring(rgb.indexOf(",") + 1, rgb.lastIndexOf(","))).intValue();
255
                                                                        int b = Integer.valueOf(rgb.substring(rgb.lastIndexOf(",") + 1, rgb.length())).intValue();
256

    
257
                                                                        colorItem.setColor(new Color(r, g, b, a));
258
                                                                }
259
                                                                if (parser.getAttributeName(i).equals("interpolated")) {
260
                                                                        colorItem.setInterpolated(Integer.parseInt((String) parser.getAttributeValue(i)));
261
                                                                }
262
                                                        }
263

    
264
                                                        rows.add(colorItem);
265
                                                        continue;
266
                                                }
267
                                                if (parser.getName().equals("Alpha")) {
268
                                                        ColorItem colorItem = new ColorItem();
269
                                                        for (int i = 0; i < parser.getAttributeCount(); i++) {
270
                                                                if (parser.getAttributeName(i).equals("value")) {
271
                                                                        colorItem.setValue(Double.parseDouble((String) parser.getAttributeValue(i)));
272
                                                                        ColorItem aux = getColorItem(rows, Double.parseDouble((String) parser.getAttributeValue(i)));
273
                                                                        if (aux != null) {
274
                                                                                colorItem.setNameClass(aux.getNameClass());
275
                                                                                colorItem.setInterpolated(aux.getInterpolated());
276
                                                                                colorItem.setColor(new Color(aux.getColor().getRed(), aux.getColor().getGreen(), aux.getColor().getBlue(), colorItem.getColor().getAlpha()));
277
                                                                        }
278
                                                                }
279
                                                                if (parser.getAttributeName(i).equals("alpha")) {
280
                                                                        int a = Integer.parseInt(parser.getAttributeValue(i));
281

    
282
                                                                        colorItem.setColor(new Color(colorItem.getColor().getRed(), colorItem.getColor().getGreen(), colorItem.getColor().getBlue(), a));
283
                                                                }
284
                                                                if (parser.getAttributeName(i).equals("interpolated")) {
285
                                                                        colorItem.setInterpolated(Integer.parseInt((String) parser.getAttributeValue(i)));
286
                                                                }
287
                                                        }
288

    
289
                                                        rows.add(colorItem);
290
                                                        continue;
291
                                                }
292
                                        }
293
                                } finally {
294
                                        tag = parser.nextTag();
295
                                }
296
                        }
297

    
298
                        for (int i = 0; i < rows.size(); i++)
299
                                items.add(rows.get(i));
300

    
301
                        fileInputStream.close();
302
                        return paletteName;
303
                } catch (FileNotFoundException fnfEx) {
304
                        fnfEx.printStackTrace();
305
                } catch (XmlPullParserException xmlEx) {
306
                        System.out.println("El fichero de paletas predeterminadas no tiene la estructura correcta:\n        " + xmlEx.getMessage());
307
                } catch (IOException e) {
308
                }
309
                return null;
310
        }
311
}