OpenLayers OpenLayers

Changeset 5617

Show
Ignore:
Timestamp:
01/01/08 17:23:02 (1 year ago)
Author:
tschaub
Message:

read/write wmc

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC.js

    r5615 r5617  
    11/** 
    22 * @requires OpenLayers/Format/XML.js 
    3  * 
     3 */ 
     4 
     5/** 
    46 * Class: OpenLayers.Format.WMC 
    57 * Read and write Web Map Context documents. 
    6  *  
     8 * 
    79 * Inherits from: 
    810 *  - <OpenLayers.Format.XML> 
    911 */ 
    10 OpenLayers.Format.WMC = OpenLayers.Class(OpenLayers.Format.XML,
     12OpenLayers.Format.WMC = OpenLayers.Class(
    1113     
    1214    /** 
     
    3840     */ 
    3941    initialize: function(options) { 
    40         OpenLayers.Format.XML.prototype.initialize.apply(this, [options]); 
     42        OpenLayers.Util.extend(this, options); 
    4143        this.options = options; 
    4244    }, 
     
    136138    mapToContext: function(map) { 
    137139        var context = {}; 
     140        return context; 
    138141    }, 
    139142 
  • sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1.js

    r5615 r5617  
    11/** 
    2  * @requires OpenLayers/Format/WMC.js 
    3  * 
     2 * @requires OpenLayers/Format/XML.js 
     3 */ 
     4 
     5/** 
    46 * Class: OpenLayers.Format.WMC.v1 
    57 * Superclass for WMC version 1 parsers. 
     8 * 
     9 * Inherits from: 
     10 *  - <OpenLayers.Format.XML> 
    611 */ 
    7 OpenLayers.Format.WMC.v1 = OpenLayers.Class(
     12OpenLayers.Format.WMC.v1 = OpenLayers.Class(OpenLayers.Format.XML,
    813     
    914    /** 
     
    1217     */ 
    1318    namespaces: { 
    14         context: "http://www.opengeospatial.net/context", 
     19        wmc: "http://www.opengeospatial.net/context", 
    1520        sld: "http://www.opengeospatial.net/sld", 
    1621        xlink: "http://www.w3.org/1999/xlink" 
    1722    }, 
    18  
     23     
     24    /** 
     25     * Method: getNamespacePrefix 
     26     * Get the namespace prefix for a given uri from the <namespaces> object. 
     27     * 
     28     * Returns: 
     29     * {String} A namespace prefix or null if none found. 
     30     */ 
     31    getNamespacePrefix: function(uri) { 
     32        var prefix = null; 
     33        if(uri == null) { 
     34            prefix = this.namespaces[this.defaultPrefix]; 
     35        } else { 
     36            for(prefix in this.namespaces) { 
     37                if(this.namespaces[prefix] == uri) { 
     38                    break; 
     39                } 
     40            } 
     41        } 
     42        return prefix; 
     43    }, 
     44     
     45    /** 
     46     * Property: defaultPrefix 
     47     */ 
     48    defaultPrefix: "wmc", 
     49 
     50    /** 
     51     * Property: rootPrefix 
     52     * {String} Prefix on the root node that maps to the context namespace URI. 
     53     */ 
     54    rootPrefix: null, 
     55     
    1956    /** 
    2057     * Constructor: OpenLayers.Format.WMC.v1 
     
    3168 
    3269    /** 
    33      * APIMethod: read 
     70     * Method: read 
    3471     * Read capabilities data from a string, and return a list of layers.  
    3572     *  
     
    4582        } 
    4683        var root = data.documentElement; 
     84        this.rootPrefix = root.prefix; 
    4785        var context = { 
    4886            version: root.getAttribute("version") 
     
    5795    runChildNodes: function(obj, node) { 
    5896        var children = node.childNodes; 
    59         var childNode, processor
     97        var childNode, processor, prefix
    6098        for(var i=0; i<children.length; ++i) { 
    6199            childNode = children[i]; 
    62100            if(childNode.nodeType == 1) { 
    63                 processor = this["read" + childNode.nodeName]; 
     101                prefix = (childNode.prefix == this.rootPrefix) ? 
     102                            this.defaultPrefix : 
     103                            this.getNamespacePrefix(childNode.namespaceURI); 
     104                processor = this["read_" + prefix + "_" + childNode.nodeName]; 
    64105                if(processor) { 
    65106                    processor.apply(this, [obj, childNode]); 
     
    70111     
    71112    /** 
    72      * Method: readGeneral 
    73      */ 
    74     readGeneral: function(context, node) { 
     113     * Method: read_wmc_General 
     114     */ 
     115    read_wmc_General: function(context, node) { 
    75116        this.runChildNodes(context, node); 
    76117    }, 
    77118     
    78119    /** 
    79      * Method: readBoundingBox 
    80      */ 
    81     readBoundingBox: function(context, node) { 
     120     * Method: read_wmc_BoundingBox 
     121     */ 
     122    read_wmc_BoundingBox: function(context, node) { 
    82123        context.srs = node.getAttribute("SRS"); 
    83124        context.bounds = new OpenLayers.Bounds( 
     
    90131     
    91132    /** 
    92      * Method: readLayerList 
    93      */ 
    94     readLayerList: function(context, node) { 
     133     * Method: read_wmc_LayerList 
     134     */ 
     135    read_wmc_LayerList: function(context, node) { 
    95136        context.layers = []; 
    96137        this.runChildNodes(context, node); 
     
    98139     
    99140    /** 
    100      * Method: readLayer 
    101      */ 
    102     readLayer: function(context, node) { 
     141     * Method: read_wmc_Layer 
     142     */ 
     143    read_wmc_Layer: function(context, node) { 
    103144        var layerInfo = { 
    104145            visibility: (node.getAttribute("hidden") != "1"), 
     
    123164     
    124165    /** 
    125      * Method: readServer 
    126      */ 
    127     readServer: function(layerInfo, node) { 
     166     * Method: read_wmc_Server 
     167     */ 
     168    read_wmc_Server: function(layerInfo, node) { 
    128169        layerInfo.version = node.getAttribute("version"); 
    129170        this.runChildNodes(layerInfo, node); 
     
    131172 
    132173    /** 
    133      * Method: readFormatList 
    134      */ 
    135     readFormatList: function(layerInfo, node) { 
     174     * Method: read_wmc_FormatList 
     175     */ 
     176    read_wmc_FormatList: function(layerInfo, node) { 
    136177        this.runChildNodes(layerInfo, node); 
    137178    }, 
    138179 
    139180    /** 
    140      * Method: readFormat 
    141      */ 
    142     readFormat: function(layerInfo, node) { 
     181     * Method: read_wmc_Format 
     182     */ 
     183    read_wmc_Format: function(layerInfo, node) { 
    143184        var format = this.getChildValue(node) 
    144185        layerInfo.formats.push(format); 
     
    149190     
    150191    /** 
    151      * Method: readStyleList 
    152      */ 
    153     readStyleList: function(layerInfo, node) { 
     192     * Method: read_wmc_StyleList 
     193     */ 
     194    read_wmc_StyleList: function(layerInfo, node) { 
    154195        this.runChildNodes(layerInfo, node); 
    155196    }, 
    156197 
    157198    /** 
    158      * Method: readStyle 
    159      */ 
    160     readStyle: function(layerInfo, node) { 
     199     * Method: read_wmc_Style 
     200     */ 
     201    read_wmc_Style: function(layerInfo, node) { 
    161202        var style = {}; 
    162203        this.runChildNodes(style, node); 
     
    168209 
    169210    /** 
    170      * Method: readOnlineResource 
    171      */ 
    172     readOnlineResource: function(obj, node) { 
     211     * Method: read_wmc_OnlineResource 
     212     */ 
     213    read_wmc_OnlineResource: function(obj, node) { 
    173214        obj.href = this.getAttributeNS( 
    174             node, "http://www.w3.org/1999/xlink", "href" 
     215            node, this.namespaces.xlink, "href" 
    175216        ); 
    176217    }, 
    177218     
    178219    /** 
    179      * Method: readName 
    180      */ 
    181     readName: function(obj, node) { 
     220     * Method: read_wmc_Name 
     221     */ 
     222    read_wmc_Name: function(obj, node) { 
    182223        var name = this.getChildValue(node); 
    183224        if(name) { 
     
    187228 
    188229    /** 
    189      * Method: readTitle 
    190      */ 
    191     readTitle: function(obj, node) { 
     230     * Method: read_wmc_Title 
     231     */ 
     232    read_wmc_Title: function(obj, node) { 
    192233        var title = this.getChildValue(node); 
    193234        if(title) { 
     
    197238 
    198239    /** 
    199      * Method: readAbstract 
    200      */ 
    201     readAbstract: function(obj, node) { 
     240     * Method: read_wmc_Abstract 
     241     */ 
     242    read_wmc_Abstract: function(obj, node) { 
    202243        var abst = this.getChildValue(node); 
    203244        if(abst) { 
     
    207248     
    208249    /** 
    209      * Method: readLatLonBoundingBox 
    210      */ 
    211     readLatLonBoundingBox: function(layer, node) { 
     250     * Method: read_wmc_LatLonBoundingBox 
     251     */ 
     252    read_wmc_LatLonBoundingBox: function(layer, node) { 
    212253        layer.llbbox = [ 
    213254            parseFloat(node.getAttribute("minx")), 
     
    219260 
    220261    /** 
    221      * Method: readLegendURL 
    222      */ 
    223     readLegendURL: function(style, node) { 
     262     * Method: read_wmc_LegendURL 
     263     */ 
     264    read_wmc_LegendURL: function(style, node) { 
    224265        var legend = { 
    225266            width: node.getAttribute('width'), 
     
    228269        var links = node.getElementsByTagName("OnlineResource"); 
    229270        if(links.length > 0) { 
    230             this.readOnlineResource(legend, links[0]); 
     271            this.read_wmc_OnlineResource(legend, links[0]); 
    231272        } 
    232273        style.legend = legend; 
    233274    }, 
    234275     
     276    /** 
     277     * Method: write 
     278     * 
     279     * Parameters: 
     280     * context - {Object} An object representing the map context. 
     281     * options - {Object} Optional object. 
     282     * 
     283     * Returns: 
     284     * {String} A WMC document string. 
     285     */ 
     286    write: function(context, options) { 
     287        var root = this.createElementDefaultNS("ViewContext"); 
     288        this.setAttributes(root, { 
     289            version: this.VERSION, 
     290            id: (options && typeof options.id == "string") ? 
     291                    options.id : 
     292                    OpenLayers.Util.createUniqueID("OpenLayers_Context_") 
     293        }); 
     294 
     295        // required General element 
     296        root.appendChild(this.write_wmc_General(context)); 
     297 
     298        // required LayerList element 
     299        root.appendChild(this.write_wmc_LayerList(context)); 
     300 
     301        return OpenLayers.Format.XML.prototype.write.apply(this, [root]); 
     302    }, 
     303     
     304    /** 
     305     * Method: createElementDefaultNS 
     306     * Shorthand for createElementNS with namespace from <defaultPrefix>. 
     307     *     Can optionally be used to set attributes and a text child value. 
     308     * 
     309     * Parameters: 
     310     * name - {String} The qualified node name. 
     311     * childValue - {String} Optional value for text child node. 
     312     * attributes - {Object} Optional object representing attributes. 
     313     * 
     314     * Returns: 
     315     * {Element} An element node. 
     316     */ 
     317    createElementDefaultNS: function(name, childValue, attributes) { 
     318        var node = this.createElementNS( 
     319            this.namespaces[this.defaultPrefix], 
     320            name 
     321        ); 
     322        if(childValue) { 
     323            node.appendChild(this.createTextNode(childValue)); 
     324        } 
     325        if(attributes) { 
     326            this.setAttributes(node, attributes); 
     327        } 
     328        return node; 
     329    }, 
     330     
     331    /** 
     332     * Method: setAttributes 
     333     * Set multiple attributes given key value pairs from an object. 
     334     * 
     335     * Parameters: 
     336     * node - {Element} An element node. 
     337     * obj - {Object} An object whose properties represent attribute names and 
     338     *     values represent attribute values. 
     339     */ 
     340    setAttributes: function(node, obj) { 
     341        for(var name in obj) { 
     342            node.setAttribute(name, obj[name]); 
     343        } 
     344    }, 
     345 
     346    /** 
     347     * Method: write_wmc_General 
     348     * Create a General node given an context object. 
     349     * 
     350     * Parameters: 
     351     * context - {Object} Context object. 
     352     * 
     353     * Returns: 
     354     * {Element} A WMC General element node. 
     355     */ 
     356    write_wmc_General: function(context) { 
     357        var node = this.createElementDefaultNS("General"); 
     358 
     359        // optional Window element 
     360        if(context.size) { 
     361            node.appendChild(this.createElementDefaultNS( 
     362                "Window", null, 
     363                { 
     364                    width: context.size.w, 
     365                    height: context.size.h 
     366                } 
     367            )); 
     368        } 
     369         
     370        // required BoundingBox element 
     371        var bounds = context.bounds; 
     372        node.appendChild(this.createElementDefaultNS( 
     373            "BoundingBox", null, 
     374            { 
     375                minx: bounds.left, miny: bounds.bottom, 
     376                maxx: bounds.right, maxy: bounds.top, 
     377                SRS: context.srs 
     378            } 
     379        )); 
     380 
     381        // required Title element 
     382        node.appendChild(this.createElementDefaultNS( 
     383            "Title", context.title 
     384        )); 
     385         
     386        return node; 
     387    }, 
     388     
     389    /** 
     390     * Method: write_wmc_LayerList 
     391     * Create a LayerList node given an context object. 
     392     * 
     393     * Parameters: 
     394     * context - {Object} Context object. 
     395     * 
     396     * Returns: 
     397     * {Element} A WMC LayerList element node. 
     398     */ 
     399    write_wmc_LayerList: function(context) { 
     400        var list = this.createElementDefaultNS("LayerList"); 
     401         
     402        var layer; 
     403        for(var i=0; i<context.layers.length; ++i) { 
     404            list.appendChild(this.write_wmc_Layer(context.layers[i])); 
     405        } 
     406         
     407        return list; 
     408    }, 
     409 
     410    /** 
     411     * Method: write_wmc_Layer 
     412     * Create a Layer node given a layer object. 
     413     * 
     414     * Parameters: 
     415     * layer - {<OpenLayers.Layer.WMS>} Layer object. 
     416     * 
     417     * Returns: 
     418     * {Element} A WMC Layer element node. 
     419     */ 
     420    write_wmc_Layer: function(layer) { 
     421        var node = this.createElementDefaultNS( 
     422            "Layer", null, { 
     423                queryable: "true", 
     424                hidden: layer.visibility ? "true" : "false" 
     425            } 
     426        ); 
     427         
     428        // required Name element 
     429        node.appendChild(this.createElementDefaultNS( 
     430            "Name", layer.params["LAYERS"] 
     431        )); 
     432         
     433        // required Title element 
     434        node.appendChild(this.createElementDefaultNS( 
     435            "Title", layer.name 
     436        )); 
     437 
     438        // required Server element 
     439        node.appendChild(this.write_wmc_Server(layer)); 
     440 
     441        return node; 
     442    }, 
     443 
     444    /** 
     445     * Method: write_wmc_Server 
     446     * Create a Server node given a layer object. 
     447     * 
     448     * Parameters: 
     449     * layer - {<OpenLayers.Layer.WMS>} Layer object. 
     450     * 
     451     * Returns: 
     452     * {Element} A WMC Server element node. 
     453     */ 
     454    write_wmc_Server: function(layer) { 
     455        var node = this.createElementDefaultNS("Server"); 
     456        this.setAttributes(node, { 
     457            service: "OGC:WMS", 
     458            version: layer.params["VERSION"] 
     459        }); 
     460         
     461        // required OnlineResource element 
     462        node.appendChild(this.write_wmc_OnlineResource(layer.url)); 
     463         
     464        return node; 
     465    }, 
     466 
     467    /** 
     468     * Method: write_wmc_FormatList 
     469     * Create a FormatList node given a layer. 
     470     * 
     471     * Parameters: 
     472     * layer - {<OpenLayers.Layer.WMS>} Layer object. 
     473     * 
     474     * Returns: 
     475     * {Element} A WMC FormatList element node. 
     476     */ 
     477    write_wmc_FormatList: function(layer) { 
     478        var node = this.createElementDefaultNS("FormatList"); 
     479        node.appendChild(this.createElementDefaultNS( 
     480            "Format", layer.params["FORMAT"], {current: "true"} 
     481        )); 
     482 
     483        return node; 
     484    }, 
     485 
     486    /** 
     487     * Method: write_wmc_StyleList 
     488     * Create a StyleList node given a layer. 
     489     * 
     490     * Parameters: 
     491     * layer - {<OpenLayers.Layer.WMS>} Layer object. 
     492     * 
     493     * Returns: 
     494     * {Element} A WMC StyleList element node. 
     495     */ 
     496    write_wmc_StyleList: function(layer) { 
     497        var node = this.createElementDefaultNS("StyleList"); 
     498        node.appendChild(this.createElementDefaultNS( 
     499            "Style", layer.params["STYLES"], {current: "true"} 
     500        )); 
     501 
     502        return node; 
     503    }, 
     504 
     505    /** 
     506     * Method: write_wmc_OnlineResource 
     507     * Create an OnlineResource node given a URL. 
     508     * 
     509     * Parameters: 
     510     * href - {String} URL for the resource. 
     511     * 
     512     * Returns: 
     513     * {Element} A WMC OnlineResource element node. 
     514     */ 
     515    write_wmc_OnlineResource: function(href) { 
     516        var node = this.createElementDefaultNS("OnlineResource"); 
     517        this.setAttributeNS(node, this.namespaces.xlink, "xlink:type", "simple"); 
     518        this.setAttributeNS(node, this.namespaces.xlink, "xlink:href", href); 
     519        return node; 
     520    }, 
    235521 
    236522    CLASS_NAME: "OpenLayers.Format.WMC.v1"  
  • sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_0_0.js

    r5615 r5617  
    11/** 
    2  * @requires OpenLayers/Format/WMC.js 
    3  * 
     2 * @requires OpenLayers/Format/WMC/v1.js 
     3 */ 
     4 
     5/** 
    46 * Class: OpenLayers.Format.WMC.v1_0_0 
    57 * Read and write WMC version 1.0.0. 
  • sandbox/tschaub/wmc/lib/OpenLayers/Format/WMC/v1_1_0.js

    r5615 r5617  
    11/** 
    2  * @requires OpenLayers/Format/WMC.js 
    3  * 
     2 * @requires OpenLayers/Format/WMC/v1.js 
     3 */ 
     4 
     5/** 
    46 * Class: OpenLayers.Format.WMC.v1_1_0 
    57 * Read and write WMC version 1.1.0. 
  • sandbox/tschaub/wmc/lib/OpenLayers/Format/XML.js

    r5616 r5617  
    349349        return found; 
    350350    }, 
     351     
     352    /** 
     353     * APIMethod: setAttributeNS 
     354     * Adds a new attribute or changes the value of an attribute with the given 
     355     *     namespace and name. 
     356     * 
     357     * Parameters: 
     358     * node - {Element} Element node on which to set the attribute. 
     359     * namespace - {String} Namespace URI for the attribute. 
     360     * name - {String} Qualified name (prefix:localName) for the attribute. 
     361     * value - {String} Attribute value. 
     362     */ 
     363    setAttributeNS: function(node, namespace, name, value) { 
     364        if(node.setAttributeNS) { 
     365            node.setAttributeNS(namespace, name, value); 
     366        } else { 
     367            throw "setAttributeNS not implemented"; 
     368        } 
     369    }, 
    351370 
    352371    CLASS_NAME: "OpenLayers.Format.XML"