Statistics
| Revision:

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

History | View | Annotate | Download (4.37 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.dataset.serializer;
20

    
21
import java.io.IOException;
22
import java.io.Reader;
23
import java.io.StringReader;
24

    
25
import org.cresques.cts.IProjection;
26
import org.gvsig.raster.dataset.io.rmf.ClassSerializer;
27
import org.gvsig.raster.dataset.io.rmf.ParsingException;
28
import org.gvsig.raster.projection.CRS;
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 una proyeccion 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 una proyeccion es la siguiente:
43
 * </P>
44
 * <P>
45
 *
46
 * @version 20/05/2008
47
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
48
 */
49
public class ProjectionRmfSerializer extends ClassSerializer {
50
        private final String MAIN_TAG   = "Projection";
51
        private IProjection  projection = null;
52

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

    
62
        public ProjectionRmfSerializer(IProjection projection) {
63
                this.projection = projection;
64
        }
65

    
66
        /**
67
         * Constructor.
68
         */
69
        public ProjectionRmfSerializer() {
70
        }
71

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

    
80
        /*
81
         * (non-Javadoc)
82
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
83
         */
84
        public Object getResult() {
85
                return projection;
86
        }
87

    
88
        /*
89
         * (non-Javadoc)
90
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
91
         */
92
        public void read(String xml) throws ParsingException {
93
                KXmlParser parser = new KXmlParser();
94
                Reader reader = new StringReader(xml);
95
                try {
96
                        parser.setInput(reader);
97
                } catch (XmlPullParserException e) {
98
                        throw new ParsingException(xml);
99
                }
100

    
101
                try {
102
                        int tag = parser.nextTag();
103

    
104
                        if (parser.getEventType() != KXmlParser.END_DOCUMENT) {
105
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
106

    
107
                                while (tag != KXmlParser.END_DOCUMENT) {
108
                                        switch (tag) {
109
                                                case KXmlParser.START_TAG:
110
                                                        if (parser.getName().equals("WktProjection")) {
111
                                                                for (int i = 0; i < parser.getAttributeCount(); i++) {
112
                                                                        if (parser.getAttributeName(i).equals("value")) {
113
                                                                                projection = CRS.convertWktToIProjection((String) parser.getAttributeValue(i));
114
                                                                        }
115
                                                                }
116
                                                        }
117
                                        }
118
                                        tag = parser.next();
119
                                }
120
                        }
121
                        reader.close();
122
                } catch (XmlPullParserException e) {
123
                        throw new ParsingException(xml);
124
                } catch (IOException e) {
125
                        throw new ParsingException(xml);
126
                }
127
        }
128

    
129
        /*
130
         * (non-Javadoc)
131
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
132
         */
133
        public String write() throws IOException {
134
                StringBuffer b = new StringBuffer();
135

    
136
                if (projection == null)
137
                        return null;
138

    
139
                b.append("<" + MAIN_TAG + ">\n");
140
                b.append("\t<WktProjection");
141
                b.append(" value=\"" + CRS.convertIProjectionToWkt(projection) + "\"/>\n");
142
                b.append("</" + MAIN_TAG + ">\n");
143

    
144
                return b.toString();
145
        }
146
}