OpenLayers OpenLayers

Ticket #533: sldRenderer.2.diff

File sldRenderer.2.diff, 151.1 kB (added by ahocevar, 10 months ago)

new version of the patch, see http://trac.openlayers.org/ticket/533#comment:6 for details.

  • /mnt/d/eclipse/workspace/openlayers/tests/Format/test_SLD.html

    old new  
     1<html>  
     2<head>  
     3    <script src="../../lib/OpenLayers.js"></script>  
     4    <script type="text/javascript"> 
     5 
     6    var test_content = '<sld:StyledLayerDescriptor xmlns:sld="http://www.opengis.net/sld" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml"><sld:NamedLayer><sld:UserStyle><sld:Name>foo</sld:Name><sld:FeatureTypeStyle><sld:Rule><sld:Name>bar</sld:Name><ogc:Filter></ogc:Filter><sld:PolygonSymbolizer><sld:Fill><sld:CssParameter name="fill"><ogc:Literal>blue</ogc:Literal></sld:CssParameter></sld:Fill></sld:PolygonSymbolizer></sld:Rule></sld:FeatureTypeStyle></sld:UserStyle></sld:NamedLayer></sld:StyledLayerDescriptor>'; 
     7 
     8    function test_Format_SLD_constructor(t) {  
     9        t.plan(3);  
     10          
     11        var options = {'foo': 'bar'};  
     12        var format = new OpenLayers.Format.SLD(options);  
     13        t.ok(format instanceof OpenLayers.Format.SLD,  
     14             "new OpenLayers.Format.SLD returns object" );  
     15        t.eq(format.foo, "bar", "constructor sets options correctly");  
     16        t.eq(typeof format.read, "function", "format has a read function");  
     17    } 
     18 
     19    function test_Format_SLD_read(t) { 
     20        t.plan(4); 
     21        var styles = (new OpenLayers.Format.SLD()).read(this.test_content); 
     22         
     23        t.ok(styles.foo != undefined, "SLD correctly reads a UserStyle named \"foo\""); 
     24        t.eq(styles.foo.rules.length, 1, "The number of rules for the UserStyle is correct"); 
     25        t.eq(styles.foo.rules[0].name, "bar", "The first rule's name is \"bar\""); 
     26        t.eq(styles.foo.rules[0].symbolizer.Polygon.fillColor, "blue", "The fillColor for the Polygon symbolizer is correct"); 
     27    } 
     28 
     29    </script>  
     30</head>  
     31<body>  
     32</body>  
     33</html>  
  • /mnt/d/eclipse/workspace/openlayers/tests/Rule/test_FeatureId.html

    old new  
     1<html>  
     2<head>  
     3    <script src="../../lib/OpenLayers.js"></script>  
     4    <script type="text/javascript"> 
     5 
     6    function test_FeatureId_constructor(t) {  
     7        t.plan(3);  
     8          
     9        var options = {'foo': 'bar'};  
     10        var rule = new OpenLayers.Rule.FeatureId(options);  
     11        t.ok(rule instanceof OpenLayers.Rule.FeatureId,  
     12             "new OpenLayers.Rule.FeatureId returns object" );  
     13        t.eq(rule.foo, "bar", "constructor sets options correctly");  
     14        t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");  
     15    } 
     16 
     17    function test_FeatureId_destroy(t) { 
     18        t.plan(1); 
     19         
     20        var rule = new OpenLayers.Rule.FeatureId(); 
     21        rule.destroy(); 
     22        t.eq(rule.symbolizer, null, "symbolizer hash nulled properly"); 
     23    } 
     24     
     25    function test_FeatureId_evaluate(t) { 
     26        t.plan(3); 
     27         
     28        var rule = new OpenLayers.Rule.FeatureId( 
     29                {fids: ["fid_1", "fid_3"]}); 
     30 
     31        var ruleResults = { 
     32                "fid_1" : true, 
     33                "fid_2" : false, 
     34                "fid_3" : true}; 
     35        for (var i in ruleResults) { 
     36            var feature = new OpenLayers.Feature.Vector(); 
     37            feature.fid = i; 
     38            var result = rule.evaluate(feature); 
     39            t.eq(result, ruleResults[i], "feature "+i+" evaluates to "+result.toString()+" correctly."); 
     40            feature.destroy(); 
     41        } 
     42    } 
     43    </script>  
     44</head>  
     45<body>  
     46</body>  
     47</html>  
  • /mnt/d/eclipse/workspace/openlayers/tests/Rule/test_Comparison.html

    old new  
     1<html>  
     2<head>  
     3    <script src="../../lib/OpenLayers.js"></script>  
     4    <script type="text/javascript"> 
     5 
     6    function test_Comparison_constructor(t) {  
     7        t.plan(3);  
     8          
     9        var options = {'foo': 'bar'};  
     10        var rule = new OpenLayers.Rule.Comparison(options);  
     11        t.ok(rule instanceof OpenLayers.Rule.Comparison,  
     12             "new OpenLayers.Rule.Comparison returns object" );  
     13        t.eq(rule.foo, "bar", "constructor sets options correctly");  
     14        t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");  
     15    } 
     16 
     17    function test_Comparison_destroy(t) { 
     18        t.plan(1); 
     19         
     20        var rule = new OpenLayers.Rule.Comparison(); 
     21        rule.destroy(); 
     22        t.eq(rule.symbolizer, null, "symbolizer hash nulled properly"); 
     23    } 
     24     
     25    function test_Comparison_evaluate(t) { 
     26        t.plan(3); 
     27         
     28        var rule = new OpenLayers.Rule.Comparison({ 
     29                property: "area", 
     30                lowerBoundary: 1000, 
     31                upperBoundary: 5000, 
     32                type: OpenLayers.Rule.Comparison.Type.BETWEEN}); 
     33 
     34        var features = [ 
     35                new OpenLayers.Feature.Vector(null, { 
     36                        area: 2000}), 
     37                new OpenLayers.Feature.Vector(null, { 
     38                        area: 6000}), 
     39                new OpenLayers.Feature.Vector(null, { 
     40                        area: 4999})]; 
     41        var ruleResults = { 
     42                0: true, 
     43                1: false, 
     44                2: true}; 
     45        for (var i in ruleResults) { 
     46            var result = rule.evaluate(features[i]); 
     47            t.eq(result, ruleResults[i], "feature "+i+ 
     48                    " evaluates to "+result.toString()+" correctly."); 
     49        } 
     50    } 
     51    </script>  
     52</head>  
     53<body>  
     54</body>  
     55</html>  
  • /mnt/d/eclipse/workspace/openlayers/tests/Rule/test_Logical.html

    old new  
     1<html>  
     2<head>  
     3    <script src="../../lib/OpenLayers.js"></script>  
     4    <script type="text/javascript"> 
     5 
     6    function test_Logical_constructor(t) {  
     7        t.plan(3);  
     8          
     9        var options = {'foo': 'bar'};  
     10        var rule = new OpenLayers.Rule.Logical(options);  
     11        t.ok(rule instanceof OpenLayers.Rule.Logical,  
     12             "new OpenLayers.Rule.Logical returns object" );  
     13        t.eq(rule.foo, "bar", "constructor sets options correctly");  
     14        t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");  
     15    } 
     16 
     17    function test_Logical_destroy(t) { 
     18        t.plan(1); 
     19         
     20        var rule = new OpenLayers.Rule.Logical(); 
     21        rule.destroy(); 
     22        t.eq(rule.children, null, "children array nulled properly"); 
     23    } 
     24     
     25    function test_Logical_evaluate(t) { 
     26        t.plan(1); 
     27         
     28        var rule = new OpenLayers.Rule.Logical({ 
     29                type: OpenLayers.Rule.Logical.Type.NOT}); 
     30        rule.children.push(new OpenLayers.Rule()); 
     31         
     32        var feature = new OpenLayers.Feature.Vector(); 
     33 
     34        t.eq(rule.evaluate(feature), false, 
     35                "feature evaluates to false correctly."); 
     36    } 
     37    </script>  
     38</head>  
     39<body>  
     40</body>  
     41</html>  
  • /mnt/d/eclipse/workspace/openlayers/tests/test_Style.html

    old new  
     1<html>  
     2<head>  
     3    <script src="../lib/OpenLayers.js"></script>  
     4    <script type="text/javascript"> 
     5 
     6    function test_Style_constructor(t) {  
     7        t.plan(3);  
     8          
     9        var options = {'foo': 'bar'};  
     10        var style = new OpenLayers.Style(options);  
     11        t.ok(style instanceof OpenLayers.Style,  
     12             "new OpenLayers.Style returns object" );  
     13        t.eq(style.foo, "bar", "constructor sets options correctly");  
     14        t.eq(typeof style.createStyle, "function", "style has a createStyle function");  
     15    } 
     16 
     17    function test_Style_destroy(t) { 
     18        t.plan(1); 
     19         
     20        var style = new OpenLayers.Style(); 
     21        style.destroy(); 
     22        t.eq(style.rules, null, "rules array nulled properly"); 
     23    } 
     24 
     25    </script>  
     26</head>  
     27<body>  
     28</body>  
     29</html>  
  • /mnt/d/eclipse/workspace/openlayers/tests/test_Rule.html

    old new  
     1<html>  
     2<head>  
     3    <script src="../lib/OpenLayers.js"></script>  
     4    <script type="text/javascript"> 
     5 
     6    function test_Rule_constructor(t) {  
     7        t.plan(3);  
     8          
     9        var options = {'foo': 'bar'};  
     10        var rule = new OpenLayers.Rule(options);  
     11        t.ok(rule instanceof OpenLayers.Rule,  
     12             "new OpenLayers.Rule returns object" );  
     13        t.eq(rule.foo, "bar", "constructor sets options correctly");  
     14        t.eq(typeof rule.evaluate, "function", "rule has an evaluate function");  
     15    } 
     16 
     17    function test_Rule_destroy(t) { 
     18        t.plan(1); 
     19         
     20        var rule = new OpenLayers.Rule(); 
     21        rule.destroy(); 
     22        t.eq(rule.symbolizer, null, "symbolizer hash nulled properly"); 
     23    } 
     24 
     25    </script>  
     26</head>  
     27<body>  
     28</body>  
     29</html>  
  • /mnt/d/eclipse/workspace/openlayers/tests/list-tests.html

    old new  
    2222    <li>test_Format.html</li> 
    2323    <li>Format/test_XML.html</li> 
    2424    <li>Format/test_KML.html</li> 
     25    <li>Format/test_SLD.html</li> 
    2526    <li>Format/test_GeoRSS.html</li> 
    2627    <li>Format/test_JSON.html</li> 
    2728    <li>Format/test_GeoJSON.html</li> 
     
    3435    <li>Popup/test_Anchored.html</li> 
    3536    <li>test_Feature.html</li> 
    3637    <li>Feature/test_Vector.html</li> 
     38    <li>test_Style.html</li> 
     39    <li>test_Rule.html</li> 
     40    <li>Rule/test_FeatureId.html</li> 
     41    <li>Rule/test_Logical.html</li> 
     42    <li>Rule/test_Comparison.html</li> 
    3743    <li>test_Events.html</li> 
    3844    <li>test_Util.html</li> 
    3945    <li>test_Layer.html</li> 
  • /mnt/d/eclipse/workspace/openlayers/lib/OpenLayers/Format/SLD.js

    old new  
     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/Rule/FeatureId.js 
     10 * @requires OpenLayers/Rule/Logical.js 
     11 * @requires OpenLayers/Rule/Comparison.js 
     12 * 
     13 * Class: OpenLayers.Format.SLD 
     14 * Read/Wite SLD. Create a new instance with the <OpenLayers.Format.SLD> 
     15 *     constructor. 
     16 *  
     17 * Inherits from: 
     18 *  - <OpenLayers.Format.XML> 
     19 */ 
     20OpenLayers.Format.SLD = OpenLayers.Class(OpenLayers.Format.XML, { 
     21     
     22    /** 
     23     * APIProperty: sldns 
     24     * Namespace used for sld. 
     25     */ 
     26    sldns: "http://www.opengis.net/sld", 
     27     
     28    /** 
     29     * APIProperty: ogcns 
     30     * Namespace used for ogc. 
     31     */ 
     32    ogcns: "http://www.opengis.net/ogc", 
     33     
     34    /** 
     35     * APIProperty: gmlns 
     36     * Namespace used for gml. 
     37     */ 
     38    gmlns: "http://www.opengis.net/gml", 
     39     
     40    /** 
     41     * Constructor: OpenLayers.Format.SLD 
     42     * Create a new parser for SLD 
     43     * 
     44     * Parameters: 
     45     * options - {Object} An optional object whose properties will be set on 
     46     *                    this instance. 
     47     */ 
     48    initialize: function(options) { 
     49        OpenLayers.Format.XML.prototype.initialize.apply(this, [options]); 
     50    }, 
     51 
     52    /** 
     53     * APIMethod: read 
     54     * Read data from a string, and return a list of features.  
     55     *  
     56     * Parameters: 
     57     * data - {String} or {XMLNode} data to read/parse. 
     58     * 
     59     * Returns: 
     60     * {Object} Hash of SLD UserStyles, containing 
     61     * SLD rules. 
     62     */ 
     63    read: function(data) { 
     64        if (typeof data == "string") {  
     65            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); 
     66        } 
     67         
     68        var userStyles = this.getElementsByTagNameNS(data, this.sldns, 
     69                "UserStyle"); 
     70        if (userStyles.length == 0) { 
     71            return {}; 
     72        } 
     73        var styles = {}; 
     74        for (var i=0; i<userStyles.length; i++) { 
     75            var name = this.parseProperty(userStyles[i], this.sldns, "Name"); 
     76            styles[name] = this.parseUserStyle(userStyles[i], name); 
     77        } 
     78         
     79        return styles; 
     80    }, 
     81 
     82    /** 
     83     * Method: parseUserStyle 
     84     * parses a sld userStyle for rules 
     85     *  
     86     * Parameters: 
     87     * xmlNode - {<DOMElement>} xml node to read the style from 
     88     * name - {<String>} name of the style 
     89     *  
     90     * Returns: 
     91     * {<OpenLayers.Style>} 
     92     */ 
     93    parseUserStyle: function(xmlNode, name) { 
     94        var userStyle = new OpenLayers.Style({name: name}); 
     95         
     96        userStyle.isDefault = this.parseProperty(xmlNode, this.sldns, 
     97                "IsDefault") == 1 ? true : false; 
     98         
     99        // get the name of the layer if we have a NamedLayer 
     100        var namedLayerNode = xmlNode.parentNode; 
     101        var nameNodes = this.getElementsByTagNameNS(namedLayerNode, this.sldns, "Name"); 
     102        if (namedLayerNode.nodeName.indexOf("NamedLayer") != -1 && 
     103                nameNodes && 
     104                nameNodes.length > 0 && 
     105                nameNodes[0].parentNode == namedLayerNode) { 
     106            userStyle.layerName = this.getChildValue(nameNodes[0]); 
     107        } 
     108          
     109        var ruleNodes = this.getElementsByTagNameNS(xmlNode, this.sldns, 
     110                "Rule"); 
     111        if (ruleNodes.length == 0) { return []; } 
     112 
     113        var rules = userStyle.rules; 
     114        for (var i=0; i<ruleNodes.length; i++) { 
     115            var name = this.parseProperty(ruleNodes[i], this.sldns, "Name"); 
     116            rules.push(this.parseRule(ruleNodes[i], name)); 
     117        } 
     118 
     119        return userStyle; 
     120    },         
     121     
     122    /** 
     123     * Method: parseRule 
     124     * This function is the core of the SLD parsing code in OpenLayers. 
     125     * It creates the rule with its constraints and symbolizers. 
     126     * 
     127     * Parameters: 
     128     * xmlNode - {<DOMElement>} 
     129     *  
     130     * Returns: 
     131     * {Object} Hash of rule properties 
     132     */ 
     133    parseRule: function(xmlNode, name) { 
     134 
     135        // FILTERS 
     136         
     137        var filter = this.getElementsByTagNameNS(xmlNode, this.ogcns, "Filter"); 
     138        if (filter && filter.length > 0) { 
     139            var rule = this.parseFilter(filter[0]); 
     140        } else { 
     141            // rule applies to all features 
     142            var rule = new OpenLayers.Rule(); 
     143        } 
     144        rule.name = name; 
     145         
     146        // SCALE DENOMINATORS 
     147         
     148        // MinScaleDenominator 
     149        var minScale = this.getElementsByTagNameNS(xmlNode, 
     150                this.sldns, "MinScaleDenominator"); 
     151        if (minScale && minScale.length > 0) { 
     152            rule.minScaleDenominator = parseFloat( 
     153                    this.getChildValue(minScale[0])); 
     154        } 
     155         
     156        // MaxScaleDenominator 
     157        var maxScale = this.getElementsByTagNameNS(xmlNode, 
     158                this.sldns, "MaxScaleDenominator"); 
     159        if (maxScale && maxScale.length > 0) { 
     160            rule.maxScaleDenominator = parseFloat( 
     161                    this.getChildValue(maxScale[0])); 
     162        } 
     163         
     164        // STYLES 
     165         
     166        // walk through all symbolizers 
     167        var prefixes = OpenLayers.Style.SYMBOLIZER_PREFIXES; 
     168        for (var s=0; s<prefixes.length; s++) { 
     169             
     170            // symbolizer type 
     171            var symbolizer = this.getElementsByTagNameNS(xmlNode, this.sldns, 
     172                    prefixes[s]+"Symbolizer"); 
     173             
     174            if (symbolizer && symbolizer.length > 0) { 
     175             
     176                var style = {}; 
     177             
     178                // externalGraphic 
     179                var graphic = this.getElementsByTagNameNS(symbolizer[0], 
     180                        this.sldns, "Graphic"); 
     181                if (graphic && graphic.length > 0) { 
     182                    style.externalGraphic = this.parseProperty(graphic[0], 
     183                            this.sldns, "OnlineResource", "xlink:href"); 
     184                    style.pointRadius = this.parseProperty(graphic[0], 
     185                            this.sldns, "Size"); 
     186                    style.graphicOpacity = this.parseProperty(graphic[0], 
     187                            this.sldns, "Opacity"); 
     188                } 
     189                 
     190                // fill 
     191                var fill = this.getElementsByTagNameNS(symbolizer[0], 
     192                        this.sldns, "Fill"); 
     193                if (fill && fill.length > 0) { 
     194                    style.fillColor = this.parseProperty(fill[0], this.sldns, 
     195                            "CssParameter", "name", "fill"); 
     196                    style.fillOpacity = this.parseProperty(fill[0], 
     197                            this.sldns, "CssParameter", "name", 
     198                            "fill-opacity") || 1; 
     199                } 
     200             
     201                // stroke 
     202                var stroke = this.getElementsByTagNameNS(symbolizer[0], 
     203                        this.sldns, "Stroke"); 
     204                if (stroke && stroke.length > 0) { 
     205                    style.strokeColor = this.parseProperty(stroke[0], 
     206                            this.sldns, "CssParameter", "name", "stroke"); 
     207                    style.strokeOpacity = this.parseProperty(stroke[0], 
     208                            this.sldns, "CssParameter", "name", 
     209                            "stroke-opacity") || 1; 
     210                    style.strokeWidth = this.parseProperty(stroke[0], 
     211                            this.sldns, "CssParameter", "name", 
     212                            "stroke-width"); 
     213                    style.strokeLinecap = this.parseProperty(stroke[0], 
     214                            this.sldns, "CssParameter", "name", 
     215                            "stroke-linecap"); 
     216                } 
     217                 
     218                // set the [point|line|polygon]Symbolizer property of the rule 
     219                rule.symbolizer[prefixes[s]] = style; 
     220            } 
     221        } 
     222 
     223        return rule; 
     224    }, 
     225     
     226    /** 
     227     * Method: parseFilter 
     228     * Parses ogc fiters. 
     229     * 
     230     * Parameters: 
     231     * xmlNode - {<DOMElement>} 
     232     *  
     233     * Returns: 
     234     * {<OpenLayers.Rule>} rule representing the filter 
     235     */ 
     236    parseFilter: function(xmlNode) { 
     237        var nodeName = (xmlNode.prefix) ? 
     238               xmlNode.nodeName.split(":")[1] : 
     239               xmlNode.nodeName; 
     240 
     241        // ogc:FeatureId filter 
     242        var fidFilter = (nodeName == "FeatureId") ? 
     243                xmlNode : 
     244                this.getElementsByTagNameNS(xmlNode, this.ogcns, "FeatureId"); 
     245        if (fidFilter && fidFilter.length > 0) { 
     246            var rule = new OpenLayers.Rule.FeatureId(); 
     247            for (var i=0; i<fidFilter.length; i++) { 
     248                rule.fids.push(fidFilter[i].getAttribute("fid")); 
     249            } 
     250            return rule; 
     251        } 
     252         
     253        // ogc:And filter 
     254        var andFilter = (nodeName == "And") ? 
     255                xmlNode : 
     256                this.getElementsByTagNameNS(xmlNode, this.ogcns, "And"); 
     257        if (andFilter.length > 0) { 
     258            andFilter = andFilter[0]; 
     259        } 
     260        if (andFilter.childNodes && andFilter.parentNode == xmlNode) { 
     261            var rule = new OpenLayers.Rule.Logical( 
     262                    {type: OpenLayers.Rule.Logical.Type.AND}); 
     263            var filters = andFilter.childNodes;  
     264            for (var i=0; i<filters.length; i++) { 
     265                if (filters[i].nodeType == 1) { 
     266                    rule.children.push(this.parseFilter(filters[i])); 
     267                } 
     268            } 
     269            return rule; 
     270        } 
     271 
     272        // ogc:Or filter 
     273        var orFilter = (nodeName == "Or") ? 
     274                xmlNode : 
     275                this.getElementsByTagNameNS(xmlNode, this.ogcns, "Or"); 
     276        if (orFilter.length > 0) { 
     277            orFilter = orFilter[0]; 
     278        } 
     279        if (orFilter.childNodes && orFilter.parentNode == xmlNode) { 
     280            var rule = new OpenLayers.Rule.Logical( 
     281                    {type: OpenLayers.Rule.Logical.Type.OR}) 
     282            var filters = orFilter.childNodes;  
     283            for (var i=0; i<filters.length; i++) { 
     284                if (filters[i].nodeType == 1) { 
     285                    rule.children.push(this.parseFilter(filters[i])); 
     286                } 
     287            } 
     288            return rule; 
     289        } 
     290 
     291        // ogc:Not filter 
     292        var notFilter = (nodeName == "Not") ? 
     293                xmlNode : 
     294                this.getElementsByTagNameNS(xmlNode, this.ogcns, "Not"); 
     295        if (notFilter.length > 0) { 
     296            notFilter = notFilter[0]; 
     297        } 
     298        if (notFilter.childNodes && notFilter.parentNode == xmlNode) { 
     299            var rule = new OpenLayers.Rule.Logical( 
     300                    {type: OpenLayers.Rule.Logical.Type.NOT}); 
     301            rule.children.push(this.parseFilter(notFilter)); 
     302            return rule; 
     303        } 
     304         
     305        // Comparison filters 
     306        for (var i in OpenLayers.Rule.Comparison.Type) { 
     307            // calculate the rule node name 
     308            var type = OpenLayers.String.camelize("-property-is-"+ 
     309                    i.replace( 
     310                    /_/g, "-").toLowerCase()); 
     311            var comparisonFilter = (nodeName == type) ? 
     312                    xmlNode : 
     313                    this.getElementsByTagNameNS(xmlNode, this.ogcns, type); 
     314            if (comparisonFilter.length > 0) { 
     315                comparisonFilter = comparisonFilter[0]; 
     316            } 
     317            if (comparisonFilter.childNodes) { 
     318                var rule = new OpenLayers.Rule.Comparison({ 
     319                        type: OpenLayers.Rule.Comparison.Type[i], 
     320                        property: this.parseProperty( 
     321                                comparisonFilter, this.ogcns, "PropertyName")}); 
     322                // ogc:PropertyIsBetween 
     323                if (OpenLayers.Rule.Comparison.Type[i] == 
     324                            OpenLayers.Rule.Comparison.Type.BETWEEN) { 
     325                    rule.lowerBoundary = this.parseProperty( 
     326                            comparisonFilter, this.ogcns, "LowerBoundary"); 
     327                    rule.upperBoudary = this.parseProperty( 
     328                            comparisonFilter, this.ogcns, "UpperBoundary"); 
     329                } else { 
     330                    rule.value = this.parseProperty( 
     331                            comparisonFilter, this.ogcns, "Literal"); 
     332                    // ogc:PropertyIsLike 
     333                    if (OpenLayers.Rule.Comparison.Type[i] == 
     334                                OpenLayers.Rule.Comparison.Type.LIKE) { 
     335                        var wildCard = comparisonFilter.getAttribute("wildCard"); 
     336                        var singleChar = comparisonFilter.getAttribute("singleChar"); 
     337                        var escape = comparisonFilter.getAttribute("escape"); 
     338                        rule.value2regex(wildCard, singleChar, escape); 
     339                    } 
     340                } 
     341                return rule; 
     342            } 
     343        } 
     344         
     345        // if we get here, the filter was empty 
     346        return new OpenLayers.Rule(); 
     347    }, 
     348     
     349    /** 
     350     * Method: parseProperty 
     351     * Convenience method to parse the different kinds of properties 
     352     * found in the sld and ogc namespace. 
     353     * Parses an ogc node that can either contain a value directly, 
     354     * or inside a <Literal> property. The parsing can also be limited 
     355     * to nodes with certain attribute names and/or values 
     356     * 
     357     * Parameters: 
     358     * xmlNode        - {<DOMElement>} 
     359     * namespace      - {String} namespace of the node to find 
     360     * propertyName   - {String} name of the property to parse 
     361     * attributeName  - {String} optional name of the property to match 
     362     * attributeValue - {String} optional value of the specified attribute 
     363     *  
     364     * Returns: 
     365     * {String} The value for the requested property 
     366     */     
     367    parseProperty: function(xmlNode, namespace, propertyName, attributeName, 
     368                                                              attributeValue) { 
     369        var result = null; 
     370        var propertyNodeList = this.getElementsByTagNameNS( 
     371                xmlNode, namespace, propertyName); 
     372                 
     373        if (propertyNodeList && propertyNodeList.length > 0) { 
     374            var propertyNode = attributeName ? 
     375                    this.getNodeWithAttribute(propertyNodeList,  
     376                            attributeName) : 
     377                    propertyNodeList[0]; 
     378 
     379            // strip namespace from attribute name for Opera browsers 
     380            if (window.opera && attributeName) { 
     381                var nsDelimiterPos = attributeName.indexOf(":"); 
     382                if (nsDelimiterPos != -1) { 
     383                    attributeName = attributeName.substring(++nsDelimiterPos); 
     384                } 
     385            } 
     386             
     387            // get the property value from the node matching attributeName 
     388            // and attributeValue, eg.: 
     389            // <CssParameter name="stroke"> 
     390            //     <ogc:Literal>red</ogc:Literal> 
     391            // </CssParameter> 
     392            // or: 
     393            // <CssParameter name="stroke">red</CssParameter> 
     394            if (attributeName && attributeValue) { 
     395                propertyNode = this.getNodeWithAttribute(propertyNodeList, 
     396                        attributeName, attributeValue); 
     397                result = this.parseParameter(propertyNode); 
     398            } 
     399 
     400            // get the attribute value and use it as result, eg.: 
     401            // <sld:OnlineResource xlink:href="../img/marker.png"/> 
     402            if (attributeName && !attributeValue) { 
     403                var propertyNode = this.getNodeWithAttribute(propertyNodeList, 
     404                        attributeName); 
     405                result = propertyNode.getAttribute(attributeName);                 
     406            } 
     407             
     408            // get the property value directly or from an ogc:propertyName, 
     409            // ogc:Literal or any other property at the level of the property 
     410            // node, eg.: 
     411            // <sld:Opacity>0.5</sld:Opacity> 
     412            if (!attributeName) { 
     413                var result = this.parseParameter(propertyNode); 
     414            } 
     415        } 
     416         
     417        // adjust the result to be a trimmed string or a number 
     418        if (result) { 
     419            result = OpenLayers.String.trim(result); 
     420            if (!isNaN(result)) { 
     421                result = parseFloat(result); 
     422            } 
     423        } 
     424         
     425        return result; 
     426    }, 
     427     
     428    /** 
     429     * Method: parseParameter 
     430     * parses a property for propertyNames, Literals and textContent and 
     431     * creates the according value string. 
     432     *  
     433     * Parameters: 
     434     * xmlNode - {<DOMElement>} 
     435     *  
     436     * Returns: 
     437     * {String} a string holding a value suitable for OpenLayers.Style.value 
     438     */ 
     439    parseParameter: function(xmlNode) { 
     440        if (!xmlNode) { 
     441            return null; 
     442        } 
     443        var childNodes = xmlNode.childNodes; 
     444        if (!childNodes) { 
     445            return null; 
     446        } 
     447 
     448        var value = new Array(childNodes.length); 
     449        for (var i=0; i<childNodes.length; i++) { 
     450            if (childNodes[i].nodeName.indexOf("Literal") != -1) { 
     451                value[i] = this.getChildValue(childNodes[i]); 
     452            } else 
     453            if (childNodes[i].nodeName.indexOf("propertyName") != -1) { 
     454                value[i] = "${" + this.getChildValue(childNodes[i]) + "}"; 
     455            } else 
     456            if (childNodes[i].nodeType == 3) { 
     457                value[i] = childNodes[i].text || childNodes[i].textContent; 
     458            } 
     459        } 
     460        return value.join(""); 
     461    }, 
     462         
     463    /** 
     464     * Method: getNodeWithAttribute 
     465     * Walks through a list of xml nodes and returns the fist node that has an 
     466     * attribute with the name and optional value specified. 
     467     *  
     468     * Parameters: 
     469     * xmlNodeList    - {Array(<DOMElement>)} list to search 
     470     * attributeName  - {String} name of the attribute to match 
     471     * attributeValue - {String} optional value of the attribute 
     472     */ 
     473    getNodeWithAttribute: function(xmlNodeList, attributeName, attributeValue) { 
     474        for (var i=0; i<xmlNodeList.length; i++) { 
     475            var currentAttributeValue = 
     476                    xmlNodeList[i].getAttribute(attributeName); 
     477            if (currentAttributeValue) { 
     478                if (!attributeValue) { 
     479                    return xmlNodeList[i]; 
     480                } else if (currentAttributeValue == attributeValue) { 
     481                    return xmlNodeList[i]; 
     482                } 
     483            } 
     484        } 
     485    }, 
     486     
     487    CLASS_NAME: "OpenLayers.Format.SLD"  
     488}); 
  • /mnt/d/eclipse/workspace/openlayers/lib/OpenLayers/Rule/FeatureId.js

    old new  
     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/** 
     7 * @requires: OpenLayers/Rule.js 
     8 * 
     9 * Class: OpenLayers.Rule 
     10 *  
     11 * This class represents a ogc:FeatureId Rule, as being used for rule-based SLD 
     12 * styling 
     13 *  
     14 * Inherits from 
     15 * - <OpenLayers.Rule> 
     16 */ 
     17OpenLayers.Rule.FeatureId = OpenLayers.Class(OpenLayers.Rule, { 
     18 
     19    /**  
     20     * APIProperty: fid  
     21     * {Array(<String>)} Feature Ids to evaluate this rule against. To be passed 
     22     * To be passed inside the params object. 
     23     */ 
     24    fids: null, 
     25     
     26    /**  
     27     * Constructor: OpenLayers.Rule.FeatureId 
     28     * Creates an ogc:FeatureId rule. 
     29     * 
     30     * Parameters: 
     31     * options - {Object} An optional object with properties to set on the 
     32     *           rule 
     33     *  
     34     * Returns: 
     35     * {<OpenLayers.Rule>} 
     36     *  
     37     * Inherits from: 
     38     *  - <OpenLayers.Rule> 
     39     */ 
     40    initialize: function(options) { 
     41        this.fids = []; 
     42        OpenLayers.Rule.prototype.initialize.apply( 
     43                this, [options]); 
     44    }, 
     45 
     46    /** 
     47     * APIMethod: evaluate 
     48     * evaluates this rule for a specific feature 
     49     *  
     50     * Parameters: 
     51     * feature - {<OpenLayers.Feature.Vector>} feature to apply the rule to. 
     52     */ 
     53    evaluate: function(feature) { 
     54        for (var i=0; i<this.fids.length; i++) { 
     55            if (feature.fid == this.fids[i]) { 
     56                return true; 
     57            } 
     58        } 
     59        return false; 
     60    }, 
     61     
     62    CLASS_NAME: "OpenLayers.Rule.FeatureId" 
     63}); 
  • /mnt/d/eclipse/workspace/openlayers/lib/OpenLayers/Rule/Comparison.js

    old new  
     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/** 
     7 * @requires: OpenLayers/Rule.js 
     8 * @requires: OpenLayers/Style.js 
     9 * 
     10 * Class: OpenLayers.Rule.Comparison 
     11 *  
     12 * This class represents the comparison rules, as being used for rule-based  
     13 * SLD styling 
     14 *  
     15 * Inherits from 
     16 * - <OpenLayers.Rule> 
     17 */ 
     18OpenLayers.Rule.Comparison = OpenLayers.Class(OpenLayers.Rule, { 
     19 
     20    /** 
     21     * APIProperty: type 
     22     * {<OpenLayers.Rule.Comparison.Type>} type: type of the comparison. 
     23     */ 
     24    type: null, 
     25     
     26    /** 
     27     * APIProperty: property 
     28     * {String} 
     29     * name of the feature attribute to compare 
     30     */ 
     31    property: null, 
     32     
     33    /** 
     34     * APIProperty: value 
     35     * {Number} or {String} 
     36     * comparison value for binary comparisons. In the case of a String, this 
     37     * can be a combination of text and propertyNames in the form 
     38     * "literal ${propertyName}" 
     39     */ 
     40    value: null, 
     41     
     42    /** 
     43     * APIProperty: lowerBoundary 
     44     * {Number} or {String} 
     45     * lower boundary for between comparisons. In the case of a String, this 
     46     * can be a combination of text and propertyNames in the form 
     47     * "literal ${propertyName}" 
     48     */ 
     49    lowerBoundary: null, 
     50     
     51    /** 
     52     * APIProperty: upperBoundary 
     53     * {Number} or {String} 
     54     * upper boundary for between comparisons. In the case of a String, this 
     55     * can be a combination of text and propertyNames in the form 
     56     * "literal ${propertyName}" 
     57     */ 
     58    upperBoundary: null, 
     59 
     60    /**  
     61     * Constructor: OpenLayers.Rule.Logical 
     62     * Creates a logical rule (And, Or, Not). 
     63     * 
     64     * Parameters: 
     65     * params  - {Object} Hash of parameters for this rule: 
     66     *              -  
     67     *              - value:  
     68     * options - {Object} An optional object with properties to set on the 
     69     *           rule 
     70     *  
     71     * Returns: 
     72     * {<OpenLayers.Rule>} 
     73     */ 
     74    initialize: function(options) { 
     75        OpenLayers.Rule.prototype.initialize.apply( 
     76                this, [options]); 
     77    }, 
     78 
     79    /** 
     80     * APIMethod: evaluate 
     81     * evaluates this rule for a specific feature 
     82     *  
     83     * Parameters: 
     84     * feature - {<OpenLayers.Feature.Vector>} feature to apply the rule to. 
     85     *  
     86     * Returns: 
     87     * {boolean} true if the rule applies, false if it does not 
     88     */ 
     89    evaluate: function(feature) { 
     90        var attributes = feature.attributes || feature.data; 
     91        switch(this.type) { 
     92            case OpenLayers.Rule.Comparison.Type.EQUAL_TO: 
     93            case OpenLayers.Rule.Comparison.Type.LESS_THAN: 
     94            case OpenLayers.Rule.Comparison.Type.GREATER_THAN: 
     95            case OpenLayers.Rule.Comparison.Type.LESS_THAN_OR_EQUAL_TO: 
     96            case OpenLayers.Rule.Comparison.Type.GREATER_THAN_OR_EQUAL_TO: 
     97                return this.binaryCompare(feature, this.property, 
     98                        OpenLayers.Style.createLiteral(this.value, feature)); 
     99             
     100            case OpenLayers.Rule.Comparison.Type.BETWEEN: 
     101                var result = 
     102                        attributes[this.property] > 
     103                                OpenLayers.Style.createLiteral( 
     104                                        this.lowerBoundary, feature); 
     105                result = result && 
     106                        attributes[this.property] < 
     107                                OpenLayers.Style.createLiteral( 
     108                                        this.upperBoundary, feature); 
     109                return result; 
     110            case OpenLayers.Rule.Comparison.Type.LIKE: 
     111                var regexp = new RegExp( 
     112                        OpenLayers.Style.createLiteral(this.value, feature), 
     113                                "gi"); 
     114                return regexp.test(attributes[this.property]);  
     115        } 
     116    }, 
     117     
     118    /** 
     119     * Function: value2regExp 
     120     * Converts a the value of this rule into a regular expression string, 
     121     * according to the wildcard characters specified. 
     122     *  
     123     * Parameters: 
     124     * wildCard   - {<Char>} wildcard character in the above value 
     125     * singleChar - {<Char>) single-character wildcard in the above value 
     126     * escape     - {<Char>) escape character in the above value 
     127     *  
     128     * Returns: 
     129     * {String} regular expression string 
     130     */ 
     131    value2regex: function(wildCard, singleChar, escapeChar) { 
     132        if (wildCard == ".") { 
     133            var msg = "'.' is an unsupported wildCard character in "+ 
     134                    "the OpenLayers SLD parser."; 
     135            OpenLayers.Console.error(msg); 
     136            return null; 
     137        } 
     138        var escape = "[^\\"+escapeChar+"]\\"; 
     139        this.value = this.value.replace(new RegExp(escape+singleChar, "g"), 
     140                "."); 
     141        this.value = this.value.replace(new RegExp(escape+wildCard, "g"), 
     142                ".*"); 
     143        this.value = this.value.replace(new RegExp(escapeChar, "g"), "\\"); 
     144         
     145