]> dev.renevier.net Git - syp.git/blob - openlayers/lib/OpenLayers/Format/SLD.js
initial commit
[syp.git] / openlayers / lib / OpenLayers / Format / SLD.js
1 /* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license.
2  * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt 
3  * for the full text of the license. */
4
5 /**
6  * @requires OpenLayers/Format/XML.js
7  * @requires OpenLayers/Style.js
8  * @requires OpenLayers/Rule.js
9  * @requires OpenLayers/Filter/FeatureId.js
10  * @requires OpenLayers/Filter/Logical.js
11  * @requires OpenLayers/Filter/Comparison.js
12  * @requires OpenLayers/Filter/Spatial.js
13  */
14
15 /**
16  * Class: OpenLayers.Format.SLD
17  * Read/Wite SLD. Create a new instance with the <OpenLayers.Format.SLD>
18  *     constructor.
19  * 
20  * Inherits from:
21  *  - <OpenLayers.Format.XML>
22  */
23 OpenLayers.Format.SLD = OpenLayers.Class(OpenLayers.Format.XML, {
24     
25     /**
26      * APIProperty: defaultVersion
27      * {String} Version number to assume if none found.  Default is "1.0.0".
28      */
29     defaultVersion: "1.0.0",
30     
31     /**
32      * APIProperty: version
33      * {String} Specify a version string if one is known.
34      */
35     version: null,
36     
37     /**
38      * APIProperty: namedLayersAsArray
39      * {Boolean} Generate a namedLayers array.  If false, the namedLayers
40      *     property value will be an object keyed by layer name. Default is
41      *     false.
42      */
43     namedLayersAsArray: false,
44     
45     /**
46      * Property: parser
47      * {Object} Instance of the versioned parser.  Cached for multiple read and
48      *     write calls of the same version.
49      */
50     parser: null,
51
52     /**
53      * Constructor: OpenLayers.Format.SLD
54      * Create a new parser for SLD.
55      *
56      * Parameters:
57      * options - {Object} An optional object whose properties will be set on
58      *     this instance.
59      */
60     initialize: function(options) {
61         OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
62     },
63
64     /**
65      * APIMethod: write
66      * Write a SLD document given a list of styles.
67      *
68      * Parameters:
69      * sld - {Object} An object representing the SLD.
70      * options - {Object} Optional configuration object.
71      *
72      * Returns:
73      * {String} An SLD document string.
74      */
75     write: function(sld, options) {
76         var version = (options && options.version) ||
77                       this.version || this.defaultVersion;
78         if(!this.parser || this.parser.VERSION != version) {
79             var format = OpenLayers.Format.SLD[
80                 "v" + version.replace(/\./g, "_")
81             ];
82             if(!format) {
83                 throw "Can't find a SLD parser for version " +
84                       version;
85             }
86             this.parser = new format(this.options);
87         }
88         var root = this.parser.write(sld);
89         return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
90     },
91     
92     /**
93      * APIMethod: read
94      * Read and SLD doc and return an object representing the SLD.
95      *
96      * Parameters:
97      * data - {String | DOMElement} Data to read.
98      * options - {Object} Options for the reader.
99      *
100      * Returns:
101      * {Object} An object representing the SLD.
102      */
103     read: function(data, options) {
104         if(typeof data == "string") {
105             data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
106         }
107         var root = data.documentElement;
108         var version = this.version;
109         if(!version) {
110             version = root.getAttribute("version");
111             if(!version) {
112                 version = this.defaultVersion;
113             }
114         }
115         if(!this.parser || this.parser.VERSION != version) {
116             var format = OpenLayers.Format.SLD[
117                 "v" + version.replace(/\./g, "_")
118             ];
119             if(!format) {
120                 throw "Can't find a SLD parser for version " +
121                       version;
122             }
123             this.parser = new format(this.options);
124         }
125         var sld = this.parser.read(data, options);
126         return sld;
127     },
128
129     CLASS_NAME: "OpenLayers.Format.SLD" 
130 });