Statistics
| Revision:

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

History | View | Annotate | Download (3.99 KB)

1 11158 jorpiell
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
8
import org.gvsig.gpe.GPEContentHandler;
9
import org.gvsig.gpe.GPEErrorHandler;
10
import org.gvsig.gpe.GPEParser;
11
import org.xmlpull.v1.XmlPullParser;
12
13
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
14
 *
15
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
16
 *
17
 * This program is free software; you can redistribute it and/or
18
 * modify it under the terms of the GNU General Public License
19
 * as published by the Free Software Foundation; either version 2
20
 * of the License, or (at your option) any later version.
21
 *
22
 * This program is distributed in the hope that it will be useful,
23
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
 * GNU General Public License for more details.
26
 *
27
 * You should have received a copy of the GNU General Public License
28
 * along with this program; if not, write to the Free Software
29
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
30
 *
31
 * For more information, contact:
32
 *
33
 *  Generalitat Valenciana
34
 *   Conselleria d'Infraestructures i Transport
35
 *   Av. Blasco Ib??ez, 50
36
 *   46010 VALENCIA
37
 *   SPAIN
38
 *
39
 *      +34 963862235
40
 *   gvsig@gva.es
41
 *      www.gvsig.gva.es
42
 *
43
 *    or
44
 *
45
 *   IVER T.I. S.A
46
 *   Salamanca 50
47
 *   46005 Valencia
48
 *   Spain
49
 *
50
 *   +34 963163400
51
 *   dac@iver.es
52
 */
53
/* CVS MESSAGES:
54
 *
55
 * $Id$
56
 * $Log$
57 11165 jorpiell
 * Revision 1.2  2007-04-12 11:47:15  jorpiell
58
 * Add a getParser method
59
 *
60
 * Revision 1.1  2007/04/12 10:23:41  jorpiell
61 11158 jorpiell
 * Add some writers and the GPEXml parser
62
 *
63
 *
64
 */
65
/**
66
 * This class can be implemented by all the classes that
67
 * implements a GPE driver based on the XML format.
68
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
69
 */
70
public abstract class GPEXmlParser extends GPEParser {
71
        private InputStream inputStream = null;
72 11165 jorpiell
        private XmlPullParser parser = null;
73 11158 jorpiell
74
        public GPEXmlParser(GPEContentHandler contents, GPEErrorHandler errors) {
75
                super(contents, errors);
76
        }
77
78
        /*
79
         * (non-Javadoc)
80
         * @see org.gvsig.gpe.GPEParser#parse(java.io.File)
81
         */
82
        public void parse(File file) throws Exception{
83
                inputStream = createInputStream(file);
84 11165 jorpiell
                parser = GPEXmlParserFactory.getParser();
85 11158 jorpiell
                parser.setInput(getInputStream(), getEncoding());
86
                initParse();
87
        }
88
89
        /**
90
         * Creates an input stream from a file.
91
         * @param file
92
         * @return
93
         * @throws FileNotFoundException
94
         */
95
        protected abstract InputStream createInputStream(File file) throws FileNotFoundException;
96
97
        /**
98
         * This method start the parse process. It is called
99
         * before the XML parser is initialized
100
         */
101
        protected abstract void initParse();
102
103
        /**
104
         * @return Returns the encoding.
105
         * @throws IOException
106
         * @throws KmlException
107
         */
108
        private String getEncoding() throws IOException {
109
                String encoding = "UTF-8";
110
                InputStream is = getInputStream();
111
                int index = -1;
112
                int endIndex = -1;
113
                String header = "";
114
                boolean endHeader = false;
115
116
                int i = 1;
117
                while((!endHeader) && (i < 100)){
118
                        byte[] buffer = new byte[1];
119
                        is.read(buffer);
120
                        header = header + new String(buffer);
121
                        //This if is to locate the encoding string
122
                        if (index == -1) {
123
                                String searchText = "encoding=\"";
124
                                index = header.indexOf(searchText);
125
                                if (index > -1){
126
                                        header = "";
127
                                }
128
                        }
129
                        //This if is to locate the encoding value
130
                        if (index > -1){
131
                                String searchText = "\"";
132
                                endIndex = header.indexOf(searchText);
133
                                if (endIndex > -1){
134
                                        encoding = header.substring(0, endIndex);
135
                                }
136
                        }
137
                        //To finish to parse the header
138
                        if (endIndex > -1){
139
                                String searchText = ">";
140
                                int headerEndIndex = header.indexOf(searchText);
141
                                if (headerEndIndex > -1){
142
                                        endHeader = true;
143
                                }
144
                        }
145
                        i++;
146
                }
147
                return encoding;
148
        }
149
150
        /**
151
         * @return the inputStream
152
         */
153
        private InputStream getInputStream() {
154
                return inputStream;
155 11165 jorpiell
        }
156
157
        /**
158
         * @return the parser
159
         */
160
        protected XmlPullParser getParser() {
161
                return parser;
162 11158 jorpiell
        }
163
164
}