Changeset 5617
- Timestamp:
- 01/01/08 17:23:02 (1 year ago)
- Files:
-
- sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC.js (modified) (3 diffs)
- sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1.js (modified) (17 diffs)
- sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_0_0.js (modified) (1 diff)
- sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_1_0.js (modified) (1 diff)
- sandbox/tschaub/wmc/lib/OpenLayers/Format/XML.js (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC.js
r5615 r5617 1 1 /** 2 2 * @requires OpenLayers/Format/XML.js 3 * 3 */ 4 5 /** 4 6 * Class: OpenLayers.Format.WMC 5 7 * Read and write Web Map Context documents. 6 * 8 * 7 9 * Inherits from: 8 10 * - <OpenLayers.Format.XML> 9 11 */ 10 OpenLayers.Format.WMC = OpenLayers.Class( OpenLayers.Format.XML,{12 OpenLayers.Format.WMC = OpenLayers.Class({ 11 13 12 14 /** … … 38 40 */ 39 41 initialize: function(options) { 40 OpenLayers. Format.XML.prototype.initialize.apply(this, [options]);42 OpenLayers.Util.extend(this, options); 41 43 this.options = options; 42 44 }, … … 136 138 mapToContext: function(map) { 137 139 var context = {}; 140 return context; 138 141 }, 139 142 sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1.js
r5615 r5617 1 1 /** 2 * @requires OpenLayers/Format/WMC.js 3 * 2 * @requires OpenLayers/Format/XML.js 3 */ 4 5 /** 4 6 * Class: OpenLayers.Format.WMC.v1 5 7 * Superclass for WMC version 1 parsers. 8 * 9 * Inherits from: 10 * - <OpenLayers.Format.XML> 6 11 */ 7 OpenLayers.Format.WMC.v1 = OpenLayers.Class( {12 OpenLayers.Format.WMC.v1 = OpenLayers.Class(OpenLayers.Format.XML, { 8 13 9 14 /** … … 12 17 */ 13 18 namespaces: { 14 context: "http://www.opengeospatial.net/context",19 wmc: "http://www.opengeospatial.net/context", 15 20 sld: "http://www.opengeospatial.net/sld", 16 21 xlink: "http://www.w3.org/1999/xlink" 17 22 }, 18 23 24 /** 25 * Method: getNamespacePrefix 26 * Get the namespace prefix for a given uri from the <namespaces> object. 27 * 28 * Returns: 29 * {String} A namespace prefix or null if none found. 30 */ 31 getNamespacePrefix: function(uri) { 32 var prefix = null; 33 if(uri == null) { 34 prefix = this.namespaces[this.defaultPrefix]; 35 } else { 36 for(prefix in this.namespaces) { 37 if(this.namespaces[prefix] == uri) { 38 break; 39 } 40 } 41 } 42 return prefix; 43 }, 44 45 /** 46 * Property: defaultPrefix 47 */ 48 defaultPrefix: "wmc", 49 50 /** 51 * Property: rootPrefix 52 * {String} Prefix on the root node that maps to the context namespace URI. 53 */ 54 rootPrefix: null, 55 19 56 /** 20 57 * Constructor: OpenLayers.Format.WMC.v1 … … 31 68 32 69 /** 33 * APIMethod: read70 * Method: read 34 71 * Read capabilities data from a string, and return a list of layers. 35 72 * … … 45 82 } 46 83 var root = data.documentElement; 84 this.rootPrefix = root.prefix; 47 85 var context = { 48 86 version: root.getAttribute("version") … … 57 95 runChildNodes: function(obj, node) { 58 96 var children = node.childNodes; 59 var childNode, processor ;97 var childNode, processor, prefix; 60 98 for(var i=0; i<children.length; ++i) { 61 99 childNode = children[i]; 62 100 if(childNode.nodeType == 1) { 63 processor = this["read" + childNode.nodeName]; 101 prefix = (childNode.prefix == this.rootPrefix) ? 102 this.defaultPrefix : 103 this.getNamespacePrefix(childNode.namespaceURI); 104 processor = this["read_" + prefix + "_" + childNode.nodeName]; 64 105 if(processor) { 65 106 processor.apply(this, [obj, childNode]); … … 70 111 71 112 /** 72 * Method: read General73 */ 74 read General: function(context, node) {113 * Method: read_wmc_General 114 */ 115 read_wmc_General: function(context, node) { 75 116 this.runChildNodes(context, node); 76 117 }, 77 118 78 119 /** 79 * Method: read BoundingBox80 */ 81 read BoundingBox: function(context, node) {120 * Method: read_wmc_BoundingBox 121 */ 122 read_wmc_BoundingBox: function(context, node) { 82 123 context.srs = node.getAttribute("SRS"); 83 124 context.bounds = new OpenLayers.Bounds( … … 90 131 91 132 /** 92 * Method: read LayerList93 */ 94 read LayerList: function(context, node) {133 * Method: read_wmc_LayerList 134 */ 135 read_wmc_LayerList: function(context, node) { 95 136 context.layers = []; 96 137 this.runChildNodes(context, node); … … 98 139 99 140 /** 100 * Method: read Layer101 */ 102 read Layer: function(context, node) {141 * Method: read_wmc_Layer 142 */ 143 read_wmc_Layer: function(context, node) { 103 144 var layerInfo = { 104 145 visibility: (node.getAttribute("hidden") != "1"), … … 123 164 124 165 /** 125 * Method: read Server126 */ 127 read Server: function(layerInfo, node) {166 * Method: read_wmc_Server 167 */ 168 read_wmc_Server: function(layerInfo, node) { 128 169 layerInfo.version = node.getAttribute("version"); 129 170 this.runChildNodes(layerInfo, node); … … 131 172 132 173 /** 133 * Method: read FormatList134 */ 135 read FormatList: function(layerInfo, node) {174 * Method: read_wmc_FormatList 175 */ 176 read_wmc_FormatList: function(layerInfo, node) { 136 177 this.runChildNodes(layerInfo, node); 137 178 }, 138 179 139 180 /** 140 * Method: read Format141 */ 142 read Format: function(layerInfo, node) {181 * Method: read_wmc_Format 182 */ 183 read_wmc_Format: function(layerInfo, node) { 143 184 var format = this.getChildValue(node) 144 185 layerInfo.formats.push(format); … … 149 190 150 191 /** 151 * Method: read StyleList152 */ 153 read StyleList: function(layerInfo, node) {192 * Method: read_wmc_StyleList 193 */ 194 read_wmc_StyleList: function(layerInfo, node) { 154 195 this.runChildNodes(layerInfo, node); 155 196 }, 156 197 157 198 /** 158 * Method: read Style159 */ 160 read Style: function(layerInfo, node) {199 * Method: read_wmc_Style 200 */ 201 read_wmc_Style: function(layerInfo, node) { 161 202 var style = {}; 162 203 this.runChildNodes(style, node); … … 168 209 169 210 /** 170 * Method: read OnlineResource171 */ 172 read OnlineResource: function(obj, node) {211 * Method: read_wmc_OnlineResource 212 */ 213 read_wmc_OnlineResource: function(obj, node) { 173 214 obj.href = this.getAttributeNS( 174 node, "http://www.w3.org/1999/xlink", "href"215 node, this.namespaces.xlink, "href" 175 216 ); 176 217 }, 177 218 178 219 /** 179 * Method: read Name180 */ 181 read Name: function(obj, node) {220 * Method: read_wmc_Name 221 */ 222 read_wmc_Name: function(obj, node) { 182 223 var name = this.getChildValue(node); 183 224 if(name) { … … 187 228 188 229 /** 189 * Method: read Title190 */ 191 read Title: function(obj, node) {230 * Method: read_wmc_Title 231 */ 232 read_wmc_Title: function(obj, node) { 192 233 var title = this.getChildValue(node); 193 234 if(title) { … … 197 238 198 239 /** 199 * Method: read Abstract200 */ 201 read Abstract: function(obj, node) {240 * Method: read_wmc_Abstract 241 */ 242 read_wmc_Abstract: function(obj, node) { 202 243 var abst = this.getChildValue(node); 203 244 if(abst) { … … 207 248 208 249 /** 209 * Method: read LatLonBoundingBox210 */ 211 read LatLonBoundingBox: function(layer, node) {250 * Method: read_wmc_LatLonBoundingBox 251 */ 252 read_wmc_LatLonBoundingBox: function(layer, node) { 212 253 layer.llbbox = [ 213 254 parseFloat(node.getAttribute("minx")), … … 219 260 220 261 /** 221 * Method: read LegendURL222 */ 223 read LegendURL: function(style, node) {262 * Method: read_wmc_LegendURL 263 */ 264 read_wmc_LegendURL: function(style, node) { 224 265 var legend = { 225 266 width: node.getAttribute('width'), … … 228 269 var links = node.getElementsByTagName("OnlineResource"); 229 270 if(links.length > 0) { 230 this.read OnlineResource(legend, links[0]);271 this.read_wmc_OnlineResource(legend, links[0]); 231 272 } 232 273 style.legend = legend; 233 274 }, 234 275 276 /** 277 * Method: write 278 * 279 * Parameters: 280 * context - {Object} An object representing the map context. 281 * options - {Object} Optional object. 282 * 283 * Returns: 284 * {String} A WMC document string. 285 */ 286 write: function(context, options) { 287 var root = this.createElementDefaultNS("ViewContext"); 288 this.setAttributes(root, { 289 version: this.VERSION, 290 id: (options && typeof options.id == "string") ? 291 options.id : 292 OpenLayers.Util.createUniqueID("OpenLayers_Context_") 293 }); 294 295 // required General element 296 root.appendChild(this.write_wmc_General(context)); 297 298 // required LayerList element 299 root.appendChild(this.write_wmc_LayerList(context)); 300 301 return OpenLayers.Format.XML.prototype.write.apply(this, [root]); 302 }, 303 304 /** 305 * Method: createElementDefaultNS 306 * Shorthand for createElementNS with namespace from <defaultPrefix>. 307 * Can optionally be used to set attributes and a text child value. 308 * 309 * Parameters: 310 * name - {String} The qualified node name. 311 * childValue - {String} Optional value for text child node. 312 * attributes - {Object} Optional object representing attributes. 313 * 314 * Returns: 315 * {Element} An element node. 316 */ 317 createElementDefaultNS: function(name, childValue, attributes) { 318 var node = this.createElementNS( 319 this.namespaces[this.defaultPrefix], 320 name 321 ); 322 if(childValue) { 323 node.appendChild(this.createTextNode(childValue)); 324 } 325 if(attributes) { 326 this.setAttributes(node, attributes); 327 } 328 return node; 329 }, 330 331 /** 332 * Method: setAttributes 333 * Set multiple attributes given key value pairs from an object. 334 * 335 * Parameters: 336 * node - {Element} An element node. 337 * obj - {Object} An object whose properties represent attribute names and 338 * values represent attribute values. 339 */ 340 setAttributes: function(node, obj) { 341 for(var name in obj) { 342 node.setAttribute(name, obj[name]); 343 } 344 }, 345 346 /** 347 * Method: write_wmc_General 348 * Create a General node given an context object. 349 * 350 * Parameters: 351 * context - {Object} Context object. 352 * 353 * Returns: 354 * {Element} A WMC General element node. 355 */ 356 write_wmc_General: function(context) { 357 var node = this.createElementDefaultNS("General"); 358 359 // optional Window element 360 if(context.size) { 361 node.appendChild(this.createElementDefaultNS( 362 "Window", null, 363 { 364 width: context.size.w, 365 height: context.size.h 366 } 367 )); 368 } 369 370 // required BoundingBox element 371 var bounds = context.bounds; 372 node.appendChild(this.createElementDefaultNS( 373 "BoundingBox", null, 374 { 375 minx: bounds.left, miny: bounds.bottom, 376 maxx: bounds.right, maxy: bounds.top, 377 SRS: context.srs 378 } 379 )); 380 381 // required Title element 382 node.appendChild(this.createElementDefaultNS( 383 "Title", context.title 384 )); 385 386 return node; 387 }, 388 389 /** 390 * Method: write_wmc_LayerList 391 * Create a LayerList node given an context object. 392 * 393 * Parameters: 394 * context - {Object} Context object. 395 * 396 * Returns: 397 * {Element} A WMC LayerList element node. 398 */ 399 write_wmc_LayerList: function(context) { 400 var list = this.createElementDefaultNS("LayerList"); 401 402 var layer; 403 for(var i=0; i<context.layers.length; ++i) { 404 list.appendChild(this.write_wmc_Layer(context.layers[i])); 405 } 406 407 return list; 408 }, 409 410 /** 411 * Method: write_wmc_Layer 412 * Create a Layer node given a layer object. 413 * 414 * Parameters: 415 * layer - {<OpenLayers.Layer.WMS>} Layer object. 416 * 417 * Returns: 418 * {Element} A WMC Layer element node. 419 */ 420 write_wmc_Layer: function(layer) { 421 var node = this.createElementDefaultNS( 422 "Layer", null, { 423 queryable: "true", 424 hidden: layer.visibility ? "true" : "false" 425 } 426 ); 427 428 // required Name element 429 node.appendChild(this.createElementDefaultNS( 430 "Name", layer.params["LAYERS"] 431 )); 432 433 // required Title element 434 node.appendChild(this.createElementDefaultNS( 435 "Title", layer.name 436 )); 437 438 // required Server element 439 node.appendChild(this.write_wmc_Server(layer)); 440 441 return node; 442 }, 443 444 /** 445 * Method: write_wmc_Server 446 * Create a Server node given a layer object. 447 * 448 * Parameters: 449 * layer - {<OpenLayers.Layer.WMS>} Layer object. 450 * 451 * Returns: 452 * {Element} A WMC Server element node. 453 */ 454 write_wmc_Server: function(layer) { 455 var node = this.createElementDefaultNS("Server"); 456 this.setAttributes(node, { 457 service: "OGC:WMS", 458 version: layer.params["VERSION"] 459 }); 460 461 // required OnlineResource element 462 node.appendChild(this.write_wmc_OnlineResource(layer.url)); 463 464 return node; 465 }, 466 467 /** 468 * Method: write_wmc_FormatList 469 * Create a FormatList node given a layer. 470 * 471 * Parameters: 472 * layer - {<OpenLayers.Layer.WMS>} Layer object. 473 * 474 * Returns: 475 * {Element} A WMC FormatList element node. 476 */ 477 write_wmc_FormatList: function(layer) { 478 var node = this.createElementDefaultNS("FormatList"); 479 node.appendChild(this.createElementDefaultNS( 480 "Format", layer.params["FORMAT"], {current: "true"} 481 )); 482 483 return node; 484 }, 485 486 /** 487 * Method: write_wmc_StyleList 488 * Create a StyleList node given a layer. 489 * 490 * Parameters: 491 * layer - {<OpenLayers.Layer.WMS>} Layer object. 492 * 493 * Returns: 494 * {Element} A WMC StyleList element node. 495 */ 496 write_wmc_StyleList: function(layer) { 497 var node = this.createElementDefaultNS("StyleList"); 498 node.appendChild(this.createElementDefaultNS( 499 "Style", layer.params["STYLES"], {current: "true"} 500 )); 501 502 return node; 503 }, 504 505 /** 506 * Method: write_wmc_OnlineResource 507 * Create an OnlineResource node given a URL. 508 * 509 * Parameters: 510 * href - {String} URL for the resource. 511 * 512 * Returns: 513 * {Element} A WMC OnlineResource element node. 514 */ 515 write_wmc_OnlineResource: function(href) { 516 var node = this.createElementDefaultNS("OnlineResource"); 517 this.setAttributeNS(node, this.namespaces.xlink, "xlink:type", "simple"); 518 this.setAttributeNS(node, this.namespaces.xlink, "xlink:href", href); 519 return node; 520 }, 235 521 236 522 CLASS_NAME: "OpenLayers.Format.WMC.v1" sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_0_0.js
r5615 r5617 1 1 /** 2 * @requires OpenLayers/Format/WMC.js 3 * 2 * @requires OpenLayers/Format/WMC/v1.js 3 */ 4 5 /** 4 6 * Class: OpenLayers.Format.WMC.v1_0_0 5 7 * Read and write WMC version 1.0.0. sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_1_0.js
r5615 r5617 1 1 /** 2 * @requires OpenLayers/Format/WMC.js 3 * 2 * @requires OpenLayers/Format/WMC/v1.js 3 */ 4 5 /** 4 6 * Class: OpenLayers.Format.WMC.v1_1_0 5 7 * Read and write WMC version 1.1.0. sandbox/tschaub/wmc/lib/OpenLayers/Format/XML.js
r5616 r5617 349 349 return found; 350 350 }, 351 352 /** 353 * APIMethod: setAttributeNS 354 * Adds a new attribute or changes the value of an attribute with the given 355 * namespace and name. 356 * 357 * Parameters: 358 * node - {Element} Element node on which to set the attribute. 359 * namespace - {String} Namespace URI for the attribute. 360 * name - {String} Qualified name (prefix:localName) for the attribute. 361 * value - {String} Attribute value. 362 */ 363 setAttributeNS: function(node, namespace, name, value) { 364 if(node.setAttributeNS) { 365 node.setAttributeNS(namespace, name, value); 366 } else { 367 throw "setAttributeNS not implemented"; 368 } 369 }, 351 370 352 371 CLASS_NAME: "OpenLayers.Format.XML"
