OpenLayers OpenLayers

Changeset 3319

Show
Ignore:
Timestamp:
06/11/07 18:42:37 (1 year ago)
Author:
tschaub
Message:

get rid of cruft in Format/XML.js - flesh out read/write support for GeoRSS

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/tschaub/xml/examples/vector-formats.html

    r3318 r3319  
    117117                element.value = features.length + ' feature' + plural + ' added' 
    118118            } else { 
    119                 element.value = 'Bad input'
     119                element.value = 'Bad input ' + type
    120120            } 
    121121        } 
  • sandbox/tschaub/xml/lib/OpenLayers/Format/GeoRSS.js

    r3318 r3319  
    1818     
    1919    /** 
     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    /** 
    20102     * Accept Feature Collection, and return a string.  
    21103     *  
    22104     * @param {Array} List of features to serialize into a string. 
    23105     */ 
    24     write: function(features) { 
     106    write: function(features) { 
    25107        var georss; 
    26108        if(features instanceof Array) { 
     
    35117        return OpenLayers.Format.XML.prototype.write.apply(this, [georss]); 
    36118     }, 
    37      
     119 
    38120    /**  
    39121     * Accept an OpenLayers.Feature.Vector, and build a geometry for it. 
     
    74156        // match MultiPolygon or Polygon 
    75157        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       
    80162        // match MultiLineString or LineString 
    81163        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       
    86168        // match MultiPoint or Point 
    87169        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)); 
    90172        } else {     
    91           alert("Couldn't parse " + geometry.CLASS_NAME); 
     173            alert("Couldn't parse " + geometry.CLASS_NAME); 
    92174        }   
    93175        return gml;          
  • sandbox/tschaub/xml/lib/OpenLayers/Format/XML.js

    r3229 r3319  
    8888    write: function(node, namespaces) { 
    8989        var data; 
    90         if(this.hasXMLSerializer) { 
     90        if(this.xmldom) { 
     91            data = node.xml; 
     92        } else { 
    9193            var serializer = new XMLSerializer(); 
    9294            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(''); 
    14095        } 
    14196        return data; 
     
    153108    createElementNS: function(uri, name) { 
    154109        var element; 
    155         if(this.hasCreateElementNS) { 
     110        if(this.xmldom) { 
     111            element = this.xmldom.createNode(1, name, uri); 
     112        } else { 
    156113            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             } 
    175114        } 
    176115        return element; 
     
    194133 
    195134    /** 
     135     * @param {Element} node 
    196136     * @param {String} uri Namespace URI 
    197137     * @param {String} name Local name of the tag 
     
    200140     * @type NodeList|Array 
    201141     */ 
    202     getElementByTagNameNS: function(uri, name) { 
     142    getElementsByTagNameNS: function(node, uri, name) { 
    203143        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); 
    208146        } 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); 
    218148        } 
    219149        return elements;