Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / engine / AbstractDriver.java @ 10269

History | View | Annotate | Download (4.2 KB)

1
package org.gvsig.remoteClient.gml.engine;
2

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

    
6
import org.gvsig.exceptions.BaseException;
7
import org.gvsig.remoteClient.gml.engine.readers.IReaderFile;
8
import org.gvsig.remoteClient.gml.factories.IGeometriesFactory;
9
import org.gvsig.remoteClient.kml.exceptions.KmlException;
10

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

    
91
        /**
92
         * Initializes the IReader, tries to parse  the file header 
93
         * to obtain the version and parses all the needed schemas
94
         * @param factory
95
         * Geometries factory that is used to create the parsed 
96
         * geometries
97
         * @throws BaseException 
98
         */
99
        protected abstract void initialize(IGeometriesFactory factory) throws BaseException;
100

    
101
        /**
102
         * @return Returns the reader.
103
         */
104
        public IReaderFile getReader() {
105
                return reader;
106
        }
107

    
108
        /**
109
         * @param reader The reader to set.
110
         */
111
        protected void setReader(IReaderFile reader) {
112
                this.reader = reader;
113
        }
114

    
115
        /**
116
         * @return Returns the inputStream.
117
         */
118
        public InputStream getInputStream() {
119
                return inputStream;
120
        }
121

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