Statistics
| Revision:

root / trunk / libraries / libGPE-KML / src / org / gvsig / gpe / kml / engine / AbstractDriver.java @ 10637

History | View | Annotate | Download (4.39 KB)

1
package org.gvsig.gpe.kml.engine;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5

    
6
import org.gvsig.exceptions.BaseException;
7
import org.gvsig.gpe.gml.factories.IGeometriesFactory;
8
import org.gvsig.gpe.kml.exceptions.KmlException;
9

    
10
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
11
 *
12
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
13
 *
14
 * This program is free software; you can redistribute it and/or
15
 * modify it under the terms of the GNU General Public License
16
 * as published by the Free Software Foundation; either version 2
17
 * of the License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
27
 *
28
 * For more information, contact:
29
 *
30
 *  Generalitat Valenciana
31
 *   Conselleria d'Infraestructures i Transport
32
 *   Av. Blasco Ib??ez, 50
33
 *   46010 VALENCIA
34
 *   SPAIN
35
 *
36
 *      +34 963862235
37
 *   gvsig@gva.es
38
 *      www.gvsig.gva.es
39
 *
40
 *    or
41
 *
42
 *   IVER T.I. S.A
43
 *   Salamanca 50
44
 *   46005 Valencia
45
 *   Spain
46
 *
47
 *   +34 963163400
48
 *   dac@iver.es
49
 */
50
/* CVS MESSAGES:
51
 *
52
 * $Id: AbstractDriver.java 10637 2007-03-07 08:19:11Z jorpiell $
53
 * $Log$
54
 * Revision 1.1  2007-03-07 08:19:10  jorpiell
55
 * Pasadas las clases de KML de libGPE-GML a libGPE-KML
56
 *
57
 * Revision 1.1  2007/02/28 11:48:31  csanchez
58
 * *** empty log message ***
59
 *
60
 * Revision 1.1  2007/02/20 10:53:20  jorpiell
61
 * A?adidos los proyectos de kml y gml antiguos
62
 *
63
 * Revision 1.1  2007/02/12 13:49:18  jorpiell
64
 * A?adido el driver de KML
65
 *
66
 *
67
 */
68
/**
69
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
70
 */
71
public abstract class AbstractDriver implements IDriver{
72
        private InputStream inputStream = null;
73
        private IReaderFile reader = null;        
74
        protected IFeaturesParser featuresParser = null;
75
        
76
        /**
77
         * Constructor
78
         * @param file
79
         * File to read        
80
         * @param factory
81
         * Geometries factory that is used to create the parsed geometries
82
         * When the file is not found
83
         * @throws BaseException 
84
         */
85
        public AbstractDriver(InputStream file, IGeometriesFactory factory) throws BaseException{
86
                //file to parse...
87
                this.inputStream = file;                        
88
                initializeReader();
89
                initialize(factory);
90
        }
91
        
92
        /**
93
         * Initializes the file reader
94
         *
95
         */
96
        protected abstract void initializeReader() throws BaseException;
97
                
98

    
99
        /**
100
         * Initializes the IReader, tries to parse  the file header 
101
         * to obtain the version and parses all the needed schemas
102
         * @param factory
103
         * Geometries factory that is used to create the parsed 
104
         * geometries
105
         * @throws BaseException 
106
         */
107
        protected abstract void initialize(IGeometriesFactory factory) throws BaseException;
108

    
109
        /**
110
         * @return Returns the reader.
111
         */
112
        public IReaderFile getReader() {
113
                return reader;
114
        }
115

    
116
        /**
117
         * @param reader The reader to set.
118
         */
119
        protected void setReader(IReaderFile reader) {
120
                this.reader = reader;
121
        }
122

    
123
        /**
124
         * @return Returns the inputStream.
125
         */
126
        public InputStream getInputStream() {
127
                return inputStream;
128
        }
129

    
130
        /**
131
         * @return Returns the encoding.
132
         * @throws KmlException 
133
         */
134
        protected String getEncoding() throws KmlException {
135
                String encoding = "UTF-8";
136
                InputStream is = getInputStream();                
137
                int index = -1;
138
                int endIndex = -1;
139
                String header = "";
140
                boolean endHeader = false;
141
                try {
142
                        int i = 1;
143
                        while((!endHeader) && (i < 100)){
144
                                byte[] buffer = new byte[1];
145
                                is.read(buffer);
146
                                header = header + new String(buffer);
147
                                //This if is to locate the encoding string
148
                                if (index == -1) { 
149
                                        String searchText = "encoding=\"";
150
                                        index = header.indexOf(searchText);
151
                                        if (index > -1){
152
                                                header = "";
153
                                        }
154
                                }
155
                                //This if is to locate the encoding value
156
                                if (index > -1){
157
                                        String searchText = "\"";
158
                                        endIndex = header.indexOf(searchText);
159
                                        if (endIndex > -1){
160
                                                encoding = header.substring(0, endIndex);
161
                                        }
162
                                }
163
                                //To finish to parse the header
164
                                if (endIndex > -1){
165
                                        String searchText = ">";
166
                                        int headerEndIndex = header.indexOf(searchText);
167
                                        if (headerEndIndex > -1){
168
                                                endHeader = true;
169
                                        }
170
                                }
171
                                i++;
172
                        }                
173
                } catch (IOException e) {
174
                        throw new KmlException(getInputStream(),e);
175
                }                
176
                return encoding;
177
        }        
178
}