root / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / serializer / ColorInterpretationRmfSerializer.java @ 18040
History | View | Annotate | Download (4.41 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.dataset.serializer; |
20 |
|
21 |
import java.io.IOException; |
22 |
import java.io.Reader; |
23 |
import java.io.StringReader; |
24 |
|
25 |
import org.gvsig.raster.dataset.io.rmf.ClassSerializer; |
26 |
import org.gvsig.raster.dataset.io.rmf.ParsingException; |
27 |
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation; |
28 |
import org.kxml2.io.KXmlParser; |
29 |
import org.xmlpull.v1.XmlPullParserException; |
30 |
|
31 |
/**
|
32 |
* <P>
|
33 |
* Clase para convertir a XML la informaci?n de interpretaci?n de color de cada
|
34 |
* banda del raster.
|
35 |
* </P>
|
36 |
* \<ColorInterpretation\>
|
37 |
* \<BandCount\>3\</BandCount\><BR>
|
38 |
* \<Band\>Red\</Band\><BR>
|
39 |
* \<Band\>Green\</Band\><BR>
|
40 |
* \<Band\>Blue\</Band\><BR>
|
41 |
* \</ColorInterpretation\><BR>
|
42 |
*
|
43 |
* @version 14/01/2008
|
44 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
45 |
*
|
46 |
*/
|
47 |
public class ColorInterpretationRmfSerializer extends ClassSerializer { |
48 |
|
49 |
//TAGS
|
50 |
public static final String MAIN_TAG = "ColorInterpretation"; |
51 |
public static final String BAND = "Band"; |
52 |
public static final String BANDCOUNT = "BandCount"; |
53 |
|
54 |
private DatasetColorInterpretation datasetCI = null; |
55 |
|
56 |
/**
|
57 |
* Constructor. Asigna la tabla a serializar.
|
58 |
* @param ColorTable tabla a convertir en XML
|
59 |
*/
|
60 |
public ColorInterpretationRmfSerializer(DatasetColorInterpretation datasetCI) {
|
61 |
this.datasetCI = datasetCI;
|
62 |
} |
63 |
|
64 |
/**
|
65 |
* Constructor.
|
66 |
*/
|
67 |
public ColorInterpretationRmfSerializer() {
|
68 |
} |
69 |
|
70 |
|
71 |
/*
|
72 |
* (non-Javadoc)
|
73 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
|
74 |
*/
|
75 |
public void read(String xml) throws ParsingException { |
76 |
String cInterp = null; |
77 |
datasetCI = new DatasetColorInterpretation();
|
78 |
int band = 0; |
79 |
|
80 |
KXmlParser parser = new KXmlParser();
|
81 |
Reader reader = new StringReader(xml); |
82 |
try {
|
83 |
parser.setInput(reader); |
84 |
} catch (XmlPullParserException e) {
|
85 |
throw new ParsingException(xml); |
86 |
} |
87 |
try {
|
88 |
int tag = parser.nextTag();
|
89 |
|
90 |
if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
|
91 |
parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
|
92 |
while(tag != KXmlParser.END_DOCUMENT) {
|
93 |
switch(tag) {
|
94 |
case KXmlParser.START_TAG:
|
95 |
if (parser.getName().compareTo(MAIN_TAG) == 0) { |
96 |
int nBands = Integer.valueOf(parserString(parser, BANDCOUNT, null)).intValue(); |
97 |
datasetCI.initColorInterpretation(nBands); |
98 |
for (int i = 0; i < nBands; i++) { |
99 |
cInterp = parserString(parser, BAND, null);
|
100 |
datasetCI.setColorInterpValue(i, cInterp); |
101 |
band ++; |
102 |
} |
103 |
} |
104 |
break;
|
105 |
case KXmlParser.END_TAG:
|
106 |
break;
|
107 |
case KXmlParser.TEXT:
|
108 |
break;
|
109 |
} |
110 |
tag = parser.next(); |
111 |
} |
112 |
parser.require(KXmlParser.END_DOCUMENT, null, null); |
113 |
} |
114 |
|
115 |
} catch (XmlPullParserException e) {
|
116 |
throw new ParsingException(xml); |
117 |
} catch (IOException e) { |
118 |
throw new ParsingException(xml); |
119 |
} |
120 |
|
121 |
} |
122 |
|
123 |
/*
|
124 |
* (non-Javadoc)
|
125 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
|
126 |
*/
|
127 |
public String write() { |
128 |
StringBuffer b = new StringBuffer(); |
129 |
if(datasetCI == null) |
130 |
return null; |
131 |
|
132 |
b.append("<" + MAIN_TAG + ">\n"); |
133 |
putProperty(b, BANDCOUNT, datasetCI.length(), 1);
|
134 |
for (int i = 0; i < datasetCI.length(); i++) { |
135 |
String ci = datasetCI.get(i);
|
136 |
if(ci != null) |
137 |
putProperty(b, BAND, ci, 1);
|
138 |
} |
139 |
b.append("</" + MAIN_TAG + ">\n"); |
140 |
return b.toString();
|
141 |
} |
142 |
|
143 |
/*
|
144 |
* (non-Javadoc)
|
145 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
|
146 |
*/
|
147 |
public Object getResult() { |
148 |
return datasetCI;
|
149 |
} |
150 |
|
151 |
/*
|
152 |
* (non-Javadoc)
|
153 |
* @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
|
154 |
*/
|
155 |
public String getMainTag() { |
156 |
return MAIN_TAG;
|
157 |
} |
158 |
} |