[mapguide-commits] r4673 - in sandbox/rfc71: mapviewerjava mapviewernet mapviewerphp viewerfiles

svn_mapguide at osgeo.org svn_mapguide at osgeo.org
Thu Mar 18 04:06:20 EDT 2010


Author: jng
Date: 2010-03-18 04:06:18 -0400 (Thu, 18 Mar 2010)
New Revision: 4673

Modified:
   sandbox/rfc71/mapviewerjava/getselectedfeatures.jsp
   sandbox/rfc71/mapviewernet/getselectedfeatures.aspx
   sandbox/rfc71/mapviewerphp/getselectedfeatures.php
   sandbox/rfc71/viewerfiles/ajaxmappane.templ
Log:
#1053: Changes to support locale

- Pass a LOCALE parameter to getselectedfeatures.xxx
- Format feature reader values using this locale (from LOCALE, or GetDefaultLocale() if there is none). For .net it forbids the use of neutral cultures, so in the case of a neutral culture (eg. "en" instead of "en-US") fall back to CultureInfo.InvariantCulture
- Add JsonEscape() all 3 impls. This calls EscapeForHtml() and does additional escaping of backslashes.

Modified: sandbox/rfc71/mapviewerjava/getselectedfeatures.jsp
===================================================================
--- sandbox/rfc71/mapviewerjava/getselectedfeatures.jsp	2010-03-17 20:00:50 UTC (rev 4672)
+++ sandbox/rfc71/mapviewerjava/getselectedfeatures.jsp	2010-03-18 08:06:18 UTC (rev 4673)
@@ -18,7 +18,8 @@
 
     String mapName;
     String sessionId;
