Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.gml / src / main / java / org / gvsig / fmap / dal / store / gml / virtualrows / XmlCommons.java @ 47655

History | View | Annotate | Download (5.02 KB)

1
package org.gvsig.fmap.dal.store.gml.virtualrows;
2

    
3
import java.io.BufferedReader;
4
import java.io.File;
5
import java.io.FileInputStream;
6
import java.io.InputStream;
7
import java.io.InputStreamReader;
8
import java.nio.charset.Charset;
9
import org.apache.commons.io.IOUtils;
10
import org.apache.commons.io.input.BOMInputStream;
11
import org.apache.commons.lang3.StringUtils;
12
import org.apache.tika.detect.AutoDetectReader;
13
import org.gvsig.tools.ToolsLocator;
14
import org.gvsig.tools.i18n.I18nManager;
15
import org.gvsig.tools.task.SimpleTaskStatus;
16
import org.xml.sax.InputSource;
17

    
18
/**
19
 *
20
 * @author jjdelcerro
21
 */
22
@SuppressWarnings("UseSpecificCatch")
23
public class XmlCommons {
24

    
25
    public static Charset detectCharset(InputStream is) { 
26
        try {            
27
            AutoDetectReader reader = new AutoDetectReader(is);
28
            return reader.getCharset();
29
        } catch(Throwable t) {
30
            return null;
31
        }
32
    }
33
    
34
    public static String detectCharsetName(InputStream is) {        
35
        Charset charset = detectCharset(is);
36
        if( charset==null ) {
37
            return null;
38
        }
39
        return charset.name();
40
    }
41
    
42
    public static InputSource openReader(File xmlfile, Charset charset) {
43
        try {
44
            FileInputStream fis = new FileInputStream(xmlfile);
45

    
46
            InputSource is = new InputSource();
47
            is.setPublicId(xmlfile.getAbsolutePath());
48
            is.setByteStream(fis);
49
            if( charset!=null ) {
50
                is.setEncoding(charset.name());
51
            }            
52
            return openReader(is);
53
        } catch(Throwable t) {
54
            throw new RuntimeException("Can't open xml input stream.",t);
55
        }
56
    }
57
    
58
    public static InputSource openReader(InputStream xml, Charset charset) {
59
            InputSource is = new InputSource();
60
            is.setByteStream(xml);
61
            if( charset!=null ) {
62
                is.setEncoding(charset.name());
63
            }            
64
            return openReader(is);
65
    }
66
    
67
    public static InputSource openReader(InputSource is) {
68
        try {            
69
            if(StringUtils.isBlank(is.getEncoding())){
70
//                EncodingDetector encodingDetector = TikaConfig.getDefaultConfig().getEncodingDetector();
71
//                BufferedInputStream bis = new BufferedInputStream(is.getByteStream());
72
//                Charset charset = encodingDetector.detect(bis, new Metadata());
73
//                is.setEncoding(charset.name());
74
//                is.setByteStream(bis);
75
                AutoDetectReader reader = new AutoDetectReader(is.getByteStream());
76
                is.setCharacterStream(reader);
77
                is.setEncoding(reader.getCharset().name());
78
            } else {
79
                BOMInputStream bomIs = new BOMInputStream(is.getByteStream());
80
                is.setByteStream(bomIs);
81
                InputStreamReader reader = new InputStreamReader(
82
                        is.getByteStream(),
83
                        is.getEncoding()
84
                );
85
                is.setCharacterStream(reader);
86
            }
87
            return is;
88
        } catch(Throwable t) {
89
            throw new RuntimeException("Can't open xml input stream.",t);
90
        }
91
    }
92

    
93
    public static long countLines(File xml, Charset charset, SimpleTaskStatus status) {
94
        try {
95
            FileInputStream fis = new FileInputStream(xml);
96
            return countLines(fis, charset, status);
97
        } catch(Throwable t) {
98
            throw new RuntimeException("Can't count lines.",t);
99
        }
100
    }
101

    
102
    public static long countLines(InputStream xml, Charset charset, SimpleTaskStatus status) {
103
        try {
104
            long count = 0;
105
//            Reader reader = null;
106
            BufferedReader br = null;
107
            status.setIndeterminate();
108
            status.setCurValue(0);
109
            try {
110
                InputSource is = new InputSource(xml);
111
                if( charset!=null ) {
112
                    is.setEncoding(charset.name());
113
                }
114
                is = openReader(is);
115
                br = new BufferedReader(is.getCharacterStream());
116
                int n = 1;
117
                I18nManager i18n = ToolsLocator.getI18nManager();
118
                while( br.readLine()!=null ) {
119
                    if(count%n == 0){
120
                        status.message(i18n.getTranslation("_Calculating_lines"));
121
                        status.setCurValue(count);
122
                    }
123
//                    status.incrementCurrentValue();
124
            
125
                    count++;
126
                    if(count > 100000){
127
                        n = 10000;
128
                    } else if(count > 10000){
129
                        n = 1000;
130
                    } else if(count > 1000){
131
                        n = 100;
132
                    } else if(count > 100){
133
                        n = 10;
134
                    }
135
                }
136
            } finally {
137
                IOUtils.closeQuietly(br);
138
            }
139
            return count;
140
        } catch(Throwable t) {
141
            throw new RuntimeException("Can't count lines.",t);
142
        }
143
    }    
144
}