[OpenLayers-Commits] r11023 - in sandbox/tschaub/xdomain/lib: . OpenLayers/Protocol

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Mon Jan 10 09:29:38 EST 2011


Author: pgiraud
Date: 2011-01-10 06:29:38 -0800 (Mon, 10 Jan 2011)
New Revision: 11023

Added:
   sandbox/tschaub/xdomain/lib/OpenLayers/Protocol/FilterSerializer.js
Modified:
   sandbox/tschaub/xdomain/lib/OpenLayers.js
   sandbox/tschaub/xdomain/lib/OpenLayers/Protocol/HTTP.js
   sandbox/tschaub/xdomain/lib/OpenLayers/Protocol/Script.js
Log:
Adding a OL.Protocol.FilterSerializer mixin to handle the filterToParams in the HTTP and Script protocols

Added: sandbox/tschaub/xdomain/lib/OpenLayers/Protocol/FilterSerializer.js
===================================================================
--- sandbox/tschaub/xdomain/lib/OpenLayers/Protocol/FilterSerializer.js	                        (rev 0)
+++ sandbox/tschaub/xdomain/lib/OpenLayers/Protocol/FilterSerializer.js	2011-01-10 14:29:38 UTC (rev 11023)
@@ -0,0 +1,126 @@
+/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for 
+ * full list of contributors). Published under the Clear BSD license.  
+ * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
+ * full text of the license. */
+
+/**
+ * Class: OpenLayers.Protocol.FilterSerializer
+ * A mixin for protocols that need to serialize filter parameters.
+ */
+OpenLayers.Protocol.FilterSerializer = {
+
+    /**
+     * Method: filterToParams
+     * Convert an <OpenLayers.Filter> object to parameters.
+     *
+     * Parameters:
+     * filter - {OpenLayers.Filter} filter to convert.
+     * params - {Object} The parameters object.
+     *
+     * Returns:
+     * {Object} The resulting parameters object.
+     */
+    filterToParams: function(filter, params) {
+        params = params || {};
+        var className = filter.CLASS_NAME;
+        var filterType = className.substring(className.lastIndexOf(".") + 1);
+        switch(filterType) {
+            case "Spatial":
+                switch(filter.type) {
+                    case OpenLayers.Filter.Spatial.BBOX:
+                        params.bbox = filter.value.toArray();
+                        break;
+                    case OpenLayers.Filter.Spatial.DWITHIN:
+                        params.tolerance = filter.distance;
+                        // no break here
+                    case OpenLayers.Filter.Spatial.WITHIN:
+                        params.lon = filter.value.x;
+                        params.lat = filter.value.y;
+                        break;
+                    default:
+                        OpenLayers.Console.warn(
+                            "Unknown spatial filter type " + filter.type);
+                }
+                break;
+            case "Comparison":
+                var op = OpenLayers.Protocol.HTTP.COMP_TYPE_TO_OP_STR[filter.type];
+                if(op !== undefined) {
+                    var value = filter.value;
+                    if(filter.type == OpenLayers.Filter.Comparison.LIKE) {
+                        value = this.regex2value(value);
+                        if(this.wildcarded) {
+                            value = "%" + value + "%";
+                        }
+                    }
+                    params[filter.property + "__" + op] = value;
+                    params.queryable = params.queryable || [];
+                    params.queryable.push(filter.property);
+                } else {
+                    OpenLayers.Console.warn(
+                        "Unknown comparison filter type " + filter.type);
+                }
+                break;
+            case "Logical":
+                if(filter.type === OpenLayers.Filter.Logical.AND) {
+                    for(var i=0,len=filter.filters.length; i<len; i++) {
+                        params = this.filterToParams(filter.filters[i], params);
+                    }
+                } else {
+                    OpenLayers.Console.warn(
+                        "Unsupported logical filter type " + filter.type);
+                }
+                break;
+            default:
+                OpenLayers.Console.warn("Unknown filter type " + filterType);
+        }
+        return params;
+    },
+
+    /**
+     * Method: regex2value
+     * Convert the value from a regular expression string to a LIKE/ILIKE
+     * string known to the web service.
+     *
+     * Parameters:
+     * value - {String} The regex string.
+     *
+     * Returns:
+     * {String} The converted string.
+     */
+    regex2value: function(value) {
+
+        // highly sensitive!! Do not change this without running the
+        // Protocol/HTTP.html unit tests
+
+        // convert % to \%
+        value = value.replace(/%/g, "\\%");
+
+        // convert \\. to \\_ (\\.* occurences converted later)
+        value = value.replace(/\\\\\.(\*)?/g, function($0, $1) {
+            return $1 ? $0 : "\\\\_";
+        });
+
+        // convert \\.* to \\%
+        value = value.replace(/\\\\\.\*/g, "\\\\%");
+
+        // convert . to _ (\. and .* occurences converted later)
+        value = value.replace(/(\\)?\.(\*)?/g, function($0, $1, $2) {
+            return $1 || $2 ? $0 : "_";
+        });
+
+        // convert .* to % (\.* occurnces converted later)
+        value = value.replace(/(\\)?\.\*/g, function($0, $1) {
+            return $1 ? $0 : "%";
+        });
+
+        // convert \. to .
+        value = value.replace(/\\\./g, ".");
+
+        // replace \* with * (watching out for \\*)
+        value = value.replace(/(\\)?\\\*/g, function($0, $1) {
+            return $1 ? $0 : "*";
+        });
+
+        return value;
+    }
+};

