Mercurial > dive4elements > gnv-client
comparison gnv/src/main/java/de/intevation/gnv/action/mapviewer/parser/XMLExternalCallParser.java @ 402:b88e881e8e94
Added the first Implementation (not complete and not ready to use) for the Interface from the MapViewer to the GNV
gnv/trunk@573 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Tim Englich <tim.englich@intevation.de> |
---|---|
date | Tue, 19 Jan 2010 14:47:36 +0000 |
parents | |
children | 15ac78a91d1b |
comparison
equal
deleted
inserted
replaced
401:002a4d38c16d | 402:b88e881e8e94 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.action.mapviewer.parser; | |
5 | |
6 import java.io.InputStream; | |
7 import java.util.ArrayList; | |
8 import java.util.Collection; | |
9 | |
10 | |
11 import org.apache.log4j.Logger; | |
12 import org.w3c.dom.Document; | |
13 import org.w3c.dom.Element; | |
14 import org.w3c.dom.NodeList; | |
15 | |
16 import de.intevation.gnv.artifactdatabase.objects.map.DefaultLayer; | |
17 import de.intevation.gnv.artifactdatabase.objects.map.DefaultMapService; | |
18 import de.intevation.gnv.artifactdatabase.objects.map.Layer; | |
19 import de.intevation.gnv.artifactdatabase.objects.map.MapService; | |
20 import de.intevation.gnv.util.XMLUtils; | |
21 | |
22 /** | |
23 * @author Tim Englich <tim.englich@intevation.de> | |
24 * | |
25 */ | |
26 public class XMLExternalCallParser implements ExternalCallParser { | |
27 | |
28 /** | |
29 * the logger, used to log exceptions and additonaly information | |
30 */ | |
31 private static Logger log = Logger.getLogger(XMLExternalCallParser.class); | |
32 | |
33 private static String XPATH_GEOMETRY = "/gnviewer/location/data"; | |
34 private static String XPATH_SRS = "/gnviewer/location/srs"; | |
35 private static String XPATH_MAPSERVICES_NODESET = "/gnviewer/mapservices/mapservice"; | |
36 private static String XPATH_LAYER = "layer"; | |
37 private static String ATTRIBUTE_ID = "id"; | |
38 private static String ATTRIBUTE_NAME = "name"; | |
39 private static String ATTRIBUTE_TYPE = "type"; | |
40 private static String ATTRIBUTE_URL = "url"; | |
41 | |
42 private String geometry = null; | |
43 private String srs = null; | |
44 | |
45 private Collection<MapService> mapServices = null; | |
46 | |
47 private InputStream inputStream = null; | |
48 | |
49 /** | |
50 * Constructor | |
51 */ | |
52 public XMLExternalCallParser(InputStream inputStream) { | |
53 this.inputStream = inputStream; | |
54 } | |
55 | |
56 /** | |
57 * @see de.intevation.gnv.action.mapviewer.parser.ExternalCallParser#getGeometry() | |
58 */ | |
59 public String getGeometry() { | |
60 return this.geometry; | |
61 } | |
62 | |
63 /** | |
64 * @see de.intevation.gnv.action.mapviewer.parser.ExternalCallParser#getMapServices() | |
65 */ | |
66 public Collection<MapService> getMapServices() { | |
67 return this.mapServices; | |
68 } | |
69 | |
70 /** | |
71 * @see de.intevation.gnv.action.mapviewer.parser.ExternalCallParser#parse() | |
72 */ | |
73 public void parse() throws ExternalCallParserException { | |
74 if (inputStream != null){ | |
75 XMLUtils xmlUtils = new XMLUtils(); | |
76 Document document = xmlUtils.readDocument(this.inputStream); | |
77 if (document != null){ | |
78 | |
79 this.geometry = xmlUtils.getStringXPath(document, XPATH_GEOMETRY); | |
80 this.srs = xmlUtils.getStringXPath(document, XPATH_SRS); | |
81 NodeList mapservicesNodes = | |
82 xmlUtils.getNodeSetXPath(document, XPATH_MAPSERVICES_NODESET); | |
83 if (mapservicesNodes != null){ | |
84 this.mapServices = new ArrayList<MapService>(mapservicesNodes.getLength()); | |
85 for (int i = 0; i < mapservicesNodes.getLength(); i++){ | |
86 Element mapserviceNode = (Element)mapservicesNodes.item(i); | |
87 String mapserviceID = mapserviceNode.getAttribute(ATTRIBUTE_ID); | |
88 String mapserviceType = mapserviceNode.getAttribute(ATTRIBUTE_TYPE); | |
89 String mapserviceUrl = mapserviceNode.getAttribute(ATTRIBUTE_URL); | |
90 NodeList layerNodes = xmlUtils.getNodeSetXPath(mapserviceNode, XPATH_LAYER); | |
91 Collection<Layer> layer = null; | |
92 if (layerNodes != null && layerNodes.getLength() > 0){ | |
93 layer = new ArrayList<Layer>(layerNodes.getLength()); | |
94 layer = this.extractLayer(layer, null, layerNodes); | |
95 }else{ | |
96 log.debug("No Layer given for this Mapservice"); | |
97 } | |
98 MapService mapService = | |
99 new DefaultMapService(mapserviceID, layer, | |
100 mapserviceType, mapserviceUrl); | |
101 this.mapServices.add(mapService); | |
102 } | |
103 | |
104 }else{ | |
105 String errMsg = "XML-Document does not contain any Mapservices which are required."; | |
106 log.error(errMsg); | |
107 throw new ExternalCallParserException(errMsg); | |
108 } | |
109 | |
110 }else{ | |
111 String errMsg = "XML-Document could not be read from InputStream."; | |
112 log.error(errMsg); | |
113 throw new ExternalCallParserException(errMsg); | |
114 } | |
115 }else{ | |
116 String errMsg = "No InputStream given for parsing the Call."; | |
117 log.error(errMsg); | |
118 throw new ExternalCallParserException(errMsg); | |
119 } | |
120 } | |
121 | |
122 /** | |
123 * This Method extracts all Layers and put them into the Collection. | |
124 * @param layer | |
125 * @param groupId | |
126 * @param layerNodes | |
127 * @return | |
128 */ | |
129 private Collection<Layer> extractLayer(Collection<Layer> layer, String groupId, NodeList layerNodes){ | |
130 XMLUtils xmlUtils = new XMLUtils(); | |
131 for (int i = 0; i < layerNodes.getLength(); i++){ | |
132 Element layerNode = (Element)layerNodes.item(i); | |
133 String id = layerNode.getAttribute(ATTRIBUTE_ID); | |
134 String name = layerNode.getAttribute(ATTRIBUTE_NAME); | |
135 NodeList localLayerNodes = xmlUtils.getNodeSetXPath(layerNode, XPATH_LAYER); | |
136 Layer tmpLayer = new DefaultLayer(id, name, | |
137 (localLayerNodes != null && | |
138 localLayerNodes.getLength() > 0), | |
139 groupId); | |
140 layer.add(tmpLayer); | |
141 if (localLayerNodes != null && localLayerNodes.getLength() > 0){ | |
142 layer = this.extractLayer(layer, id, localLayerNodes); | |
143 } | |
144 } | |
145 return layer; | |
146 } | |
147 | |
148 /** | |
149 * @see de.intevation.gnv.action.mapviewer.parser.ExternalCallParser#getSRS() | |
150 */ | |
151 public String getSRS() { | |
152 return this.srs; | |
153 } | |
154 | |
155 } |