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 / MarkElement.java @ 46
History | View | Annotate | Download (9.72 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.ArrayList; |
30 |
import java.util.HashMap; |
31 |
import java.util.List; |
32 |
import java.util.Map; |
33 |
|
34 |
import org.xmlpull.v1.XmlPullParser; |
35 |
import org.xmlpull.v1.XmlPullParserException; |
36 |
|
37 |
import org.gvsig.sldsupport.exception.InvalidSLDObjectException; |
38 |
import org.gvsig.sldsupport.exception.SLDReadException; |
39 |
import org.gvsig.sldsupport.exception.UnsupportedSLDObjectException; |
40 |
import org.gvsig.sldsupport.impl.util.SLDUtils; |
41 |
import org.gvsig.sldsupport.impl.util.XmlBuilder; |
42 |
import org.gvsig.sldsupport.sld.SLDTags; |
43 |
import org.gvsig.sldsupport.sld.graphic.SLDMark; |
44 |
import org.gvsig.sldsupport.sld.symbol.misc.SLDFill; |
45 |
import org.gvsig.sldsupport.sld.symbol.misc.SLDStroke; |
46 |
|
47 |
public class MarkElement { |
48 |
|
49 |
public static List<String> WELL_KNOWN_NAMES = null; |
50 |
|
51 |
static {
|
52 |
WELL_KNOWN_NAMES = new ArrayList<String>(); |
53 |
WELL_KNOWN_NAMES.add("square");
|
54 |
WELL_KNOWN_NAMES.add("circle");
|
55 |
WELL_KNOWN_NAMES.add("triangle");
|
56 |
WELL_KNOWN_NAMES.add("star");
|
57 |
WELL_KNOWN_NAMES.add("cross");
|
58 |
WELL_KNOWN_NAMES.add("x");
|
59 |
} |
60 |
|
61 |
public static void append(SLDMark obj, XmlBuilder xb, String version) |
62 |
throws InvalidSLDObjectException, UnsupportedSLDObjectException {
|
63 |
|
64 |
xb.openTag(SLDTags.MARK); |
65 |
// ===============================================================
|
66 |
if ((version != null) |
67 |
&& (version.compareToIgnoreCase(SLDUtils.VERSION_1_1_0) == 0)) {
|
68 |
appendMarkTypev110(obj, xb); |
69 |
} else {
|
70 |
appendMarkTypev100(obj, xb); |
71 |
} |
72 |
// =============== fill, stroke
|
73 |
if (obj.getFill() != null) { |
74 |
FillElement.append(obj.getFill(), xb, version); |
75 |
} |
76 |
// =====
|
77 |
if (obj.getStroke() != null) { |
78 |
StrokeElement.append(obj.getStroke(), xb, version); |
79 |
} |
80 |
// ===============================================================
|
81 |
xb.closeTag(); |
82 |
|
83 |
} |
84 |
|
85 |
private static void appendMarkTypev100(SLDMark obj, XmlBuilder xb) |
86 |
throws InvalidSLDObjectException, UnsupportedSLDObjectException {
|
87 |
|
88 |
if (obj.getMarkType() == SLDMark.MARK_TYPE_WELL_KNOWN_NAME) {
|
89 |
if (obj.getWellKnownName() == null) { |
90 |
throw new InvalidSLDObjectException(SLDTags.MARK, |
91 |
"No well-known name found");
|
92 |
} |
93 |
checkWellKnownName(obj.getWellKnownName()); |
94 |
xb.writeTag(SLDTags.WELLKNOWNNAME, obj.getWellKnownName()); |
95 |
} |
96 |
} |
97 |
|
98 |
private static void checkWellKnownName(String name) |
99 |
throws UnsupportedSLDObjectException {
|
100 |
|
101 |
if (!WELL_KNOWN_NAMES.contains(name.toLowerCase())) {
|
102 |
throw new UnsupportedSLDObjectException(SLDTags.MARK, |
103 |
"Unsupported well-known name for mark: " + name);
|
104 |
} |
105 |
} |
106 |
|
107 |
private static void appendMarkTypev110(SLDMark obj, XmlBuilder xb) |
108 |
throws InvalidSLDObjectException, UnsupportedSLDObjectException {
|
109 |
|
110 |
boolean online_inlne = false; |
111 |
switch (obj.getMarkType()) {
|
112 |
case SLDMark.MARK_TYPE_WELL_KNOWN_NAME:
|
113 |
if (obj.getWellKnownName() == null) { |
114 |
throw new InvalidSLDObjectException(SLDTags.MARK, |
115 |
"No well-known name found");
|
116 |
} |
117 |
checkWellKnownName(obj.getWellKnownName()); |
118 |
xb.writeTag(SLDTags.WELLKNOWNNAME, obj.getWellKnownName()); |
119 |
break;
|
120 |
case SLDMark.MARK_TYPE_INLINE_CONTENT:
|
121 |
online_inlne = true;
|
122 |
if (obj.getInlineContent() == null) { |
123 |
throw new InvalidSLDObjectException(SLDTags.MARK, |
124 |
"No inline content found");
|
125 |
} |
126 |
xb.openTag(SLDTags.INLINE_CONTENT); |
127 |
xb.writeRaw(new String(obj.getInlineContent())); |
128 |
xb.closeTag(); |
129 |
break;
|
130 |
case SLDMark.MARK_TYPE_ONLINE_RESOURCE:
|
131 |
online_inlne = true;
|
132 |
if (obj.getOnlineResource() == null) { |
133 |
throw new InvalidSLDObjectException(SLDTags.MARK, |
134 |
"No online resource URL found");
|
135 |
} |
136 |
Map<String, String> atts = new HashMap<String, String>(); |
137 |
atts.put(SLDTags.XLINK_HREF, obj.getOnlineResource()); |
138 |
xb.writeTag(SLDTags.ONLINE_RESOURCE, atts); |
139 |
break;
|
140 |
default:
|
141 |
// Simple, nothing to do
|
142 |
break;
|
143 |
} |
144 |
// =====================
|
145 |
if (online_inlne) {
|
146 |
if (obj.getFormat() == null) { |
147 |
throw new InvalidSLDObjectException(SLDTags.MARK, |
148 |
"Format entity is mandatory in this case");
|
149 |
} |
150 |
xb.writeTag(SLDTags.FORMAT, obj.getFormat()); |
151 |
// =========
|
152 |
if (obj.getMarkIndex() != -1) { |
153 |
xb.writeTag(SLDTags.MARK_INDEX, |
154 |
Integer.toString(obj.getMarkIndex()));
|
155 |
} |
156 |
} |
157 |
} |
158 |
|
159 |
public static SLDMark parse(XmlPullParser parser, String version) |
160 |
throws XmlPullParserException, IOException, SLDReadException { |
161 |
|
162 |
parser.require(XmlPullParser.START_TAG, null, SLDTags.MARK);
|
163 |
int tag = 0; |
164 |
|
165 |
/*
|
166 |
* Default type is SLDMark.MARK_TYPE_SIMPLE
|
167 |
*/
|
168 |
SLDMark resp = new SLDMark();
|
169 |
|
170 |
tag = parser.nextTag(); |
171 |
String name = parser.getName();
|
172 |
String txt = null; |
173 |
while (!(SLDUtils.isStr(name, SLDTags.MARK) && (tag == XmlPullParser.END_TAG))) {
|
174 |
|
175 |
switch (tag) {
|
176 |
case XmlPullParser.START_TAG:
|
177 |
if (SLDUtils.isStr(name, SLDTags.WELLKNOWNNAME)) {
|
178 |
txt = parser.nextText(); |
179 |
resp.setMarkType(SLDMark.MARK_TYPE_WELL_KNOWN_NAME); |
180 |
resp.setWellKnownName(txt); |
181 |
parser.nextTag(); |
182 |
break;
|
183 |
} |
184 |
if (SLDUtils.isStr(name, SLDTags.ONLINE_RESOURCE)) {
|
185 |
// xlink:href="http://somesite.com/something.xml"
|
186 |
txt = parser.getAttributeValue(null, SLDTags.XLINK_HREF);
|
187 |
if (txt == null) { |
188 |
throw new SLDReadException( |
189 |
"URL not found (xlink:href att is missing) in online resource entity");
|
190 |
} |
191 |
resp.setOnlineResource(txt); |
192 |
resp.setMarkType(SLDMark.MARK_TYPE_ONLINE_RESOURCE); |
193 |
resp.setOnlineResource(txt); |
194 |
parser.nextTag(); |
195 |
parser.nextTag(); |
196 |
break;
|
197 |
} |
198 |
if (SLDUtils.isStr(name, SLDTags.INLINE_CONTENT)) {
|
199 |
/*
|
200 |
* This is unlikely to work if content is binary
|
201 |
*/
|
202 |
txt = parser.nextText(); |
203 |
resp.setMarkType(SLDMark.MARK_TYPE_INLINE_CONTENT); |
204 |
resp.setInlineContent(txt.getBytes()); |
205 |
parser.nextTag(); |
206 |
break;
|
207 |
} |
208 |
if (SLDUtils.isStr(name, SLDTags.FORMAT)) {
|
209 |
txt = parser.nextText(); |
210 |
resp.setFormat(txt); |
211 |
parser.nextTag(); |
212 |
break;
|
213 |
} |
214 |
if (SLDUtils.isStr(name, SLDTags.MARK_INDEX)) {
|
215 |
txt = parser.nextText(); |
216 |
int ind = SLDUtils.parseInteger(txt);
|
217 |
resp.setMarkIndex(ind); |
218 |
parser.nextTag(); |
219 |
break;
|
220 |
} |
221 |
// =============================================
|
222 |
if (SLDUtils.isStr(name, SLDTags.FILL)) {
|
223 |
SLDFill fil = FillElement.parse(parser, version); |
224 |
resp.setFill(fil); |
225 |
break;
|
226 |
} |
227 |
if (SLDUtils.isStr(name, SLDTags.STROKE)) {
|
228 |
SLDStroke stro = StrokeElement.parse(parser, version); |
229 |
resp.setStroke(stro); |
230 |
break;
|
231 |
} |
232 |
/*
|
233 |
* Any other entity causes parsing error
|
234 |
*/
|
235 |
throw new SLDReadException( |
236 |
"Bad SLD file. Unexpected entity in Mark: " + name);
|
237 |
case XmlPullParser.END_TAG:
|
238 |
break;
|
239 |
case XmlPullParser.TEXT:
|
240 |
break;
|
241 |
} |
242 |
tag = parser.getEventType(); |
243 |
name = parser.getName(); |
244 |
} |
245 |
|
246 |
parser.nextTag(); |
247 |
return resp;
|
248 |
} |
249 |
} |