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 / ProjectionRmfSerializer.java @ 1794

History | View | Annotate | Download (4.71 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.cresques.cts.IProjection;
29
import org.gvsig.fmap.dal.coverage.RasterLocator;
30
import org.gvsig.fmap.dal.coverage.exception.ParsingException;
31
import org.gvsig.fmap.dal.coverage.util.CRSUtils;
32
import org.gvsig.raster.impl.store.rmf.ClassSerializer;
33
import org.gvsig.tools.ToolsLocator;
34
import org.gvsig.tools.extensionpoint.ExtensionPoint;
35
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
36
import org.kxml2.io.KXmlParser;
37
import org.xmlpull.v1.XmlPullParserException;
38
/**
39
 * <P>
40
 * Clase para convertir a XML una proyeccion y obtener el valor desde un XML.
41
 * Esta clase implementa el interfaz IRmfBlock con los m?todos de escritura y
42
 * lectura. Estos ser?n utilizados por el gestor de ficheros RMF para escribir y
43
 * leer datos.
44
 * </P>
45
 * <P>
46
 * La estructura XML de una proyeccion es la siguiente:
47
 * </P>
48
 * <P>
49
 *
50
 * @version 20/05/2008
51
 * @author BorSanZa - Borja S?nchez Zamorano 
52
 */
53
public class ProjectionRmfSerializer extends ClassSerializer {
54
        private final String MAIN_TAG   = "Projection";
55
        private IProjection  projection = null;
56

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

    
66
        public ProjectionRmfSerializer(IProjection projection) {
67
                this.projection = projection;
68
        }
69

    
70
        /**
71
         * Constructor.
72
         */
73
        public ProjectionRmfSerializer() {
74
        }
75

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

    
84
        /*
85
         * (non-Javadoc)
86
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#getResult()
87
         */
88
        public Object getResult() {
89
                return projection;
90
        }
91

    
92
        /*
93
         * (non-Javadoc)
94
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#read(java.lang.String)
95
         */
96
        public void read(String xml) throws ParsingException {
97
                CRSUtils util = RasterLocator.getManager().getCRSUtils();
98
                KXmlParser parser = new KXmlParser();
99
                Reader reader = new StringReader(xml);
100
                try {
101
                        parser.setInput(reader);
102
                } catch (XmlPullParserException e) {
103
                        throw new ParsingException(xml);
104
                }
105

    
106
                try {
107
                        int tag = parser.nextTag();
108

    
109
                        if (parser.getEventType() != KXmlParser.END_DOCUMENT) {
110
                                parser.require(KXmlParser.START_TAG, null, MAIN_TAG);
111

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

    
134
        /*
135
         * (non-Javadoc)
136
         * @see org.gvsig.raster.dataset.io.rmf.IRmfBlock#write()
137
         */
138
        public String write() throws IOException {
139
                CRSUtils util = RasterLocator.getManager().getCRSUtils();
140
                StringBuffer b = new StringBuffer();
141

    
142
                if (projection == null)
143
                        return null;
144

    
145
                b.append("<" + MAIN_TAG + ">\n");
146
                b.append("\t<WktProjection");
147
                String wkt = util.convertIProjectionToWkt(projection);
148
                b.append(" value=\"" + removeQuotes(wkt) + "\"/>\n");
149
                b.append("</" + MAIN_TAG + ">\n");
150

    
151
                return b.toString();
152
        }
153
        
154
        private String removeQuotes(String wkt) {
155
                return wkt.replace("\"", "");
156
        }
157
}