~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to gis/dhis-gis-geostat/mfbase/openlayers/lib/OpenLayers/Format/WMC/v1.js

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
 
2
 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
 
3
 * full text of the license. */
 
4
 
 
5
/**
 
6
 * @requires OpenLayers/Format/XML.js
 
7
 */
 
8
 
 
9
/**
 
10
 * Class: OpenLayers.Format.WMC.v1
 
11
 * Superclass for WMC version 1 parsers.
 
12
 *
 
13
 * Inherits from:
 
14
 *  - <OpenLayers.Format.XML>
 
15
 */
 
16
OpenLayers.Format.WMC.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
 
17
    
 
18
    /**
 
19
     * Property: namespaces
 
20
     * {Object} Mapping of namespace aliases to namespace URIs.
 
21
     */
 
22
    namespaces: {
 
23
        ol: "http://openlayers.org/context",
 
24
        wmc: "http://www.opengis.net/context",
 
25
        sld: "http://www.opengis.net/sld",
 
26
        xlink: "http://www.w3.org/1999/xlink",
 
27
        xsi: "http://www.w3.org/2001/XMLSchema-instance"
 
28
    },
 
29
    
 
30
    /**
 
31
     * Property: schemaLocation
 
32
     * {String} Schema location for a particular minor version.
 
33
     */
 
34
    schemaLocation: "",
 
35
 
 
36
    /**
 
37
     * Method: getNamespacePrefix
 
38
     * Get the namespace prefix for a given uri from the <namespaces> object.
 
39
     *
 
40
     * Returns:
 
41
     * {String} A namespace prefix or null if none found.
 
42
     */
 
43
    getNamespacePrefix: function(uri) {
 
44
        var prefix = null;
 
45
        if(uri == null) {
 
46
            prefix = this.namespaces[this.defaultPrefix];
 
47
        } else {
 
48
            for(prefix in this.namespaces) {
 
49
                if(this.namespaces[prefix] == uri) {
 
50
                    break;
 
51
                }
 
52
            }
 
53
        }
 
54
        return prefix;
 
55
    },
 
56
    
 
57
    /**
 
58
     * Property: defaultPrefix
 
59
     */
 
60
    defaultPrefix: "wmc",
 
61
 
 
62
    /**
 
63
     * Property: rootPrefix
 
64
     * {String} Prefix on the root node that maps to the context namespace URI.
 
65
     */
 
66
    rootPrefix: null,
 
67
    
 
68
    /**
 
69
     * Property: defaultStyleName
 
70
     * {String} Style name used if layer has no style param.  Default is "".
 
71
     */
 
72
    defaultStyleName: "",
 
73
    
 
74
    /**
 
75
     * Property: defaultStyleTitle
 
76
     * {String} Default style title.  Default is "Default".
 
77
     */
 
78
    defaultStyleTitle: "Default",
 
79
    
 
80
    /**
 
81
     * Constructor: OpenLayers.Format.WMC.v1
 
82
     * Instances of this class are not created directly.  Use the
 
83
     *     <OpenLayers.Format.WMC> constructor instead.
 
84
     *
 
85
     * Parameters:
 
86
     * options - {Object} An optional object whose properties will be set on
 
87
     *     this instance.
 
88
     */
 
89
    initialize: function(options) {
 
90
        OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
 
91
    },
 
92
 
 
93
    /**
 
94
     * Method: read
 
95
     * Read capabilities data from a string, and return a list of layers. 
 
96
     * 
 
97
     * Parameters: 
 
98
     * data - {String} or {DOMElement} data to read/parse.
 
99
     *
 
100
     * Returns:
 
101
     * {Array} List of named layers.
 
102
     */
 
103
    read: function(data) {
 
104
        if(typeof data == "string") {
 
105
            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
 
106
        }
 
107
        var root = data.documentElement;
 
108
        this.rootPrefix = root.prefix;
 
109
        var context = {
 
110
            version: root.getAttribute("version")
 
111
        };
 
112
        this.runChildNodes(context, root);
 
113
        return context;
 
114
    },
 