-    String locale;
+    String localeCode;
+    Locale locale;
 
     class SelectionSet
     {
@@ -143,31 +144,31 @@
         switch(propType)
         {
             case MgPropertyType.Boolean:
-                value = reader.GetBoolean(propName) + "";
+                value = String.format(locale, "%s", reader.GetBoolean(propName));
                 break;
             case MgPropertyType.Byte:
-                value = reader.GetByte(propName) + "";
+                value = String.format(locale, "%d", reader.GetByte(propName));
                 break; 
             case MgPropertyType.DateTime:
-                value = GetDateTimeString(reader.GetDateTime(propName));
+                value = GetDateTimeString(reader.GetDateTime(propName)); // yyyy-mm-dd is enforced regardless of locale
                 break;
             case MgPropertyType.Single:
-                value = reader.GetSingle(propName) + "";
+                value = String.format(locale, "%f", reader.GetSingle(propName));
                 break;
             case MgPropertyType.Double:
-                value = reader.GetDouble(propName) + "";
+                value = String.format(locale, "%f", reader.GetDouble(propName));
                 break;
             case MgPropertyType.Int16:
-                value = reader.GetInt16(propName) + "";
+                value = String.format(locale, "%d", reader.GetInt16(propName));
                 break;
             case MgPropertyType.Int32:
-                value = reader.GetInt32(propName) + "";
+                value = String.format(locale, "%d", reader.GetInt32(propName));
                 break;
             case MgPropertyType.Int64:
-                value = reader.GetInt64(propName) + "";
+                value = String.format(locale, "%d", reader.GetInt64(propName));
                 break;
             case MgPropertyType.String:
-                value = EscapeForHtml(reader.GetString(propName));
+                value = JsonEscape(reader.GetString(propName)); // string content is arbitrary
                 break;
             default: //NOT PRESENTABLE IN PROPERTY GRID
                 value = "";
@@ -178,7 +179,6 @@
 
     static String GetDateTimeString(MgDateTime value) throws MgException
     {
-        //TODO: Use current culture to determine date format?
         return value.GetYear() + "-" + value.GetMonth() + "-" + value.GetDay();
     }
     
@@ -186,13 +186,16 @@
     {
         mapName = GetParameter(request, "MAPNAME");
         sessionId = GetParameter(request, "SESSION");
-        locale = GetParameter(request, "LOCALE");
-        if (locale == null || locale.length() == 0)
-            locale = GetDefaultLocale();
+        localeCode = GetParameter(request, "LOCALE");
     }
     
-    static String JsonifyError(Exception ex, String locale)
+    String JsonEscape(String str)
     {
+        return EscapeForHtml(str).replace("\\", "\\\\");
+    }
+    
+    String JsonifyError(Exception ex)
+    {
         if (ex == null)
             return "";
         /*
@@ -209,19 +212,19 @@
         String msg = ex.getMessage();
         if (msg == null || msg.length() == 0)
         {
-            msg = MgLocalizer.GetString("SERVERERROR", locale);
+            msg = MgLocalizer.GetString("SERVERERROR", localeCode);
         }
         //Begin response
         sb.append("{\"Error\":{");
         //Exception message
-        sb.append("\"Message\":\"" + msg + "\",");
+        sb.append("\"Message\":\"" + JsonEscape(msg) + "\",");
         StringBuffer strace = new StringBuffer();
         StackTraceElement[] st = ex.getStackTrace();
         for (int i = 0; i < st.length; i++)
         {
             strace.append(st[i].getClassName() + "." + st[i].getMethodName() + "(" + st[i].getLineNumber() + ")\\n");
         }
-        sb.append("\"StackTrace\":\"" + strace.toString() + "\"");
+        sb.append("\"StackTrace\":\"" + JsonEscape(strace.toString()) + "\"");
         //End response
         sb.append("}}");
         return sb.toString();
@@ -307,9 +310,14 @@
 <%
     mapName = "";
     sessionId = "";
-    locale = "en";
+    localeCode = "";
 
     GetParameters(request);
+    
+    if (null == localeCode || localeCode.length() == 0)
+        localeCode = GetDefaultLocale();
+        
+    locale = new Locale(localeCode);
 
     try
     {
@@ -422,12 +430,12 @@
     {
         response.addHeader("Content-Type", "application/json");
         response.addHeader("X-JSON", "true");
-        response.getWriter().write(JsonifyError(ex, locale));
+        response.getWriter().write(JsonifyError(ex));
     }
     catch (Exception ex)
     {
         response.addHeader("Content-Type", "application/json");
         response.addHeader("X-JSON", "true");
-        response.getWriter().write(JsonifyError(ex, locale));
+        response.getWriter().write(JsonifyError(ex));
     }
 %>
\ No newline at end of file

Modified: sandbox/rfc71/mapviewernet/getselectedfeatures.aspx
===================================================================
--- sandbox/rfc71/mapviewernet/getselectedfeatures.aspx	2010-03-17 20:00:50 UTC (rev 4672)
+++ sandbox/rfc71/mapviewernet/getselectedfeatures.aspx	2010-03-18 08:06:18 UTC (rev 4673)
@@ -79,6 +79,8 @@
     
     String mapName;
     String sessionId;
+    String locale;
+    CultureInfo culture;
 
     static NameValueCollection GetLayerPropertyMappings(MgResourceService resSvc, MgLayerBase layer)
     {
@@ -103,31 +105,31 @@
         switch(propType)
         {
             case MgPropertyType.Boolean:
-                value = reader.GetBoolean(propName).ToString();
+                value = String.Format(culture, "{0}", reader.GetBoolean(propName));
                 break;
             case MgPropertyType.Byte:
-                value = reader.GetByte(propName).ToString();
+                value = String.Format(culture, "{0:d}", reader.GetByte(propName));
                 break; 
             case MgPropertyType.DateTime:
-                value = GetDateTimeString(reader.GetDateTime(propName));
+                value = GetDateTimeString(reader.GetDateTime(propName)); // yyyy-mm-dd is enforced regardless of locale
                 break;
             case MgPropertyType.Single:
-                value = reader.GetSingle(propName).ToString();
+                value = String.Format(culture, "{0:f}", reader.GetSingle(propName));
                 break;
             case MgPropertyType.Double:
-                value = reader.GetDouble(propName).ToString();
+                value = String.Format(culture, "{0:f}", reader.GetDouble(propName));
                 break;
             case MgPropertyType.Int16:
-                value = reader.GetInt16(propName).ToString();
+                value = String.Format(culture, "{0:d}", reader.GetInt16(propName));
                 break;
             case MgPropertyType.Int32:
-                value = reader.GetInt32(propName).ToString();
+                value = String.Format(culture, "{0:d}", reader.GetInt32(propName));
                 break;
             case MgPropertyType.Int64:
-                value = reader.GetInt64(propName).ToString();
+                value = String.Format(culture, "{0:d}", reader.GetInt64(propName));
                 break;
             case MgPropertyType.String:
-                value = EscapeForHtml(reader.GetString(propName));
+                value = JsonEscape(reader.GetString(propName)); //String content is arbitrary
                 break;
             default: //NOT PRESENTABLE IN PROPERTY GRID
                 value = "";
@@ -138,7 +140,6 @@
 
     static String GetDateTimeString(MgDateTime value)
     {
-        //TODO: Use current culture to determine date format?
         return String.Format("{0}-{1}-{2}", value.Year, value.Month, value.Day);
     }
     
@@ -146,6 +147,7 @@
     {
         mapName = param["MAPNAME"];
         sessionId = param["SESSION"];
+        locale = param["LOCALE"];
     }
 
     void GetRequestParameters()
@@ -156,18 +158,15 @@
             GetParameters(Request.QueryString);
     }
     
-    static String JsonifyError(Exception ex)
+    String JsonEscape(String str)
     {
-        /*
-        {
-            "Error" : {
-                "Message" : <exception-message>,
-                "StackTrace" : <exception-stack-trace>
-            }
-        }
-        */
-        return String.Format("{\"Error\":{\"Message\":\"{0}\",\"StackTrace\":\"{1}\"}}", ex.Message, ex.StackTrace);
+        return EscapeForHtml(str).Replace("\\", "\\\\");
     }
+    
+    String JsonifyError(Exception ex)
+    {
+        return "{\"Error\":{\"Message\":\"" + JsonEscape(ex.Message) + "\",\"StackTrace\":\"" + JsonEscape(ex.StackTrace) + "\"}}";
+    }
 
     static String GetJson(SelectionSet set)
     {
@@ -251,8 +250,20 @@
 
     mapName = "";
     sessionId = "";
+    locale = "";
 
     GetRequestParameters();
+    
+    if (String.IsNullOrEmpty(locale))
+        locale = GetDefaultLocale();
+        
+    culture = CultureInfo.GetCultureInfo(locale);
+    
+    //HACK: The default locale (en) resolves to a neutral culture, .net forbids the use of 
+    //neutral cultures for formatting purposes, so default to InvariantCulture if the resolved
+    //culture is not neutral.
+    if (culture.IsNeutralCulture)
+        culture = CultureInfo.InvariantCulture; //We need a non-neutral culture
 
     try
     {

Modified: sandbox/rfc71/mapviewerphp/getselectedfeatures.php
===================================================================
--- sandbox/rfc71/mapviewerphp/getselectedfeatures.php	2010-03-17 20:00:50 UTC (rev 4672)
+++ sandbox/rfc71/mapviewerphp/getselectedfeatures.php	2010-03-18 08:06:18 UTC (rev 4673)
@@ -100,10 +100,14 @@
 
     $mapName = "";
     $sessionId = "";
-    //$selText = "";
-    $queryInfo = false;
+    $locale = "";
 
     GetRequestParameters();
+    
+    if (null == $locale || strlen($locale) == 0)
+        $locale = GetDefaultLocale();
+        
+    setlocale(LC_ALL, $locale);
    
 	try
 	{
@@ -127,9 +131,6 @@
 		// Create the selection set
         $selection = new MgSelection($map);
         $selection->Open($resourceSrvc, $mapName);
-        //if($selText != "") {
-        //    $selection->FromXml($selText);
-        //}
 		
 		$layers = $selection->GetLayers();
         if($layers != null)
@@ -344,39 +345,39 @@
             break;
         case MgPropertyType::Boolean:
             $tStr = "Boolean";
-            $val = $fr->GetBoolean($propName);
+            $val = sprintf("%s", $fr->GetBoolean($propName));
             break;
         case MgPropertyType::Byte:
             $tStr = "Byte";
-            $val = $fr->GetByte($propName);
+            $val = sprintf("%d", $fr->GetByte($propName));
             break;
         case MgPropertyType::DateTime:
             $tStr = "DateTime";
-            $val = printDateTime($fr->GetDateTime($propName));
+            $val = printDateTime($fr->GetDateTime($propName)); // yyyy-mm-dd is enforced regardless of locale
             break;
         case MgPropertyType::Single:
             $tStr = "Single";
-            $val = $fr->GetSingle($propName);
+            $val = sprintf("%f", $fr->GetSingle($propName));
             break;
         case MgPropertyType::Double:
             $tStr = "Double";
-            $val = $fr->GetDouble($propName);
+            $val = sprintf("%f", $fr->GetDouble($propName));
             break;
         case MgPropertyType::Int16:
             $tStr = "Int16";
-            $val = $fr->GetInt16($propName);
+            $val = sprintf("%d", $fr->GetInt16($propName));
             break;
         case MgPropertyType::Int32:
             $tStr = "Int32";
-            $val = $fr->GetInt32($propName);
+            $val = sprintf("%d", $fr->GetInt32($propName));
             break;
         case MgPropertyType::Int64:
             $tStr = "Int64";
-            $val = $fr->GetInt64($propName);
+            $val = sprintf("%d", $fr->GetInt64($propName));
             break;
         case MgPropertyType::String:
             $tStr = "String";
-            $val = addslashes($fr->GetString($propName));
+            $val = JsonEscape($fr->GetString($propName)); //String content is arbitrary
             break;
         case MgPropertyType::Blob: //NOT PRESENTABLE IN PROPERTY GRID
             $tStr = "BLOB";
@@ -430,13 +431,18 @@
 
 function GetParameters($params)
 {
-    global $mapName, $sessionId, $selText, $queryInfo;
+    global $mapName, $sessionId, $locale;
 
     $mapName = $params['MAPNAME'];
     $sessionId = $params['SESSION'];
-	//$selText = UnescapeMagicQuotes($params['selection']);
+    $locale = $params['LOCALE'];
 }
 
+function JsonEscape($str)
+{
+    return str_replace("\\", "\\\\", EscapeForHtml($str));
+}
+
 function JsonifyError($msg, $st)
 {
     /*
@@ -447,7 +453,9 @@
         }
     }
     */
-    $json = "{\"Error\":{\"Message\":\"$msg\",\"StackTrace\":\"$st\"}}";
+    $emsg = JsonEscape($msg);
+    $est = JsonEscape($st);
+    $json = "{\"Error\":{\"Message\":\"$emsg\",\"StackTrace\":\"$est\"}}";
     return $json;
 }
 

Modified: sandbox/rfc71/viewerfiles/ajaxmappane.templ
===================================================================
--- sandbox/rfc71/viewerfiles/ajaxmappane.templ	2010-03-17 20:00:50 UTC (rev 4672)
+++ sandbox/rfc71/viewerfiles/ajaxmappane.templ	2010-03-18 08:06:18 UTC (rev 4673)
@@ -3830,7 +3830,7 @@
     var mapname = GetMapName();
     var session = GetSessionId();
     var selRequest = CreateRequestHandler();
-    var reqParams = "MAPNAME="+mapname+"&SESSION="+session+"&SEQ="+Math.random();
+    var reqParams = "MAPNAME="+mapname+"&SESSION="+session+"&LOCALE="+locale+"&SEQ="+Math.random();
     selRequest.open("POST", featureRequestUrl, false);
     selRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     selRequest.send(reqParams);



More information about the mapguide-commits mailing list