Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / dataset / io / rmf / ClassSerializer.java @ 12254

History | View | Annotate | Download (4.89 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.io.rmf;
20

    
21
import java.io.IOException;
22

    
23
import org.kxml2.io.KXmlParser;
24
import org.xmlpull.v1.XmlPullParserException;
25

    
26
/**
27
 * Clase de la que debe heredar cualquier clase que convierta objetos a XML 
28
 * para ser guardados en ficheros rmf.
29
 * 
30
 * 23-abr-2007
31
 * @author Nacho Brodin (nachobrodin@gmail.com)
32
 */
33
public abstract class ClassSerializer implements IRmfBlock {
34

    
35
        /**
36
         * Parsea un tag con un String como texto de la forma \<TAG\>String\</TAG\>
37
         * @param parser         KXmlParser
38
         * @param parseableTag   cadena de texto con el nombre del tag  
39
         * @return        valor double recuperado
40
         * @throws XmlPullParserException
41
         * @throws IOException
42
         */
43
        protected String parserString(KXmlParser parser, String parseableTag) throws XmlPullParserException, IOException {
44
                String value = null;
45
                boolean end = false;
46
                boolean tagOk = false;
47
            int tag = parser.next();
48
            while (!end) {
49
                    switch(tag) {
50
                        case KXmlParser.START_TAG:
51
                                if(parser.getName() != null){        
52
                                                if (parser.getName().compareTo(parseableTag) == 0)
53
                                                        tagOk = true;
54
                                        }                                
55
                                        break;
56
                         case KXmlParser.END_TAG:
57
                                 if (parser.getName().compareTo(parseableTag) == 0)
58
                                         end = true;
59
                                break;
60
                        case KXmlParser.TEXT:
61
                                if(tagOk) {
62
                                        value = parser.getText();
63
                                        tagOk = false;
64
                                }
65
                                break;
66
                    }
67
                    if (!end)
68
                            tag = parser.next();
69
            }
70
            return value;
71
        }
72
        
73
        /**
74
         * Convierte una lista de valores en un solo String en un array de enteros. El 
75
         * String debe representar una lista de numeros separados por espacios.
76
         * <P>
77
         * String v = "4 5 65 2 1"
78
         * </P>
79
         * <P>
80
         * Se converir?a en: 
81
         * </P>
82
         * <P>
83
         * int[] i = new int[]{4, 5, 65, 2, 1};
84
         * </P>
85
         * @param list Cadena con la lista de valores
86
         * @return Array de valores long
87
         * @throws XmlPullParserException
88
         * @throws IOException
89
         */
90
        protected int[] convertStringInIntArray(String list) throws XmlPullParserException, IOException {
91
                String[] sValues = list.split(" ");
92
                int[] iValues = new int[sValues.length];
93
                for (int i = 0; i < sValues.length; i++) {
94
                        try {
95
                                iValues[i] = Integer.parseInt(sValues[i]);
96
                        } catch (NumberFormatException e) {
97
                                //iValues[i] vale cero
98
                        }
99
                }
100
                return iValues;
101
        }
102
        
103
        /**
104
         * Convierte una lista de valores en un solo String en un array de double. El 
105
         * String debe representar una lista de numeros separados por espacios.
106
         * <P>
107
         * String v = "4.2 5 65.5 2 1"
108
         * </P>
109
         * <P>
110
         * Se converir?a en: 
111
         * </P>
112
         * <P>
113
         * double[] i = new double[]{4.2, 5, 65.5, 2, 1};
114
         * </P>
115
         * @param list Cadena con la lista de valores
116
         * @return Array de valores double
117
         * @throws XmlPullParserException
118
         * @throws IOException
119
         */
120
        protected double[] convertStringInDoubleArray(String list) throws XmlPullParserException, IOException {
121
                String[] sValues = list.split(" ");
122
                double[] dValues = new double[sValues.length];
123
                for (int i = 0; i < sValues.length; i++) {
124
                        try {
125
                                dValues[i] = Double.parseDouble(sValues[i]);
126
                        } catch (NumberFormatException e) {
127
                                //dValues[i] vale cero
128
                        }
129
                }
130
                return dValues;
131
        }
132
        
133
        /**
134
         * A?ade al buffer la propiedad (String) especificada en los par?metros
135
         * @param b Buffer
136
         * @param prop Nombre de la propiedad (Tag)
137
         * @param value Valor de la propiedad
138
         */
139
        public void putProperty(StringBuffer b, String prop, String value) {
140
                b.append("\t<" + prop  + ">");
141
                b.append(value);
142
                b.append("</" + prop + ">\n");
143
        }
144
        
145
        /**
146
         * A?ade al buffer la propiedad (int) especificada en los par?metros
147
         * @param b Buffer
148
         * @param prop Nombre de la propiedad (Tag)
149
         * @param value Valor de la propiedad
150
         */
151
        public void putProperty(StringBuffer b, String prop, int value) {
152
                putProperty(b, prop, String.valueOf(value));
153
        }
154
        
155
        /**
156
         * A?ade al buffer la propiedad (double) especificada en los par?metros
157
         * @param b Buffer
158
         * @param prop Nombre de la propiedad (Tag)
159
         * @param value Valor de la propiedad
160
         */
161
        public void putProperty(StringBuffer b, String prop, double value) {
162
                putProperty(b, prop, String.valueOf(value));
163
        }
164
        
165
}