115
    
 
116
    /**
 
117
     * Method: runChildNodes
 
118
     */
 
119
    runChildNodes: function(obj, node) {
 
120
        var children = node.childNodes;
 
121
        var childNode, processor, prefix, local;
 
122
        for(var i=0, len=children.length; i<len; ++i) {
 
123
            childNode = children[i];
 
124
            if(childNode.nodeType == 1) {
 
125
                prefix = this.getNamespacePrefix(childNode.namespaceURI);
 
126
                local = childNode.nodeName.split(":").pop();
 
127
                processor = this["read_" + prefix + "_" + local];
 
128
                if(processor) {
 
129
                    processor.apply(this, [obj, childNode]);
 
130
                }
 
131
            }
 
132
        }
 
133
    },
 
134
    
 
135
    /**
 
136
     * Method: read_wmc_General
 
137
     */
 
138
    read_wmc_General: function(context, node) {
 
139
        this.runChildNodes(context, node);
 
140
    },
 
141
    
 
142
    /**
 
143
     * Method: read_wmc_BoundingBox
 
144
     */
 
145
    read_wmc_BoundingBox: function(context, node) {
 
146
        context.projection = node.getAttribute("SRS");
 
147
        context.bounds = new OpenLayers.Bounds(
 
148
            parseFloat(node.getAttribute("minx")),
 
149
            parseFloat(node.getAttribute("miny")),
 
150
            parseFloat(node.getAttribute("maxx")),
 
151
            parseFloat(node.getAttribute("maxy"))
 
152
        );
 
153
    },
 
154
    
 
155
    /**
 
156
     * Method: read_wmc_LayerList
 
157
     */
 
158
    read_wmc_LayerList: function(context, node) {
 
159
        context.layers = [];
 
160
        this.runChildNodes(context, node);
 
161
    },
 
162
    
 
163
    /**
 
164
     * Method: read_wmc_Layer
 
165
     */
 
166
    read_wmc_Layer: function(context, node) {
 
167
        var layerInfo = {
 
168
            params: {},
 
169
            options: {
 
170
                visibility: (node.getAttribute("hidden") != "1"),
 
171
                queryable: (node.getAttribute("queryable") == "1")
 
172
                
 
173
            },
 
174
            formats: [],
 
175
            styles: []
 
176
        };
 
177
        this.runChildNodes(layerInfo, node);
 
178
        // set properties common to multiple objects on layer options/params
 
179
        layerInfo.params.layers = layerInfo.name;
 
180
        layerInfo.options.maxExtent = layerInfo.maxExtent;
 
181
        // create the layer
 
182
        var layer = this.getLayerFromInfo(layerInfo);
 
183
        context.layers.push(layer);
 
184
    },
 
185
    
 
186
    /**
 
187
     * Method: getLayerFromInfo
 
188
     * Create a WMS layer from a layerInfo object.
 
189
     *
 
190
     * Parameters:
 
191
     * layerInfo - {Object} An object representing a WMS layer.
 
192
     *
 
193
     * Returns:
 
194
     * {<OpenLayers.Layer.WMS>} A WMS layer.
 
195
     */
 
196
    getLayerFromInfo: function(layerInfo) {
 
197
        var options = layerInfo.options;
 
198
        if (this.layerOptions) {
 
199
            OpenLayers.Util.applyDefaults(options, this.layerOptions);
 
200
        }
 
201
        var layer = new OpenLayers.Layer.WMS(
 
202
            layerInfo.title,
 
203
            layerInfo.href,
 
204
            layerInfo.params,
 
205
            options
 
206
        );
 
207
        return layer;
 
208
    },
 
209
    
 
210
    /**
 
211
     * Method: read_wmc_Extension
 
212
     */
 
213
    read_wmc_Extension: function(obj, node) {
 
214
        this.runChildNodes(obj, node);
 
215
    },
 
216
 
 
217
    /**
 
218
     * Method: read_ol_units
 
219
     */
 
220
    read_ol_units: function(layerInfo, node) {
 
221
        layerInfo.options.units = this.getChildValue(node);
 
222
    },
 
