Statistics
| Revision:

root / trunk / libraries / libGPE-GML / src / org / gvsig / gpe / xml / GPEXmlParser.java @ 11247

History | View | Annotate | Download (5.87 KB)

1
package org.gvsig.gpe.xml;
2

    
3
import java.io.File;
4
import java.io.FileNotFoundException;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.lang.reflect.InvocationTargetException;
8

    
9
import org.gvsig.gpe.GPEContentHandler;
10
import org.gvsig.gpe.GPEErrorHandler;
11
import org.gvsig.gpe.GPEParser;
12
import org.xmlpull.v1.XmlPullParser;
13
import org.xmlpull.v1.XmlPullParserException;
14

    
15
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
16
 *
17
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
18
 *
19
 * This program is free software; you can redistribute it and/or
20
 * modify it under the terms of the GNU General Public License
21
 * as published by the Free Software Foundation; either version 2
22
 * of the License, or (at your option) any later version.
23
 *
24
 * This program is distributed in the hope that it will be useful,
25
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27
 * GNU General Public License for more details.
28
 *
29
 * You should have received a copy of the GNU General Public License
30
 * along with this program; if not, write to the Free Software
31
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
32
 *
33
 * For more information, contact:
34
 *
35
 *  Generalitat Valenciana
36
 *   Conselleria d'Infraestructures i Transport
37
 *   Av. Blasco Ib??ez, 50
38
 *   46010 VALENCIA
39
 *   SPAIN
40
 *
41
 *      +34 963862235
42
 *   gvsig@gva.es
43
 *      www.gvsig.gva.es
44
 *
45
 *    or
46
 *
47
 *   IVER T.I. S.A
48
 *   Salamanca 50
49
 *   46005 Valencia
50
 *   Spain
51
 *
52
 *   +34 963163400
53
 *   dac@iver.es
54
 */
55
/* CVS MESSAGES:
56
 *
57
 * $Id: GPEXmlParser.java 11247 2007-04-19 07:30:40Z jorpiell $
58
 * $Log$
59
 * Revision 1.4  2007-04-19 07:30:40  jorpiell
60
 * Add the add methods to teh contenhandler and change the register mode
61
 *
62
 * Revision 1.3  2007/04/18 12:54:45  csanchez
63
 * Actualizacion protoripo libGPE
64
 *
65
 * Revision 1.2  2007/04/12 11:47:15  jorpiell
66
 * Add a getParser method
67
 *
68
 * Revision 1.1  2007/04/12 10:23:41  jorpiell
69
 * Add some writers and the GPEXml parser
70
 *
71
 *
72
 */
73
/**
74
 * This class can be implemented by all the classes that 
75
 * implements a GPE driver based on the XML format.
76
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
77
 */
