svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / utils / EncodingXMLParser.java @ 40769
History | View | Annotate | Download (2.56 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | 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 | /**
|
||
37 | * This class is a XML pull parser that discover and manage
|
||
38 | * the file encoding
|
||
39 | * @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
||
40 | */
|
||
41 | public class EncodingXMLParser extends KXmlParser{ |
||
42 | |||
43 | /**
|
||
44 | * This method reads the first bytes of the file and
|
||
45 | * try to discover the file encoding. It parses the file
|
||
46 | * with retrieved encoding.
|
||
47 | * @param file
|
||
48 | * @throws XmlPullParserException
|
||
49 | * @throws IOException
|
||
50 | */
|
||
51 | public void setInput(File file) throws XmlPullParserException, IOException{ |
||
52 | FileReader reader = null; |
||
53 | reader = new FileReader(file); |
||
54 | BufferedReader br = new BufferedReader(reader); |
||
55 | String encoding = "UTF-8"; |
||
56 | |||
57 | char[] buffer = new char[(int) file.length()]; |
||
58 | reader.read(buffer); |
||
59 | String string = new String(buffer); |
||
60 | |||
61 | // patch for ArcIMS + WMS connector > 9.0 bug
|
||
62 | int a = string.toLowerCase().indexOf("<?xml"); |
||
63 | if (a !=-1){ |
||
64 | string = string.substring(a, string.length()); |
||
65 | } |
||
66 | // end patch
|
||
67 | |||
68 | StringBuffer st = new StringBuffer(string); |
||
69 | String searchText = "encoding=\""; |
||
70 | int index = st.indexOf(searchText);
|
||
71 | if (index>-1) { |
||
72 | st.delete(0, index+searchText.length());
|
||
73 | encoding = st.substring(0, st.indexOf("\"")); |
||
74 | } |
||
75 | |||
76 | if (a > 0){ |
||
77 | // patch for ArcIMS + WMS connector > 9.0 bug
|
||
78 | super.setInput(new StringReader(string)); |
||
79 | // end patch
|
||
80 | }else{
|
||
81 | super.setInput( new FileInputStream(file), encoding); |
||
82 | } |
||
83 | } |
||
84 | } |