Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRaster / src / org / gvsig / raster / datastruct / serializer / NoDataRmfSerializer.java @ 26873

History | View | Annotate | Download (5.22 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.io.IOException;
22
import java.io.Reader;
23
import java.io.StringReader;
24

    
25
import org.gvsig.raster.RasterLibrary;
26
import org.gvsig.raster.dataset.io.rmf.ClassSerializer;
27
import org.gvsig.raster.dataset.io.rmf.ParsingException;
28
import org.gvsig.raster.datastruct.NoData;
29
import org.gvsig.tools.ToolsLocator;
30
import org.gvsig.tools.extensionpoint.ExtensionPoint;
31
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
32
import org.kxml2.io.KXmlParser;
33
import org.xmlpull.v1.XmlPullParserException;
34
/**
35
 * <P>
36
 * Clase para convertir a XML un valor NoData y obtener el valor desde un XML.
37
 * Esta clase implementa el interfaz IRmfBlock con los m?todos de escritura y
38
 * lectura. Estos ser?n utilizados por el gestor de ficheros RMF para escribir y
39
 * leer datos.
40
 * </P>
41
 * <P>
42
 * La estructura XML de un valor NoData es la siguiente:
43
 * </P>
44
 * <P>
45
 * &lt;NoData value="-99.999"><BR>
46
 * &lt;/NoData><BR>
47
 *
48
 * @version 18/12/2007
49
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
50
 */
51
public class NoDataRmfSerializer extends ClassSerializer {
52
        private final String MAIN_TAG = "NoData";
53
        private NoData noData = null;
54

    
55
        /**
56
         * Registra NoDataRmfSerializer en los puntos de extension de Serializer
57
         */
58
        public static void register() {
59
                ExtensionPointManager extensionPoints =ToolsLocator.getExtensionPointManager();
60
                ExtensionPoint point=extensionPoints.get("Serializer");
61
                point.append("NoData", "", NoDataRmfSerializer.class);
62
        }
63

    
64
        /**
65
         * Constructor. Asigna el valor NoData a serializar
66
         * @param noData   Valor NoData
67
         * @param type     Tipo de NoData
68
         * @param dataType Tipo de datos de la capa
69
         */
70
        public NoDataRmfSerializer(NoData noData) {
71
                this.noData = noData;
72
        }
73

    
74
        /**
75
         * Constructor.
76
         */
77
        public NoDataRmfSerializer() {}
78

    
79
        /*
80
         * (non-Javadoc)
81
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
82
         */
83
        public String getMainTag() {
84
                return MAIN_TAG;
85
        }
86

    
87
        /**
88
         * Devuelve el valor noData, en caso de no existir lo crea.
89
         * @return
90
         */
91
        private NoData getNoData() {
92
                if (noData == null)
93
                        noData = new NoData();
94
                return noData;
95
        }
96

    
97
        /*
98
         * (non-Javadoc)
99
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
100
         */
101
        public Object getResult() {
102
                if (noData == null)
103
                        return null;
104
                if (getNoData().getType() == -1)
105
                        return null;
106
                if (getNoData().getType() == RasterLibrary.NODATATYPE_LAYER)
107
                        return null;
108
                return noData;
109
        }
110

    
111
        /*
112
         * (non-Javadoc)
113
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
114
         */
115
        public void read(String xml) throws ParsingException {
116
                KXmlParser parser = new KXmlParser();
117
                Reader reader = new StringReader(xml);
118
                try {
119
                        parser.setInput(reader);
120
                } catch (XmlPullParserException e) {
121
                        throw new ParsingException(xml);
122
                }
123

    
124
                try {
125
                        int tag = parser.nextTag();
126

    
127
                        if (parser.getEventType() != KXmlParser.END_DOCUMENT) {
128
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
129

    
130
                                while (tag != KXmlParser.END_DOCUMENT) {
131
                                        switch (tag) {
132
                                                case KXmlParser.START_TAG:
133
                                                        if (parser.getName().equals("Data")) {
134
                                                                for (int i = 0; i < parser.getAttributeCount(); i++) {
135
                                                                        if (parser.getAttributeName(i).equals("value")) {
136
                                                                                getNoData().setValue(Double.parseDouble((String) parser.getAttributeValue(i)));
137
                                                                        }
138
                                                                        if (parser.getAttributeName(i).equals("type")) {
139
                                                                                getNoData().setType(Integer.parseInt((String) parser.getAttributeValue(i)));
140
                                                                        }
141
                                                                }
142
                                                        }
143
                                        }
144
                                        tag = parser.next();
145
                                }
146
                        }
147
                        reader.close();
148
                } catch (XmlPullParserException e) {
149
                        throw new ParsingException(xml);
150
                } catch (IOException e) {
151
                        throw new ParsingException(xml);
152
                }
153
        }
154

    
155
        /*
156
         * (non-Javadoc)
157
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
158
         */
159
        public String write() throws IOException {
160
                if (noData == null)
161
                        return "";
162

    
163
                if (getNoData().getType() == RasterLibrary.NODATATYPE_LAYER)
164
                        return "";
165

    
166
                StringBuffer b = new StringBuffer();
167

    
168
                b.append("<" + MAIN_TAG + ">\n");
169
                b.append("\t<Data");
170
                if (getNoData().getType() == RasterLibrary.NODATATYPE_USER)
171
                        b.append(" value=\"" + getNoData().getValue() + "\"");
172
                b.append(" type=\"" + getNoData().getType() + "\"/>\n");
173
                b.append("</" + MAIN_TAG + ">\n");
174

    
175
                return b.toString();
176
        }
177
}