Statistics
| Revision:

gvsig-sldtools / org.gvsig.sld / org.gvsig.sldconverter / org.gvsig.sldconverter.lib / org.gvsig.sldconverter.lib.impl / src / main / java / org / gvsig / sldconverter / impl / DefaultSLDConverterManager.java @ 46

History | View | Annotate | Download (6.67 KB)

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

    
28
import java.util.HashSet;
29
import java.util.Iterator;
30
import java.util.Set;
31

    
32
import org.gvsig.fmap.mapcontext.rendering.legend.ILegend;
33
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
34
import org.gvsig.sldconverter.SLDConverterManager;
35
import org.gvsig.sldconverter.exception.UnsupportedLegendException;
36
import org.gvsig.sldconverter.exception.UnsupportedSymbolException;
37
import org.gvsig.sldconverter.legend.LegendToSLDConverter;
38
import org.gvsig.sldconverter.legend.LegendToSLDConverterFactory;
39
import org.gvsig.sldconverter.legend.SLDToLegendConverter;
40
import org.gvsig.sldconverter.legend.SLDToLegendConverterFactory;
41
import org.gvsig.sldconverter.symbol.SLDToSymbolConverter;
42
import org.gvsig.sldconverter.symbol.SLDToSymbolConverterFactory;
43
import org.gvsig.sldconverter.symbol.SymbolToSLDConverter;
44
import org.gvsig.sldconverter.symbol.SymbolToSLDConverterFactory;
45
import org.gvsig.sldsupport.exception.UnsupportedSLDObjectException;
46
import org.gvsig.sldsupport.sld.layer.SLDLayer;
47
import org.gvsig.sldsupport.sld.symbol.SLDSymbol;
48

    
49
public class DefaultSLDConverterManager implements  SLDConverterManager {
50

    
51
        private Set<SLDToLegendConverterFactory> legendFromSLD =
52
                        new HashSet<SLDToLegendConverterFactory>();
53
        private Set<LegendToSLDConverterFactory> legendToSLD =
54
                        new HashSet<LegendToSLDConverterFactory>();
55
        // =========================================================
56
        private Set<SLDToSymbolConverterFactory> symbolFromSLD =
57
                        new HashSet<SLDToSymbolConverterFactory>();
58
        private Set<SymbolToSLDConverterFactory> symbolToSLD =
59
                        new HashSet<SymbolToSLDConverterFactory>();
60
        // =========================================================
61
        
62
        
63
        public SLDSymbol toSLDSymbol(ISymbol sym) throws UnsupportedSymbolException {
64
                
65
                if (sym == null) {
66
                        throw new UnsupportedSymbolException("ISymbol", "Null");
67
                }
68
                
69
                int max = 0;
70
                SymbolToSLDConverterFactory factToUse = null;
71
                Iterator<SymbolToSLDConverterFactory> iter = symbolToSLD.iterator();
72
                SymbolToSLDConverterFactory fact = null;
73
                int aux = 0;
74
                while (iter.hasNext()) {
75
                        fact = iter.next();
76
                        aux = fact.canConvert(sym);
77
                        if (aux > max) {
78
                                max = aux;
79
                                factToUse = fact;
80
                        }
81
                }
82
                
83
                if (max == 0) {
84
                        throw new UnsupportedSymbolException(
85
                                        sym.getClass().getName(), "No converter found.");
86
                }
87
                
88
                SymbolToSLDConverter conv = factToUse.createSymbolToSLDConverter();
89
                return conv.convert(sym);
90
        }
91
        
92

    
93
        public ISymbol toSymbol(SLDSymbol sldsym) throws UnsupportedSLDObjectException {
94

    
95
                if (sldsym == null) {
96
                        throw new UnsupportedSLDObjectException("SLDSymbol", "Null");
97
                }
98
                int max = 0;
99
                SLDToSymbolConverterFactory factToUse = null;
100
                Iterator<SLDToSymbolConverterFactory> iter = symbolFromSLD.iterator();
101
                SLDToSymbolConverterFactory fact = null;
102
                int aux = 0;
103
                while (iter.hasNext()) {
104
                        fact = iter.next();
105
                        aux = fact.canConvert(sldsym);
106
                        if (aux > max) {
107
                                max = aux;
108
                                factToUse = fact;
109
                        }
110
                }
111
                
112
                if (max == 0) {
113
                        throw new UnsupportedSLDObjectException(
114
                                        sldsym.getClass().getSimpleName(), "No converter found.");
115
                }
116
                
117
                SLDToSymbolConverter conv = factToUse.createSLDToSymbolConverter();
118
                return conv.convert(sldsym);                
119
        }
120

    
121
        public SLDLayer toSLDLayer(ILegend legend)
122
                        throws UnsupportedLegendException, UnsupportedSymbolException {
123
                
124
                if (legend == null) {
125
                        throw new UnsupportedLegendException("ILegend", "Null");
126
                }
127
                
128
                int max = 0;
129
                LegendToSLDConverterFactory factToUse = null;
130
                Iterator<LegendToSLDConverterFactory> iter = legendToSLD.iterator();
131
                LegendToSLDConverterFactory fact = null;
132
                int aux = 0;
133
                while (iter.hasNext()) {
134
                        fact = iter.next();
135
                        aux = fact.canConvert(legend);
136
                        if (aux > max) {
137
                                max = aux;
138
                                factToUse = fact;
139
                        }
140
                }
141
                
142
                if (max == 0) {
143
                        throw new UnsupportedLegendException(
144
                                        legend.getClass().getName(), "No converter found.");
145
                }
146
                
147
                LegendToSLDConverter conv = factToUse.createLegendToSLDConverter();
148
                return conv.convert(legend);                
149
        }
150

    
151
        public ILegend toLegend(SLDLayer sldlayer) throws UnsupportedSLDObjectException {
152

    
153
                if (sldlayer == null) {
154
                        throw new UnsupportedSLDObjectException("SLDLayer", "Null");
155
                }
156
                int max = 0;
157
                SLDToLegendConverterFactory factToUse = null;
158
                Iterator<SLDToLegendConverterFactory> iter = legendFromSLD.iterator();
159
                SLDToLegendConverterFactory fact = null;
160
                int aux = 0;
161
                while (iter.hasNext()) {
162
                        fact = iter.next();
163
                        aux = fact.canConvert(sldlayer);
164
                        if (aux > max) {
165
                                max = aux;
166
                                factToUse = fact;
167
                        }
168
                }
169
                
170
                if (max == 0) {
171
                        throw new UnsupportedSLDObjectException(
172
                                        sldlayer.getClass().getSimpleName(), "No converter found.");
173
                }
174
                
175
                SLDToLegendConverter conv = factToUse.createSLDToLegendConverter();
176
                return conv.convert(sldlayer);                
177
        }
178
        
179
        // =============================================================
180
        // =============================================================
181

    
182
        public void registerSLDToSymbolConverter(SLDToSymbolConverterFactory fact) {
183
                if (fact != null) {
184
                        this.symbolFromSLD.add(fact);
185
                }
186
        }
187

    
188
        public void registerSymbolToSLDConverter(SymbolToSLDConverterFactory fact) {
189
                if (fact != null) {
190
                        this.symbolToSLD.add(fact);
191
                }
192
        }
193

    
194
        public void registerSLDToLegendConverter(SLDToLegendConverterFactory fact) {
195
                if (fact != null) {
196
                        this.legendFromSLD.add(fact);
197
                }
198
        }
199

    
200
        public void registerLegendToSLDConverter(LegendToSLDConverterFactory fact) {
201
                if (fact != null) {
202
                        this.legendToSLD.add(fact);
203
                }
204
        }
205

    
206
}