Changeset 8007
- Timestamp:
- 09/12/08 10:16:13 (2 years ago)
- Files:
-
- trunk/openlayers/examples/vector-formats.html (modified) (4 diffs)
- trunk/openlayers/lib/OpenLayers.js (modified) (1 diff)
- trunk/openlayers/lib/OpenLayers/Format/GML (added)
- trunk/openlayers/lib/OpenLayers/Format/GML/Base.js (added)
- trunk/openlayers/lib/OpenLayers/Format/GML/v2.js (added)
- trunk/openlayers/lib/OpenLayers/Format/GML/v3.js (added)
- trunk/openlayers/lib/OpenLayers/Format/XML.js (modified) (8 diffs)
- trunk/openlayers/tests/Format/GML (added)
- trunk/openlayers/tests/Format/GML/cases.js (added)
- trunk/openlayers/tests/Format/GML/v2.html (added)
- trunk/openlayers/tests/Format/GML/v3.html (added)
- trunk/openlayers/tests/Format/XML.html (modified) (1 diff)
- trunk/openlayers/tests/list-tests.html (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openlayers/examples/vector-formats.html
r7095 r8007 53 53 'internalProjection': map.baseLayer.projection, 54 54 'externalProjection': new OpenLayers.Projection(OpenLayers.Util.getElement("inproj").value) 55 } 55 }; 56 56 var out_options = { 57 57 'internalProjection': map.baseLayer.projection, 58 58 'externalProjection': new OpenLayers.Projection(OpenLayers.Util.getElement("outproj").value) 59 } 59 }; 60 var gmlOptions = { 61 featureType: "feature", 62 featureNS: "http://example.com/feature" 63 }; 64 var gmlOptionsIn = OpenLayers.Util.extend( 65 OpenLayers.Util.extend({}, gmlOptions), 66 in_options 67 ); 68 var gmlOptionsOut = OpenLayers.Util.extend( 69 OpenLayers.Util.extend({}, gmlOptions), 70 out_options 71 ); 60 72 formats = { 61 73 'in': { … … 63 75 geojson: new OpenLayers.Format.GeoJSON(in_options), 64 76 georss: new OpenLayers.Format.GeoRSS(in_options), 65 gml: new OpenLayers.Format.GML(in_options), 77 gml2: new OpenLayers.Format.GML.v2(gmlOptionsIn), 78 gml3: new OpenLayers.Format.GML.v3(gmlOptionsIn), 66 79 kml: new OpenLayers.Format.KML(in_options) 67 80 }, … … 70 83 geojson: new OpenLayers.Format.GeoJSON(out_options), 71 84 georss: new OpenLayers.Format.GeoRSS(out_options), 72 gml: new OpenLayers.Format.GML(out_options), 85 gml2: new OpenLayers.Format.GML.v2(gmlOptionsOut), 86 gml3: new OpenLayers.Format.GML.v3(gmlOptionsOut), 73 87 kml: new OpenLayers.Format.KML(out_options) 74 88 } … … 170 184 <option value="kml">KML</option> 171 185 <option value="georss">GeoRSS</option> 172 <option value="gml">GML</option> 186 <option value="gml2">GML (v2)</option> 187 <option value="gml3">GML (v3)</option> 173 188 <option value="wkt">Well-Known Text (WKT)</option> 174 189 </select> trunk/openlayers/lib/OpenLayers.js
r8005 r8007 207 207 "OpenLayers/Format/XML.js", 208 208 "OpenLayers/Format/GML.js", 209 "OpenLayers/Format/GML/Base.js", 210 "OpenLayers/Format/GML/v2.js", 211 "OpenLayers/Format/GML/v3.js", 209 212 "OpenLayers/Format/KML.js", 210 213 "OpenLayers/Format/GeoRSS.js", trunk/openlayers/lib/OpenLayers/Format/XML.js
r7982 r8007 23 23 * Property: namespaces 24 24 * {Object} Mapping of namespace aliases to namespace URIs. Properties 25 * of this object should not be set individually. 26 */ 27 namespaces: {}, 28 29 /** 30 * Property: defaultNamespace 25 * of this object should not be set individually. Read-only. All 26 * XML subclasses should have their own namespaces object. Use 27 * <setNamespace> to add or set a namespace alias after construction. 28 */ 29 namespaces: null, 30 31 /** 32 * Property: namespaceAlias 33 * {Object} Mapping of namespace URI to namespace alias. This object 34 * is read-only. Use <setNamespace> to add or set a namespace alias. 35 */ 36 namespaceAlias: null, 37 38 /** 39 * Property: defaultPrefix 31 40 * {String} The default namespace alias for creating element nodes. 32 41 */ 33 default Namespace: null,42 defaultPrefix: null, 34 43 35 44 /** … … 76 85 } 77 86 OpenLayers.Format.prototype.initialize.apply(this, [options]); 87 // clone the namespace object and set all namespace aliases 88 this.namespaces = OpenLayers.Util.extend({}, this.namespaces); 89 this.namespaceAlias = {}; 90 for(var alias in this.namespaces) { 91 this.namespaceAlias[this.namespaces[alias]] = alias; 92 } 78 93 }, 79 94 … … 85 100 this.xmldom = null; 86 101 OpenLayers.Format.prototype.destroy.apply(this, arguments); 102 }, 103 104 /** 105 * Method: setNamespace 106 * Set a namespace alias and URI for the format. 107 * 108 * Parameters: 109 * alias - {String} The namespace alias (prefix). 110 * uri - {String} The namespace URI. 111 */ 112 setNamespace: function(alias, uri) { 113 this.namespaces[alias] = uri; 114 this.namespaceAlias[uri] = alias; 87 115 }, 88 116 … … 330 358 */ 331 359 getChildValue: function(node, def) { 332 var value; 333 if (node && node.firstChild && node.firstChild.nodeValue) { 334 value = node.firstChild.nodeValue; 335 } else { 336 value = (def != undefined) ? def : ""; 360 var value = def || ""; 361 if(node) { 362 var child = node.firstChild; 363 if(child) { 364 value = child.nodeValue || value; 365 } 337 366 } 338 367 return value; … … 426 455 427 456 /** 428 * Method: getNamespacePrefix429 * Get the namespace prefix for a given uri from the <namespaces> object.430 *431 * Returns:432 * {String} A namespace prefix or null if none found.433 */434 getNamespacePrefix: function(uri) {435 var prefix = null;436 if(uri == null) {437 prefix = this.defaultPrefix;438 } else {439 var prefix = null;440 for(var p in this.namespaces) {441 if(this.namespaces[p] == uri) {442 prefix = p;443 break;444 }445 }446 }447 return prefix;448 },449 450 /**451 457 * Method: createElementNSPlus 452 458 * Shorthand for creating namespaced elements with optional attributes and … … 535 541 obj = {}; 536 542 } 537 var prefix = this.getNamespacePrefix(node.namespaceURI); 538 var local = node.nodeName.split(":").pop(); 539 var group = this.readers[prefix]; 543 var group = this.readers[this.namespaceAlias[node.namespaceURI]]; 540 544 if(group) { 545 var local = node.localName || node.nodeName.split(":").pop(); 541 546 var reader = group[local] || group["*"]; 542 547 if(reader) { … … 565 570 var children = node.childNodes; 566 571 var child; 567 for(var i=0 ; i<children.length; ++i) {572 for(var i=0, len=children.length; i<len; ++i) { 568 573 child = children[i]; 569 574 if(child.nodeType == 1) { … … 602 607 } else { 603 608 if(parent) { 604 prefix = this. getNamespacePrefix(parent.namespaceURI);609 prefix = this.namespaceAlias[parent.namespaceURI]; 605 610 } else { 606 611 prefix = this.defaultPrefix; trunk/openlayers/tests/Format/XML.html
r7982 r8007 292 292 } 293 293 294 function test_getNamespacePrefix(t) { 295 t.plan(6); 296 297 // test that getNamespacePrefix returns null with no ns defined 298 var format = new OpenLayers.Format.XML(); 299 var got = format.getNamespacePrefix("http://example.com/foo"); 300 t.eq(got, null, "returns null when no namespaces are defined"); 301 302 format.defaultPrefix = "def"; 303 format.namespaces = { 304 "def": "http://example.com/default", 305 "foo": "http://example.com/foo", 306 "bar": "http://example.com/bar" 307 }; 308 309 var cases = [ 310 {uri: null, expect: "def"}, 311 {uri: "http://example.com/default", expect: "def"}, 312 {uri: "http://example.com/foo", expect: "foo"}, 313 {uri: "http://example.com/bar", expect: "bar"}, 314 {uri: "http://example.com/nothing", expect: null} 315 ]; 316 317 var test; 318 for(var i=0; i<cases.length; ++i) { 319 test = cases[i]; 320 t.eq(format.getNamespacePrefix(test.uri), test.expect, 321 "uri: " + test.uri + " works"); 322 } 294 function test_namespaces(t) { 295 t.plan(2); 296 297 var format = new OpenLayers.Format.XML({ 298 namespaces: { 299 "def": "http://example.com/default", 300 "foo": "http://example.com/foo", 301 "bar": "http://example.com/bar" 302 }, 303 defaultPrefix: "def" 304 }); 305 306 // test that prototype has not been altered 307 t.eq(OpenLayers.Format.XML.prototype.namespaces, null, 308 "setting namespaces at construction does not modify prototype"); 309 310 // test that namespaceAlias has been set 311 t.eq(format.namespaceAlias["http://example.com/foo"], "foo", 312 "namespaceAlias mapping has been set"); 313 314 } 315 316 function test_setNamespace(t) { 317 t.plan(3); 318 319 var format = new OpenLayers.Format.XML(); 320 321 // test that namespaces is an object 322 t.ok(format.namespaces instanceof Object, "empty namespace object set"); 323 324 format.setNamespace("foo", "http://example.com/foo"); 325 t.eq(format.namespaces["foo"], "http://example.com/foo", "alias -> uri mapping set"); 326 t.eq(format.namespaceAlias["http://example.com/foo"], "foo", "uri -> alias mapping set"); 323 327 324 328 } trunk/openlayers/tests/list-tests.html
r8005 r8007 44 44 <li>Format/GeoRSS.html</li> 45 45 <li>Format/GML.html</li> 46 <li>Format/GML/v2.html</li> 47 <li>Format/GML/v3.html</li> 46 48 <li>Format/GPX.html</li> 47 49 <li>Format/JSON.html</li>