223
    
 
224
    /**
 
225
     * Method: read_ol_maxExtent
 
226
     */
 
227
    read_ol_maxExtent: function(obj, node) {
 
228
        var bounds = new OpenLayers.Bounds(
 
229
            node.getAttribute("minx"), node.getAttribute("miny"),
 
230
            node.getAttribute("maxx"), node.getAttribute("maxy")
 
231
        );
 
232
        obj.maxExtent = bounds;
 
233
    },
 
234
    
 
235
    /**
 
236
     * Method: read_ol_transparent
 
237
     */
 
238
    read_ol_transparent: function(layerInfo, node) {
 
239
        layerInfo.params.transparent = this.getChildValue(node);
 
240
    },
 
241
 
 
242
    /**
 
243
     * Method: read_ol_numZoomLevels
 
244
     */
 
245
    read_ol_numZoomLevels: function(layerInfo, node) {
 
246
        layerInfo.options.numZoomLevels = parseInt(this.getChildValue(node));
 
247
    },
 
248
 
 
249
    /**
 
250
     * Method: read_ol_opacity
 
251
     */
 
252
    read_ol_opacity: function(layerInfo, node) {
 
253
        layerInfo.options.opacity = parseFloat(this.getChildValue(node));
 
254
    },
 
255
 
 
256
    /**
 
257
     * Method: read_ol_singleTile
 
258
     */
 
259
    read_ol_singleTile: function(layerInfo, node) {
 
260
        layerInfo.options.singleTile = (this.getChildValue(node) == "true");
 
261
    },
 
262
 
 
263
    /**
 
264
     * Method: read_ol_isBaseLayer
 
265
     */
 
266
    read_ol_isBaseLayer: function(layerInfo, node) {
 
267
        layerInfo.options.isBaseLayer = (this.getChildValue(node) == "true");
 
268
    },
 
269
 
 
270
    /**
 
271
     * Method: read_ol_displayInLayerSwitcher
 
272
     */
 
273
    read_ol_displayInLayerSwitcher: function(layerInfo, node) {
 
274
        layerInfo.options.displayInLayerSwitcher =
 
275
            (this.getChildValue(node) == "true");
 
276
    },
 
277
 
 
278
    /**
 
279
     * Method: read_wmc_Server
 
280
     */
 
281
    read_wmc_Server: function(layerInfo, node) {
 
282
        layerInfo.params.version = node.getAttribute("version");
 
283
        this.runChildNodes(layerInfo, node);
 
284
    },
 
285
 
 
286
    /**
 
287
     * Method: read_wmc_FormatList
 
288
     */
 
289
    read_wmc_FormatList: function(layerInfo, node) {
 
290
        this.runChildNodes(layerInfo, node);
 
291
    },
 
292
 
 
293
    /**
 
294
     * Method: read_wmc_Format
 
295
     */
 
296
    read_wmc_Format: function(layerInfo, node) {
 
297
        var format = this.getChildValue(node);
 
298
        layerInfo.formats.push(format);
 
299
        if(node.getAttribute("current") == "1") {
 
300
            layerInfo.params.format = format;
 
301
        }
 
302
    },
 
303
    
 
304
    /**
 
305
     * Method: read_wmc_StyleList
 
306
     */
 
307
    read_wmc_StyleList: function(layerInfo, node) {
 
308
        this.runChildNodes(layerInfo, node);
 
309
    },
 
310
 
 
311
    /**
 
312
     * Method: read_wmc_Style
 
313
     */
 
314
    read_wmc_Style: function(layerInfo, node) {
 
315
        var style = {};
 
316
        this.runChildNodes(style, node);
 
317
        if(node.getAttribute("current") == "1") {
 
318
            // three style types to consider
 
319
            // 1) linked SLD
 
320
            // 2) inline SLD
 
321
            // 3) named style
 
322
            // running child nodes always gets name, optionally gets href or body
 
323
            if(style.href) {
 
324
                layerInfo.params.sld = style.href;
 
325
            } else if(style.body) {
 
326
                layerInfo.params.sld_body = style.body;
 
327
            } else {
 
328
                layerInfo.params.styles = style.name;
 
329
            }
 
330
        }
 
331
        layerInfo.styles.push(style);
 
332
    },
 
