svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.xml2db / org.gvsig.xml2db.lib / org.gvsig.xml2db.lib.impl / src / main / java / org / gvsig / xml2db / lib / impl / Xml2dbCommons.java @ 47360
History | View | Annotate | Download (5.45 KB)
1 |
package org.gvsig.xml2db.lib.impl; |
---|---|
2 |
|
3 |
import static com.vividsolutions.jts.geom.CoordinateSequence.M; |
4 |
import java.io.BufferedInputStream; |
5 |
import java.io.BufferedReader; |
6 |
import java.io.File; |
7 |
import java.io.FileInputStream; |
8 |
import java.io.IOException; |
9 |
import java.io.InputStream; |
10 |
import java.io.InputStreamReader; |
11 |
import java.io.Reader; |
12 |
import java.nio.charset.Charset; |
13 |
import java.nio.charset.IllegalCharsetNameException; |
14 |
import java.nio.charset.UnsupportedCharsetException; |
15 |
import org.apache.commons.io.IOUtils; |
16 |
import org.apache.commons.io.input.BOMInputStream; |
17 |
import org.apache.commons.io.input.CloseShieldInputStream; |
18 |
import org.apache.commons.lang3.StringUtils; |
19 |
import org.apache.tika.config.TikaConfig; |
20 |
import org.apache.tika.detect.AutoDetectReader; |
21 |
import org.apache.tika.detect.EncodingDetector; |
22 |
import org.apache.tika.metadata.Metadata; |
23 |
import org.gvsig.tools.ToolsLocator; |
24 |
import org.gvsig.tools.i18n.I18nManager; |
25 |
import org.gvsig.tools.task.SimpleTaskStatus; |
26 |
import org.xml.sax.InputSource; |
27 |
|
28 |
/**
|
29 |
*
|
30 |
* @author jjdelcerro
|
31 |
*/
|
32 |
@SuppressWarnings("UseSpecificCatch") |
33 |
public class Xml2dbCommons { |
34 |
|
35 |
public static Charset detectCharset(InputStream is) { |
36 |
try {
|
37 |
AutoDetectReader reader = new AutoDetectReader(is);
|
38 |
return reader.getCharset();
|
39 |
} catch(Throwable t) { |
40 |
return null; |
41 |
} |
42 |
} |
43 |
|
44 |
public static String detectCharsetName(InputStream is) { |
45 |
Charset charset = detectCharset(is);
|
46 |
if( charset==null ) { |
47 |
return null; |
48 |
} |
49 |
return charset.name();
|
50 |
} |
51 |
|
52 |
public static InputSource openReader(File xmlfile, Charset charset) { |
53 |
try {
|
54 |
FileInputStream fis = new FileInputStream(xmlfile); |
55 |
|
56 |
InputSource is = new InputSource();
|
57 |
is.setPublicId(xmlfile.getAbsolutePath()); |
58 |
is.setByteStream(fis); |
59 |
if( charset!=null ) { |
60 |
is.setEncoding(charset.name()); |
61 |
} |
62 |
return openReader(is);
|
63 |
} catch(Throwable t) { |
64 |
throw new RuntimeException("Can't open xml input stream.",t); |
65 |
} |
66 |
} |
67 |
|
68 |
public static InputSource openReader(InputStream xml, Charset charset) { |
69 |
InputSource is = new InputSource();
|
70 |
is.setByteStream(xml); |
71 |
if( charset!=null ) { |
72 |
is.setEncoding(charset.name()); |
73 |
} |
74 |
return openReader(is);
|
75 |
} |
76 |
|
77 |
public static InputSource openReader(InputSource is) { |
78 |
try {
|
79 |
if(StringUtils.isBlank(is.getEncoding())){
|
80 |
// EncodingDetector encodingDetector = TikaConfig.getDefaultConfig().getEncodingDetector();
|
81 |
// BufferedInputStream bis = new BufferedInputStream(is.getByteStream());
|
82 |
// Charset charset = encodingDetector.detect(bis, new Metadata());
|
83 |
// is.setEncoding(charset.name());
|
84 |
// is.setByteStream(bis);
|
85 |
AutoDetectReader reader = new AutoDetectReader(is.getByteStream());
|
86 |
is.setCharacterStream(reader); |
87 |
is.setEncoding(reader.getCharset().name()); |
88 |
} else {
|
89 |
BOMInputStream bomIs = new BOMInputStream(is.getByteStream());
|
90 |
is.setByteStream(bomIs); |
91 |
InputStreamReader reader = new InputStreamReader( |
92 |
is.getByteStream(), |
93 |
is.getEncoding() |
94 |
); |
95 |
is.setCharacterStream(reader); |
96 |
} |
97 |
return is;
|
98 |
} catch(Throwable t) { |
99 |
throw new RuntimeException("Can't open xml input stream.",t); |
100 |
} |
101 |
} |
102 |
|
103 |
public static long countLines(File xml, Charset charset, SimpleTaskStatus status) { |
104 |
try {
|
105 |
FileInputStream fis = new FileInputStream(xml); |
106 |
return countLines(fis, charset, status);
|
107 |
} catch(Throwable t) { |
108 |
throw new RuntimeException("Can't count lines.",t); |
109 |
} |
110 |
} |
111 |
|
112 |
public static long countLines(InputStream xml, Charset charset, SimpleTaskStatus status) { |
113 |
try {
|
114 |
long count = 0; |
115 |
// Reader reader = null;
|
116 |
BufferedReader br = null; |
117 |
status.setIndeterminate(); |
118 |
status.setCurValue(0);
|
119 |
try {
|
120 |
InputSource is = new InputSource(xml);
|
121 |
if( charset!=null ) { |
122 |
is.setEncoding(charset.name()); |
123 |
} |
124 |
is = openReader(is); |
125 |
br = new BufferedReader(is.getCharacterStream()); |
126 |
int n = 1; |
127 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
128 |
while( br.readLine()!=null ) { |
129 |
if(count%n == 0){ |
130 |
status.message(i18n.getTranslation("_Calculating_lines"));
|
131 |
status.setCurValue(count); |
132 |
} |
133 |
// status.incrementCurrentValue();
|
134 |
|
135 |
count++; |
136 |
if(count > 100000){ |
137 |
n = 10000;
|
138 |
} else if(count > 10000){ |
139 |
n = 1000;
|
140 |
} else if(count > 1000){ |
141 |
n = 100;
|
142 |
} else if(count > 100){ |
143 |
n = 10;
|
144 |
} |
145 |
} |
146 |
} finally {
|
147 |
IOUtils.closeQuietly(br); |
148 |
} |
149 |
return count;
|
150 |
} catch(Throwable t) { |
151 |
throw new RuntimeException("Can't count lines.",t); |
152 |
} |
153 |
} |
154 |
} |