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

History | View | Annotate | Download (7.04 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.util.SLDUtils;
38
import org.gvsig.sldsupport.impl.util.XmlBuilder;
39
import org.gvsig.sldsupport.sld.SLDTags;
40
import org.gvsig.sldsupport.sld.graphic.SLDAnchorPoint;
41
import org.gvsig.sldsupport.sld.graphic.SLDDisplacement;
42
import org.gvsig.sldsupport.sld.graphic.SLDExternalGraphic;
43
import org.gvsig.sldsupport.sld.graphic.SLDGraphic;
44
import org.gvsig.sldsupport.sld.graphic.SLDGraphicStackElement;
45
import org.gvsig.sldsupport.sld.graphic.SLDMark;
46
import org.gvsig.sldsupport.sld.symbol.misc.SLDParameterValue;
47

    
48
public class GraphicElement {
49

    
50
    public static void append(SLDGraphic obj, XmlBuilder xb, String version)
51
        throws InvalidSLDObjectException, UnsupportedSLDObjectException {
52

    
53
        xb.openTag(SLDTags.GRAPHIC);
54
        // ===========================================================
55
        List<SLDGraphicStackElement> list = obj.getElementStack();
56
        for (int i = 0; i < list.size(); i++) {
57
            if (list.get(i) instanceof SLDExternalGraphic) {
58
                ExternalGraphicElement.append((SLDExternalGraphic) list.get(i),
59
                    xb, version);
60
            } else {
61
                if (list.get(i) instanceof SLDMark) {
62
                    MarkElement.append((SLDMark) list.get(i), xb, version);
63
                }
64
            }
65
        }
66
        // ======================================== op sz rot
67
        if (obj.getOpacity() != null) {
68
            ParameterValueElement.append(SLDTags.OPACITY, obj.getOpacity(), xb,
69
                version);
70
        }
71
        // =========
72
        if (obj.getSize() != null) {
73
            ParameterValueElement.append(SLDTags.SIZE, obj.getSize(), xb,
74
                version);
75
        }
76
        // =========
77
        if (obj.getRotation() != null) {
78
            ParameterValueElement.append(SLDTags.ROTATION, obj.getRotation(),
79
                xb, version);
80
        }
81
        // ======================================== v. 1.1.0, anch, displ
82
        if ((version != null)
83
            && (version.compareToIgnoreCase(SLDUtils.VERSION_1_1_0) == 0)) {
84

    
85
            if (obj.getAnchor() != null) {
86
                AnchorPointElement.append(obj.getAnchor(), xb, version);
87
            }
88
            // =========
89
            if (obj.getDisplacement() != null) {
90
                DisplacementElement.append(obj.getDisplacement(), xb, version);
91
            }
92
        }
93
        // ===========================================================
94
        xb.closeTag();
95
    }
96

    
97
    public static SLDGraphic parse(XmlPullParser parser, String version)
98
        throws XmlPullParserException, IOException, SLDReadException {
99

    
100
        parser.require(XmlPullParser.START_TAG, null, SLDTags.GRAPHIC);
101
        int tag = 0;
102

    
103
        SLDGraphic resp = new SLDGraphic();
104

    
105
        tag = parser.nextTag();
106
        String name = parser.getName();
107
        String txt = null;
108
        while (!(SLDUtils.isStr(name, SLDTags.GRAPHIC) && (tag == XmlPullParser.END_TAG))) {
109

    
110
            switch (tag) {
111
            case XmlPullParser.START_TAG:
112
                if (SLDUtils.isStr(name, SLDTags.EXTERNALGRAPHIC)) {
113
                    SLDExternalGraphic ext =
114
                        ExternalGraphicElement.parse(parser, version);
115
                    resp.getElementStack().add(ext);
116
                    break;
117
                }
118
                if (SLDUtils.isStr(name, SLDTags.MARK)) {
119
                    SLDMark mk = MarkElement.parse(parser, version);
120
                    resp.getElementStack().add(mk);
121
                    break;
122
                }
123
                if (SLDUtils.isStr(name, SLDTags.OPACITY)) {
124
                    SLDParameterValue pv =
125
                        ParameterValueElement.parse(parser, version);
126
                    resp.setOpacity(pv);
127
                    break;
128
                }
129
                if (SLDUtils.isStr(name, SLDTags.SIZE)) {
130
                    SLDParameterValue pv =
131
                        ParameterValueElement.parse(parser, version);
132
                    resp.setSize(pv);
133
                    break;
134
                }
135
                if (SLDUtils.isStr(name, SLDTags.ROTATION)) {
136
                    SLDParameterValue pv =
137
                        ParameterValueElement.parse(parser, version);
138
                    resp.setRotation(pv);
139
                    break;
140
                }
141
                if (SLDUtils.isStr(name, SLDTags.ANCHOR_POINT)) {
142
                    SLDAnchorPoint ap =
143
                        AnchorPointElement.parse(parser, version);
144
                    resp.setAnchor(ap);
145
                    break;
146
                }
147
                if (SLDUtils.isStr(name, SLDTags.DISPLACEMENT)) {
148
                    SLDDisplacement di =
149
                        DisplacementElement.parse(parser, version);
150
                    resp.setDisplacement(di);
151
                    break;
152
                }
153
                /*
154
                 * Any other entity causes parsing error
155
                 */
156
                throw new SLDReadException(
157
                    "Bad SLD file. Unexpected entity in graphic: " + name);
158
            case XmlPullParser.END_TAG:
159
                break;
160
            case XmlPullParser.TEXT:
161
                break;
162
            }
163
            tag = parser.getEventType();
164
            name = parser.getName();
165
        }
166

    
167
        parser.nextTag();
168
        /*
169
         * Set default mark if no mark/graphic was set
170
         */
171
        if (resp.getElementStack().size() == 0) {
172
            resp.setDefaultMark();
173
        }
174
        return resp;
175
    }
176
}