Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libRemoteServices / src / org / gvsig / remoteclient / utils / EncodingXMLParser.java @ 29671

History | View | Annotate | Download (2.96 KB)

1
package org.gvsig.remoteclient.utils;
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
import java.io.Reader;
10
import java.io.StringBufferInputStream;
11
import java.io.StringReader;
12

    
13
import org.kxml2.io.KXmlParser;
14
import org.xmlpull.v1.XmlPullParserException;
15

    
16
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
17
 *
18
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
19
 *
20
 * This program is free software; you can redistribute it and/or
21
 * modify it under the terms of the GNU General Public License
22
 * as published by the Free Software Foundation; either version 2
23
 * of the License, or (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU General Public License
31
 * along with this program; if not, write to the Free Software
32
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
33
 *
34
 * For more information, contact:
35
 *
36
 *  Generalitat Valenciana
37
 *   Conselleria d'Infraestructures i Transport
38
 *   Av. Blasco Ib??ez, 50
39
 *   46010 VALENCIA
40
 *   SPAIN
41
 *
42
 *      +34 963862235
43
 *   gvsig@gva.es
44
 *      www.gvsig.gva.es
45
 *
46
 *    or
47
 *
48
 *   IVER T.I. S.A
49
 *   Salamanca 50
50
 *   46005 Valencia
51
 *   Spain
52
 *
53
 *   +34 963163400
54
 *   dac@iver.es
55
 */
56
/* CVS MESSAGES:
57
 *
58
 * $Id$
59
 * $Log$
60
 *
61
 */
62
/**
63
 * This class is a XML pull parser that discover and manage
64
 * the file encoding
65
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
66
 */
67
public class EncodingXMLParser extends KXmlParser{
68

    
69
        /**
70
         * This method reads the first bytes of the file and
71
         * try to discover the file encoding. It parses the file
72
         * with retrieved encoding.
73
         * @param file
74
         * @throws XmlPullParserException 
75
         * @throws IOException 
76
         */
77
        public void setInput(File file) throws XmlPullParserException, IOException{
78
                FileReader reader = null;
79
                reader = new FileReader(file);
80
                BufferedReader br = new BufferedReader(reader);
81
                String encoding = "UTF-8";
82

    
83
                char[] buffer = new char[(int) file.length()];
84
                reader.read(buffer);
85
                String string = new String(buffer);
86

    
87
                // patch for ArcIMS + WMS connector > 9.0 bug
88
                int a = string.toLowerCase().indexOf("<?xml");
89
                if (a !=-1){
90
                        string = string.substring(a, string.length());
91
                }
92
                // end patch
93

    
94
                StringBuffer st = new StringBuffer(string);
95
                String searchText = "encoding=\"";
96
                int index = st.indexOf(searchText);
97
                if (index>-1) {
98
                        st.delete(0, index+searchText.length());
99
                        encoding = st.substring(0, st.indexOf("\""));
100
                }                        
101
                
102
                if (a > 0){
103
                        // patch for ArcIMS + WMS connector > 9.0 bug
104
                        super.setInput(new StringReader(string));
105
                        // end patch
106
                }else{
107
                        super.setInput( new FileInputStream(file), encoding);
108
                }
109
        }
110
}
111