Revision 17654

View differences:

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

  
50
	/**
51
	 * Constructor. Asigna el valor NoData a serializar
52
	 * @param Double valor NoData
53
	 */
54
	public NoDataRmfSerializer(Double noData) {
55
		this.noData = noData;
56
	}
57

  
58
	/**
59
	 * Constructor.
60
	 */
61
	public NoDataRmfSerializer() {
62
	}
63

  
64
	/*
65
	 * (non-Javadoc)
66
	 * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getMainTag()
67
	 */
68
	public String getMainTag() {
69
		return MAIN_TAG;
70
	}
71

  
72
	/*
73
	 * (non-Javadoc)
74
	 * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
75
	 */
76
	public Object getResult() {
77
		return noData;
78
	}
79

  
80
	/*
81
	 * (non-Javadoc)
82
	 * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
83
	 */
84
	public void read(String xml) throws ParsingException {
85
		KXmlParser parser = new KXmlParser();
86
		Reader reader = new StringReader(xml);
87
		try {
88
			parser.setInput(reader);
89
		} catch (XmlPullParserException e) {
90
			throw new ParsingException(xml);
91
		}
92

  
93
		try {
94
			parser.nextTag();
95

  
96
			if (parser.getEventType() != KXmlParser.END_DOCUMENT) {
97
				parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
98

  
99
				for (int i = 0; i < parser.getAttributeCount(); i++) {
100
					if (parser.getAttributeName(i).equals("value"))
101
						noData = Double.valueOf(parser.getAttributeValue(i));
102
				}
103
			}
104
			reader.close();
105
		} catch (XmlPullParserException e) {
106
			throw new ParsingException(xml);
107
		} catch (IOException e) {
108
			throw new ParsingException(xml);
109
		}
110
	}
111

  
112
	/*
113
	 * (non-Javadoc)
114
	 * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
115
	 */
116
	public String write() throws IOException {
117
		StringBuffer b = new StringBuffer();
118

  
119
		if (noData == null)
120
			return "";
121

  
122
		b.append("<" + MAIN_TAG + " value=\"" + noData.toString() + "\">\n");
123
		b.append("</" + MAIN_TAG + ">\n");
124

  
125
		return b.toString();
126
	}
127
}

Also available in: Unified diff