Statistics
| Revision:

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 @ 2178

History | View | Annotate | Download (4.94 KB)

1
/* 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
import org.gvsig.raster.impl.store.properties.DataStoreColorInterpretation;
31
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
        public ColorInterpretationRmfSerializer(DataStoreColorInterpretation datasetCI) {
74
                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
                datasetCI = new DataStoreColorInterpretation();
90

    
91
                KXmlParser parser = new KXmlParser();
92
                Reader reader = new StringReader(xml);
93
                try {
94
                        parser.setInput(reader);
95
                } catch (XmlPullParserException e) {
96
                        throw new ParsingException(xml);
97
                }
98
                try {
99
                        int tag = parser.nextTag();
100

    
101
                        if ( parser.getEventType() != KXmlParser.END_DOCUMENT ){
102
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
103
                                while(tag != KXmlParser.END_DOCUMENT) {
104
                                        switch(tag) {
105
                                                case KXmlParser.START_TAG:
106
                                                        if (parser.getName().compareTo(MAIN_TAG) == 0) {
107
                                                                int nBands = Integer.valueOf(parserString(parser, BANDCOUNT, null)).intValue();
108
                                                                datasetCI.initColorInterpretation(nBands);
109
                                                                for (int i = 0; i < nBands; i++) {
110
                                                                        cInterp = parserString(parser, BAND, null);
111
                                                                        datasetCI.setColorInterpValue(i, cInterp);
112
                                                                }
113
                                                        }
114
                                                        break;
115
                                                case KXmlParser.END_TAG:
116
                                                        break;
117
                                                case KXmlParser.TEXT:
118
                                                        break;
119
                                        }
120
                                        tag = parser.next();
121
                                }
122
                                parser.require(KXmlParser.END_DOCUMENT, null, null);
123
                        }
124

    
125
                } catch (XmlPullParserException e) {
126
                        throw new ParsingException(xml);
127
                } catch (IOException e) {
128
                        throw new ParsingException(xml);
129
                }
130
        }
131

    
132
        /*
133
         * (non-Javadoc)
134
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
135
         */
136
        public String write() {
137
                StringBuffer b = new StringBuffer();
138
                if (datasetCI == null)
139
                        return null;
140

    
141
                b.append("<" + MAIN_TAG + ">\n");
142
                putProperty(b, BANDCOUNT, datasetCI.length(), 1);
143
                
144
                for (int i = 0; i < datasetCI.length(); i++) {
145
                        String ci = datasetCI.get(i);
146
                        if (ci != null)
147
                                putProperty(b, BAND, ci, 1);
148
                }
149
                b.append("</" + MAIN_TAG + ">\n");
150
                return b.toString();
151
        }
152

    
153
        /*
154
         * (non-Javadoc)
155
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
156
         */
157
        public Object getResult() {
158
                return datasetCI;
159
        }
160

    
161
        /*
162
         *  (non-Javadoc)
163
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
164
         */
165
        public String getMainTag() {
166
                return MAIN_TAG;
167
        }
168
}