333
    
 
334
    /**
 
335
     * Method: read_wmc_SLD
 
336
     */
 
337
    read_wmc_SLD: function(style, node) {
 
338
        this.runChildNodes(style, node);
 
339
        // style either comes back with an href or a body property
 
340
    },
 
341
    
 
342
    /**
 
343
     * Method: read_sld_StyledLayerDescriptor
 
344
     */
 
345
    read_sld_StyledLayerDescriptor: function(sld, node) {
 
346
        var xml = OpenLayers.Format.XML.prototype.write.apply(this, [node]);
 
347
        sld.body = xml;
 
348
    },
 
349
 
 
350
    /**
 
351
     * Method: read_wmc_OnlineResource
 
352
     */
 
353
    read_wmc_OnlineResource: function(obj, node) {
 
354
        obj.href = this.getAttributeNS(
 
355
            node, this.namespaces.xlink, "href"
 
356
        );
 
357
    },
 
358
    
 
359
    /**
 
360
     * Method: read_wmc_Name
 
361
     */
 
362
    read_wmc_Name: function(obj, node) {
 
363
        var name = this.getChildValue(node);
 
364
        if(name) {
 
365
            obj.name = name;
 
366
        }
 
367
    },
 
368
 
 
369
    /**
 
370
     * Method: read_wmc_Title
 
371
     */
 
372
    read_wmc_Title: function(obj, node) {
 
373
        var title = this.getChildValue(node);
 
374
        if(title) {
 
375
            obj.title = title;
 
376
        }
 
377
    },
 
378
 
 
379
    /**
 
380
     * Method: read_wmc_MetadataURL
 
381
     */
 
382
    read_wmc_MetadataURL: function(layerInfo, node) {
 
383
        var metadataURL = {};
 
384
        var links = node.getElementsByTagName("OnlineResource");
 
385
        if(links.length > 0) {
 
386
            this.read_wmc_OnlineResource(metadataURL, links[0]);
 
387
        }
 
388
        layerInfo.options.metadataURL = metadataURL.href;
 
389
 
 
390
    },
 
391
 
 
392
    /**
 
393
     * Method: read_wmc_Abstract
 
394
     */
 
395
    read_wmc_Abstract: function(obj, node) {
 
396
        var abst = this.getChildValue(node);
 
397
        if(abst) {
 
398
            obj["abstract"] = abst;
 
399
        }
 
400
    },
 
401
    
 
402
    /**
 
403
     * Method: read_wmc_LatLonBoundingBox
 
404
     */
 
405
    read_wmc_LatLonBoundingBox: function(layer, node) {
 
406
        layer.llbbox = [
 
407
            parseFloat(node.getAttribute("minx")),
 
408
            parseFloat(node.getAttribute("miny")),
 
409
            parseFloat(node.getAttribute("maxx")),
 
410
            parseFloat(node.getAttribute("maxy"))
 
411
        ];
 
412
    },
 
413
 
 
414
    /**
 
415
     * Method: read_wmc_LegendURL
 
416
     */
 
417
    read_wmc_LegendURL: function(style, node) {
 
418
        var legend = {
 
419
            width: node.getAttribute('width'),
 
420
            height: node.getAttribute('height')
 
421
        };
 
422
        var links = node.getElementsByTagName("OnlineResource");
 
423
        if(links.length > 0) {
 
424
            this.read_wmc_OnlineResource(legend, links[0]);
 
425
        }
 
426
        style.legend = legend;
 
427
    },
 
428
    
 
429
    /**
 
430
     * Method: write
 
431
     *
 
432
     * Parameters:
 
433
     * context - {Object} An object representing the map context.
 
434
     * options - {Object} Optional object.
 
435
     *
 
436
     * Returns:
 
437
     * {String} A WMC document string.
 
438
     */
 
