Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / utils / EncodingXMLParser.java @ 40559

History | View | Annotate | Download (3.79 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.remoteclient.utils;
25

    
26
import java.io.BufferedReader;
27
import java.io.File;
28
import java.io.FileInputStream;
29
import java.io.FileReader;
30
import java.io.IOException;
31
import java.io.StringReader;
32

    
33
import org.kxml2.io.KXmlParser;
34
import org.xmlpull.v1.XmlPullParserException;
35

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

    
89
        /**
90
         * This method reads the first bytes of the file and
91
         * try to discover the file encoding. It parses the file
92
         * with retrieved encoding.
93
         * @param file
94
         * @throws XmlPullParserException 
95
         * @throws IOException 
96
         */
97
        public void setInput(File file) throws XmlPullParserException, IOException{
98
                FileReader reader = null;
99
                reader = new FileReader(file);
100
                BufferedReader br = new BufferedReader(reader);
101
                String encoding = "UTF-8";
102

    
103
                char[] buffer = new char[(int) file.length()];
104
                reader.read(buffer);
105
                String string = new String(buffer);
106

    
107
                // patch for ArcIMS + WMS connector > 9.0 bug
108
                int a = string.toLowerCase().indexOf("<?xml");
109
                if (a !=-1){
110
                        string = string.substring(a, string.length());
111
                }
112
                // end patch
113

    
114
                StringBuffer st = new StringBuffer(string);
115
                String searchText = "encoding=\"";
116
                int index = st.indexOf(searchText);
117
                if (index>-1) {
118
                        st.delete(0, index+searchText.length());
119
                        encoding = st.substring(0, st.indexOf("\""));
120
                }                        
121
                
122
                if (a > 0){
123
                        // patch for ArcIMS + WMS connector > 9.0 bug
124
                        super.setInput(new StringReader(string));
125
                        // end patch
126
                }else{
127
                        super.setInput( new FileInputStream(file), encoding);
128
                }
129
        }
130
}
131