Statistics
| Revision:

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 / ProjectionExtractorImpl.java @ 47347

History | View | Annotate | Download (5.47 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.xml2db.lib.impl;
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 javax.xml.parsers.SAXParser;
17
import javax.xml.parsers.SAXParserFactory;
18
import org.apache.commons.lang3.StringUtils;
19
import org.apache.commons.lang3.mutable.MutableObject;
20
import org.cresques.cts.IProjection;
21
import org.gvsig.fmap.crs.CRSFactory;
22
import org.gvsig.fmap.dal.DALLocator;
23
import org.gvsig.fmap.dal.DataManager;
24
import org.gvsig.tools.ToolsLocator;
25
import org.gvsig.tools.i18n.I18nManager;
26
import org.gvsig.tools.task.SimpleTaskStatus;
27
import org.slf4j.Logger;
28
import org.slf4j.LoggerFactory;
29
import org.xml.sax.Attributes;
30
import org.xml.sax.InputSource;
31
import org.xml.sax.Locator;
32
import org.xml.sax.SAXException;
33
import org.xml.sax.helpers.DefaultHandler;
34

    
35
/**
36
 *
37
 * @author jjdelcerro
38
 */
39
public class ProjectionExtractorImpl {
40

    
41
    private static final Logger LOGGER = LoggerFactory.getLogger(ProjectionExtractorImpl.class);
42
    
43
    private static class ProjectionExtractorException extends RuntimeException {
44
        
45
    }
46
    
47
    private IProjection extractProjection(InputSource is, SimpleTaskStatus status) {
48
        if( is == null || is.getCharacterStream() == null ) {
49
            throw new IllegalArgumentException("input source is null");
50
        }
51
        MutableObject<IProjection> proj = new MutableObject<>();
52
        try {
53
            final DataManager dataManager = DALLocator.getDataManager();
54
            I18nManager i18n = ToolsLocator.getI18nManager();
55
            status.message(i18n.getTranslation("_Reading_xml"));
56
            status.setIndeterminate();
57
            
58
            SAXParserFactory spf = SAXParserFactory.newInstance();
59
            spf.setNamespaceAware(true);
60
            SAXParser saxParser = spf.newSAXParser();
61
            
62
            List<String> path = new ArrayList<>();
63
            
64
            saxParser.parse(is, new DefaultHandler() {
65
                private Locator locator;
66
                int size;
67
                int refreshInterval = 1;
68
                StringBuilder chars = new StringBuilder();
69
                
70
                @Override
71
                public void setDocumentLocator(Locator locator) {
72
                    this.locator = locator;
73
                }
74
                
75
                @Override
76
                public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
77
                    for (int i = 0; i < attributes.getLength(); i++) {
78
                        String name = attributes.getLocalName(i);                        
79
                        String value = attributes.getValue(i);
80
                        if( StringUtils.containsIgnoreCase(name, "srid") ) {
81
                            proj.setValue(getProjection(value));
82
                            if( proj.getValue() !=null ) {
83
                                throw new ProjectionExtractorException();
84
                            }
85
                        }
86
                    }
87
                    chars.setLength(0);
88
                }
89
                
90
                @Override
91
                public void endElement(String uri, String localName, String qName) throws SAXException {
92
                    String value = this.chars.toString();
93
                    if (StringUtils.containsIgnoreCase(localName, "srid")) {
94
                        proj.setValue(getProjection(value));
95
                        if (proj.getValue() != null) {
96
                            throw new ProjectionExtractorException();
97
                        }
98
                    }
99
                    chars.setLength(0);
100
                }
101
                
102
                @Override
103
                public void characters(char[] ch, int start, int length) throws SAXException {
104
                    this.chars.append(ch, start, length);
105
                }
106
                
107
                
108
            });
109
            return null;
110
        } catch (ProjectionExtractorException ex) {
111
            return proj.getValue();
112
        } catch (Exception ex) {
113
            throw new RuntimeException("Can't extract projection.", ex);
114
        }
115
    }
116
    
117
    private IProjection getProjection(String value) {
118
        if( StringUtils.isBlank(value) ) {
119
            return null;
120
        }
121
        IProjection proj = null;
122
        try {
123
            proj = CRSFactory.getCRS(value);
124
        } catch(Throwable t) {
125
            
126
        }
127
        if( proj != null ) {
128
            return proj;
129
        }
130
        try {
131
            proj = CRSFactory.getCRS("EPSG:"+value);
132
        } catch(Throwable t) {
133
            
134
        }
135
        return proj;
136
    }
137
    
138
    public IProjection extractProjection(File xml, Charset charset, SimpleTaskStatus status) {
139
        InputSource is = Xml2dbCommons.openReader(xml,charset);
140
        return extractProjection(is, status);
141
    }
142
    
143
    public IProjection extractProjection(InputStream xml, Charset charset, SimpleTaskStatus status)  {
144
        InputSource is = Xml2dbCommons.openReader(xml, charset);
145
        return extractProjection(is, status);
146
    }
147
    
148
    public IProjection extractProjection(Reader reader, SimpleTaskStatus status) {
149
        InputSource is = new InputSource(reader);
150
        return extractProjection(is, status);
151
    }
152
    
153
}