svn-gvsig-desktop / tags / v1_1_Build_1011 / libraries / libRemoteServices / src / org / gvsig / remoteClient / utils / EncodingXMLParser.java @ 12904
History | View | Annotate | Download (3.16 KB)
1 | 12484 | jorpiell | package org.gvsig.remoteClient.utils; |
---|---|---|---|
2 | |||
3 | import java.io.BufferedReader; |
||
4 | import java.io.ByteArrayInputStream; |
||
5 | import java.io.File; |
||
6 | import java.io.FileInputStream; |
||
7 | import java.io.FileNotFoundException; |
||
8 | import java.io.FileReader; |
||
9 | import java.io.IOException; |
||
10 | import java.io.Reader; |
||
11 | |||
12 | import org.kxml2.io.KXmlParser; |
||
13 | import org.xmlpull.v1.XmlPullParserException; |
||
14 | |||
15 | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
||
16 | *
|
||
17 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
18 | *
|
||
19 | * This program is free software; you can redistribute it and/or
|
||
20 | * modify it under the terms of the GNU General Public License
|
||
21 | * as published by the Free Software Foundation; either version 2
|
||
22 | * of the License, or (at your option) any later version.
|
||
23 | *
|
||
24 | * This program is distributed in the hope that it will be useful,
|
||
25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
27 | * GNU General Public License for more details.
|
||
28 | *
|
||
29 | * You should have received a copy of the GNU General Public License
|
||
30 | * along with this program; if not, write to the Free Software
|
||
31 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
32 | *
|
||
33 | * For more information, contact:
|
||
34 | *
|
||
35 | * Generalitat Valenciana
|
||
36 | * Conselleria d'Infraestructures i Transport
|
||
37 | * Av. Blasco Ib??ez, 50
|
||
38 | * 46010 VALENCIA
|
||
39 | * SPAIN
|
||
40 | *
|
||
41 | * +34 963862235
|
||
42 | * gvsig@gva.es
|
||
43 | * www.gvsig.gva.es
|
||
44 | *
|
||
45 | * or
|
||
46 | *
|
||
47 | * IVER T.I. S.A
|
||
48 | * Salamanca 50
|
||
49 | * 46005 Valencia
|
||
50 | * Spain
|
||
51 | *
|
||
52 | * +34 963163400
|
||
53 | * dac@iver.es
|
||
54 | */
|
||
55 | /* CVS MESSAGES:
|
||
56 | *
|
||
57 | * $Id$
|
||
58 | * $Log$
|
||
59 | * Revision 1.1.2.1 2007-07-05 12:30:11 jorpiell
|
||
60 | * The EncodingXMLParser has been created. This parser has to be used instead of the KxmlParser
|
||
61 | *
|
||
62 | *
|
||
63 | */
|
||
64 | /**
|
||
65 | * This class is a XML pull parser that discover and manage
|
||
66 | * the file encoding
|
||
67 | * @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
||
68 | */
|
||
69 | public class EncodingXMLParser extends KXmlParser{ |
||
70 | |||
71 | /**
|
||
72 | * * This method reads the first bytes of the file and
|
||
73 | * try to discover the file encoding. It parses the file
|
||
74 | * with retrieved encoding.
|
||
75 | * @param file
|
||
76 | * @throws XmlPullParserException
|
||
77 | * @throws FileNotFoundException
|
||
78 | */
|
||
79 | public void setInput(File file) throws FileNotFoundException, XmlPullParserException{ |
||
80 | FileReader reader = null; |
||
81 | String encoding = "UTF-8"; |
||
82 | try {
|
||
83 | reader = new FileReader(file); |
||
84 | BufferedReader br = new BufferedReader(reader); |
||
85 | char[] buffer = new char[100]; |
||
86 | br.read(buffer); |
||
87 | String string = new String(buffer); |
||
88 | |||
89 | // patch for ArcIMS + WMS connector > 9.0 bug
|
||
90 | int a = string.toLowerCase().indexOf("<?xml"); |
||
91 | if (a !=-1) |
||
92 | string = string.substring(a, string.length()); |
||
93 | // end patch
|
||
94 | |||
95 | StringBuffer st = new StringBuffer(string); |
||
96 | String searchText = "encoding=\""; |
||
97 | int index = st.indexOf(searchText);
|
||
98 | if (index>-1) { |
||
99 | st.delete(0, index+searchText.length());
|
||
100 | encoding = st.substring(0, st.indexOf("\"")); |
||
101 | } |
||
102 | } catch(FileNotFoundException ex) { |
||
103 | ex.printStackTrace(); |
||
104 | } catch (IOException e) { |
||
105 | e.printStackTrace(); |
||
106 | } |
||
107 | super.setInput(new FileInputStream(file), encoding); |
||
108 | } |
||
109 | } |