439
    write: function(context, options) {
 
440
        var root = this.createElementDefaultNS("ViewContext");
 
441
        this.setAttributes(root, {
 
442
            version: this.VERSION,
 
443
            id: (options && typeof options.id == "string") ?
 
444
                    options.id :
 
445
                    OpenLayers.Util.createUniqueID("OpenLayers_Context_")
 
446
        });
 
447
        
 
448
        // add schemaLocation attribute
 
449
        this.setAttributeNS(
 
450
            root, this.namespaces.xsi,
 
451
            "xsi:schemaLocation", this.schemaLocation
 
452
        );
 
453
        
 
454
        // required General element
 
455
        root.appendChild(this.write_wmc_General(context));
 
456
 
 
457
        // required LayerList element
 
458
        root.appendChild(this.write_wmc_LayerList(context));
 
459
 
 
460
        return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
 
461
    },
 
462
    
 
463
    /**
 
464
     * Method: createElementDefaultNS
 
465
     * Shorthand for createElementNS with namespace from <defaultPrefix>.
 
466
     *     Can optionally be used to set attributes and a text child value.
 
467
     *
 
468
     * Parameters:
 
469
     * name - {String} The qualified node name.
 
470
     * childValue - {String} Optional value for text child node.
 
471
     * attributes - {Object} Optional object representing attributes.
 
472
     *
 
473
     * Returns:
 
474
     * {Element} An element node.
 
475
     */
 
476
    createElementDefaultNS: function(name, childValue, attributes) {
 
477
        var node = this.createElementNS(
 
478
            this.namespaces[this.defaultPrefix],
 
479
            name
 
480
        );
 
481
        if(childValue) {
 
482
            node.appendChild(this.createTextNode(childValue));
 
483
        }
 
484
        if(attributes) {
 
485
            this.setAttributes(node, attributes);
 
486
        }
 
487
        return node;
 
488
    },
 
489
    
 
490
    /**
 
491
     * Method: setAttributes
 
492
     * Set multiple attributes given key value pairs from an object.
 
493
     *
 
494
     * Parameters:
 
495
     * node - {Element} An element node.
 
496
     * obj - {Object} An object whose properties represent attribute names and
 
497
     *     values represent attribute values.
 
498
     */
 
499
    setAttributes: function(node, obj) {
 
500
        var value;
 
501
        for(var name in obj) {
 
502
            value = obj[name].toString();
 
503
            if(value.match(/[A-Z]/)) {
 
504
                // safari lowercases attributes with setAttribute
 
505
                this.setAttributeNS(node, null, name, value);
 
506
            } else {
 
507
                node.setAttribute(name, value);
 
508
            }
 
509
        }
 
510
    },
 
511
 
 
512
    /**
 
513
     * Method: write_wmc_General
 
514
     * Create a General node given an context object.
 
515
     *
 
516
     * Parameters:
 
517
     * context - {Object} Context object.
 
518
     *
 
519
     * Returns:
 
520
     * {Element} A WMC General element node.
 
521
     */
 
522
    write_wmc_General: function(context) {
 
523
        var node = this.createElementDefaultNS("General");
 
524
 
 
525
        // optional Window element
 
526
        if(context.size) {
 
527
            node.appendChild(this.createElementDefaultNS(
 
528
                "Window", null,
 
529
                {
 
530
                    width: context.size.w,
 
531
                    height: context.size.h
 
532
                }
 
533
            ));
 
534
        }
 
535
        
 
536
        // required BoundingBox element
 
537
        var bounds = context.bounds;
 
538
        node.appendChild(this.createElementDefaultNS(
 
539
            "BoundingBox", null,
 
540
            {
 
541
                minx: bounds.left.toPrecision(10),
 
542
                miny: bounds.bottom.toPrecision(10),
 
543
                maxx: bounds.right.toPrecision(10),
 
544
                maxy: bounds.top.toPrecision(10),
 
545
                SRS: context.projection
 
546
            }
 
547
        ));
 
548
 
 
549
        // required Title element
 
550
        node.appendChild(this.createElementDefaultNS(
 
551
            "Title", context.title
 
552
        ));
 
553
        
 
554
        // OpenLayers specific map properties
 
555
        node.appendChild(this.write_ol_MapExtension(context));
 
556
        
 
557
        return node;
 
558
    },
 
