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.csv / src / main / java / org / gvsig / fmap / dal / store / gml / virtualrows / StructureExtractorImpl.java @ 47638

History | View | Annotate | Download (9.67 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.store.gml.virtualrows;
7

    
8
import java.io.File;
9
import java.io.FileNotFoundException;
10
import java.io.IOException;
11
import java.io.InputStream;
12
import java.io.Reader;
13
import java.nio.charset.Charset;
14
import java.util.ArrayList;
15
import java.util.List;
16
import java.util.Locale;
17
import javax.xml.parsers.SAXParser;
18
import javax.xml.parsers.SAXParserFactory;
19
import org.apache.commons.lang3.StringUtils;
20
import org.apache.tika.utils.CharsetUtils;
21
import org.gvsig.fmap.dal.DALLocator;
22
import org.gvsig.fmap.dal.DataManager;
23
import org.gvsig.fmap.dal.store.gml.virtualrows.xmlinfo.XMLAttributeInfoImpl;
24
import org.gvsig.fmap.dal.store.gml.virtualrows.xmlinfo.XMLInfoImpl;
25
import org.gvsig.tools.ToolsLocator;
26
import org.gvsig.tools.i18n.I18nManager;
27
import org.gvsig.tools.task.SimpleTaskStatus;
28
import org.slf4j.Logger;
29
import org.slf4j.LoggerFactory;
30
import org.xml.sax.Attributes;
31
import org.xml.sax.InputSource;
32
import org.xml.sax.Locator;
33
import org.xml.sax.SAXException;
34
import org.xml.sax.helpers.DefaultHandler;
35

    
36
/**
37
 *
38
 * @author jjdelcerro
39
 */
40
public class StructureExtractorImpl { //implements StructureExtractor {
41

    
42
    private static final Logger LOGGER = LoggerFactory.getLogger(StructureExtractorImpl.class);
43

    
44
    @SuppressWarnings("UseSpecificCatch")
45
    private void extractTags(XMLInfoImpl xmlinfo, Reader reader, SimpleTaskStatus status) {
46
        if( reader == null ) {
47
            throw new IllegalArgumentException("reader is null");
48
        }
49
        try {
50
            final DataManager dataManager = DALLocator.getDataManager();
51
            I18nManager i18n = ToolsLocator.getI18nManager();
52
            status.message(i18n.getTranslation("_Reading_xml")+" 1/5");
53
            status.setRangeOfValues(0, xmlinfo.getCountLines());
54
            
55
            SAXParserFactory spf = SAXParserFactory.newInstance();
56
            spf.setNamespaceAware(true);
57
            SAXParser saxParser = spf.newSAXParser();
58
            InputSource is = new InputSource(reader);
59
            
60
            List<String> path = new ArrayList<>();
61
            
62
            saxParser.parse(is, new DefaultHandler() {
63
                private Locator locator;
64
                int size;
65
                int refreshInterval = 1;
66
                StringBuilder chars = new StringBuilder();
67
                
68
                @Override
69
                public void setDocumentLocator(Locator locator) {
70
                    this.locator = locator;
71
                }
72
                
73
                @Override
74
                public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
75
                    int line = this.locator.getLineNumber();
76
                    int column = this.locator.getColumnNumber()-2-localName.length();
77

    
78
                    if(line % refreshInterval == 0) {
79
                        status.setCurValue(line);
80
                    }
81
                    
82
                    if(line > 100000){
83
                        refreshInterval = 10000;
84
                    } else if(line > 10000){
85
                        refreshInterval = 1000;
86
                    } else if(line > 1000){
87
                        refreshInterval = 100;
88
                    } else if(line > 100){
89
                        refreshInterval = 10;
90
                    }
91

    
92

    
93
                    String idvalue = dataManager.createUniqueID();
94
                    
95
                    path.add(localName);
96
                    String path_s = StringUtils.join(path, "/");
97
                    XMLAttributeInfoImpl info = xmlinfo.getTag(path_s);
98
                    if( info == null ) {
99
                        info = new XMLAttributeInfoImpl(xmlinfo.getLocale(), path_s, getNs(qName));
100
                        xmlinfo.addTag(info);
101
                        
102
                    }
103
                    if( path.size()>1 ) {
104
                        List<String> parentpath = path.subList(0, path.size()-1);
105
                        String parentpath_s = StringUtils.join(parentpath, "/");
106
                        XMLAttributeInfoImpl parentinfo = xmlinfo.getTag(parentpath_s);
107
                        parentinfo.incrChildCount(localName);
108
                        parentinfo.setLastChildID(localName, idvalue);
109
                    }
110
                    
111
                    for (int i = 0; i < attributes.getLength(); i++) {
112
                        String name = attributes.getLocalName(i);                        
113
                        String value = attributes.getValue(i);
114
                        String idvalueChild = dataManager.createUniqueID();
115
                        XMLAttributeInfoImpl infoChild = xmlinfo.getTag(path_s+"/"+name);
116
                        if( infoChild == null ) {
117
                            infoChild = new XMLAttributeInfoImpl(xmlinfo.getLocale(), path_s+"/"+name, getNs(qName));
118
                            infoChild.setIsAttr(true);
119
                            xmlinfo.addTag(infoChild);
120
                        }
121
                        info.incrChildCount(infoChild.getName());
122
                        info.setLastChildID(infoChild.getName(), idvalueChild);
123
                        infoChild.addValue(value);
124
                    }
125
                    chars.setLength(0);
126
                }
127
                
128
                @Override
129
                public void endElement(String uri, String localName, String qName) throws SAXException {
130
                    int line = this.locator.getLineNumber();
131

    
132
//                    status.setCurValue(line);
133

    
134
                    String path_s = StringUtils.join(path, "/");
135
                    XMLAttributeInfoImpl info = xmlinfo.getTag(path_s);
136
                    if( info == null ) {
137
                        info = new XMLAttributeInfoImpl(xmlinfo.getLocale(), path_s, getNs(qName));
138
                        xmlinfo.addTag(info);
139
                    }
140

    
141
                    XMLAttributeInfoImpl parentinfo = null;
142
                    if( path.size()>1 ) {
143
                        List<String> parentpath = path.subList(0, path.size()-1);
144
                        String parentpath_s = StringUtils.join(parentpath, "/");
145
                        parentinfo = xmlinfo.getTag(parentpath_s);
146
                    }
147
                    
148
                    if( info.hasChilds() || (parentinfo != null && (parentinfo.getChildsCount(localName)>1))) {
149
                        String value = this.chars.toString();
150
                        if( StringUtils.isNotBlank(value) ) {
151
                            String name = info.getName()+"$v";                        
152
                            String idvalueChild = dataManager.createUniqueID();
153
                            XMLAttributeInfoImpl infoChild = xmlinfo.getTag(path_s+"/"+name);
154
                            if( infoChild == null ) {
155
                                infoChild = new XMLAttributeInfoImpl(xmlinfo.getLocale(), path_s+"/"+name, getNs(qName));
156
                                xmlinfo.addTag(infoChild);
157
                            }
158
                            info.incrChildCount(infoChild.getName());
159
                            info.setLastChildID(infoChild.getName(), idvalueChild);
160
                            infoChild.addValue(value);
161
                        }
162
                    } else {
163
                        String value = this.chars.toString();
164
                        info.addValue(value);
165
                    }
166
                    info.consolidateChildCounters();
167
                    path.remove(path.size()-1);
168
                    chars.setLength(0);
169
                }
170
                
171
                @Override
172
                public void characters(char[] ch, int start, int length) throws SAXException {
173
                    int line = this.locator.getLineNumber();
174

    
175
//                    status.setCurValue(line);
176
                    this.chars.append(ch, start, length);
177
                }
178
                
179
                
180
            });
181
        } catch (Exception ex) {
182
            throw new RuntimeException("Can't extract tags.", ex);
183
        }
184
    }
185
    
186
    public XMLInfoImpl extractStructure(File xml, Charset charset, Locale locale, SimpleTaskStatus status) throws FileNotFoundException, IOException {
187
        XMLInfoImpl xmlinfo = new XMLInfoImpl();
188
        xmlinfo.setLocale(locale);
189
        long count = XmlCommons.countLines(xml, charset, status);
190
        xmlinfo.setCountLines(count);
191
        InputSource is = XmlCommons.openReader(xml,charset);
192
        return extractStructure(is, xmlinfo, status);
193
    }
194
    
195
    public XMLInfoImpl extractStructure(InputStream xml, Charset charset, Locale locale, SimpleTaskStatus status) throws IOException  {
196
        XMLInfoImpl xmlinfo = new XMLInfoImpl();
197
        xmlinfo.setLocale(locale);
198
        xmlinfo.setCountLines(-1);
199
        InputSource is = XmlCommons.openReader(xml, charset);
200
        return extractStructure(is, xmlinfo, status);
201
    }
202
    
203
    public XMLInfoImpl extractStructure(Reader reader, Locale locale, SimpleTaskStatus status) {
204
        XMLInfoImpl xmlinfo = new XMLInfoImpl();
205
        xmlinfo.setLocale(locale);
206
        InputSource is = new InputSource(reader);
207
        return extractStructure(is, xmlinfo, status);
208
    }
209
    
210
    public XMLInfoImpl extractStructure(InputSource is, XMLInfoImpl xmlinfo, SimpleTaskStatus status) {
211
        
212
        if( xmlinfo.getCharset()==null ) {
213
            xmlinfo.setCharset(CharsetUtils.forName(is.getEncoding()));
214
        }
215
        
216
        extractTags(xmlinfo, is.getCharacterStream(), status);
217
        
218
        return xmlinfo;
219
    }
220
    
221
    private String getNs(String s){
222
        String[] ss = s.split(":");
223
        if(ss.length == 1){
224
            return null;
225
        }
226
        return ss[0];
227
    }
228
    
229
}