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

History | View | Annotate | Download (5.52 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

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

    
33
import org.gvsig.sldsupport.exception.InvalidSLDObjectException;
34
import org.gvsig.sldsupport.exception.SLDReadException;
35
import org.gvsig.sldsupport.exception.UnsupportedSLDObjectException;
36
import org.gvsig.sldsupport.impl.util.SLDUtils;
37
import org.gvsig.sldsupport.impl.util.XmlBuilder;
38
import org.gvsig.sldsupport.sld.SLDTags;
39
import org.gvsig.sldsupport.sld.graphic.SLDGraphicFill;
40
import org.gvsig.sldsupport.sld.symbol.misc.SLDFill;
41
import org.gvsig.sldsupport.sld.symbol.misc.SLDSvgCssParameter;
42

    
43
public class FillElement {
44

    
45
    public static void append(SLDFill obj, XmlBuilder xb, String version)
46
        throws InvalidSLDObjectException, UnsupportedSLDObjectException {
47

    
48
        xb.openTag(SLDTags.FILL);
49
        // ======================
50
        if (obj.getFillGraphic() != null) {
51
            GraphicFillElement.append(new SLDGraphicFill(obj.getFillGraphic()),
52
                xb, version);
53
        }
54
        // =========
55
        if (obj.getFillColor() != null) {
56
            SLDSvgCssParameter p = new SLDSvgCssParameter();
57
            p.setName(SLDTags.FILL_ATTR);
58
            p.getExpressionList().add(obj.getFillColor());
59
            SvgCssParameterElement.append(p, xb, version);
60
        }
61
        // =========
62
        if (obj.getOpacity() != null) {
63
            SLDSvgCssParameter p = new SLDSvgCssParameter();
64
            p.setName(SLDTags.FILLOPACITY_ATTR);
65
            p.getExpressionList().add(obj.getOpacity());
66
            SvgCssParameterElement.append(p, xb, version);
67
        }
68
        // ======================
69
        xb.closeTag();
70
    }
71

    
72
    public static SLDFill parse(XmlPullParser parser, String version)
73
        throws XmlPullParserException, IOException, SLDReadException {
74

    
75
        parser.require(XmlPullParser.START_TAG, null, SLDTags.FILL);
76
        int tag = 0;
77

    
78
        SLDFill resp = new SLDFill();
79

    
80
        tag = parser.nextTag();
81
        String name = parser.getName();
82
        String txt = null;
83
        String attName = null;
84
        while (!(SLDUtils.isStr(name, SLDTags.FILL) && (tag == XmlPullParser.END_TAG))) {
85

    
86
            switch (tag) {
87
            case XmlPullParser.START_TAG:
88
                if (SLDUtils.isStr(name, SLDTags.GRAPHICFILL)) {
89
                    SLDGraphicFill gf =
90
                        GraphicFillElement.parse(parser, version);
91
                    /*
92
                     * SLDFill has a 'fillGraphic' field which is a Graphic,
93
                     * it does not have a field of FillGraphic type which
94
                     * has a graphic (we are collapsing an element)
95
                     */
96
                    resp.setFillGraphic(gf.getGraphic());
97
                    break;
98
                }
99
                if (SLDUtils.isStr(name, SLDTags.CSSPARAMETER)
100
                    || SLDUtils.isStr(name, SLDTags.SVGPARAMETER)) {
101

    
102
                    SLDSvgCssParameter sp =
103
                        SvgCssParameterElement.parse(parser, version);
104
                    if (SLDUtils.isStr(sp.getName(), SLDTags.FILL_ATTR)) {
105
                        if (sp.getExpressionList().size() > 0) {
106
                            // Using first item only
107
                            resp.setFillColor(sp.getExpressionList().get(0));
108
                        }
109
                    } else {
110
                        if (SLDUtils.isStr(sp.getName(),
111
                            SLDTags.FILLOPACITY_ATTR)) {
112
                            if (sp.getExpressionList().size() > 0) {
113
                                // Using first item only
114
                                resp.setOpacity(sp.getExpressionList().get(0));
115
                            }
116
                        }
117
                    }
118
                    break;
119
                }
120
                /*
121
                 * Any other entity causes parsing error
122
                 */
123
                throw new SLDReadException(
124
                    "Bad SLD file. Unexpected entity in fill element: " + name);
125
            case XmlPullParser.END_TAG:
126
                break;
127
            case XmlPullParser.TEXT:
128
                break;
129
            }
130
            tag = parser.getEventType();
131
            name = parser.getName();
132
        }
133

    
134
        parser.nextTag();
135
        return resp;
136
    }
137
}