559
    
 
560
    /**
 
561
     * Method: write_ol_MapExtension
 
562
     */
 
563
    write_ol_MapExtension: function(context) {
 
564
        var node = this.createElementDefaultNS("Extension");
 
565
        
 
566
        var bounds = context.maxExtent;
 
567
        if(bounds) {
 
568
            var maxExtent = this.createElementNS(
 
569
                this.namespaces.ol, "ol:maxExtent"
 
570
            );
 
571
            this.setAttributes(maxExtent, {
 
572
                minx: bounds.left.toPrecision(10),
 
573
                miny: bounds.bottom.toPrecision(10),
 
574
                maxx: bounds.right.toPrecision(10),
 
575
                maxy: bounds.top.toPrecision(10)
 
576
            });
 
577
            node.appendChild(maxExtent);
 
578
        }
 
579
        
 
580
        return node;
 
581
    },
 
582
    
 
583
    /**
 
584
     * Method: write_wmc_LayerList
 
585
     * Create a LayerList node given an context object.
 
586
     *
 
587
     * Parameters:
 
588
     * context - {Object} Context object.
 
589
     *
 
590
     * Returns:
 
591
     * {Element} A WMC LayerList element node.
 
592
     */
 
593
    write_wmc_LayerList: function(context) {
 
594
        var list = this.createElementDefaultNS("LayerList");
 
595
        
 
596
        var layer;
 
597
        for(var i=0, len=context.layers.length; i<len; ++i) {
 
598
            layer = context.layers[i];
 
599
            if(layer instanceof OpenLayers.Layer.WMS) {
 
600
                list.appendChild(this.write_wmc_Layer(layer));
 
601
            }
 
602
        }
 
603
        
 
604
        return list;
 
605
    },
 
606
 
 
607
    /**
 
608
     * Method: write_wmc_Layer
 
609
     * Create a Layer node given a layer object.
 
610
     *
 
611
     * Parameters:
 
612
     * layer - {<OpenLayers.Layer.WMS>} Layer object.
 
613
     *
 
614
     * Returns:
 
615
     * {Element} A WMC Layer element node.
 
616
     */
 
617
    write_wmc_Layer: function(layer) {
 
618
        var node = this.createElementDefaultNS(
 
619
            "Layer", null, {
 
620
                queryable: layer.queryable ? "1" : "0",
 
621
                hidden: layer.visibility ? "0" : "1"
 
622
            }
 
623
        );
 
624
        
 
625
        // required Server element
 
626
        node.appendChild(this.write_wmc_Server(layer));
 
627
 
 
628
        // required Name element
 
629
        node.appendChild(this.createElementDefaultNS(
 
630
            "Name", layer.params["LAYERS"]
 
631
        ));
 
632
        
 
633
        // required Title element
 
634
        node.appendChild(this.createElementDefaultNS(
 
635
            "Title", layer.name
 
636
        ));
 
637
 
 
638
        // optional MetadataURL element
 
639
        if (layer.metadataURL) {
 
640
            node.appendChild(this.write_wmc_MetadataURL(layer));
 
641
        }
 
642
        
 
643
        // optional FormatList element
 
644
        node.appendChild(this.write_wmc_FormatList(layer));
 
645
 
 
646
        // optional StyleList element
 
647
        node.appendChild(this.write_wmc_StyleList(layer));
 
648
        
 
649
        // OpenLayers specific properties go in an Extension element
 
650
        node.appendChild(this.write_wmc_LayerExtension(layer));
 
651
 
 
652
        return node;
 
653
    },
 
654
    
 
655
    /**
 
656
     * Method: write_wmc_LayerExtension
 
657
     * Add OpenLayers specific layer parameters to an Extension element.
 
658
     *
 
659
     * Parameters:
 
660
     * layer - {<OpenLayers.Layer.WMS>} A WMS layer.
 
661
     *
 
662
     * Returns:
 
663
     * {Element} A WMC Extension element (for a layer).
 
664
     */
 
