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

History | View | Annotate | Download (6.09 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.symbol;
27

    
28
import java.io.IOException;
29
import java.util.Map;
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.DisplacementElement;
38
import org.gvsig.sldsupport.impl.sld.parsing.FillElement;
39
import org.gvsig.sldsupport.impl.sld.parsing.GeometryElement;
40
import org.gvsig.sldsupport.impl.sld.parsing.StrokeElement;
41
import org.gvsig.sldsupport.impl.util.SLDUtils;
42
import org.gvsig.sldsupport.impl.util.XmlBuilder;
43
import org.gvsig.sldsupport.sld.SLDTags;
44
import org.gvsig.sldsupport.sld.graphic.SLDDisplacement;
45
import org.gvsig.sldsupport.sld.symbol.SLDPolygonSymbol;
46
import org.gvsig.sldsupport.sld.symbol.misc.SLDFill;
47
import org.gvsig.sldsupport.sld.symbol.misc.SLDGeometry;
48
import org.gvsig.sldsupport.sld.symbol.misc.SLDStroke;
49

    
50
/**
51
 * Used to parse a parameter value inside another entity
52
 * 
53
 * @author jldominguez
54
 * 
55
 */
56
public class PolygonSymbolElement {
57

    
58
    public static void append(SLDPolygonSymbol obj, XmlBuilder xb,
59
        Map<String, String> atts) throws InvalidSLDObjectException,
60
        UnsupportedSLDObjectException {
61

    
62
        String v = obj.getVersion();
63
        if ((v == null) && (atts != null)) {
64
            v = atts.get(SLDTags.VERSION_ATTR);
65
        }
66
        if (v == null) {
67
            v = SLDUtils.DEFAULT_VERSION;
68
        }
69

    
70
        String ent = "";
71
        if (v.compareToIgnoreCase(SLDUtils.VERSION_1_0_0) == 0) {
72
            ent = SLDTags.POLYGONSYMBOLIZER;
73
        } else {
74
            ent = SLDTags.POLYGONSYMBOL;
75
        }
76

    
77
        if (atts == null) {
78
            xb.openTag(ent);
79
        } else {
80
            xb.openTag(ent, atts);
81
        }
82

    
83
        // ==================================================
84
        String gn = obj.getGeometryPropertyName();
85
        if (gn != null) {
86
            GeometryElement.append(new SLDGeometry(gn), xb, v);
87
        }
88
        // =================
89
        SLDFill fill = obj.getFill();
90
        if (fill != null) {
91
            FillElement.append(fill, xb, v);
92
        }
93
        // =================
94
        SLDStroke stro = obj.getStroke();
95
        if (stro != null) {
96
            StrokeElement.append(stro, xb, v);
97
        }
98
        // ================= Only in version 1.1.0
99
        if (v.compareToIgnoreCase(SLDUtils.VERSION_1_1_0) == 0) {
100
            SLDDisplacement disp = obj.getDisplacement();
101
            if (disp != null) {
102
                DisplacementElement.append(disp, xb, v);
103
            }
104
        }
105
        // ==================================================
106
        xb.closeTag();
107
    }
108

    
109
    public static SLDPolygonSymbol parse(XmlPullParser parser, String version)
110
        throws XmlPullParserException, IOException, SLDReadException {
111

    
112
        String ent_name = parser.getName();
113
        SLDPolygonSymbol resp = new SLDPolygonSymbol();
114
        resp.setVersion(version);
115
        int tag = 0;
116

    
117
        tag = parser.nextTag();
118
        String name = parser.getName();
119
        String txt = null;
120
        while (!(SLDUtils.isStr(name, ent_name) && (tag == XmlPullParser.END_TAG))) {
121

    
122
            switch (tag) {
123
            case XmlPullParser.START_TAG:
124
                if (SLDUtils.isStr(name, SLDTags.GEOMETRY)) {
125
                    SLDGeometry geom = GeometryElement.parse(parser, version);
126
                    resp.setGeometryPropertyName(geom.getPropertyName());
127
                    break;
128
                }
129
                if (SLDUtils.isStr(name, SLDTags.FILL)) {
130
                    SLDFill fil = FillElement.parse(parser, version);
131
                    resp.setFill(fil);
132
                    break;
133
                }
134
                if (SLDUtils.isStr(name, SLDTags.STROKE)) {
135
                    SLDStroke st = StrokeElement.parse(parser, version);
136
                    resp.setStroke(st);
137
                    break;
138
                }
139
                if (SLDUtils.isStr(name, SLDTags.DISPLACEMENT)) {
140
                    resp.setVersion(SLDUtils.VERSION_1_1_0);
141
                    SLDDisplacement disp =
142
                        DisplacementElement.parse(parser, version);
143
                    resp.setDisplacement(disp);
144
                    break;
145
                }
146
                /*
147
                 * Any other entity causes parsing error
148
                 */
149
                throw new SLDReadException(
150
                    "Bad SLD file. Unexpected entity in polygon symbol: "
151
                        + name);
152
            case XmlPullParser.END_TAG:
153
                break;
154
            case XmlPullParser.TEXT:
155
                break;
156
            }
157
            tag = parser.getEventType();
158
            name = parser.getName();
159
        }
160

    
161
        parser.nextTag();
162
        return resp;
163
    }
164
}