Modified: sandbox/tschaub/xdomain/lib/OpenLayers/Protocol/HTTP.js
===================================================================
--- sandbox/tschaub/xdomain/lib/OpenLayers/Protocol/HTTP.js	2011-01-10 13:55:54 UTC (rev 11022)
+++ sandbox/tschaub/xdomain/lib/OpenLayers/Protocol/HTTP.js	2011-01-10 14:29:38 UTC (rev 11023)
@@ -99,6 +99,10 @@
         this.params = {};
         this.headers = {};
         OpenLayers.Protocol.prototype.initialize.apply(this, arguments);
+
+        if (OpenLayers.Protocol.FilterSerializer) {
+            OpenLayers.Util.extend(this, OpenLayers.Protocol.FilterSerializer);
+        }
     },
     
     /**
@@ -139,6 +143,7 @@
         options.params = OpenLayers.Util.applyDefaults(
             options.params, this.options.params);
         if(options.filter) {
+            // requires the OpenLayers.Protocol.FilterSerializer mixin
             options.params = this.filterToParams(
                 options.filter, options.params);
         }
@@ -180,121 +185,6 @@
     },
 
     /**
-     * Method: filterToParams
-     * Convert an <OpenLayers.Filter> object to parameters.
-     *
-     * Parameters:
-     * filter - {OpenLayers.Filter} filter to convert.
-     * params - {Object} The parameters object.
-     *
-     * Returns:
-     * {Object} The resulting parameters object.
-     */
-    filterToParams: function(filter, params) {
-        params = params || {};
-        var className = filter.CLASS_NAME;
-        var filterType = className.substring(className.lastIndexOf(".") + 1);
-        switch(filterType) {
-            case "Spatial":
-                switch(filter.type) {
-                    case OpenLayers.Filter.Spatial.BBOX:
-                        params.bbox = filter.value.toArray();
-                        break;
-                    case OpenLayers.Filter.Spatial.DWITHIN:
-                        params.tolerance = filter.distance;
-                        // no break here
-                    case OpenLayers.Filter.Spatial.WITHIN:
-                        params.lon = filter.value.x;
-                        params.lat = filter.value.y;
-                        break;
-                    default:
-                        OpenLayers.Console.warn(
-                            "Unknown spatial filter type " + filter.type);
-                }
-                break;
-            case "Comparison":
-                var op = OpenLayers.Protocol.HTTP.COMP_TYPE_TO_OP_STR[filter.type];
-                if(op !== undefined) {
-                    var value = filter.value;
-                    if(filter.type == OpenLayers.Filter.Comparison.LIKE) {
-                        value = this.regex2value(value);
-                        if(this.wildcarded) {
-                            value = "%" + value + "%";
-                        }
-                    }
-                    params[filter.property + "__" + op] = value;
-                    params.queryable = params.queryable || [];
-                    params.queryable.push(filter.property);
-                } else {
-                    OpenLayers.Console.warn(
-                        "Unknown comparison filter type " + filter.type);
-                }
-                break;
-            case "Logical":
-                if(filter.type === OpenLayers.Filter.Logical.AND) {
-                    for(var i=0,len=filter.filters.length; i<len; i++) {
-                        params = this.filterToParams(filter.filters[i], params);
-                    }
-                } else {
-                    OpenLayers.Console.warn(
-                        "Unsupported logical filter type " + filter.type);
-                }
-                break;
-            default:
-                OpenLayers.Console.warn("Unknown filter type " + filterType);
-        }
-        return params;
-    },
-
-    /**
-     * Method: regex2value
-     * Convert the value from a regular expression string to a LIKE/ILIKE
-     * string known to the web service.
-     *
-     * Parameters:
-     * value - {String} The regex string.
-     *
-     * Returns:
-     * {String} The converted string.
-     */
-    regex2value: function(value) {
-
-        // highly sensitive!! Do not change this without running the
-        // Protocol/HTTP.html unit tests
-
-        // convert % to \%
-        value = value.replace(/%/g, "\\%");
-
-        // convert \\. to \\_ (\\.* occurences converted later)
-        value = value.replace(/\\\\\.(\*)?/g, function($0, $1) {
-            return $1 ? $0 : "\\\\_";
-        });
-
-        // convert \\.* to \\%
-        value = value.replace(/\\\\\.\*/g, "\\\\%");
-
-        // convert . to _ (\. and .* occurences converted later)
-        value = value.replace(/(\\)?\.(\*)?/g, function($0, $1, $2) {
-            return $1 || $2 ? $0 : "_";
-        });
-
-        // convert .* to % (\.* occurnces converted later)
-        value = value.replace(/(\\)?\.\*/g, function($0, $1) {
-            return $1 ? $0 : "%";
-        });
-
-        // convert \. to .
-        value = value.replace(/\\\./g, ".");
-
-        // replace \* with * (watching out for \\*)
-        value = value.replace(/(\\)?\\\*/g, function($0, $1) {
-            return $1 ? $0 : "*";
-        });
-
-        return value;
-    },
-
-    /**
      * APIMethod: create
      * Construct a request for writing newly created features.
      *

Modified: sandbox/tschaub/xdomain/lib/OpenLayers/Protocol/Script.js
===================================================================
--- sandbox/tschaub/xdomain/lib/OpenLayers/Protocol/Script.js	2011-01-10 13:55:54 UTC (rev 11022)
+++ sandbox/tschaub/xdomain/lib/OpenLayers/Protocol/Script.js	2011-01-10 14:29:38 UTC (rev 11023)
@@ -113,6 +113,9 @@
         if (!this.format) {
             this.format = new OpenLayers.Format.GeoJSON();
         }
+        if (OpenLayers.Protocol.FilterSerializer) {
+            OpenLayers.Util.extend(this, OpenLayers.Protocol.FilterSerializer);
+        }
     },
     
     /**
@@ -142,6 +145,7 @@
             options.params, this.options.params
         );
         if (options.filter) {
+            // requires the OpenLayers.Protocol.FilterSerializer mixin
             options.params = this.filterToParams(
                 options.filter, options.params
             );
@@ -159,23 +163,6 @@
         return response;
     },
     
-    /**
-     * Method: filterToParams
-     * Convert an <OpenLayers.Filter> object to parameters.
-     *
-     * Parameters:
-     * filter - {OpenLayers.Filter} filter to convert.
-     * params - {Object} The parameters object.
-     *
-     * Returns:
-     * {Object} The resulting parameters object.
-     */
-    filterToParams: function(filter, params) {
-        return OpenLayers.Protocol.HTTP.prototype.filterToParams.apply(
-            OpenLayers.Protocol.HTTP.prototype, arguments
-        );
-    },
-    
     /** 
      * Method: createRequest
      * Issues a request for features by creating injecting a script in the 

Modified: sandbox/tschaub/xdomain/lib/OpenLayers.js
===================================================================
--- sandbox/tschaub/xdomain/lib/OpenLayers.js	2011-01-10 13:55:54 UTC (rev 11022)
+++ sandbox/tschaub/xdomain/lib/OpenLayers.js	2011-01-10 14:29:38 UTC (rev 11023)
@@ -214,6 +214,7 @@
             "OpenLayers/Filter/Comparison.js",
             "OpenLayers/Filter/Spatial.js",
             "OpenLayers/Protocol.js",
+            "OpenLayers/Protocol/FilterSerializer.js",
             "OpenLayers/Protocol/HTTP.js",
             "OpenLayers/Protocol/Script.js",
             "OpenLayers/Protocol/SQL.js",



More information about the Commits mailing list