Statistics
| Revision:

gvsig-sldtools / org.gvsig.sld / org.gvsig.sld.app / org.gvsig.sld.app.mainplugin / src / main / java / org / gvsig / sldconverter / legend / driver / LegendToSLDWriter.java @ 46

History | View | Annotate | Download (5.4 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.sldconverter.legend.driver;
25

    
26
import java.io.File;
27
import java.io.IOException;
28

    
29
import org.gvsig.fmap.mapcontext.exceptions.WriteLegendException;
30
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
31
import org.gvsig.fmap.mapcontext.rendering.legend.driver.ILegendWriter;
32
import org.gvsig.sldconverter.SLDConverterLocator;
33
import org.gvsig.sldconverter.SLDConverterManager;
34
import org.gvsig.sldsupport.SLDSupportLocator;
35
import org.gvsig.sldsupport.SLDSupportManager;
36
import org.gvsig.sldsupport.sld.SLDRoot;
37
import org.gvsig.sldsupport.sld.layer.SLDLayer;
38

    
39
/**
40
 * Legend writer based on the SLD converter
41
 */
42
public class LegendToSLDWriter implements ILegendWriter {
43

    
44
    public void write(ILegend legend, File outFile, String format)
45
        throws WriteLegendException, IOException {
46
        
47
        SLDSupportManager sldSupMan = null;
48
        sldSupMan = SLDSupportLocator.getInstance().getSLDSupportManager();
49
        if (sldSupMan == null) {
50
            throw new WriteLegendException(legend,
51
                new Exception("No SLD support manager registered."));
52
        }
53
        
54
        SLDConverterManager sldConMan = null;
55
        sldConMan = SLDConverterLocator.getInstance().getSLDConverterManager();        
56
        if (sldConMan == null) {
57
            throw new WriteLegendException(legend,
58
                new Exception("No SLD conversion manager registered."));
59
        }
60
        
61
        String fext = getFileExtension(format);
62
        if (fext.compareToIgnoreCase(SLDSupportManager.FILE_EXTENSION) != 0) {
63
            throw new WriteLegendException(
64
                legend, new Exception("Unexpected format requested: " + format));
65
        }
66
        
67
        String fver = this.getFormatVersion(format);
68
        if (fver == null) {
69
            fver = "1.0.0";
70
        }
71
        SLDLayer lyr = null;
72
        try {
73
            lyr = sldConMan.toSLDLayer(legend);
74
        } catch (Exception e) {
75
            throw new WriteLegendException(legend, e);
76
        }
77
        
78
        if (lyr == null) {
79
            throw new WriteLegendException(legend,
80
                new Exception("Unable to convert to SLD layer"));
81
        }
82
        
83
        SLDRoot root = new SLDRoot(fver);
84
        root.getLayers().add(lyr);
85
        
86
        try {
87
            sldSupMan.write(root, fver, outFile);
88
        } catch (Exception e) {
89
            throw new WriteLegendException(legend, e);
90
        }        
91
    }
92
    
93
    // ========================================================
94
    
95
    /**
96
     * Returns null if mime format is not parsed properly
97
     * 
98
     * @param fmt MIME format
99
     * 
100
     * @return
101
     */
102
    private String getFileExtension(String fmt) {
103
        // Example: "text/xml; subtype=gml/2.1.2" => "gml"
104
        if (fmt == null) {
105
            return null;
106
        }
107
        String[] parts = fmt.split(";");
108
        String aux = "";
109
        if (parts.length > 1) {
110
            aux = parts[1].trim();
111
        } else {
112
            aux = parts[0].trim();
113
        }
114
        parts = aux.split("=");
115
        if (parts.length > 1) {
116
            aux = parts[1].trim();
117
            // Example: aux = "gml/2.1.2"
118
            parts = aux.split("/");
119
            return parts[0].length() == 0 ? null : parts[0];
120
        } else {
121
            aux = parts[0].trim();
122
            // Example: "text/xml"
123
            parts = aux.split("/");
124
            if (parts.length > 1) {
125
                return parts[1].length() == 0 ? null : parts[1];
126
            } else {
127
                return aux.length() == 0 ? null : aux;
128
            }
129
        }
130
    }
131
    
132
    
133
    /**
134
     * Returns null if mime format is not parsed properly
135
     * 
136
     * @param fmt MIME format
137
     * 
138
     * @return
139
     */
140
    private String getFormatVersion(String fmt) {
141
        // Example: "text/xml; subtype=gml/2.1.2" => "2.1.2"
142
        if (fmt == null) {
143
            return null;
144
        }
145
        String[] parts = fmt.split(";");
146
        String aux = "";
147
        if (parts.length > 1) {
148
            aux = parts[1].trim();
149
        } else {
150
            return null;
151
        }
152
        parts = aux.split("=");
153
        if (parts.length > 1) {
154
            aux = parts[1].trim();
155
            // Example: aux = "gml/2.1.2"
156
            parts = aux.split("/");
157
            return parts[1].length() == 0 ? null : parts[1];
158
        } else {
159
            return null;
160
        }
161
    }   
162

    
163
    
164
    
165
    
166
    
167
}