Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGPE-XML / src / org / gvsig / gpe / xml / parser / GPEXmlParser.java @ 21991

History | View | Annotate | Download (5.48 KB)

1
package org.gvsig.gpe.xml.parser;
2

    
3
import java.io.File;
4
import java.io.FileNotFoundException;
5
import java.io.IOException;
6
import java.io.InputStream;
7
import java.io.PushbackInputStream;
8

    
9
import org.gvsig.gpe.parser.GPEParser;
10
import org.gvsig.gpe.xml.exceptions.GPEXmlEmptyFileException;
11
import org.gvsig.gpe.xml.stream.IXmlStreamReader;
12
import org.gvsig.gpe.xml.stream.XmlStreamException;
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: GPEXmlParser.java 193 2007-11-26 08:58:56Z jpiera $
57
 * $Log$
58
 * Revision 1.11  2007/06/22 12:22:40  jorpiell
59
 * The typeNotFoundException has been deleted. It never was thrown
60
 *
61
 * Revision 1.10  2007/06/14 13:50:05  jorpiell
62
 * The schema jar name has been changed
63
 *
64
 * Revision 1.9  2007/06/07 14:53:30  jorpiell
65
 * Add the schema support
66
 *
67
 * Revision 1.8  2007/05/09 06:54:25  jorpiell
68
 * Change the File by URI
69
 *
70
 * Revision 1.7  2007/05/07 07:07:04  jorpiell
71
 * Add a constructor with the name and the description fields
72
 *
73
 * Revision 1.6  2007/04/19 12:01:55  jorpiell
74
 * Updated the getEncoding method
75
 *
76
 * Revision 1.5  2007/04/19 11:56:03  csanchez
77
 * Actualizacion protoripo libGPE
78
 *
79
 * Revision 1.4  2007/04/19 07:30:40  jorpiell
80
 * Add the add methods to teh contenhandler and change the register mode
81
 *
82
 * Revision 1.3  2007/04/18 12:54:45  csanchez
83
 * Actualizacion protoripo libGPE
84
 *
85
 * Revision 1.2  2007/04/12 11:47:15  jorpiell
86
 * Add a getParser method
87
 *
88
 * Revision 1.1  2007/04/12 10:23:41  jorpiell
89
 * Add some writers and the GPEXml parser
90
 *
91
 *
92
 */
93
/**
94
 * This class can be implemented by all the classes that implements a GPE driver based on the XML
95
 * format.
96
 * 
97
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
98
 */
99
public abstract class GPEXmlParser extends GPEParser {
100
    private IXmlStreamReader parser = null;
101

    
102
    public GPEXmlParser() {
103
        super();
104
    }
105

    
106
    /*
107
     * (non-Javadoc)
108
     * 
109
     * @see org.gvsig.gpe.GPEParser#parseStream()
110
     */
111
    protected void parseStream() {
112
        try {
113
            parser = getXmlReader();            
114
            initParse();
115

    
116
        } // EL PARSER LANZAR? UN EVENTO AL ERRORHANDLER SEG?N EL ERROR RECOGIDO
117
        catch (IOException e) {
118
            getErrorHandler().addError(e);
119
        } catch (GPEXmlEmptyFileException e) {
120
            getErrorHandler().addError(e);
121
        }
122
    }
123

    
124
    private IXmlStreamReader getXmlReader() throws XmlStreamException {
125
        PushbackInputStream pushBackStream = new PushbackInputStream(getInputStream());
126

    
127
        IXmlStreamReader reader;
128
        try {
129
            int firstByte = pushBackStream.read();
130
            pushBackStream.unread(firstByte);
131
            
132
            if (0x01 == firstByte) {
133
                // binary
134
                reader = GPEXmlParserFactory.getParser("text/x-bxml", pushBackStream);
135
            } else {
136
                // text
137
                reader = GPEXmlParserFactory.getParser("text/xml", pushBackStream);
138
            }
139
        } catch (IOException e) {
140
            throw new XmlStreamException(e);
141
        }
142
        return reader;
143
    }
144
    
145
    /*
146
     * (non-Javadoc)
147
     * 
148
     * @see org.gvsig.gpe.GPEParser#parseURI()
149
     */
150
    protected void parseURI() {
151
        try {
152
            // Creates an input stream to read the file
153
            File file = new File(getMainFile());
154
            InputStream inputStream = createInputStream(file);
155
            setInputStream(inputStream);
156
            parseStream();
157
        } catch (FileNotFoundException e) {
158
            System.out.println("ERROR: Fichero " + getMainFile().getPath() + " no encontrado");
159
            getErrorHandler().addError(e);
160
        } catch (IOException e) {
161
            System.out.println("ERROR: No se puede leer/escribir la codificaci?n del fichero: "
162
                    + e.getMessage());
163
            getErrorHandler().addError(e);
164
        }
165
    }
166

    
167
    /**
168
     * Creates an input stream from a file.
169
     * 
170
     * @param file
171
     * @return InputStream
172
     * @throws FileNotFoundException
173
     */
174
    protected abstract InputStream createInputStream(File file) throws FileNotFoundException;
175

    
176
    /**
177
     * This method start the parse process. It is called after the XML parser is initialized
178
     */
179
    protected abstract void initParse() throws GPEXmlEmptyFileException, XmlStreamException;
180

    
181
    /**
182
     * @return the parser
183
     */
184
    protected IXmlStreamReader getParser() {
185
        return parser;
186
    }
187

    
188
}