Statistics
| Revision:

gvsig-sldtools / org.gvsig.sld / org.gvsig.sldsupport / org.gvsig.sldsupport.lib / org.gvsig.sldsupport.lib.impl / src / main / java / org / gvsig / sldsupport / impl / sld / parsing / RuleElement.java @ 46

History | View | Annotate | Download (8.12 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.sldsupport.impl.sld.parsing;
27

    
28
import java.io.IOException;
29
import java.util.List;
30

    
31
import org.xmlpull.v1.XmlPullParser;
32
import org.xmlpull.v1.XmlPullParserException;
33

    
34
import org.gvsig.sldsupport.exception.InvalidSLDObjectException;
35
import org.gvsig.sldsupport.exception.SLDReadException;
36
import org.gvsig.sldsupport.exception.UnsupportedSLDObjectException;
37
import org.gvsig.sldsupport.impl.sld.parsing.symbol.SymbolElement;
38
import org.gvsig.sldsupport.impl.util.SLDUtils;
39
import org.gvsig.sldsupport.impl.util.XmlBuilder;
40
import org.gvsig.sldsupport.sld.SLDTags;
41
import org.gvsig.sldsupport.sld.filter.SLDFilter;
42
import org.gvsig.sldsupport.sld.graphic.SLDLegendGraphic;
43
import org.gvsig.sldsupport.sld.rule.SLDRule;
44
import org.gvsig.sldsupport.sld.symbol.SLDSymbol;
45

    
46
public class RuleElement {
47

    
48
    public static void append(SLDRule obj, XmlBuilder xb, String version)
49
        throws InvalidSLDObjectException, UnsupportedSLDObjectException {
50

    
51
        xb.openTag(SLDTags.RULE);
52
        // ==========================
53
        if (obj.getName() != null) {
54
            xb.writeTag(SLDTags.NAME, obj.getName());
55
        }
56
        // =========
57
        if ((version != null)
58
            && (version.compareToIgnoreCase(SLDUtils.VERSION_1_1_0) == 0)) {
59
            // 1.1.0
60
            if (obj.getDescription() != null) {
61
                xb.writeTag(SLDTags.DESCRIPTION, obj.getDescription());
62
            }
63
        } else {
64
            // 1.0.0
65
            if (obj.getTitle() != null) {
66
                xb.writeTag(SLDTags.TITLE, obj.getTitle());
67
            }
68
            if (obj.getDescription() != null) {
69
                xb.writeTag(SLDTags.ABSTRACT, obj.getDescription());
70
            }
71
        }
72
        // =========
73
        if (obj.getLegendGraphic() != null) {
74
            LegendGraphicElement.append(obj.getLegendGraphic(), xb, version);
75
        }
76
        // =========
77
        if (obj.getFilter() != null) {
78
            FilterElement.append(obj.getFilter(), xb, version);
79
        }
80
        // =========
81
        if (obj.getMinScaleDenominator() != -1) {
82
            xb.writeTag(SLDTags.MINSCALEDENOMINATOR,
83
                SLDUtils.df.format(obj.getMinScaleDenominator()));
84
        }
85
        if (obj.getMaxScaleDenominator() != -1) {
86
            xb.writeTag(SLDTags.MAXSCALEDENOMINATOR,
87
                SLDUtils.df.format(obj.getMaxScaleDenominator()));
88
        }
89
        // =========
90
        List<SLDSymbol> syms = obj.getSymbols();
91
        if ((syms == null) || (syms.size() == 0)) {
92
            throw new InvalidSLDObjectException(SLDTags.RULE,
93
                "Symbol list is empty");
94
        } else {
95
            for (int i = 0; i < syms.size(); i++) {
96
                SymbolElement.append(syms.get(i), xb, null);
97
            }
98
        }
99
        // ==========================
100
        xb.closeTag();
101
    }
102

    
103
    public static SLDRule parse(XmlPullParser parser, String version)
104
        throws XmlPullParserException, IOException, SLDReadException {
105

    
106
        parser.require(XmlPullParser.START_TAG, null, SLDTags.RULE);
107
        int tag = 0;
108

    
109
        SLDRule resp = new SLDRule();
110

    
111
        tag = parser.nextTag();
112
        String name = parser.getName();
113
        String txt = null;
114
        while (!(SLDUtils.isStr(name, SLDTags.RULE) && (tag == XmlPullParser.END_TAG))) {
115

    
116
            switch (tag) {
117
            case XmlPullParser.START_TAG:
118
                if (SLDUtils.isStr(name, SLDTags.NAME)) {
119
                    txt = parser.nextText();
120
                    resp.setName(txt);
121
                    parser.nextTag();
122
                    break;
123
                }
124
                if (SLDUtils.isStr(name, SLDTags.TITLE)) {
125
                    txt = parser.nextText();
126
                    resp.setTitle(txt);
127
                    parser.nextTag();
128
                    break;
129
                }
130
                if (SLDUtils.isStr(name, SLDTags.ABSTRACT)
131
                    || SLDUtils.isStr(name, SLDTags.DESCRIPTION)) {
132
                    txt = parser.nextText();
133
                    resp.setDescription(txt);
134
                    parser.nextTag();
135
                    break;
136
                }
137
                if (SLDUtils.isStr(name, SLDTags.LEGENDGRAPHIC)) {
138
                    SLDLegendGraphic legr =
139
                        LegendGraphicElement.parse(parser, version);
140
                    resp.setLegendGraphic(legr);
141
                    break;
142
                }
143

    
144
                if (SLDUtils.isStr(name, SLDTags.FILTER)
145
                    || SLDUtils.isStr(name, SLDTags.ELSEFILTER)) {
146
                    SLDFilter filt = FilterElement.parse(parser, version);
147
                    resp.setFilter(filt);
148
                    break;
149
                }
150

    
151
                if (SLDUtils.isStr(name, SLDTags.MINSCALEDENOMINATOR)) {
152
                    txt = parser.nextText();
153
                    resp.setMinScaleDenominator(SLDUtils.parseDouble(txt));
154
                    parser.nextTag();
155
                    break;
156
                }
157
                if (SLDUtils.isStr(name, SLDTags.MAXSCALEDENOMINATOR)) {
158
                    txt = parser.nextText();
159
                    resp.setMaxScaleDenominator(SLDUtils.parseDouble(txt));
160
                    parser.nextTag();
161
                    break;
162
                }
163
                // ===================================
164
                // ===================================
165
                if (SLDUtils.isStr(name, SLDTags.POINTSYMBOL)
166
                    || SLDUtils.isStr(name, SLDTags.POINTSYMBOLIZER)
167
                    || SLDUtils.isStr(name, SLDTags.LINESYMBOL)
168
                    || SLDUtils.isStr(name, SLDTags.LINESYMBOLIZER)
169
                    || SLDUtils.isStr(name, SLDTags.POLYGONSYMBOL)
170
                    || SLDUtils.isStr(name, SLDTags.POLYGONSYMBOLIZER)) {
171
                    SLDSymbol sym = SymbolElement.parse(parser, version);
172
                    resp.getSymbols().add(sym);
173
                    break;
174
                }
175
                /*
176
                 * Text, Raster, MappedColor symbols not supported
177
                 */
178
                if (SLDUtils.isStr(name, SLDTags.TEXTSYMBOL)
179
                    || SLDUtils.isStr(name, SLDTags.TEXTSYMBOLIZER)
180
                    || SLDUtils.isStr(name, SLDTags.RASTERSYMBOL)
181
                    || SLDUtils.isStr(name, SLDTags.RASTERSYMBOLIZER)
182
                    || SLDUtils.isStr(name, SLDTags.MAPPEDCOLORSYMBOL)) {
183
                    throw new SLDReadException("Unsupported symbol: " + name);
184
                }
185

    
186
                /*
187
                 * Any other entity causes parsing error
188
                 */
189
                throw new SLDReadException(
190
                    "Bad SLD file. Unexpected entity in Rule: " + name);
191

    
192
            case XmlPullParser.END_TAG:
193
                break;
194
            case XmlPullParser.TEXT:
195
                break;
196
            }
197
            tag = parser.getEventType();
198
            name = parser.getName();
199
        }
200
        parser.nextTag();
201
        return resp;
202
    }
203
}