svn-gvsig-desktop / branches / v02_desarrollo / libraries / sld / temp / org.gvsig.sldsupport.lib.impl / src / main / java / org / gvsig / sldsupport / impl / sld1_0_0 / handler / Handler1_0_0.java @ 40799
History | View | Annotate | Download (5.71 KB)
1 |
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib��ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.sldsupport.impl.sld1_0_0.handler; |
42 |
|
43 |
|
44 |
import java.io.IOException; |
45 |
import java.io.InputStream; |
46 |
|
47 |
import org.gvsig.sldsupport.exception.SLDException; |
48 |
import org.gvsig.sldsupport.exception.SLDReadException; |
49 |
import org.gvsig.sldsupport.impl.parser.ExtendedKXmlParser; |
50 |
import org.gvsig.sldsupport.impl.sld1_0_0.parsing.NamedLayerElement; |
51 |
import org.gvsig.sldsupport.impl.sld1_0_0.parsing.UserLayerElement; |
52 |
import org.gvsig.sldsupport.impl.util.SLDUtils; |
53 |
import org.gvsig.sldsupport.sld.SLDObject; |
54 |
import org.gvsig.sldsupport.sld.SLDRoot; |
55 |
import org.gvsig.sldsupport.sld.SLDTags; |
56 |
import org.gvsig.sldsupport.sld.layer.SLDLayer; |
57 |
import org.gvsig.sldsupport.sld.symbol.SLDSymbol; |
58 |
import org.kxml2.io.KXmlParser; |
59 |
import org.xmlpull.v1.XmlPullParser; |
60 |
import org.xmlpull.v1.XmlPullParserException; |
61 |
|
62 |
|
63 |
public class Handler1_0_0 { |
64 |
|
65 |
public Handler1_0_0() {
|
66 |
|
67 |
} |
68 |
|
69 |
public static SLDObject parse(InputStream inStream, String encoding, String version) |
70 |
throws XmlPullParserException, IOException, SLDException { |
71 |
|
72 |
int tag;
|
73 |
XmlPullParser parser = new ExtendedKXmlParser();
|
74 |
parser.setInput(inStream, encoding); |
75 |
|
76 |
tag = parser.getEventType(); |
77 |
if (tag == XmlPullParser.START_DOCUMENT) {
|
78 |
parser.nextTag(); |
79 |
parser.require(KXmlParser.START_TAG, null, SLDTags.SLD_ROOT);
|
80 |
// Now we are in root
|
81 |
} else {
|
82 |
if (tag != XmlPullParser.START_TAG) {
|
83 |
throw new IOException( |
84 |
"Misplaced input stream (it's not before opening tag)");
|
85 |
} |
86 |
} |
87 |
|
88 |
String name = parser.getName();
|
89 |
String v = null; |
90 |
|
91 |
if (name.compareToIgnoreCase(SLDTags.SLD_ROOT) == 0) { |
92 |
v = parser.getAttributeValue(null, SLDTags.VERSION_ATTR);
|
93 |
if (v == null) { |
94 |
throw new SLDReadException( |
95 |
"Version attribute is missing in " + SLDTags.SLD_ROOT);
|
96 |
} |
97 |
|
98 |
SLDRoot root = parseRoot(parser, v); |
99 |
tag = parser.getEventType(); |
100 |
// Must be end-document
|
101 |
if (tag != KXmlParser.END_DOCUMENT) {
|
102 |
throw new SLDReadException( |
103 |
"Expected end of document after closing '"
|
104 |
+ SLDTags.SLD_ROOT + "' tag");
|
105 |
} |
106 |
return root;
|
107 |
|
108 |
} else {
|
109 |
if (name.compareToIgnoreCase(SLDTags.USERDEFINEDLAYER) == 0 |
110 |
|| name.compareToIgnoreCase(SLDTags.NAMEDLAYER) == 0) {
|
111 |
/*
|
112 |
* This line is out of the SLD standard
|
113 |
*/
|
114 |
v = parser.getAttributeValue(null, SLDTags.VERSION_ATTR);
|
115 |
return parseLayer(parser, v);
|
116 |
} else {
|
117 |
if (name.compareToIgnoreCase(SLDTags.LINESYMBOLIZER) == 0 |
118 |
|| name.compareToIgnoreCase(SLDTags.POLYGONSYMBOLIZER) == 0
|
119 |
|| name.compareToIgnoreCase(SLDTags.POINTSYMBOLIZER) == 0
|
120 |
|| name.compareToIgnoreCase(SLDTags.TEXTSYMBOLIZER) == 0
|
121 |
|| name.compareToIgnoreCase(SLDTags.RASTERSYMBOLIZER) == 0) {
|
122 |
|
123 |
/*
|
124 |
* This line is out of the SLD standard
|
125 |
*/
|
126 |
v = parser.getAttributeValue(null, SLDTags.VERSION_ATTR);
|
127 |
return parseSymbol(parser, v);
|
128 |
} |
129 |
} |
130 |
} |
131 |
throw new SLDReadException( |
132 |
"Misplaced input stream. Bad opening tag: " + name);
|
133 |
} |
134 |
|
135 |
private static SLDSymbol parseSymbol(XmlPullParser parser, String version) |
136 |
throws XmlPullParserException, IOException, SLDReadException { |
137 |
|
138 |
return null; |
139 |
} |
140 |
|
141 |
|
142 |
private static SLDLayer parseLayer(XmlPullParser parser, String version) |
143 |
throws XmlPullParserException, IOException, SLDReadException { |
144 |
|
145 |
String name = parser.getName();
|
146 |
SLDLayer resp = null;
|
147 |
|
148 |
if (SLDUtils.isStr(name, SLDTags.NAMEDLAYER)) {
|
149 |
resp = NamedLayerElement.parse(parser, version); |
150 |
} else {
|
151 |
if (SLDUtils.isStr(name, SLDTags.USERDEFINEDLAYER)) {
|
152 |
resp = UserLayerElement.parse(parser, version); |
153 |
} else {
|
154 |
throw new SLDReadException( |
155 |
"Expected layer, found: '" + name + "' tag"); |
156 |
|
157 |
} |
158 |
} |
159 |
return resp;
|
160 |
|
161 |
} |
162 |
|
163 |
private static SLDRoot parseRoot(XmlPullParser parser, String version) |
164 |
throws XmlPullParserException, IOException, SLDReadException { |
165 |
|
166 |
parser.require(KXmlParser.START_TAG, null, SLDTags.SLD_ROOT);
|
167 |
int tag = parser.nextTag();
|
168 |
String name = parser.getName();
|
169 |
SLDLayer layer_item = null;
|
170 |
SLDRoot resp = new SLDRoot(version);
|
171 |
|
172 |
while (tag == KXmlParser.START_TAG &&
|
173 |
(SLDUtils.isStr(name, SLDTags.USERDEFINEDLAYER) || |
174 |
SLDUtils.isStr(name, SLDTags.NAMEDLAYER))) { |
175 |
layer_item = parseLayer(parser, version); |
176 |
resp.getLayers().add(layer_item); |
177 |
// =============
|
178 |
name = parser.getName(); |
179 |
tag = parser.getEventType(); |
180 |
} |
181 |
parser.require(KXmlParser.END_TAG, null, SLDTags.SLD_ROOT);
|
182 |
parser.next(); // goes to end of doc
|
183 |
return resp;
|
184 |
} |
185 |
} |