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 / FeatureStyleElement.java @ 46

History | View | Annotate | Download (8.92 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.HashMap;
30
import java.util.List;
31
import java.util.Map;
32

    
33
import org.xmlpull.v1.XmlPullParser;
34
import org.xmlpull.v1.XmlPullParserException;
35

    
36
import org.gvsig.sldsupport.exception.InvalidSLDObjectException;
37
import org.gvsig.sldsupport.exception.SLDReadException;
38
import org.gvsig.sldsupport.exception.UnsupportedSLDObjectException;
39
import org.gvsig.sldsupport.impl.util.SLDUtils;
40
import org.gvsig.sldsupport.impl.util.XmlBuilder;
41
import org.gvsig.sldsupport.sld.SLDTags;
42
import org.gvsig.sldsupport.sld.rule.SLDRule;
43
import org.gvsig.sldsupport.sld.style.SLDFeatureStyle;
44

    
45
public class FeatureStyleElement {
46

    
47
    public static void append(SLDFeatureStyle obj, XmlBuilder xb,
48
        Map<String, String> atts) throws InvalidSLDObjectException,
49
        UnsupportedSLDObjectException {
50

    
51
        String v = obj.getVersion();
52
        if (v == null) {
53
            v = SLDUtils.DEFAULT_VERSION;
54
        }
55

    
56
        if (v.compareToIgnoreCase(SLDUtils.VERSION_1_1_0) == 0) {
57
            appendv110(obj, xb);
58
        } else {
59
            appendv100(obj, xb);
60
        }
61
    }
62

    
63
    private static void appendv100(SLDFeatureStyle obj, XmlBuilder xb)
64
        throws InvalidSLDObjectException, UnsupportedSLDObjectException {
65

    
66
        if (obj.getVersion() != null) {
67
            Map<String, String> atts = new HashMap<String, String>();
68
            atts.put(SLDTags.VERSION_ATTR, obj.getVersion());
69
            xb.openTag(SLDTags.FEATURETYPESTYLE, atts);
70
        } else {
71
            xb.openTag(SLDTags.FEATURETYPESTYLE);
72
        }
73
        // =================================
74
        if (obj.getName() != null) {
75
            xb.writeTag(SLDTags.NAME, obj.getName());
76
        }
77
        if (obj.getTitle() != null) {
78
            xb.writeTag(SLDTags.TITLE, obj.getTitle());
79
        }
80
        if (obj.getDescription() != null) {
81
            // it is called "Description" in v. 1.1.0
82
            xb.writeTag(SLDTags.ABSTRACT, obj.getDescription());
83
        }
84
        if (obj.getFeatureTypeName() != null) {
85
            xb.writeTag(SLDTags.FEATURETYPENAME, obj.getFeatureTypeName());
86
        }
87
        List<String> list = obj.getSemanticTypeIdentifiers();
88
        for (int i = 0; i < list.size(); i++) {
89
            xb.writeTag(SLDTags.SEMANTICTYPEIDENTIFIER, list.get(i));
90
        }
91
        // ================
92
        List<SLDRule> rules = obj.getRules();
93
        if ((rules == null) || (rules.size() == 0)) {
94
            throw new InvalidSLDObjectException(SLDTags.FEATURETYPESTYLE,
95
                "No rules found");
96
        } else {
97
            for (int i = 0; i < rules.size(); i++) {
98
                RuleElement.append(rules.get(i), xb, SLDUtils.VERSION_1_0_0);
99
            }
100
        }
101
        // =================================
102
        xb.closeTag();
103
    }
104

    
105
    private static void appendv110(SLDFeatureStyle obj, XmlBuilder xb)
106
        throws InvalidSLDObjectException, UnsupportedSLDObjectException {
107

    
108
        if (obj.getVersion() != null) {
109
            Map<String, String> atts = new HashMap<String, String>();
110
            atts.put(SLDTags.VERSION_ATTR, obj.getVersion());
111
            xb.openTag(SLDTags.FEATURESTYLE, atts);
112
        } else {
113
            xb.openTag(SLDTags.FEATURESTYLE);
114
        }
115
        // =================================
116
        if (obj.getName() != null) {
117
            xb.writeTag(SLDTags.NAME, obj.getName());
118
        }
119
        if (obj.getDescription() != null) {
120
            xb.writeTag(SLDTags.DESCRIPTION, obj.getDescription());
121
        }
122
        if (obj.getFeatureTypeName() != null) {
123
            xb.writeTag(SLDTags.FEATURETYPENAME, obj.getFeatureTypeName());
124
        } else {
125
            // mandatory in v 1.1.0
126
            throw new InvalidSLDObjectException(SLDTags.FEATURESTYLE,
127
                SLDTags.FEATURETYPENAME + " is mandatory in version 1.1.0");
128
        }
129
        // =========
130
        List<String> list = obj.getSemanticTypeIdentifiers();
131
        for (int i = 0; i < list.size(); i++) {
132
            xb.writeTag(SLDTags.SEMANTICTYPEIDENTIFIER, list.get(i));
133
        }
134
        // ================
135
        List<SLDRule> rules = obj.getRules();
136
        if ((rules == null) || (rules.size() == 0)) {
137
            throw new InvalidSLDObjectException(SLDTags.FEATURESTYLE,
138
                "No rules found");
139
        } else {
140
            for (int i = 0; i < rules.size(); i++) {
141
                RuleElement.append(rules.get(i), xb, SLDUtils.VERSION_1_0_0);
142
            }
143
        }
144
        // =================================
145
        xb.closeTag();
146
    }
147

    
148
    public static SLDFeatureStyle parse(XmlPullParser parser, String version)
149
        throws XmlPullParserException, IOException, SLDReadException {
150

    
151
        String ent_name = parser.getName();
152
        if (!(SLDUtils.isStr(ent_name, SLDTags.FEATURESTYLE) || SLDUtils.isStr(
153
            ent_name, SLDTags.FEATURETYPESTYLE))) {
154
            throw new SLDReadException("Bad opening tag: " + ent_name);
155
        }
156

    
157
        String txt = null;
158
        int tag = 0;
159
        SLDFeatureStyle resp = new SLDFeatureStyle();
160

    
161
        if (SLDUtils.isStr(ent_name, SLDTags.FEATURESTYLE)) {
162
            txt = parser.getAttributeValue(null, SLDTags.VERSION_ATTR);
163
            if (txt == null) {
164
                throw new SLDReadException(
165
                    "The 'version' attribute is mandatory in "
166
                        + SLDTags.FEATURESTYLE);
167
            } else {
168
                resp.setVersion(txt);
169
            }
170
        }
171

    
172
        tag = parser.nextTag();
173
        String name = parser.getName();
174
        while (!(SLDUtils.isStr(name, ent_name) && (tag == XmlPullParser.END_TAG))) {
175

    
176
            switch (tag) {
177
            case XmlPullParser.START_TAG:
178
                if (SLDUtils.isStr(name, SLDTags.NAME)) {
179
                    txt = parser.nextText();
180
                    resp.setName(txt);
181
                    parser.nextTag();
182
                    break;
183
                }
184
                if (SLDUtils.isStr(name, SLDTags.TITLE)) {
185
                    txt = parser.nextText();
186
                    resp.setTitle(txt);
187
                    parser.nextTag();
188
                    break;
189
                }
190
                if (SLDUtils.isStr(name, SLDTags.DESCRIPTION)
191
                    || SLDUtils.isStr(name, SLDTags.ABSTRACT)) {
192
                    txt = parser.nextText();
193
                    resp.setDescription(txt);
194
                    parser.nextTag();
195
                    break;
196
                }
197
                if (SLDUtils.isStr(name, SLDTags.FEATURETYPENAME)) {
198
                    txt = parser.nextText();
199
                    resp.setFeatureTypeName(txt);
200
                    parser.nextTag();
201
                    break;
202
                }
203
                if (SLDUtils.isStr(name, SLDTags.SEMANTICTYPEIDENTIFIER)) {
204
                    txt = parser.nextText();
205
                    resp.getSemanticTypeIdentifiers().add(txt);
206
                    parser.nextTag();
207
                    break;
208
                }
209
                if (SLDUtils.isStr(name, SLDTags.RULE)) {
210
                    SLDRule rule = RuleElement.parse(parser, version);
211
                    resp.getRules().add(rule);
212
                    break;
213
                }
214
                /*
215
                 * Any other entity causes parsing error
216
                 */
217
                throw new SLDReadException(
218
                    "Bad SLD file. Unexpected entity in feature[type] style: "
219
                        + name);
220
            case XmlPullParser.END_TAG:
221
                break;
222
            case XmlPullParser.TEXT:
223
                break;
224
            }
225
            tag = parser.getEventType();
226
            name = parser.getName();
227
        }
228

    
229
        parser.nextTag();
230
        return resp;
231
    }
232
}