665
    write_wmc_LayerExtension: function(layer) {
 
666
        var node = this.createElementDefaultNS("Extension");
 
667
        
 
668
        var bounds = layer.maxExtent;
 
669
        var maxExtent = this.createElementNS(
 
670
            this.namespaces.ol, "ol:maxExtent"
 
671
        );
 
672
        this.setAttributes(maxExtent, {
 
673
            minx: bounds.left.toPrecision(10),
 
674
            miny: bounds.bottom.toPrecision(10),
 
675
            maxx: bounds.right.toPrecision(10),
 
676
            maxy: bounds.top.toPrecision(10)
 
677
        });
 
678
        node.appendChild(maxExtent);
 
679
        
 
680
        var param = layer.params["TRANSPARENT"];
 
681
        if(param) {
 
682
            var trans = this.createElementNS(
 
683
                this.namespaces.ol, "ol:transparent"
 
684
            );
 
685
            trans.appendChild(this.createTextNode(param));
 
686
            node.appendChild(trans);
 
687
        }
 
688
        
 
689
        var properties = [
 
690
            "numZoomLevels", "units", "isBaseLayer",
 
691
            "opacity", "displayInLayerSwitcher", "singleTile"
 
692
        ];
 
693
        var child;
 
694
        for(var i=0, len=properties.length; i<len; ++i) {
 
695
            child = this.createOLPropertyNode(layer, properties[i]);
 
696
            if(child) {
 
697
                node.appendChild(child);
 
698
            }
 
699
        }
 
700
 
 
701
        return node;
 
702
    },
 
703
    
 
704
    /**
 
705
     * Method: createOLPropertyNode
 
706
     * Create a node representing an OpenLayers property.  If the property is
 
707
     *     null or undefined, null will be returned.
 
708
     *
 
709
     * Parameters:
 
710
     * object - {Object} An object.
 
711
     * prop - {String} A property.
 
712
     *
 
713
     * Returns:
 
714
     * {Element} A property node.
 
715
     */
 
716
    createOLPropertyNode: function(obj, prop) {
 
717
        var node = null;
 
718
        if(obj[prop] != null) {
 
719
            node = this.createElementNS(this.namespaces.ol, "ol:" + prop);
 
720
            node.appendChild(this.createTextNode(obj[prop].toString()));
 
721
        }
 
722
        return node;
 
723
    },
 
724
 
 
725
    /**
 
726
     * Method: write_wmc_Server
 
727
     * Create a Server node given a layer object.
 
728
     *
 
729
     * Parameters:
 
730
     * layer - {<OpenLayers.Layer.WMS>} Layer object.
 
731
     *
 
732
     * Returns:
 
733
     * {Element} A WMC Server element node.
 
734
     */
 
735
    write_wmc_Server: function(layer) {
 
736
        var node = this.createElementDefaultNS("Server");
 
737
        this.setAttributes(node, {
 
738
            service: "OGC:WMS",
 
739
            version: layer.params["VERSION"]
 
740
        });
 
741
        
 
742
        // required OnlineResource element
 
743
        node.appendChild(this.write_wmc_OnlineResource(layer.url));
 
744
        
 
745
        return node;
 
746
    },
 
747
 
 
748
    /**
 
749
     * Method: write_wmc_MetadataURL
 
750
     * Create a MetadataURL node given a layer object.
 
751
     *
 
752
     * Parameters:
 
753
     * layer - {<OpenLayers.Layer.WMS>} Layer object.
 
754
     *
 
755
     * Returns:
 
756
     * {Element} A WMC metadataURL element node.
 
757
     */
 
758
    write_wmc_MetadataURL: function(layer) {
 
759
        var node = this.createElementDefaultNS("MetadataURL");
 
760
 
 
761
        // required OnlineResource element
 
762
        node.appendChild(this.write_wmc_OnlineResource(layer.metadataURL));
 
763
 
 
764
        return node;
 
765
    },
 
