Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / libraries / libRemoteServices / src / org / gvsig / remoteClient / gml / factories / XMLParserFactory.java @ 9917

History | View | Annotate | Download (3.9 KB)

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

    
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.FileNotFoundException;
7
import java.io.FileReader;
8
import java.io.IOException;
9

    
10
import org.gvsig.remoteClient.gml.exceptions.GMLException;
11
import org.gvsig.remoteClient.gml.schemas.XMLSchemaParser;
12
import org.xmlpull.v1.XmlPullParserException;
13

    
14
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
15
 *
16
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
17
 *
18
 * This program is free software; you can redistribute it and/or
19
 * modify it under the terms of the GNU General Public License
20
 * as published by the Free Software Foundation; either version 2
21
 * of the License, or (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU General Public License
29
 * along with this program; if not, write to the Free Software
30
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
31
 *
32
 * For more information, contact:
33
 *
34
 *  Generalitat Valenciana
35
 *   Conselleria d'Infraestructures i Transport
36
 *   Av. Blasco Ib??ez, 50
37
 *   46010 VALENCIA
38
 *   SPAIN
39
 *
40
 *      +34 963862235
41
 *   gvsig@gva.es
42
 *      www.gvsig.gva.es
43
 *
44
 *    or
45
 *
46
 *   IVER T.I. S.A
47
 *   Salamanca 50
48
 *   46005 Valencia
49
 *   Spain
50
 *
51
 *   +34 963163400
52
 *   dac@iver.es
53
 */
54
/* CVS MESSAGES:
55
 *
56
 * $Id: XMLParserFactory.java 9917 2007-01-25 16:13:00Z jorpiell $
57
 * $Log$
58
 * Revision 1.1.2.1  2007-01-25 16:12:59  jorpiell
59
 * Se han sustituido las clases por las que hay en el nuevo driver de GML.
60
 *
61
 * Revision 1.3  2007/01/15 13:11:00  csanchez
62
 * Sistema de Warnings y Excepciones adaptado a BasicException
63
 *
64
 * Revision 1.2  2006/12/22 11:25:44  csanchez
65
 * Nuevo parser GML 2.x para gml's sin esquema
66
 *
67
 * Revision 1.1  2006/08/10 12:00:49  jorpiell
68
 * Primer commit del driver de Gml
69
 *
70
 *
71
 */
72
/**
73
 * Factory to create parsers to parse xml documents with
74
 * different schemas.
75
 * 
76
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
77
 * @author Carlos S?nchez Peri??n (sanchez_carper@gva.es)
78
 * 
79
 */
80
public class XMLParserFactory {
81
        private String encoding = "UTF-8";
82
        
83
        /**
84
         * Creates a new XML parser
85
         * First find the correct encoding to read the file
86
         * @param m_File
87
         * File to parse
88
         * @return
89
         * @throws IOException
90
         * @throws XmlPullParserException
91
         */
92
        public XMLSchemaParser createSchemaParser(File m_File) throws GMLException{
93
                FileReader reader = null;       
94
                try {
95
                        reader = new FileReader(m_File);
96
                } catch (FileNotFoundException e) {
97
                        // TODO Auto-generated catch block
98
                        throw new GMLException(m_File.getName(),e);
99
                }
100
                BufferedReader br = new BufferedReader(reader);
101
                char[] buffer = new char[100];
102
                try {
103
                        br.read(buffer);
104
                } catch (IOException e) {
105
                        // TODO Auto-generated catch block
106
                        throw new GMLException(m_File.getName(),e);
107
                }
108
                StringBuffer st = new StringBuffer(new String(buffer));
109
                
110
                // We find the encoding at the begining of the file
111
                
112
                String searchText = "encoding=\"";
113
                int index = st.indexOf(searchText);
114

    
115
                // If it find the encoding, it takes the new encoding, else it takes the default encoding "UTF-8"
116
                if (index>-1) { 
117
                        st.delete(0, index+searchText.length());
118
                        encoding = st.substring(0, st.indexOf("\""));
119
                }
120
                
121
                // make GML parser with the good enconding
122
                XMLSchemaParser parser = new XMLSchemaParser();
123
                
124
                // setImput(file,encoding): it sets the encoding to read with the KXML parser                
125
                try {
126
                        parser.setInput(new FileInputStream(m_File), encoding);
127
                } catch (FileNotFoundException e) {
128
                        // TODO Auto-generated catch block
129
                        throw new GMLException(m_File.getName(),e);
130
                } catch (XmlPullParserException e) {
131
                        // TODO Auto-generated catch block
132
                        throw new GMLException(m_File.getName(),e);
133
                }
134
                return parser;
135
        }
136
}