root / trunk / libraries / libRaster / src / org / gvsig / raster / datastruct / serializer / ColorTableLibraryPersistence.java @ 13775
History | View | Annotate | Download (17.3 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.io.InputStream; |
28 |
import java.io.StringReader; |
29 |
import java.net.URL; |
30 |
import java.util.ArrayList; |
31 |
|
32 |
import org.gvsig.raster.datastruct.ColorItem; |
33 |
import org.gvsig.raster.datastruct.ColorTable; |
34 |
import org.kxml2.io.KXmlParser; |
35 |
import org.kxml2.io.KXmlSerializer; |
36 |
import org.xmlpull.v1.XmlPullParserException; |
37 |
/**
|
38 |
* @version 02/07/2007
|
39 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
40 |
*/
|
41 |
public class ColorTableLibraryPersistence { |
42 |
|
43 |
/**
|
44 |
* Devuelve la lista de ficheros de paletas, si no existe el fichero devolvera
|
45 |
* una lista vacia
|
46 |
* @param palettesBasePath
|
47 |
* @return
|
48 |
*/
|
49 |
private static ArrayList getPaletteFileListDisc(String palettesBasePath) { |
50 |
updateVersion(palettesBasePath); |
51 |
|
52 |
File paletteFiles = new File(palettesBasePath); |
53 |
|
54 |
ArrayList fileList = new ArrayList(); |
55 |
|
56 |
if (paletteFiles.exists()) {
|
57 |
File[] list = paletteFiles.listFiles(); |
58 |
for (int i = 0; i < list.length; i++) |
59 |
fileList.add(list[i].getName()); |
60 |
} |
61 |
|
62 |
return fileList;
|
63 |
} |
64 |
|
65 |
/**
|
66 |
* Devuelve la lista de ficheros de paletas, si no existe el fichero creara
|
67 |
* una paleta por defecto y la devolvera.
|
68 |
* @param palettesBasePath
|
69 |
* @return
|
70 |
*/
|
71 |
public static ArrayList getPaletteFileList(String palettesBasePath) { |
72 |
return getPaletteFileList(palettesBasePath, true); |
73 |
} |
74 |
|
75 |
/**
|
76 |
* Devuelve la lista de ficheros de paletas, en caso de que no exista la
|
77 |
* paleta y se especifique forceCreate a true, se creara la paleta por defecto.
|
78 |
* @param palettesBasePath
|
79 |
* @param forceCreate
|
80 |
* @return
|
81 |
*/
|
82 |
public static ArrayList getPaletteFileList(String palettesBasePath, boolean forceCreate) { |
83 |
ArrayList fileList = getPaletteFileListDisc(palettesBasePath);
|
84 |
|
85 |
if (forceCreate && (fileList.size() == 0)) { |
86 |
ColorTableLibraryPersistence persistence = new ColorTableLibraryPersistence();
|
87 |
createVersionFromXML(palettesBasePath, persistence.getDefaultPaletteXML()); |
88 |
fileList = getPaletteFileListDisc(palettesBasePath); |
89 |
} |
90 |
|
91 |
return fileList;
|
92 |
} |
93 |
|
94 |
/**
|
95 |
* Devuelve el XML de una paleta por defecto
|
96 |
* @return
|
97 |
*/
|
98 |
public String getDefaultPaletteXML() { |
99 |
URL url = getClass().getResource("xml/palettes.xml"); |
100 |
StringBuffer contents = new StringBuffer(); |
101 |
try {
|
102 |
InputStream inputStream = url.openStream();
|
103 |
|
104 |
int i;
|
105 |
while (true) { |
106 |
i = inputStream.read(); |
107 |
if (i == -1) break; |
108 |
char c = (char) i; |
109 |
contents.append(c); |
110 |
} |
111 |
inputStream.close(); |
112 |
} catch (IOException e) { |
113 |
e.printStackTrace(); |
114 |
} |
115 |
|
116 |
return contents.toString();
|
117 |
} |
118 |
|
119 |
/**
|
120 |
* Crea los ficheros que forman la paleta de color de la version 1.1 a traves
|
121 |
* de un XML que se le pasa por parametro
|
122 |
* @param palettesPath
|
123 |
*/
|
124 |
private static void createVersionFromXML(String palettesBasePath, String xml) { |
125 |
new File(palettesBasePath).mkdir(); |
126 |
KXmlParser parser = new KXmlParser();
|
127 |
StringReader reader = new StringReader(xml); |
128 |
try {
|
129 |
parser.setInput(reader); |
130 |
int tag = parser.nextTag();
|
131 |
|
132 |
parser.require(KXmlParser.START_TAG, null, "palettes"); |
133 |
tag = parser.nextTag(); |
134 |
parser.require(KXmlParser.START_TAG, null, "palette_list"); |
135 |
parser.skipSubTree(); |
136 |
parser.require(KXmlParser.END_TAG, null, "palette_list"); |
137 |
tag = parser.nextTag(); |
138 |
|
139 |
while (tag == KXmlParser.START_TAG) {
|
140 |
parser.require(KXmlParser.START_TAG, null, "palette"); |
141 |
if (parser.getAttributeCount() == 2) { |
142 |
// Generar nuevo fichero
|
143 |
KXmlSerializer parserOutput = new KXmlSerializer();
|
144 |
FileOutputStream fileOutputStream = new FileOutputStream(palettesBasePath + File.separator + parser.getAttributeValue(0) + ".xml"); |
145 |
|
146 |
parserOutput.setOutput(fileOutputStream, null);
|
147 |
parserOutput.startDocument("UTF-8", null); |
148 |
parserOutput.startTag(null, "ColorTable"); |
149 |
parserOutput.attribute(null, "name", parser.getAttributeValue(0)); |
150 |
parserOutput.attribute(null, "version", "1.1"); |
151 |
|
152 |
tag = parser.nextTag(); |
153 |
parser.require(KXmlParser.START_TAG, null, "table"); |
154 |
tag = parser.nextTag(); |
155 |
|
156 |
parserOutput.text("\n");
|
157 |
ArrayList items = new ArrayList(); |
158 |
while (tag == KXmlParser.START_TAG) {
|
159 |
parser.require(KXmlParser.START_TAG, null, "entry"); |
160 |
if (parser.getAttributeCount() == 3) { |
161 |
String rgb = parser.getAttributeValue(1).substring(parser.getAttributeValue(1).indexOf(",") + 1, parser.getAttributeValue(1).length()); |
162 |
|
163 |
int a = Integer.valueOf(parser.getAttributeValue(1).substring(0, parser.getAttributeValue(1).indexOf(","))).intValue(); |
164 |
int r = Integer.valueOf(rgb.substring(0, rgb.indexOf(","))).intValue(); |
165 |
int g = Integer.valueOf(rgb.substring(rgb.indexOf(",") + 1, rgb.lastIndexOf(","))).intValue(); |
166 |
int b = Integer.valueOf(rgb.substring(rgb.lastIndexOf(",") + 1, rgb.length())).intValue(); |
167 |
|
168 |
ColorItem colorItem = new ColorItem();
|
169 |
colorItem.setColor(new Color(r, g, b, a)); |
170 |
colorItem.setInterpolated(50);
|
171 |
colorItem.setNameClass(parser.getAttributeValue(0));
|
172 |
colorItem.setValue(Double.parseDouble(parser.getAttributeValue(2))); |
173 |
items.add(colorItem); |
174 |
} |
175 |
tag = parser.nextTag(); |
176 |
parser.require(KXmlParser.END_TAG, null, "entry"); |
177 |
tag = parser.nextTag(); |
178 |
} |
179 |
parser.require(KXmlParser.END_TAG, null, "table"); |
180 |
tag = parser.nextTag(); |
181 |
|
182 |
ColorTable colorTable = new ColorTable();
|
183 |
colorTable.createPaletteFromColorItems(items, true);
|
184 |
items = colorTable.getColorItems(); |
185 |
for (int i = 0; i < items.size(); i++) { |
186 |
ColorItem colorItem = (ColorItem) items.get(i); |
187 |
parserOutput.startTag(null, "Color"); |
188 |
parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue())); |
189 |
parserOutput.attribute(null, "name", String.valueOf(colorItem.getNameClass())); |
190 |
Color color = colorItem.getColor();
|
191 |
parserOutput.attribute(null, "rgb", String.valueOf(color.getRed() + "," + color.getGreen() + "," + color.getBlue())); |
192 |
parserOutput.attribute(null, "interpolated", String.valueOf(colorItem.getInterpolated())); |
193 |
parserOutput.endTag(null, "Color"); |
194 |
parserOutput.text("\n");
|
195 |
} |
196 |
|
197 |
for (int i = 0; i < items.size(); i++) { |
198 |
ColorItem colorItem = (ColorItem) items.get(i); |
199 |
parserOutput.startTag(null, "Alpha"); |
200 |
parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue())); |
201 |
parserOutput.attribute(null, "alpha", String.valueOf(colorItem.getColor().getAlpha())); |
202 |
parserOutput.attribute(null, "interpolated", String.valueOf(colorItem.getInterpolated())); |
203 |
parserOutput.endTag(null, "Alpha"); |
204 |
parserOutput.text("\n");
|
205 |
} |
206 |
|
207 |
parserOutput.endTag(null, "ColorTable"); |
208 |
parserOutput.text("\n");
|
209 |
parserOutput.endDocument(); |
210 |
// Cerrar nuevo fichero
|
211 |
fileOutputStream.close(); |
212 |
} |
213 |
parser.require(KXmlParser.END_TAG, null, "palette"); |
214 |
tag = parser.nextTag(); |
215 |
} |
216 |
parser.require(KXmlParser.END_TAG, null, "palettes"); |
217 |
} catch (XmlPullParserException xmlEx) {
|
218 |
System.out.println("El fichero de paletas predeterminadas no tiene la estructura correcta:\n " + xmlEx.getMessage()); |
219 |
} catch (IOException e) { |
220 |
} |
221 |
} |
222 |
|
223 |
/**
|
224 |
* Si existe la version de paleta 1.0, la actualizara a la 1.1 y renombrar? la
|
225 |
* antigua version.
|
226 |
* @param palettesPath
|
227 |
*/
|
228 |
public static void updateVersion_1_0_to_1_1(String palettesBasePath) { |
229 |
File palettesFile = new File(new File(palettesBasePath).getParent().toString() + File.separator + "palettes.xml"); |
230 |
if (!palettesFile.exists())
|
231 |
return;
|
232 |
|
233 |
try {
|
234 |
FileInputStream inputStream = new FileInputStream(palettesFile); |
235 |
|
236 |
StringBuffer contents = new StringBuffer(); |
237 |
try {
|
238 |
|
239 |
int i;
|
240 |
while (true) { |
241 |
i = inputStream.read(); |
242 |
if (i == -1) break; |
243 |
char c = (char) i; |
244 |
contents.append(c); |
245 |
} |
246 |
} catch (IOException e) { |
247 |
e.printStackTrace(); |
248 |
} |
249 |
|
250 |
createVersionFromXML(palettesBasePath, contents.toString()); |
251 |
|
252 |
// Cambiar nombre a antiguo fichero
|
253 |
palettesFile.renameTo(new File(new File(palettesBasePath).getParent().toString() + File.separator + "palettes.xml~")); |
254 |
inputStream.close(); |
255 |
} catch (FileNotFoundException fnfEx) { |
256 |
fnfEx.printStackTrace(); |
257 |
} catch (IOException e) { |
258 |
e.printStackTrace(); |
259 |
} |
260 |
} |
261 |
|
262 |
/**
|
263 |
* Si existe la version de paleta 1.0, la actualizara a la 1.1 y renombrar? la
|
264 |
* antigua version.
|
265 |
* @param palettesBasePath
|
266 |
* @param colorTable
|
267 |
*/
|
268 |
public static void save_to_1_1(String palettesBasePath, ColorTable colorTable) { |
269 |
try {
|
270 |
// Generar nuevo fichero
|
271 |
KXmlSerializer parserOutput = new KXmlSerializer();
|
272 |
FileOutputStream fileOutputStream;
|
273 |
fileOutputStream = new FileOutputStream(palettesBasePath + File.separator + colorTable.getName() + ".xml"); |
274 |
parserOutput.setOutput(fileOutputStream, null);
|
275 |
parserOutput.startDocument("UTF-8", null); |
276 |
parserOutput.startTag(null, "ColorTable"); |
277 |
parserOutput.attribute(null, "name", colorTable.getName()); |
278 |
parserOutput.attribute(null, "version", "1.1"); |
279 |
parserOutput.text("\n");
|
280 |
|
281 |
ArrayList items = colorTable.getColorItems();
|
282 |
for (int i = 0; i < items.size(); i++) { |
283 |
ColorItem colorItem = (ColorItem) items.get(i); |
284 |
parserOutput.startTag(null, "Color"); |
285 |
parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue())); |
286 |
parserOutput.attribute(null, "name", String.valueOf(colorItem.getNameClass())); |
287 |
Color color = colorItem.getColor();
|
288 |
parserOutput.attribute(null, "rgb", String.valueOf(color.getRed() + "," + color.getGreen() + "," + color.getBlue())); |
289 |
parserOutput.attribute(null, "interpolated", String.valueOf(colorItem.getInterpolated())); |
290 |
parserOutput.endTag(null, "Color"); |
291 |
parserOutput.text("\n");
|
292 |
} |
293 |
|
294 |
for (int i = 0; i < items.size(); i++) { |
295 |
ColorItem colorItem = (ColorItem) items.get(i); |
296 |
parserOutput.startTag(null, "Alpha"); |
297 |
parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue())); |
298 |
parserOutput.attribute(null, "alpha", String.valueOf(colorItem.getColor().getAlpha())); |
299 |
parserOutput.attribute(null, "interpolated", String.valueOf(colorItem.getInterpolated())); |
300 |
parserOutput.endTag(null, "Alpha"); |
301 |
parserOutput.text("\n");
|
302 |
} |
303 |
|
304 |
parserOutput.endTag(null, "ColorTable"); |
305 |
parserOutput.text("\n");
|
306 |
parserOutput.endDocument(); |
307 |
// Cerrar nuevo fichero
|
308 |
fileOutputStream.close(); |
309 |
} catch (FileNotFoundException e) { |
310 |
e.printStackTrace(); |
311 |
} catch (IOException e) { |
312 |
e.printStackTrace(); |
313 |
} |
314 |
} |
315 |
|
316 |
/**
|
317 |
* Invocar? todos los metodos de actualizaciones de version
|
318 |
* @param palettesBasePath
|
319 |
*/
|
320 |
public static void updateVersion(String palettesBasePath) { |
321 |
updateVersion_1_0_to_1_1(palettesBasePath); |
322 |
//updateVersion_1_1_to_1_2(palettesBasePath);
|
323 |
} |
324 |
|
325 |
/**
|
326 |
* Devuelve el color si lo encuentra en el arraylist y lo elimina, en caso
|
327 |
* contrario devuelve null
|
328 |
* @param list
|
329 |
* @param value
|
330 |
* @return
|
331 |
*/
|
332 |
private static ColorItem getColorItem(ArrayList list, double value) { |
333 |
for (int i = 0; i < list.size(); i++) { |
334 |
if (((ColorItem) list.get(i)).getValue() == value) {
|
335 |
return (ColorItem) list.remove(i);
|
336 |
} |
337 |
} |
338 |
return null; |
339 |
} |
340 |
|
341 |
/**
|
342 |
* Lee una paleta del fichero xml de paletas y la carga en la tabla del panel.
|
343 |
* @param palettesPath Camino al fichero de paletas predefinidas.
|
344 |
* @param paletteName Nombre de paleta a cargar desde el fichero xml.
|
345 |
* @return Nombre de la paleta
|
346 |
*/
|
347 |
public static String loadPalette(String palettesBasePath, String paletteFileName, ArrayList items) { |
348 |
updateVersion(palettesBasePath); |
349 |
|
350 |
items.clear(); |
351 |
|
352 |
File palettesFile = new File(palettesBasePath + File.separator + paletteFileName); |
353 |
if (!palettesFile.exists())
|
354 |
return null; |
355 |
|
356 |
try {
|
357 |
String paletteName = ""; |
358 |
ArrayList rows = new ArrayList(); |
359 |
|
360 |
KXmlParser parser = new KXmlParser();
|
361 |
FileInputStream fileInputStream = new FileInputStream(palettesBasePath + File.separator + paletteFileName); |
362 |
parser.setInput(fileInputStream, null);
|
363 |
int tag = parser.nextTag();
|
364 |
|
365 |
parser.require(KXmlParser.START_TAG, null, "ColorTable"); |
366 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
367 |
if (parser.getAttributeName(i).equals("name")) { |
368 |
paletteName = parser.getAttributeValue(i); |
369 |
} |
370 |
} |
371 |
tag = parser.nextTag(); |
372 |
|
373 |
while (!((tag == KXmlParser.END_TAG) && (parser.getName().equals("ColorTable")))) { |
374 |
try {
|
375 |
if (tag == KXmlParser.START_TAG) {
|
376 |
if (parser.getName().equals("Color")) { |
377 |
ColorItem colorItem = new ColorItem();
|
378 |
int a = 255; |
379 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
380 |
if (parser.getAttributeName(i).equals("value")) { |
381 |
colorItem.setValue(Double.parseDouble((String) parser.getAttributeValue(i))); |
382 |
ColorItem aux = getColorItem(rows, Double.parseDouble((String) parser.getAttributeValue(i))); |
383 |
if (aux != null) |
384 |
a = aux.getColor().getAlpha(); |
385 |
} |
386 |
if (parser.getAttributeName(i).equals("name")) { |
387 |
colorItem.setNameClass((String) parser.getAttributeValue(i));
|
388 |
} |
389 |
if (parser.getAttributeName(i).equals("rgb")) { |
390 |
String rgb = parser.getAttributeValue(i);
|
391 |
int r = Integer.valueOf(rgb.substring(0, rgb.indexOf(","))).intValue(); |
392 |
int g = Integer.valueOf(rgb.substring(rgb.indexOf(",") + 1, rgb.lastIndexOf(","))).intValue(); |
393 |
int b = Integer.valueOf(rgb.substring(rgb.lastIndexOf(",") + 1, rgb.length())).intValue(); |
394 |
|
395 |
colorItem.setColor(new Color(r, g, b, a)); |
396 |
} |
397 |
if (parser.getAttributeName(i).equals("interpolated")) { |
398 |
colorItem.setInterpolated(Double.parseDouble((String) parser.getAttributeValue(i))); |
399 |
} |
400 |
} |
401 |
|
402 |
rows.add(colorItem); |
403 |
continue;
|
404 |
} |
405 |
if (parser.getName().equals("Alpha")) { |
406 |
ColorItem colorItem = new ColorItem();
|
407 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
408 |
if (parser.getAttributeName(i).equals("value")) { |
409 |
colorItem.setValue(Double.parseDouble((String) parser.getAttributeValue(i))); |
410 |
ColorItem aux = getColorItem(rows, Double.parseDouble((String) parser.getAttributeValue(i))); |
411 |
if (aux != null) { |
412 |
colorItem.setNameClass(aux.getNameClass()); |
413 |
colorItem.setInterpolated(aux.getInterpolated()); |
414 |
colorItem.setColor(new Color(aux.getColor().getRed(), aux.getColor().getGreen(), aux.getColor().getBlue(), colorItem.getColor().getAlpha())); |
415 |
} |
416 |
} |
417 |
if (parser.getAttributeName(i).equals("alpha")) { |
418 |
int a = Integer.parseInt(parser.getAttributeValue(i)); |
419 |
|
420 |
colorItem.setColor(new Color(colorItem.getColor().getRed(), colorItem.getColor().getGreen(), colorItem.getColor().getBlue(), a)); |
421 |
} |
422 |
if (parser.getAttributeName(i).equals("interpolated")) { |
423 |
colorItem.setInterpolated(Double.parseDouble((String) parser.getAttributeValue(i))); |
424 |
} |
425 |
} |
426 |
|
427 |
rows.add(colorItem); |
428 |
continue;
|
429 |
} |
430 |
} |
431 |
} finally {
|
432 |
tag = parser.nextTag(); |
433 |
} |
434 |
} |
435 |
|
436 |
for (int i = 0; i < rows.size(); i++) |
437 |
items.add(rows.get(i)); |
438 |
|
439 |
fileInputStream.close(); |
440 |
return paletteName;
|
441 |
} catch (FileNotFoundException fnfEx) { |
442 |
fnfEx.printStackTrace(); |
443 |
} catch (XmlPullParserException xmlEx) {
|
444 |
System.out.println("El fichero de paletas predeterminadas no tiene la estructura correcta:\n " + xmlEx.getMessage()); |
445 |
} catch (IOException e) { |
446 |
} |
447 |
return null; |
448 |
} |
449 |
} |