OpenLayers OpenLayers

Changeset 8007

Show
Ignore:
Timestamp:
09/12/08 10:16:13 (2 years ago)
Author:
euzuro
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/examples/vector-formats.html

    r7095 r8007  
    5353                'internalProjection': map.baseLayer.projection, 
    5454                'externalProjection': new OpenLayers.Projection(OpenLayers.Util.getElement("inproj").value) 
    55             }     
     55            };    
    5656            var out_options = { 
    5757                'internalProjection': map.baseLayer.projection, 
    5858                '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            ); 
    6072            formats = { 
    6173              'in': { 
     
    6375                geojson: new OpenLayers.Format.GeoJSON(in_options), 
    6476                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), 
    6679                kml: new OpenLayers.Format.KML(in_options) 
    6780              },  
     
    7083                geojson: new OpenLayers.Format.GeoJSON(out_options), 
    7184                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), 
    7387                kml: new OpenLayers.Format.KML(out_options) 
    7488              }  
     
    170184                <option value="kml">KML</option> 
    171185                <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> 
    173188                <option value="wkt">Well-Known Text (WKT)</option> 
    174189            </select> 
  • trunk/openlayers/lib/OpenLayers.js

    r8005 r8007  
    207207            "OpenLayers/Format/XML.js", 
    208208            "OpenLayers/Format/GML.js", 
     209            "OpenLayers/Format/GML/Base.js", 
     210            "OpenLayers/Format/GML/v2.js", 
     211            "OpenLayers/Format/GML/v3.js", 
    209212            "OpenLayers/Format/KML.js", 
    210213            "OpenLayers/Format/GeoRSS.js", 
  • trunk/openlayers/lib/OpenLayers/Format/XML.js

    r7982 r8007  
    2323     * Property: namespaces 
    2424     * {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 
    3140     * {String} The default namespace alias for creating element nodes. 
    3241     */ 
    33     defaultNamespace: null, 
     42    defaultPrefix: null, 
    3443     
    3544    /** 
     
    7685        } 
    7786        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        } 
    7893    }, 
    7994     
     
    85100        this.xmldom = null; 
    86101        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; 
    87115    }, 
    88116 
     
    330358     */ 
    331359    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            } 
    337366        } 
    338367        return value; 
     
    426455 
    427456    /** 
    428      * Method: getNamespacePrefix 
    429      * 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     /** 
    451457     * Method: createElementNSPlus 
    452458     * Shorthand for creating namespaced elements with optional attributes and 
     
    535541            obj = {}; 
    536542        } 
    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]]; 
    540544        if(group) { 
     545            var local = node.localName || node.nodeName.split(":").pop(); 
    541546            var reader = group[local] || group["*"]; 
    542547            if(reader) { 
     
    565570        var children = node.childNodes; 
    566571        var child; 
    567         for(var i=0; i<children.length; ++i) { 
     572        for(var i=0, len=children.length; i<len; ++i) { 
    568573            child = children[i]; 
    569574            if(child.nodeType == 1) { 
     
    602607        } else { 
    603608            if(parent) { 
    604                 prefix = this.getNamespacePrefix(parent.namespaceURI)
     609                prefix = this.namespaceAlias[parent.namespaceURI]
    605610            } else { 
    606611                prefix = this.defaultPrefix; 
  • trunk/openlayers/tests/Format/XML.html

    r7982 r8007  
    292292    } 
    293293     
    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"); 
    323327 
    324328    } 
  • trunk/openlayers/tests/list-tests.html

    r8005 r8007  
    4444    <li>Format/GeoRSS.html</li> 
    4545    <li>Format/GML.html</li> 
     46    <li>Format/GML/v2.html</li> 
     47    <li>Format/GML/v3.html</li> 
    4648    <li>Format/GPX.html</li> 
    4749    <li>Format/JSON.html</li>