Changeset 3319
- Timestamp:
- 06/11/07 18:42:37 (1 year ago)
- Files:
-
- sandbox/tschaub/xml/examples/vector-formats.html (modified) (1 diff)
- sandbox/tschaub/xml/lib/OpenLayers/Format/GeoRSS.js (modified) (3 diffs)
- sandbox/tschaub/xml/lib/OpenLayers/Format/XML.js (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
sandbox/tschaub/xml/examples/vector-formats.html
r3318 r3319 117 117 element.value = features.length + ' feature' + plural + ' added' 118 118 } else { 119 element.value = 'Bad input ';119 element.value = 'Bad input ' + type; 120 120 } 121 121 } sandbox/tschaub/xml/lib/OpenLayers/Format/GeoRSS.js
r3318 r3319 18 18 19 19 /** 20 * Return a list of features from a GeoRSS doc 21 * @param {Element} data 22 * @type Array(OpenLayers.Feature.Vector) 23 */ 24 read: function(doc) { 25 if (typeof doc == "string") { 26 doc = OpenLayers.Format.XML.prototype.read.apply(this, [doc]); 27 } 28 29 /* Try RSS items first, then Atom entries */ 30 var itemlist = null; 31 itemlist = this.getElementsByTagNameNS(doc, '*', 'item'); 32 if (itemlist.length == 0) { 33 itemlist = this.getElementsByTagNameNS(doc, '*', 'entry'); 34 } 35 36 var features = []; 37 for(var i=0; i<itemlist.length; i++) { 38 // this is horrendous - please change this 39 var point = OpenLayers.Util.getNodes(itemlist[i], 'georss:point'); 40 var lat = OpenLayers.Util.getNodes(itemlist[i], 'geo:lat'); 41 var lon = OpenLayers.Util.getNodes(itemlist[i], 'geo:long'); 42 if (point.length > 0) { 43 var location = point[0].firstChild.nodeValue.trim().split(/\s+/); 44 45 if (location.length !=2) { 46 var location = point[0].firstChild.nodeValue.trim().split(/\s*,\s*/); 47 } 48 } else if (lat.length > 0 && lon.length > 0) { 49 var location = [parseFloat(lat[0].firstChild.nodeValue), parseFloat(lon[0].firstChild.nodeValue)]; 50 } else { 51 continue; 52 } 53 var geometry = new OpenLayers.Geometry.Point(parseFloat(location[1]), 54 parseFloat(location[0])); 55 56 /* Provide defaults for title and description */ 57 var title = "Untitled"; 58 try { 59 title = OpenLayers.Util.getNodes(itemlist[i], 60 "title")[0].firstChild.nodeValue; 61 } 62 catch (e) { title="Untitled"; } 63 64 /* First try RSS descriptions, then Atom summaries */ 65 var descr_nodes = this.getElementsByTagNameNS(itemlist[i], 66 "*", 67 "description"); 68 if (descr_nodes.length == 0) { 69 descr_nodes = this.getElementsByTagNameNS(itemlist[i], 70 "*", 71 "summary"); 72 } 73 var description = "No description."; 74 try { 75 description = descr_nodes[0].firstChild.nodeValue; 76 } 77 catch (e) { description="No description."; } 78 79 /* If no link URL is found in the first child node, try the 80 href attribute */ 81 try { 82 var link = OpenLayers.Util.getNodes(itemlist[i], "link")[0].firstChild.nodeValue; 83 } 84 catch (e) { 85 try { 86 var link = OpenLayers.Util.getNodes(itemlist[i], "link")[0].getAttribute("href"); 87 } 88 catch (e) {} 89 } 90 91 var data = { 92 "title": title, 93 "description": description, 94 "link": link 95 }; 96 features.push(new OpenLayers.Feature.Vector(geometry, data)); 97 } 98 return features; 99 }, 100 101 /** 20 102 * Accept Feature Collection, and return a string. 21 103 * 22 104 * @param {Array} List of features to serialize into a string. 23 105 */ 24 write: function(features) {106 write: function(features) { 25 107 var georss; 26 108 if(features instanceof Array) { … … 35 117 return OpenLayers.Format.XML.prototype.write.apply(this, [georss]); 36 118 }, 37 119 38 120 /** 39 121 * Accept an OpenLayers.Feature.Vector, and build a geometry for it. … … 74 156 // match MultiPolygon or Polygon 75 157 if (geometry.CLASS_NAME == "OpenLayers.Geometry.Polygon") { 76 gml = this.createElementNS(this.georssns, 'georss:polygon');77 78 gml.appendChild(this.buildCoordinatesNode(geometry.components[0]));79 }158 gml = this.createElementNS(this.georssns, 'georss:polygon'); 159 160 gml.appendChild(this.buildCoordinatesNode(geometry.components[0])); 161 } 80 162 // match MultiLineString or LineString 81 163 else if (geometry.CLASS_NAME == "OpenLayers.Geometry.LineString") { 82 gml = this.createElementNS(this.georssns, 'georss:line');83 84 gml.appendChild(this.buildCoordinatesNode(geometry));85 }164 gml = this.createElementNS(this.georssns, 'georss:line'); 165 166 gml.appendChild(this.buildCoordinatesNode(geometry)); 167 } 86 168 // match MultiPoint or Point 87 169 else if (geometry.CLASS_NAME == "OpenLayers.Geometry.Point") { 88 gml = this.createElementNS(this.georssns, 'georss:point');89 gml.appendChild(this.buildCoordinatesNode(geometry));170 gml = this.createElementNS(this.georssns, 'georss:point'); 171 gml.appendChild(this.buildCoordinatesNode(geometry)); 90 172 } else { 91 alert("Couldn't parse " + geometry.CLASS_NAME);173 alert("Couldn't parse " + geometry.CLASS_NAME); 92 174 } 93 175 return gml; sandbox/tschaub/xml/lib/OpenLayers/Format/XML.js
r3229 r3319 88 88 write: function(node, namespaces) { 89 89 var data; 90 if(this.hasXMLSerializer) { 90 if(this.xmldom) { 91 data = node.xml; 92 } else { 91 93 var serializer = new XMLSerializer(); 92 94 data = serializer.serializeToString(node); 93 } else if(this.xmldom) {94 data = node.xml;95 } else {96 var pieces = [];97 namespaces = (!!namespaces) ? namespaces : {};98 if(node.nodeType == 1) {99 var name = node.nodeName;100 var prefix = node._prefix;101 if(prefix) {102 name = prefix + ":" + name;103 }104 pieces.push("<" + name);105 for(var i=0; i<node.attributes.length; ++i) {106 var attribute = node.attributes[i];107 if(attribute.specified && attribute.nodeName.indexOf("_") != 0) {108 pieces.push(" " + attribute.nodeName + '="' +109 attribute.nodeValue + '"');110 }111 }112 var uri = node._namespaceURI;113 if(uri) {114 var index = name.indexOf(":");115 if(index > -1) {116 var prefix = name.substring(0, index);117 if(!namespaces[prefix]) {118 namespaces[prefix] = uri;119 pieces.push(" xmlns:" + prefix + '="' + uri + '"');120 }121 }122 }123 if(node.firstChild) {124 pieces.push(">");125 for(var child = node.firstChild; child; child = child.nextSibling) {126 var p = OpenLayers.Format.XML.prototype.write.apply(this,127 [child, namespaces]);128 pieces.push(p);129 }130 pieces.push("</" + name + ">");131 }132 else {133 pieces.push("/>");134 }135 }136 else if(node.nodeType == 3) {137 pieces.push(node.nodeValue);138 }139 data = pieces.join('');140 95 } 141 96 return data; … … 153 108 createElementNS: function(uri, name) { 154 109 var element; 155 if(this.hasCreateElementNS) { 110 if(this.xmldom) { 111 element = this.xmldom.createNode(1, name, uri); 112 } else { 156 113 element = document.createElementNS(uri, name); 157 } else {158 var prefix, localName;159 var index = name.indexOf(":");160 if(index > -1) {161 prefix = name.substring(0, index);162 localName = name.substring(index + 1);163 }164 if(this.xmldom) {165 element = this.xmldom.createNode(1, name, uri);166 } else {167 if(prefix) {168 element = document.createElement(localName);169 element._prefix = prefix;170 element._namespaceURI = uri;171 } else {172 element = document.createElement(name);173 }174 }175 114 } 176 115 return element; … … 194 133 195 134 /** 135 * @param {Element} node 196 136 * @param {String} uri Namespace URI 197 137 * @param {String} name Local name of the tag … … 200 140 * @type NodeList|Array 201 141 */ 202 getElement ByTagNameNS: function(uri, name) {142 getElementsByTagNameNS: function(node, uri, name) { 203 143 var elements; 204 if(this.hasGetElementByTagNameNS) { 205 elements = parent.getElementsByTagNameNS(uri, name); 206 } else if(this.xmldom) { 207 elements = parent.getElementsByTagName(name); 144 if(this.xmldom) { 145 elements = node.getElementsByTagName(name); 208 146 } else { 209 var nodeList = parent.getElementsByTagName(name); 210 if(nodeList.length > 0) { 211 elements = []; 212 for(var i=1; i<nodeList.length; ++i) { 213 if(nodeList[i]._namespaceURI == uri) { 214 elements.push(nodeList[i]); 215 } 216 } 217 } 147 elements = node.getElementsByTagNameNS(uri, name); 218 148 } 219 149 return elements;
