1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
2 | * full list of contributors). Published under the Clear BSD license. |
---|
3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
4 | * full text of the license. */ |
---|
5 | |
---|
6 | /** |
---|
7 | * @requires OpenLayers/Format/WMSCapabilities.js |
---|
8 | * @requires OpenLayers/Format/XML.js |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * Class: OpenLayers.Format.WMSCapabilities.v1 |
---|
13 | * Abstract class not to be instantiated directly. Creates |
---|
14 | * the common parts for both WMS 1.1.X and WMS 1.3.X. |
---|
15 | * |
---|
16 | * Inherits from: |
---|
17 | * - <OpenLayers.Format.XML> |
---|
18 | */ |
---|
19 | OpenLayers.Format.WMSCapabilities.v1 = OpenLayers.Class( |
---|
20 | OpenLayers.Format.XML, { |
---|
21 | |
---|
22 | /** |
---|
23 | * Property: namespaces |
---|
24 | * {Object} Mapping of namespace aliases to namespace URIs. |
---|
25 | */ |
---|
26 | namespaces: { |
---|
27 | wms: "http://www.opengis.net/wms", |
---|
28 | xlink: "http://www.w3.org/1999/xlink", |
---|
29 | xsi: "http://www.w3.org/2001/XMLSchema-instance" |
---|
30 | }, |
---|
31 | |
---|
32 | /** |
---|
33 | * Property: defaultPrefix |
---|
34 | */ |
---|
35 | defaultPrefix: "wms", |
---|
36 | |
---|
37 | /** |
---|
38 | * Constructor: OpenLayers.Format.WMSCapabilities.v1 |
---|
39 | * Create an instance of one of the subclasses. |
---|
40 | * |
---|
41 | * Parameters: |
---|
42 | * options - {Object} An optional object whose properties will be set on |
---|
43 | * this instance. |
---|
44 | */ |
---|
45 | initialize: function(options) { |
---|
46 | OpenLayers.Format.XML.prototype.initialize.apply(this, [options]); |
---|
47 | }, |
---|
48 | |
---|
49 | /** |
---|
50 | * APIMethod: read |
---|
51 | * Read capabilities data from a string, and return a list of layers. |
---|
52 | * |
---|
53 | * Parameters: |
---|
54 | * data - {String} or {DOMElement} data to read/parse. |
---|
55 | * |
---|
56 | * Returns: |
---|
57 | * {Array} List of named layers. |
---|
58 | */ |
---|
59 | read: function(data) { |
---|
60 | if(typeof data == "string") { |
---|
61 | data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); |
---|
62 | } |
---|
63 | if(data && data.nodeType == 9) { |
---|
64 | data = data.documentElement; |
---|
65 | } |
---|
66 | var capabilities = {}; |
---|
67 | this.readNode(data, capabilities); |
---|
68 | |
---|
69 | // postprocess the layer list |
---|
70 | this.postProcessLayers(capabilities); |
---|
71 | |
---|
72 | return capabilities; |
---|
73 | }, |
---|
74 | |
---|
75 | /** |
---|
76 | * Method: postProcessLayers |
---|
77 | * Post process the layers, so that the nested layer structure is converted |
---|
78 | * to a flat layer list with only named layers. |
---|
79 | * |
---|
80 | * Parameters: |
---|
81 | * capabilities - {Object} The object (structure) returned by the parser with |
---|
82 | * all the info from the GetCapabilities response. |
---|
83 | */ |
---|
84 | postProcessLayers: function(capabilities) { |
---|
85 | if (capabilities.capability) { |
---|
86 | capabilities.capability.layers = []; |
---|
87 | var layers = capabilities.capability.nestedLayers; |
---|
88 | for (var i=0, len = layers.length; i<len; ++i) { |
---|
89 | var layer = layers[i]; |
---|
90 | this.processLayer(capabilities.capability, layer); |
---|
91 | } |
---|
92 | } |
---|
93 | }, |
---|
94 | |
---|
95 | /** |
---|
96 | * Method: processLayer |
---|
97 | * Recursive submethod of postProcessLayers. This function will among |
---|
98 | * others deal with property inheritance. |
---|
99 | * |
---|
100 | * Parameters: |
---|
101 | * capability - {Object} The capability part of the capabilities object |
---|
102 | * layer - {Object} The layer that needs processing |
---|
103 | * parentLayer - {Object} The parent layer of the respective layer |
---|
104 | */ |
---|
105 | processLayer: function(capability, layer, parentLayer) { |
---|
106 | if (layer.formats === undefined) { |
---|
107 | layer.formats = capability.request.getmap.formats; |
---|
108 | } |
---|
109 | |
---|
110 | // deal with property inheritance |
---|
111 | if(parentLayer) { |
---|
112 | // add style |
---|
113 | layer.styles = layer.styles.concat(parentLayer.styles); |
---|
114 | var attributes = ["queryable", |
---|
115 | "cascaded", |
---|
116 | "fixedWidth", |
---|
117 | "fixedHeight", |
---|
118 | "opaque", |
---|
119 | "noSubsets", |
---|
120 | "llbbox", |
---|
121 | "minScale", |
---|
122 | "maxScale", |
---|
123 | "attribution"]; |
---|
124 | |
---|
125 | var complexAttr = ["srs", |
---|
126 | "bbox", |
---|
127 | "dimensions", |
---|
128 | "authorityURLs"]; |
---|
129 | |
---|
130 | var key; |
---|
131 | for (var j=0; j<attributes.length; j++) { |
---|
132 | key = attributes[j]; |
---|
133 | if (key in parentLayer) { |
---|
134 | // only take parent value if not present (null or undefined) |
---|
135 | if (layer[key] == null) { |
---|
136 | layer[key] = parentLayer[key]; |
---|
137 | } |
---|
138 | // if attribute isn't present, and we haven't |
---|
139 | // inherited anything from a parent layer |
---|
140 | // set to default value |
---|
141 | if (layer[key] == null) { |
---|
142 | var intAttr = ["cascaded", "fixedWidth", "fixedHeight"]; |
---|
143 | var boolAttr = ["queryable", "opaque", "noSubsets"]; |
---|
144 | if (OpenLayers.Util.indexOf(intAttr, key) != -1) { |
---|
145 | layer[key] = 0; |
---|
146 | } |
---|
147 | if (OpenLayers.Util.indexOf(boolAttr, key) != -1) { |
---|
148 | layer[key] = false; |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | for (var j=0; j<complexAttr.length; j++) { |
---|
155 | key = complexAttr[j]; |
---|
156 | layer[key] = OpenLayers.Util.extend( |
---|
157 | layer[key], parentLayer[key]); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | // process sublayers |
---|
162 | for (var i=0, len=layer.nestedLayers.length; i<len; i++) { |
---|
163 | var childLayer = layer.nestedLayers[i]; |
---|
164 | this.processLayer(capability, childLayer, layer); |
---|
165 | } |
---|
166 | |
---|
167 | if (layer.name) { |
---|
168 | capability.layers.push(layer); |
---|
169 | } |
---|
170 | |
---|
171 | }, |
---|
172 | |
---|
173 | /** |
---|
174 | * Property: readers |
---|
175 | * Contains public functions, grouped by namespace prefix, that will |
---|
176 | * be applied when a namespaced node is found matching the function |
---|
177 | * name. The function will be applied in the scope of this parser |
---|
178 | * with two arguments: the node being read and a context object passed |
---|
179 | * from the parent. |
---|
180 | */ |
---|
181 | readers: { |
---|
182 | "wms": { |
---|
183 | "Service": function(node, obj) { |
---|
184 | obj.service = {}; |
---|
185 | this.readChildNodes(node, obj.service); |
---|
186 | }, |
---|
187 | "Name": function(node, obj) { |
---|
188 | obj.name = this.getChildValue(node); |
---|
189 | }, |
---|
190 | "Title": function(node, obj) { |
---|
191 | obj.title = this.getChildValue(node); |
---|
192 | }, |
---|
193 | "Abstract": function(node, obj) { |
---|
194 | obj["abstract"] = this.getChildValue(node); |
---|
195 | }, |
---|
196 | "BoundingBox": function(node, obj) { |
---|
197 | var bbox = {}; |
---|
198 | bbox.bbox = [ |
---|
199 | parseFloat(node.getAttribute("minx")), |
---|
200 | parseFloat(node.getAttribute("miny")), |
---|
201 | parseFloat(node.getAttribute("maxx")), |
---|
202 | parseFloat(node.getAttribute("maxy")) |
---|
203 | ]; |
---|
204 | var res = { |
---|
205 | x: parseFloat(node.getAttribute("resx")), |
---|
206 | y: parseFloat(node.getAttribute("resy")) |
---|
207 | }; |
---|
208 | |
---|
209 | if (! (isNaN(res.x) && isNaN(res.y))) { |
---|
210 | bbox.res = res; |
---|
211 | } |
---|
212 | // return the bbox so that descendant classes can set the |
---|
213 | // CRS and SRS and add it to the obj |
---|
214 | return bbox; |
---|
215 | }, |
---|
216 | "OnlineResource": function(node, obj) { |
---|
217 | obj.href = this.getAttributeNS(node, this.namespaces.xlink, |
---|
218 | "href"); |
---|
219 | }, |
---|
220 | "ContactInformation": function(node, obj) { |
---|
221 | obj.contactInformation = {}; |
---|
222 | this.readChildNodes(node, obj.contactInformation); |
---|
223 | }, |
---|
224 | "ContactPersonPrimary": function(node, obj) { |
---|
225 | obj.personPrimary = {}; |
---|
226 | this.readChildNodes(node, obj.personPrimary); |
---|
227 | }, |
---|
228 | "ContactPerson": function(node, obj) { |
---|
229 | obj.person = this.getChildValue(node); |
---|
230 | }, |
---|
231 | "ContactOrganization": function(node, obj) { |
---|
232 | obj.organization = this.getChildValue(node); |
---|
233 | }, |
---|
234 | "ContactPosition": function(node, obj) { |
---|
235 | obj.position = this.getChildValue(node); |
---|
236 | }, |
---|
237 | "ContactAddress": function(node, obj) { |
---|
238 | obj.contactAddress = {}; |
---|
239 | this.readChildNodes(node, obj.contactAddress); |
---|
240 | }, |
---|
241 | "AddressType": function(node, obj) { |
---|
242 | obj.type = this.getChildValue(node); |
---|
243 | }, |
---|
244 | "Address": function(node, obj) { |
---|
245 | obj.address = this.getChildValue(node); |
---|
246 | }, |
---|
247 | "City": function(node, obj) { |
---|
248 | obj.city = this.getChildValue(node); |
---|
249 | }, |
---|
250 | "StateOrProvince": function(node, obj) { |
---|
251 | obj.stateOrProvince = this.getChildValue(node); |
---|
252 | }, |
---|
253 | "PostCode": function(node, obj) { |
---|
254 | obj.postcode = this.getChildValue(node); |
---|
255 | }, |
---|
256 | "Country": function(node, obj) { |
---|
257 | obj.country = this.getChildValue(node); |
---|
258 | }, |
---|
259 | "ContactVoiceTelephone": function(node, obj) { |
---|
260 | obj.phone = this.getChildValue(node); |
---|
261 | }, |
---|
262 | "ContactFacsimileTelephone": function(node, obj) { |
---|
263 | obj.fax = this.getChildValue(node); |
---|
264 | }, |
---|
265 | "ContactElectronicMailAddress": function(node, obj) { |
---|
266 | obj.email = this.getChildValue(node); |
---|
267 | }, |
---|
268 | "Fees": function(node, obj) { |
---|
269 | var fees = this.getChildValue(node); |
---|
270 | if (fees && fees.toLowerCase() != "none") { |
---|
271 | obj.fees = fees; |
---|
272 | } |
---|
273 | }, |
---|
274 | "AccessConstraints": function(node, obj) { |
---|
275 | var constraints = this.getChildValue(node); |
---|
276 | if (constraints && constraints.toLowerCase() != "none") { |
---|
277 | obj.accessConstraints = constraints; |
---|
278 | } |
---|
279 | }, |
---|
280 | "Capability": function(node, obj) { |
---|
281 | obj.capability = {nestedLayers: []}; |
---|
282 | this.readChildNodes(node, obj.capability); |
---|
283 | }, |
---|
284 | "Request": function(node, obj) { |
---|
285 | obj.request = {}; |
---|
286 | this.readChildNodes(node, obj.request); |
---|
287 | }, |
---|
288 | "GetCapabilities": function(node, obj) { |
---|
289 | obj.getcapabilities = {formats: []}; |
---|
290 | this.readChildNodes(node, obj.getcapabilities); |
---|
291 | }, |
---|
292 | "Format": function(node, obj) { |
---|
293 | if (obj.formats instanceof Array) { |
---|
294 | obj.formats.push(this.getChildValue(node)); |
---|
295 | } else { |
---|
296 | obj.format = this.getChildValue(node); |
---|
297 | } |
---|
298 | }, |
---|
299 | "DCPType": function(node, obj) { |
---|
300 | this.readChildNodes(node, obj); |
---|
301 | }, |
---|
302 | "HTTP": function(node, obj) { |
---|
303 | this.readChildNodes(node, obj); |
---|
304 | }, |
---|
305 | "Get": function(node, obj) { |
---|
306 | this.readChildNodes(node, obj); |
---|
307 | }, |
---|
308 | "Post": function(node, obj) { |
---|
309 | this.readChildNodes(node, obj); |
---|
310 | }, |
---|
311 | "GetMap": function(node, obj) { |
---|
312 | obj.getmap = {formats: []}; |
---|
313 | this.readChildNodes(node, obj.getmap); |
---|
314 | }, |
---|
315 | "GetFeatureInfo": function(node, obj) { |
---|
316 | obj.getfeatureinfo = {formats: []}; |
---|
317 | this.readChildNodes(node, obj.getfeatureinfo); |
---|
318 | }, |
---|
319 | "Exception": function(node, obj) { |
---|
320 | obj.exception = {formats: []}; |
---|
321 | this.readChildNodes(node, obj.exception); |
---|
322 | }, |
---|
323 | "Layer": function(node, obj) { |
---|
324 | var attrNode = node.getAttributeNode("queryable"); |
---|
325 | var queryable = (attrNode && attrNode.specified) ? |
---|
326 | node.getAttribute("queryable") : null; |
---|
327 | attrNode = node.getAttributeNode("cascaded"); |
---|
328 | var cascaded = (attrNode && attrNode.specified) ? |
---|
329 | node.getAttribute("cascaded") : null; |
---|
330 | attrNode = node.getAttributeNode("opaque"); |
---|
331 | var opaque = (attrNode && attrNode.specified) ? |
---|
332 | node.getAttribute('opaque') : null; |
---|
333 | var noSubsets = node.getAttribute('noSubsets'); |
---|
334 | var fixedWidth = node.getAttribute('fixedWidth'); |
---|
335 | var fixedHeight = node.getAttribute('fixedHeight'); |
---|
336 | var layer = {nestedLayers: [], styles: [], srs: {}, |
---|
337 | metadataURLs: [], bbox: {}, dimensions: {}, |
---|
338 | authorityURLs: {}, identifiers: {}, keywords: [], |
---|
339 | queryable: (queryable && queryable !== "") ? |
---|
340 | ( queryable === "1" || queryable === "true" ) : null, |
---|
341 | cascaded: (cascaded !== null) ? parseInt(cascaded) : null, |
---|
342 | opaque: opaque ? |
---|
343 | (opaque === "1" || opaque === "true" ) : null, |
---|
344 | noSubsets: (noSubsets !== null) ? |
---|
345 | ( noSubsets === "1" || noSubsets === "true" ) : null, |
---|
346 | fixedWidth: (fixedWidth != null) ? |
---|
347 | parseInt(fixedWidth) : null, |
---|
348 | fixedHeight: (fixedHeight != null) ? |
---|
349 | parseInt(fixedHeight) : null |
---|
350 | }; |
---|
351 | obj.nestedLayers.push(layer); |
---|
352 | this.readChildNodes(node, layer); |
---|
353 | if(layer.name) { |
---|
354 | var parts = layer.name.split(":"); |
---|
355 | if(parts.length > 0) { |
---|
356 | layer.prefix = parts[0]; |
---|
357 | } |
---|
358 | } |
---|
359 | }, |
---|
360 | "Attribution": function(node, obj) { |
---|
361 | obj.attribution = {}; |
---|
362 | this.readChildNodes(node, obj.attribution); |
---|
363 | }, |
---|
364 | "LogoURL": function(node, obj) { |
---|
365 | obj.logo = { |
---|
366 | width: node.getAttribute("width"), |
---|
367 | height: node.getAttribute("height") |
---|
368 | }; |
---|
369 | this.readChildNodes(node, obj.logo); |
---|
370 | }, |
---|
371 | "Style": function(node, obj) { |
---|
372 | var style = {}; |
---|
373 | obj.styles.push(style); |
---|
374 | this.readChildNodes(node, style); |
---|
375 | }, |
---|
376 | "LegendURL": function(node, obj) { |
---|
377 | var legend = { |
---|
378 | width: node.getAttribute("width"), |
---|
379 | height: node.getAttribute("height") |
---|
380 | }; |
---|
381 | obj.legend = legend; |
---|
382 | this.readChildNodes(node, legend); |
---|
383 | }, |
---|
384 | "MetadataURL": function(node, obj) { |
---|
385 | var metadataURL = {type: node.getAttribute("type")}; |
---|
386 | obj.metadataURLs.push(metadataURL); |
---|
387 | this.readChildNodes(node, metadataURL); |
---|
388 | }, |
---|
389 | "DataURL": function(node, obj) { |
---|
390 | obj.dataURL = {}; |
---|
391 | this.readChildNodes(node, obj.dataURL); |
---|
392 | }, |
---|
393 | "FeatureListURL": function(node, obj) { |
---|
394 | obj.featureListURL = {}; |
---|
395 | this.readChildNodes(node, obj.featureListURL); |
---|
396 | }, |
---|
397 | "AuthorityURL": function(node, obj) { |
---|
398 | var name = node.getAttribute("name"); |
---|
399 | var authority = {}; |
---|
400 | this.readChildNodes(node, authority); |
---|
401 | obj.authorityURLs[name] = authority.href; |
---|
402 | }, |
---|
403 | "Identifier": function(node, obj) { |
---|
404 | var authority = node.getAttribute("authority"); |
---|
405 | obj.identifiers[authority] = this.getChildValue(node); |
---|
406 | }, |
---|
407 | "KeywordList": function(node, obj) { |
---|
408 | this.readChildNodes(node, obj); |
---|
409 | }, |
---|
410 | "SRS": function(node, obj) { |
---|
411 | obj.srs[this.getChildValue(node)] = true; |
---|
412 | } |
---|
413 | } |
---|
414 | }, |
---|
415 | |
---|
416 | CLASS_NAME: "OpenLayers.Format.WMSCapabilities.v1" |
---|
417 | |
---|
418 | }); |
---|