comparison flys-client/src/main/java/de/intevation/flys/client/server/GFIServiceImpl.java @ 1402:15ef3d3081b7

Parse GetFeatureInfo response on our own and display resulting FeatureInfo objects in the GetFeatureInfoWindow. flys-client/trunk@3287 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 18 Nov 2011 11:39:10 +0000
parents 96708d81eaf6
children 442ce7d6bc39
comparison
equal deleted inserted replaced
1401:047a44270348 1402:15ef3d3081b7
5 import java.io.InputStreamReader; 5 import java.io.InputStreamReader;
6 import java.io.IOException; 6 import java.io.IOException;
7 import java.io.StringWriter; 7 import java.io.StringWriter;
8 import java.net.URL; 8 import java.net.URL;
9 import java.net.URLConnection; 9 import java.net.URLConnection;
10 import java.util.ArrayList;
10 import java.util.List; 11 import java.util.List;
11 12
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Node;
15 import org.w3c.dom.NodeList;
16
12 import org.apache.log4j.Logger; 17 import org.apache.log4j.Logger;
13 18
14 import com.google.gwt.user.server.rpc.RemoteServiceServlet; 19 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
15 20
21 import de.intevation.artifacts.common.utils.XMLUtils;
16 22
17 import de.intevation.flys.client.shared.exceptions.ServerException; 23 import de.intevation.flys.client.shared.exceptions.ServerException;
18 import de.intevation.flys.client.shared.model.AttributedTheme; 24 import de.intevation.flys.client.shared.model.AttributedTheme;
25 import de.intevation.flys.client.shared.model.FeatureInfo;
19 import de.intevation.flys.client.shared.model.Theme; 26 import de.intevation.flys.client.shared.model.Theme;
20 27
21 import de.intevation.flys.client.client.services.GFIService; 28 import de.intevation.flys.client.client.services.GFIService;
22 29
23 30
51 * @param x 58 * @param x
52 * @param y 59 * @param y
53 * 60 *
54 * @return 61 * @return
55 */ 62 */
56 public String query( 63 public List<FeatureInfo> query(
57 List<Theme> themes, 64 List<Theme> themes,
58 String format, 65 String format,
59 String bbox, 66 String bbox,
60 String projection, 67 String projection,
61 int height, 68 int height,
66 { 73 {
67 logger.info("GFIServiceImpl.query"); 74 logger.info("GFIServiceImpl.query");
68 75
69 String path = createGetFeautureInfoURL( 76 String path = createGetFeautureInfoURL(
70 themes, format, bbox, projection, height, width, x, y); 77 themes, format, bbox, projection, height, width, x, y);
78
71 logger.debug("URL=" + path); 79 logger.debug("URL=" + path);
72 80
73 try { 81 try {
74 URL url = new URL(path); 82 URL url = new URL(path);
75 83
76 URLConnection conn = url.openConnection(); 84 URLConnection conn = url.openConnection();
77 conn.connect(); 85 conn.connect();
78 86
79 InputStream is = conn.getInputStream(); 87 InputStream is = conn.getInputStream();
80 88
81 return getResponseText(is); 89 return parseResponse(is);
82 90
83 } 91 }
84 catch (IOException ioe) { 92 catch (IOException ioe) {
85 logger.warn(ioe, ioe); 93 logger.warn(ioe, ioe);
86 } 94 }
87 95
88 throw new ServerException(ERR_GFI_REQUEST_FAILED); 96 throw new ServerException(ERR_GFI_REQUEST_FAILED);
89 }
90
91
92 protected String getResponseText(InputStream is)
93 throws ServerException {
94 BufferedReader reader = null;
95 StringWriter writer = new StringWriter();
96
97 try {
98 reader = new BufferedReader(new InputStreamReader(is));
99
100 String line = null;
101
102 if (reader.ready()) {
103 while ((line = reader.readLine()) != null) {
104 String test = line.trim();
105 if (test.startsWith("<") && !test.startsWith("</")
106 && test.indexOf("_feature") > 0)
107 {
108 writer.append("<gml:featureMember>");
109 }
110 writer.append(line);
111
112 if (test.startsWith("</") && test.indexOf("_feature") > 0) {
113 writer.append("</gml:featureMember>");
114 }
115 }
116 }
117 }
118 catch (IOException ioe) {
119 logger.warn(ioe, ioe);
120 throw new ServerException(ERR_PARSING_RESPONSE_FAILED);
121 }
122 finally {
123 if (reader != null) {
124 try {
125 reader.close();
126 }
127 catch (IOException ioe) {
128 // do nothing here
129 }
130 }
131 }
132
133 return writer.toString();
134 } 97 }
135 98
136 99
137 /** 100 /**
138 * @param map 101 * @param map
218 } 181 }
219 } 182 }
220 183
221 return sb.toString(); 184 return sb.toString();
222 } 185 }
186
187
188 protected List<FeatureInfo> parseResponse(InputStream is) {
189 logger.debug("GFIServiceImpl.parseResponse");
190
191 Document response = XMLUtils.parseDocument(is);
192
193 List<FeatureInfo> features = new ArrayList<FeatureInfo>();
194
195 parseFeatureInfos(response, features);
196
197 return features;
198 }
199
200
201 protected void parseFeatureInfos(Node node, List<FeatureInfo> features) {
202 logger.debug("GFIServiceImpl.parseFeatureInfos");
203
204 String name = node.getNodeName();
205
206 if (name.endsWith("_layer")) {
207 features.add(parseFeature(node));
208
209 return;
210 }
211
212 NodeList children = node.getChildNodes();
213
214 if (children != null && children.getLength() > 0) {
215 for (int i = 0, n = children.getLength(); i < n; i++) {
216 parseFeatureInfos(children.item(i), features);
217 }
218 }
219 }
220
221
222 protected FeatureInfo parseFeature(Node node) {
223 logger.debug("GFIServiceImpl.parseFeature");
224
225 String layername = node.getNodeName();
226
227 FeatureInfo f = new FeatureInfo(layername);
228
229 NodeList children = node.getChildNodes();
230 int numChildren = children != null ? children.getLength() : 0;
231
232 logger.debug("Feature '" + layername + "' has " + numChildren + " nodes.");
233
234 for (int i = 0; i < numChildren; i++) {
235 Node tmp = children.item(i);
236 String nodeName = tmp.getNodeName();
237
238 logger.debug(" node name: '" + nodeName + "'");
239
240 if (nodeName.equals("gml:name")) {
241 logger.debug("NAME node has child: " + tmp.getFirstChild().getNodeValue());
242 f.setLayername(tmp.getFirstChild().getNodeValue());
243 }
244 else if (nodeName.endsWith("_feature")) {
245 parseFeatureAttributes(tmp, f);
246 }
247 }
248
249 return f;
250 }
251
252
253 protected void parseFeatureAttributes(Node node, FeatureInfo f) {
254 logger.debug("GFIServiceImpl.parseFeatureAttributes");
255
256 NodeList children = node.getChildNodes();
257 int numChildren = children != null ? children.getLength() : 0;
258
259 logger.debug("Has " + numChildren + " attributes.");
260
261 for (int i = 0; i < numChildren; i++) {
262 Node tmp = children.item(i);
263 String name = tmp.getNodeName();
264
265 logger.debug(" tmp attribute name: '" + name + "'");
266
267 if (name.equals("gml:boundedBy")) {
268 // TODO
269 }
270 else {
271 Node child = tmp.getFirstChild();
272 if (child != null) {
273 f.addAttr(name, child.getNodeValue());
274 }
275 }
276 }
277 }
223 } 278 }
224 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 279 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org