766
 
 
767
    /**
 
768
     * Method: write_wmc_FormatList
 
769
     * Create a FormatList node given a layer.
 
770
     *
 
771
     * Parameters:
 
772
     * layer - {<OpenLayers.Layer.WMS>} Layer object.
 
773
     *
 
774
     * Returns:
 
775
     * {Element} A WMC FormatList element node.
 
776
     */
 
777
    write_wmc_FormatList: function(layer) {
 
778
        var node = this.createElementDefaultNS("FormatList");
 
779
        node.appendChild(this.createElementDefaultNS(
 
780
            "Format", layer.params["FORMAT"], {current: "1"}
 
781
        ));
 
782
 
 
783
        return node;
 
784
    },
 
785
 
 
786
    /**
 
787
     * Method: write_wmc_StyleList
 
788
     * Create a StyleList node given a layer.
 
789
     *
 
790
     * Parameters:
 
791
     * layer - {<OpenLayers.Layer.WMS>} Layer object.
 
792
     *
 
793
     * Returns:
 
794
     * {Element} A WMC StyleList element node.
 
795
     */
 
796
    write_wmc_StyleList: function(layer) {
 
797
        var node = this.createElementDefaultNS("StyleList");
 
798
        var style = this.createElementDefaultNS(
 
799
            "Style", null, {current: "1"}
 
800
        );
 
801
        
 
802
        // Style can come from one of three places (prioritized as below):
 
803
        // 1) an SLD parameter
 
804
        // 2) and SLD_BODY parameter
 
805
        // 3) the STYLES parameter
 
806
        
 
807
        if(layer.params["SLD"]) {
 
808
            // create link from SLD parameter
 
809
            var sld = this.createElementDefaultNS("SLD");
 
810
            var link = this.write_wmc_OnlineResource(layer.params["SLD"]);
 
811
            sld.appendChild(link);
 
812
            style.appendChild(sld);
 
813
        } else if(layer.params["SLD_BODY"]) {
 
814
            // include sld fragment from SLD_BODY parameter
 
815
            var sld = this.createElementDefaultNS("SLD");
 
816
            var body = layer.params["SLD_BODY"];
 
817
            // read in body as xml doc - assume proper namespace declarations
 
818
            var doc = OpenLayers.Format.XML.prototype.read.apply(this, [body]);
 
819
            // append to StyledLayerDescriptor node
 
820
            var imported = doc.documentElement;
 
821
            if(sld.ownerDocument && sld.ownerDocument.importNode) {
 
822
                imported = sld.ownerDocument.importNode(imported, true);
 
823
            }
 
824
            sld.appendChild(imported);
 
825
            style.appendChild(sld);            
 
826
        } else {
 
827
            // use name(s) from STYLES parameter
 
828
            var name = layer.params["STYLES"] ?
 
829
                layer.params["STYLES"] : this.defaultStyleName;
 
830
            
 
831
            style.appendChild(this.createElementDefaultNS("Name", name));
 
832
            style.appendChild(this.createElementDefaultNS(
 
833
                "Title", this.defaultStyleTitle
 
834
            ));
 
835
        }
 
836
        node.appendChild(style);
 
837
        return node;
 
838
    },
 
839
 
 
840
    /**
 
841
     * Method: write_wmc_OnlineResource
 
842
     * Create an OnlineResource node given a URL.
 
843
     *
 
844
     * Parameters:
 
845
     * href - {String} URL for the resource.
 
846
     *
 
847
     * Returns:
 
848
     * {Element} A WMC OnlineResource element node.
 
849
     */
 
850
    write_wmc_OnlineResource: function(href) {
 
851
        var node = this.createElementDefaultNS("OnlineResource");
 
852
        this.setAttributeNS(node, this.namespaces.xlink, "xlink:type", "simple");
 
853
        this.setAttributeNS(node, this.namespaces.xlink, "xlink:href", href);
 
854
        return node;
 
855
    },
 
856
 
 
857
    CLASS_NAME: "OpenLayers.Format.WMC.v1" 
 
858
 
 
859
});