gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.legend / org.gvsig.raster.lib.legend.impl / src / main / java / org / gvsig / raster / lib / legend / impl / io / gvSIGColorTableIO.java @ 6900
History | View | Annotate | Download (11.4 KB)
1 |
package org.gvsig.raster.lib.legend.impl.io; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.io.File; |
5 |
import java.io.FileInputStream; |
6 |
import java.io.FileNotFoundException; |
7 |
import java.io.FileOutputStream; |
8 |
import java.io.IOException; |
9 |
import java.io.InputStreamReader; |
10 |
import java.io.OutputStreamWriter; |
11 |
import java.util.ArrayList; |
12 |
import java.util.List; |
13 |
|
14 |
import org.gvsig.raster.lib.legend.api.RasterLegendLocator; |
15 |
import org.gvsig.raster.lib.legend.api.RasterLegendManager; |
16 |
import org.gvsig.raster.lib.legend.api.colortable.ColorTable; |
17 |
import org.gvsig.raster.lib.legend.api.colortable.ColorTableIO; |
18 |
import org.gvsig.raster.lib.legend.api.colortable.colortableclass.ColorTableClass; |
19 |
import org.gvsig.raster.lib.legend.api.exceptions.ColorTableIOException; |
20 |
|
21 |
import org.kxml2.io.KXmlParser; |
22 |
import org.kxml2.io.KXmlSerializer; |
23 |
import org.slf4j.Logger; |
24 |
import org.slf4j.LoggerFactory; |
25 |
import org.xmlpull.v1.XmlPullParserException; |
26 |
|
27 |
/**
|
28 |
* {@link ColorTableIO} to read and write gvSIG color tables.
|
29 |
*
|
30 |
* @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
31 |
*
|
32 |
*/
|
33 |
public class gvSIGColorTableIO implements ColorTableIO { |
34 |
|
35 |
private static final Logger LOG = LoggerFactory.getLogger(gvSIGColorTableIO.class); |
36 |
|
37 |
public static final String NAME = "gvSIGColorTableIO"; |
38 |
|
39 |
public static final String DESCRIPTION = "Color table IO to read and write gvSIG color tables"; |
40 |
|
41 |
@Override
|
42 |
public String getName() { |
43 |
return NAME;
|
44 |
} |
45 |
|
46 |
@Override
|
47 |
public String getDescription() { |
48 |
return DESCRIPTION;
|
49 |
} |
50 |
|
51 |
@Override
|
52 |
public ColorTable read(File file) throws ColorTableIOException { |
53 |
|
54 |
if (file.isDirectory()) {
|
55 |
throw new IllegalArgumentException("File can not be a directory."); |
56 |
} |
57 |
|
58 |
if (!file.exists() || !file.canRead()) {
|
59 |
throw new IllegalArgumentException("File does not exist or can be readed"); |
60 |
} |
61 |
|
62 |
RasterLegendManager rasterLegendManager = RasterLegendLocator.getRasterLegendManager(); |
63 |
ColorTable colorTable = rasterLegendManager.createColorTable(); |
64 |
List<ColorTableClass> classes = new ArrayList<ColorTableClass>(); |
65 |
|
66 |
KXmlParser parser = new KXmlParser();
|
67 |
FileInputStream in = null; |
68 |
InputStreamReader reader = null; |
69 |
int tag = 0; |
70 |
|
71 |
try {
|
72 |
in = new FileInputStream(file); |
73 |
reader = new InputStreamReader(in, "UTF8"); |
74 |
parser.setInput(reader); |
75 |
tag = parser.nextTag(); |
76 |
parser.require(KXmlParser.START_TAG, null, "ColorTable"); |
77 |
} catch (FileNotFoundException e) { |
78 |
LOG.error(String.format("Can not find %1s file", file.getAbsolutePath()), e); |
79 |
throw new ColorTableIOException(e); |
80 |
} catch (XmlPullParserException e) {
|
81 |
LOG.error("Can not parse XML", e);
|
82 |
throw new ColorTableIOException(e); |
83 |
} catch (IOException e) { |
84 |
LOG.error("Can not go to next XML tag or there are errors setting require tag", e);
|
85 |
throw new ColorTableIOException(e); |
86 |
} |
87 |
|
88 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
89 |
if (parser.getAttributeName(i).equals("name")) { |
90 |
colorTable.setName(parser.getAttributeValue(i)); |
91 |
continue;
|
92 |
} else if (parser.getAttributeName(i).equals("interpolated")) { |
93 |
colorTable.setInterpolated(Boolean.parseBoolean(parser.getAttributeValue(i)));
|
94 |
continue;
|
95 |
} |
96 |
|
97 |
} |
98 |
|
99 |
try {
|
100 |
tag = parser.nextTag(); |
101 |
} catch (XmlPullParserException | IOException e) { |
102 |
LOG.error("Can not go to next XML tag", e);
|
103 |
throw new ColorTableIOException(e); |
104 |
} |
105 |
|
106 |
while (!((tag == KXmlParser.END_TAG) && (parser.getName().equals("ColorTable")))) { |
107 |
try {
|
108 |
if (tag == KXmlParser.START_TAG) {
|
109 |
if (parser.getName().equals("Color")) { |
110 |
ColorTableClass colorTableClass = |
111 |
rasterLegendManager.createColorTableClass(); |
112 |
int a = 255; |
113 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
114 |
if (parser.getAttributeName(i).equals("value")) { |
115 |
colorTableClass.setValue(Double.parseDouble((String) parser |
116 |
.getAttributeValue(i))); |
117 |
ColorTableClass aux = |
118 |
getColorItem(classes, |
119 |
Double.parseDouble((String) parser.getAttributeValue(i))); |
120 |
if (aux != null) |
121 |
a = aux.getColor().getAlpha(); |
122 |
} |
123 |
if (parser.getAttributeName(i).equals("name")) { |
124 |
colorTableClass.setName((String) parser.getAttributeValue(i));
|
125 |
} |
126 |
if (parser.getAttributeName(i).equals("rgb")) { |
127 |
String rgb = parser.getAttributeValue(i);
|
128 |
int r =
|
129 |
Integer.valueOf(rgb.substring(0, rgb.indexOf(","))).intValue(); |
130 |
int g =
|
131 |
Integer.valueOf(
|
132 |
rgb.substring(rgb.indexOf(",") + 1, rgb.lastIndexOf(","))) |
133 |
.intValue(); |
134 |
int b =
|
135 |
Integer.valueOf(
|
136 |
rgb.substring(rgb.lastIndexOf(",") + 1, rgb.length())) |
137 |
.intValue(); |
138 |
|
139 |
colorTableClass.setColor(new Color(r, g, b, a)); |
140 |
} |
141 |
if (parser.getAttributeName(i).equals("interpolated")) { |
142 |
colorTableClass.setInterpolated(Double.parseDouble((String) parser |
143 |
.getAttributeValue(i))); |
144 |
} |
145 |
} |
146 |
|
147 |
classes.add(colorTableClass); |
148 |
continue;
|
149 |
} |
150 |
if (parser.getName().equals("Alpha")) { |
151 |
ColorTableClass colorItem = null;
|
152 |
for (int i = 0; i < parser.getAttributeCount(); i++) { |
153 |
if (parser.getAttributeName(i).equals("value")) { |
154 |
colorItem = |
155 |
getColorItem(classes, |
156 |
Double.parseDouble((String) parser.getAttributeValue(i))); |
157 |
} |
158 |
if (parser.getAttributeName(i).equals("alpha") && colorItem != null) { |
159 |
int a = Integer.parseInt(parser.getAttributeValue(i)); |
160 |
|
161 |
colorItem.setColor(new Color(colorItem.getColor().getRed(), |
162 |
colorItem.getColor().getGreen(), |
163 |
colorItem.getColor().getBlue(), a)); |
164 |
} |
165 |
if (parser.getAttributeName(i).equals("interpolated") && colorItem != null) { |
166 |
colorItem.setInterpolated(Double.parseDouble((String) parser |
167 |
.getAttributeValue(i))); |
168 |
} |
169 |
} |
170 |
|
171 |
classes.add(colorItem); |
172 |
continue;
|
173 |
} |
174 |
} |
175 |
} finally {
|
176 |
try {
|
177 |
tag = parser.nextTag(); |
178 |
} catch (XmlPullParserException | IOException e) { |
179 |
LOG.error("Can not go to next XML tag", e);
|
180 |
throw new ColorTableIOException(e); |
181 |
} |
182 |
} |
183 |
} |
184 |
try {
|
185 |
reader.close(); |
186 |
} catch (IOException e) { |
187 |
// Just notice but not throw an exception
|
188 |
LOG.warn("Can not close input stream", e);
|
189 |
} |
190 |
|
191 |
colorTable.setClasses(classes, false);
|
192 |
return colorTable;
|
193 |
} |
194 |
|
195 |
private ColorTableClass getColorItem(List<ColorTableClass> classes, double value) { |
196 |
for (int i = 0; i < classes.size(); i++) { |
197 |
if (classes.get(i).getValue() == value) {
|
198 |
return classes.remove(i);
|
199 |
} |
200 |
} |
201 |
return null; |
202 |
} |
203 |
|
204 |
@Override
|
205 |
public void read(ColorTable colorTable, File file) throws ColorTableIOException { |
206 |
ColorTable tmpColorTable = this.read(file);
|
207 |
colorTable.copyFrom(tmpColorTable); |
208 |
} |
209 |
|
210 |
@Override
|
211 |
public void write(ColorTable colorTable, File file) throws ColorTableIOException { |
212 |
try {
|
213 |
KXmlSerializer parserOutput = new KXmlSerializer();
|
214 |
FileOutputStream out = new FileOutputStream(file); |
215 |
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF8"); |
216 |
parserOutput.setOutput(writer); |
217 |
parserOutput.startDocument("UTF-8", null); |
218 |
parserOutput.startTag(null, "ColorTable"); |
219 |
parserOutput.attribute(null, "name", colorTable.getName()); |
220 |
parserOutput.attribute(null, "interpolated", String.valueOf(colorTable.isInterpolated())); |
221 |
parserOutput.attribute(null, "version", "1.1"); |
222 |
parserOutput.text("\n");
|
223 |
|
224 |
List<ColorTableClass> classes = colorTable.getClasses();
|
225 |
for (int i = 0; i < classes.size(); i++) { |
226 |
ColorTableClass colorItem = classes.get(i); |
227 |
parserOutput.startTag(null, "Color"); |
228 |
parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue())); |
229 |
if (colorItem.getName() != null) |
230 |
parserOutput.attribute(null, "name", String.valueOf(colorItem.getName())); |
231 |
else
|
232 |
parserOutput.attribute(null, "name", ""); |
233 |
Color color = colorItem.getColor();
|
234 |
parserOutput |
235 |
.attribute( |
236 |
null,
|
237 |
"rgb",
|
238 |
String.valueOf(color.getRed() + "," + color.getGreen() + "," |
239 |
+ color.getBlue())); |
240 |
parserOutput.attribute(null, "interpolated", |
241 |
String.valueOf(colorItem.getInterpolated()));
|
242 |
parserOutput.endTag(null, "Color"); |
243 |
parserOutput.text("\n");
|
244 |
} |
245 |
|
246 |
for (int i = 0; i < classes.size(); i++) { |
247 |
ColorTableClass colorItem = classes.get(i); |
248 |
parserOutput.startTag(null, "Alpha"); |
249 |
parserOutput.attribute(null, "value", String.valueOf(colorItem.getValue())); |
250 |
parserOutput.attribute(null, "alpha", |
251 |
String.valueOf(colorItem.getColor().getAlpha()));
|
252 |
parserOutput.attribute(null, "interpolated", |
253 |
String.valueOf(colorItem.getInterpolated()));
|
254 |
parserOutput.endTag(null, "Alpha"); |
255 |
parserOutput.text("\n");
|
256 |
} |
257 |
|
258 |
parserOutput.endTag(null, "ColorTable"); |
259 |
parserOutput.text("\n");
|
260 |
parserOutput.endDocument(); |
261 |
writer.close(); |
262 |
} catch (FileNotFoundException e) { |
263 |
LOG.error(String.format("Can not find %1s file", file.getAbsolutePath()), e); |
264 |
throw new ColorTableIOException(e); |
265 |
} catch (IOException e) { |
266 |
LOG.error("Error writting Color table to XML file", e);
|
267 |
throw new ColorTableIOException(e); |
268 |
} |
269 |
} |
270 |
} |