Added versioned GML Parser, on behalf of mr. tschaub. review=ahocevar. (Pullup #1639)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@8007 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2008-09-12 15:16:13 +00:00
parent 44ac8154c7
commit 7a5b401604
11 changed files with 2478 additions and 67 deletions

View File

@@ -22,15 +22,24 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, {
/**
* Property: namespaces
* {Object} Mapping of namespace aliases to namespace URIs. Properties
* of this object should not be set individually.
* of this object should not be set individually. Read-only. All
* XML subclasses should have their own namespaces object. Use
* <setNamespace> to add or set a namespace alias after construction.
*/
namespaces: {},
namespaces: null,
/**
* Property: defaultNamespace
* Property: namespaceAlias
* {Object} Mapping of namespace URI to namespace alias. This object
* is read-only. Use <setNamespace> to add or set a namespace alias.
*/
namespaceAlias: null,
/**
* Property: defaultPrefix
* {String} The default namespace alias for creating element nodes.
*/
defaultNamespace: null,
defaultPrefix: null,
/**
* Property: readers
@@ -75,6 +84,12 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, {
this.xmldom = new ActiveXObject("Microsoft.XMLDOM");
}
OpenLayers.Format.prototype.initialize.apply(this, [options]);
// clone the namespace object and set all namespace aliases
this.namespaces = OpenLayers.Util.extend({}, this.namespaces);
this.namespaceAlias = {};
for(var alias in this.namespaces) {
this.namespaceAlias[this.namespaces[alias]] = alias;
}
},
/**
@@ -85,6 +100,19 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, {
this.xmldom = null;
OpenLayers.Format.prototype.destroy.apply(this, arguments);
},
/**
* Method: setNamespace
* Set a namespace alias and URI for the format.
*
* Parameters:
* alias - {String} The namespace alias (prefix).
* uri - {String} The namespace URI.
*/
setNamespace: function(alias, uri) {
this.namespaces[alias] = uri;
this.namespaceAlias[uri] = alias;
},
/**
* APIMethod: read
@@ -329,11 +357,12 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, {
* {String} The value of the first child of the given node.
*/
getChildValue: function(node, def) {
var value;
if (node && node.firstChild && node.firstChild.nodeValue) {
value = node.firstChild.nodeValue;
} else {
value = (def != undefined) ? def : "";
var value = def || "";
if(node) {
var child = node.firstChild;
if(child) {
value = child.nodeValue || value;
}
}
return value;
},
@@ -424,29 +453,6 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, {
}
},
/**
* Method: getNamespacePrefix
* Get the namespace prefix for a given uri from the <namespaces> object.
*
* Returns:
* {String} A namespace prefix or null if none found.
*/
getNamespacePrefix: function(uri) {
var prefix = null;
if(uri == null) {
prefix = this.defaultPrefix;
} else {
var prefix = null;
for(var p in this.namespaces) {
if(this.namespaces[p] == uri) {
prefix = p;
break;
}
}
}
return prefix;
},
/**
* Method: createElementNSPlus
* Shorthand for creating namespaced elements with optional attributes and
@@ -534,10 +540,9 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, {
if(!obj) {
obj = {};
}
var prefix = this.getNamespacePrefix(node.namespaceURI);
var local = node.nodeName.split(":").pop();
var group = this.readers[prefix];
var group = this.readers[this.namespaceAlias[node.namespaceURI]];
if(group) {
var local = node.localName || node.nodeName.split(":").pop();
var reader = group[local] || group["*"];
if(reader) {
reader.apply(this, [node, obj]);
@@ -564,7 +569,7 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, {
}
var children = node.childNodes;
var child;
for(var i=0; i<children.length; ++i) {
for(var i=0, len=children.length; i<len; ++i) {
child = children[i];
if(child.nodeType == 1) {
this.readNode(child, obj);
@@ -601,7 +606,7 @@ OpenLayers.Format.XML = OpenLayers.Class(OpenLayers.Format, {
local = name.substring(split + 1);
} else {
if(parent) {
prefix = this.getNamespacePrefix(parent.namespaceURI);
prefix = this.namespaceAlias[parent.namespaceURI];
} else {
prefix = this.defaultPrefix;
}