gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / store / serializer / ColorInterpretationRmfSerializer.java @ 162
History | View | Annotate | Download (4.97 KB)
1 | 77 | nbrodin | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | */
|
||
22 | package org.gvsig.raster.impl.store.serializer; |
||
23 | |||
24 | import java.io.IOException; |
||
25 | import java.io.Reader; |
||
26 | import java.io.StringReader; |
||
27 | |||
28 | import org.gvsig.fmap.dal.coverage.exception.ParsingException; |
||
29 | import org.gvsig.fmap.dal.coverage.store.props.ColorInterpretation; |
||
30 | 162 | nbrodin | import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation; |
31 | 77 | nbrodin | import org.gvsig.raster.impl.store.rmf.ClassSerializer; |
32 | import org.gvsig.tools.ToolsLocator; |
||
33 | import org.gvsig.tools.extensionpoint.ExtensionPoint; |
||
34 | import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
||
35 | import org.kxml2.io.KXmlParser; |
||
36 | import org.xmlpull.v1.XmlPullParserException; |
||
37 | /**
|
||
38 | * <P>
|
||
39 | * Clase para convertir a XML la informaci?n de interpretaci?n de color de cada
|
||
40 | * banda del raster.
|
||
41 | * </P>
|
||
42 | * \<ColorInterpretation\>
|
||
43 | * \<BandCount\>3\</BandCount\><BR>
|
||
44 | * \<Band\>Red\</Band\><BR>
|
||
45 | * \<Band\>Green\</Band\><BR>
|
||
46 | * \<Band\>Blue\</Band\><BR>
|
||
47 | * \</ColorInterpretation\><BR>
|
||
48 | *
|
||
49 | * @version 14/01/2008
|
||
50 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
51 | */
|
||
52 | public class ColorInterpretationRmfSerializer extends ClassSerializer { |
||
53 | //TAGS
|
||
54 | public static final String MAIN_TAG = "ColorInterpretation"; |
||
55 | public static final String BAND = "Band"; |
||
56 | public static final String BANDCOUNT = "BandCount"; |
||
57 | |||
58 | private ColorInterpretation datasetCI = null; |
||
59 | |||
60 | /**
|
||
61 | * Registra ColorInterpretationRmfSerializer en los puntos de extension de Serializer
|
||
62 | */
|
||
63 | public static void register() { |
||
64 | ExtensionPointManager extensionPoints =ToolsLocator.getExtensionPointManager(); |
||
65 | ExtensionPoint point=extensionPoints.get("Serializer");
|
||
66 | point.append("ColorInterpretation", "", ColorInterpretationRmfSerializer.class); |
||
67 | } |
||
68 | |||
69 | /**
|
||
70 | * Constructor. Asigna la tabla a serializar.
|
||
71 | * @param ColorTable tabla a convertir en XML
|
||
72 | */
|
||
73 | 162 | nbrodin | public ColorInterpretationRmfSerializer(DataStoreColorInterpretation datasetCI) {
|
74 | 77 | nbrodin | this.datasetCI = datasetCI;
|
75 | } |
||
76 | |||
77 | /**
|
||
78 | * Constructor.
|
||
79 | */
|
||
80 | public ColorInterpretationRmfSerializer() {
|
||
81 | } |
||
82 | |||
83 | /*
|
||
84 | * (non-Javadoc)
|
||
85 | * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
|
||
86 | */
|
||
87 | public void read(String xml) throws ParsingException { |
||
88 | String cInterp = null; |
||
89 | 162 | nbrodin | datasetCI = new DataStoreColorInterpretation();
|
90 | 77 | nbrodin | int band = 0; |
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().compareTo(MAIN_TAG) == 0) { |
||
108 | int nBands = Integer.valueOf(parserString(parser, BANDCOUNT, null)).intValue(); |
||
109 | datasetCI.initColorInterpretation(nBands); |
||
110 | for (int i = 0; i < nBands; i++) { |
||
111 | cInterp = parserString(parser, BAND, null);
|
||
112 | datasetCI.setColorInterpValue(i, cInterp); |
||
113 | band ++; |
||
114 | } |
||
115 | } |
||
116 | break;
|
||
117 | case KXmlParser.END_TAG:
|
||
118 | break;
|
||
119 | case KXmlParser.TEXT:
|
||
120 | break;
|
||
121 | } |
||
122 | tag = parser.next(); |
||
123 | } |
||
124 | parser.require(KXmlParser.END_DOCUMENT, null, null); |
||
125 | } |
||
126 | |||
127 | } catch (XmlPullParserException e) {
|
||
128 | throw new ParsingException(xml); |
||
129 | } catch (IOException e) { |
||
130 | throw new ParsingException(xml); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /*
|
||
135 | * (non-Javadoc)
|
||
136 | * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
|
||
137 | */
|
||
138 | public String write() { |
||
139 | StringBuffer b = new StringBuffer(); |
||
140 | if (datasetCI == null) |
||
141 | return null; |
||
142 | |||
143 | b.append("<" + MAIN_TAG + ">\n"); |
||
144 | putProperty(b, BANDCOUNT, datasetCI.length(), 1);
|
||
145 | for (int i = 0; i < datasetCI.length(); i++) { |
||
146 | String ci = datasetCI.get(i);
|
||
147 | if (ci != null) |
||
148 | putProperty(b, BAND, ci, 1);
|
||
149 | } |
||
150 | b.append("</" + MAIN_TAG + ">\n"); |
||
151 | return b.toString();
|
||
152 | } |
||
153 | |||
154 | /*
|
||
155 | * (non-Javadoc)
|
||
156 | * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
|
||
157 | */
|
||
158 | public Object getResult() { |
||
159 | return datasetCI;
|
||
160 | } |
||
161 | |||
162 | /*
|
||
163 | * (non-Javadoc)
|
||
164 | * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
|
||
165 | */
|
||
166 | public String getMainTag() { |
||
167 | return MAIN_TAG;
|
||
168 | } |
||
169 | } |