Statistics
| Revision:

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

History | View | Annotate | Download (3.8 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

    
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: GPEXmlParser.java 11158 2007-04-12 10:23:41Z jorpiell $
56
 * $Log$
57
 * Revision 1.1  2007-04-12 10:23:41  jorpiell
58
 * Add some writers and the GPEXml parser
59
 *
60
 *
61
 */
62
/**
63
 * This class can be implemented by all the classes that 
64
 * implements a GPE driver based on the XML format.
65
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
66
 */
67
public abstract class GPEXmlParser extends GPEParser {
68
        private InputStream inputStream = null;
69
        
70
        public GPEXmlParser(GPEContentHandler contents, GPEErrorHandler errors) {
71
                super(contents, errors);                
72
        }
73
        
74
        /*
75
         * (non-Javadoc)
76
         * @see org.gvsig.gpe.GPEParser#parse(java.io.File)
77
         */
78
        public void parse(File file) throws Exception{
79
                inputStream = createInputStream(file);
80
                XmlPullParser parser = GPEXmlParserFactory.getParser();
81
                parser.setInput(getInputStream(), getEncoding());
82
                initParse();
83
        }
84
        
85
        /**
86
         * Creates an input stream from a file. 
87
         * @param file
88
         * @return
89
         * @throws FileNotFoundException 
90
         */
91
        protected abstract InputStream createInputStream(File file) throws FileNotFoundException;
92
        
93
        /**
94
         * This method start the parse process. It is called
95
         * before the XML parser is initialized
96
         */
97
        protected abstract void initParse();
98
        
99
        /**
100
         * @return Returns the encoding.
101
         * @throws IOException 
102
         * @throws KmlException 
103
         */
104
        private String getEncoding() throws IOException {
105
                String encoding = "UTF-8";
106
                InputStream is = getInputStream();                
107
                int index = -1;
108
                int endIndex = -1;
109
                String header = "";
110
                boolean endHeader = false;
111

    
112
                int i = 1;
113
                while((!endHeader) && (i < 100)){
114
                        byte[] buffer = new byte[1];
115
                        is.read(buffer);
116
                        header = header + new String(buffer);
117
                        //This if is to locate the encoding string
118
                        if (index == -1) { 
119
                                String searchText = "encoding=\"";
120
                                index = header.indexOf(searchText);
121
                                if (index > -1){
122
                                        header = "";
123
                                }
124
                        }
125
                        //This if is to locate the encoding value
126
                        if (index > -1){
127
                                String searchText = "\"";
128
                                endIndex = header.indexOf(searchText);
129
                                if (endIndex > -1){
130
                                        encoding = header.substring(0, endIndex);
131
                                }
132
                        }
133
                        //To finish to parse the header
134
                        if (endIndex > -1){
135
                                String searchText = ">";
136
                                int headerEndIndex = header.indexOf(searchText);
137
                                if (headerEndIndex > -1){
138
                                        endHeader = true;
139
                                }
140
                        }
141
                        i++;
142
                }                                
143
                return encoding;
144
        }
145

    
146
        /**
147
         * @return the inputStream
148
         */
149
        private InputStream getInputStream() {
150
                return inputStream;
151
        }        
152

    
153
}