78
public abstract class GPEXmlParser extends GPEParser {
79
        private InputStream inputStream = null;
80
        protected XmlPullParser parser = null;
81
        
82
        public GPEXmlParser() {
83
                super();                
84
        }
85
        
86
        /*
87
         * (non-Javadoc)
88
         * @see org.gvsig.gpe.GPEParser#parse(org.gvsig.gpe.GPEContentHandler, org.gvsig.gpe.GPEErrorHandler, java.io.File)
89
         */
90
        public void parse(GPEContentHandler contents, GPEErrorHandler errors, File file) {
91
                super.parse(contents, errors, file);
92
                try {
93
                        inputStream = createInputStream(file);
94
                        parser = GPEXmlParserFactory.getParser();
95
                        parser.setInput(getInputStream(), getEncoding());
96
                } // EL PARSER LANZAR? UN EVENTO AL ERRORHANDLER SEG?N EL ERROR RECOGIDO 
97
                catch (FileNotFoundException e) {
98
                        System.out.println("ERROR: Fichero "+ file.getName() +" no encontrado");
99
                        errors.addError(e);
100
                } catch (IllegalArgumentException e) {
101
                        System.out.println("ERROR: Argumento Incorrecto para KXML: "+ e.getMessage());
102
                        errors.addError(e);
103
                } catch (SecurityException e) {
104
                        System.out.println("ERROR: Fallo de seguridad en la clase KXML: "+ e.getMessage());
105
                        errors.addError(e);
106
                } catch (InstantiationException e) {
107
                        System.out.println("ERROR: Instancia de KXML incorrecta: "+ e.getMessage());
108
                        errors.addError(e);
109
                } catch (IllegalAccessException e) {
110
                        System.out.println("ERROR: Acceso Ilegal a memoria de KXML: "+ e.getMessage());
111
                        errors.addError(e);
112
                } catch (InvocationTargetException e) {
113
                        System.out.println("ERROR: Invocaci?n de KXML Incorrecta: "+ e.getMessage());
114
                        errors.addError(e);
115
                } catch (NoSuchMethodException e) {
116
                        System.out.println("ERROR: M?todo no implementado del parser KXML: "+ e.getMessage());
117
                        errors.addError(e);
118
                } catch (ClassNotFoundException e) {
119
                        System.out.println("ERROR: Clase KXML no encontrada: "+ e.getMessage());
120
                        errors.addError(e);        
121
                } catch (XmlPullParserException e) {
122
                        System.out.println("ERROR: Excepci?n en el parser KXML: "+ e.getMessage());
123
                        errors.addError(e);
124
                } catch (IOException e) {
125
                        System.out.println("ERROR: No se puede leer/escribir la codificaci?n del fichero: "+ e.getMessage());
126
                        errors.addError(e);
127
                }
128
                initParse();
129
        }
130
        
131
        /**
132
         * Creates an input stream from a file. 
133
         * @param file
134
         * @return InputStream
135
         * @throws FileNotFoundException 
136
         */
137
        protected abstract InputStream createInputStream(File file) throws FileNotFoundException;
138
        
139
        /**
140
         * This method start the parse process. It is called
141
         * after the XML parser is initialized
142
         */
143
        protected abstract void initParse();
144
        
145
        /**
146
         * @return Returns the encoding.
147
         * @throws IOException 
148
         * @throws KmlException 
149
         */
150
        private String getEncoding() throws IOException {
151
                String encoding = "UTF-8";
152
                InputStream is = getInputStream();                
153
                int index = -1;
154
                int endIndex = -1;
155
                String header = "";
156
                boolean endHeader = false;
157

    
158
                int i = 1;
159
                while((!endHeader) && (i < 100)){
160
                        byte[] buffer = new byte[1];
161
                        is.read(buffer);
162
                        header = header + new String(buffer);
163
                        //This if is to locate the encoding string
164
                        if (index == -1) { 
165
                                String searchText = "encoding=\"";
166
                                index = header.indexOf(searchText);
167
                                if (index > -1){
168
                                        header = "";
169
                                }
170
                        }
171
                        //This if is to locate the encoding value
172
                        if (index > -1){
173
                                String searchText = "\"";
174
                                endIndex = header.indexOf(searchText);
175
                                if (endIndex > -1){
176
                                        encoding = header.substring(0, endIndex);
177
                                }
178
                        }
179
                        //To finish to parse the header
180
                        if (endIndex > -1){
181
                                String searchText = ">";
182
                                int headerEndIndex = header.indexOf(searchText);
183
                                if (headerEndIndex > -1){
184
                                        endHeader = true;
185
                                }
186
                        }
187
                        i++;
188
                }                                
189
                return encoding;
190
        }
191

    
192
        /**
193
         * @return the inputStream
194
         */
195
        private InputStream getInputStream() {
196
                return inputStream;
197
        }
198

    
199
        /**
200
         * @return the parser
201
         */
202
        protected XmlPullParser getParser() {
203
                return parser;
204
        }        
205

    
206
}