Permalink
Browse files

UTFGRID: implement escaping of item and data values

  • Loading branch information...
rouault committed Mar 24, 2014
1 parent f4b969d commit 673ad22cd4227a8a1f96ab6cf6a044c62549e827
Showing with 107 additions and 7 deletions.
  1. +2 −0 mapserver.h
  2. +72 −1 mapstring.c
  3. +13 −3 maputfgrid.cpp
  4. +19 −2 maputil.c
  5. +1 −1 msautotest
View
@@ -2158,6 +2158,7 @@ void msPopulateTextSymbolForLabelAndString(textSymbolObj *ts, labelObj *l, char
MS_DLL_EXPORT int msEncodeChar(const char);
MS_DLL_EXPORT char *msEncodeUrlExcept(const char*, const char);
MS_DLL_EXPORT char *msEncodeUrl(const char*);
MS_DLL_EXPORT char *msEscapeJSonString(const char* pszJSonString);
MS_DLL_EXPORT char *msEncodeHTMLEntities(const char *string);
MS_DLL_EXPORT void msDecodeHTMLEntities(const char *string);
MS_DLL_EXPORT int msIsXMLTagValid(const char *string);
@@ -2580,6 +2581,7 @@ void msPopulateTextSymbolForLabelAndString(textSymbolObj *ts, labelObj *l, char
MS_DLL_EXPORT int msGetLabelStatus(mapObj *map, layerObj *layer, shapeObj *shape, labelObj *lbl);
MS_DLL_EXPORT int msAdjustImage(rectObj rect, int *width, int *height);
MS_DLL_EXPORT char *msEvalTextExpression(expressionObj *expr, shapeObj *shape);
MS_DLL_EXPORT char *msEvalTextExpressionJSonEscape(expressionObj *expr, shapeObj *shape);
MS_DLL_EXPORT double msAdjustExtent(rectObj *rect, int width, int height);
MS_DLL_EXPORT int msConstrainExtent(rectObj *bounds, rectObj *rect, double overlay);
MS_DLL_EXPORT int *msGetLayersIndexByGroup(mapObj *map, char *groupname, int *nCount);
View
@@ -1125,7 +1125,7 @@ char *msEncodeUrl(const char *data)
char *msEncodeUrlExcept(const char *data, const char except)
{
char *hex = "0123456789ABCDEF";
static const char *hex = "0123456789ABCDEF";
const char *i;
char *j, *code;
int inc;
@@ -1155,6 +1155,77 @@ char *msEncodeUrlExcept(const char *data, const char except)
return code;
}
/************************************************************************/
/* msEscapeJSonString() */
/************************************************************************/
/* The input (and output) string are not supposed to start/end with double */
/* quote characters. It is the responsibility of the caller to do that. */
char* msEscapeJSonString(const char* pszJSonString)
{
/* Worst case is one character to become \uABCD so 6 characters */
char* pszRet;
int i = 0, j = 0;
static const char* pszHex = "0123456789ABCDEF";
pszRet = (char*) msSmallMalloc(strlen(pszJSonString) * 6 + 1);
/* From http://www.json.org/ */
for(i = 0; pszJSonString[i] != '\0'; i++)
{
unsigned char ch = pszJSonString[i];
if( ch == '\b' )
{
pszRet[j++] = '\\';
pszRet[j++] = 'b';
}
else if( ch == '\f' )
{
pszRet[j++] = '\\';
pszRet[j++] = 'f';
}
else if( ch == '\n' )
{
pszRet[j++] = '\\';
pszRet[j++] = 'n';
}
else if( ch == '\r' )
{
pszRet[j++] = '\\';
pszRet[j++] = 'r';
}
else if( ch == '\t' )
{
pszRet[j++] = '\\';
pszRet[j++] = 't';
}
else if( ch < 32 )
{
pszRet[j++] = '\\';
pszRet[j++] = 'u';
pszRet[j++] = '0';
pszRet[j++] = '0';
pszRet[j++] = pszHex[ch / 16];
pszRet[j++] = pszHex[ch % 16];
}
else if( ch == '"' )
{
pszRet[j++] = '\\';
pszRet[j++] = '"';
}
else if( ch == '\\' )
{
pszRet[j++] = '\\';
pszRet[j++] = '\\';
}
else
{
pszRet[j++] = ch;
}
}
pszRet[j] = '\0';
return pszRet;
}
/* msEncodeHTMLEntities()
**
** Return a copy of string after replacing some problematic chars with their
View
@@ -267,7 +267,7 @@ band_type addToTable(UTFGridRenderer *r, shapeObj *p)
utfvalue = encodeForRendering(utfvalue);
/* Datas are added to the table */
r->data->table[r->data->counter].datavalues = msEvalTextExpression(&r->utflayer->utfdata, p);
r->data->table[r->data->counter].datavalues = msEvalTextExpressionJSonEscape(&r->utflayer->utfdata, p);
/* If UTFITEM is set in the mapfiles we add its value to the table */
if(r->useutfitem)
@@ -366,6 +366,7 @@ int utfgridSaveImage(imageObj *img, mapObj *map, FILE *fp, outputFormatObj *form
{
int row, col, i, imgheight, imgwidth;
band_type pixelid;
char* pszEscaped;
UTFGridRenderer *renderer = UTFGRID_RENDERER(img);
@@ -412,7 +413,11 @@ int utfgridSaveImage(imageObj *img, mapObj *map, FILE *fp, outputFormatObj *form
fprintf(fp,",");
if(renderer->useutfitem)
fprintf(fp,"\"%s\"", renderer->data->table[i].itemvalue);
{
pszEscaped = msEscapeJSonString(renderer->data->table[i].itemvalue);
fprintf(fp,"\"%s\"", pszEscaped);
msFree(pszEscaped);
}
/* If no UTFITEM specified use the serial ID as the key */
else
fprintf(fp,"\"%i\"", renderer->data->table[i].serialid);
@@ -427,10 +432,15 @@ int utfgridSaveImage(imageObj *img, mapObj *map, FILE *fp, outputFormatObj *form
fprintf(fp,",");
if(renderer->useutfitem)
fprintf(fp,"\"%s\":", renderer->data->table[i].itemvalue);
{
pszEscaped = msEscapeJSonString(renderer->data->table[i].itemvalue);
fprintf(fp,"\"%s\":", pszEscaped);
msFree(pszEscaped);
}
/* If no UTFITEM specified use the serial ID as the key */
else
fprintf(fp,"\"%i\":", renderer->data->table[i].serialid);
fprintf(fp,"%s", renderer->data->table[i].datavalues);
}
}
View
@@ -611,7 +611,8 @@ int msShapeGetClass(layerObj *layer, mapObj *map, shapeObj *shape, int *classgro
return(-1); /* no match */
}
char *msEvalTextExpression(expressionObj *expr, shapeObj *shape)
static
char *msEvalTextExpressionInternal(expressionObj *expr, shapeObj *shape, int bJSonEscape)
{
char *result=NULL;
@@ -620,6 +621,7 @@ char *msEvalTextExpression(expressionObj *expr, shapeObj *shape)
switch(expr->type) {
case(MS_STRING): {
char *target=NULL;
char *pszEscaped;
tokenListNodeObjPtr node=NULL;
tokenListNodeObjPtr nextNode=NULL;
@@ -632,7 +634,12 @@ char *msEvalTextExpression(expressionObj *expr, shapeObj *shape)
if(node->token == MS_TOKEN_BINDING_DOUBLE || node->token == MS_TOKEN_BINDING_INTEGER || node->token == MS_TOKEN_BINDING_STRING || node->token == MS_TOKEN_BINDING_TIME) {
target = (char *) msSmallMalloc(strlen(node->tokenval.bindval.item) + 3);
sprintf(target, "[%s]", node->tokenval.bindval.item);
result = msReplaceSubstring(result, target, shape->values[node->tokenval.bindval.index]);
if( bJSonEscape )
pszEscaped = msEscapeJSonString(shape->values[node->tokenval.bindval.index]);
else
pszEscaped = msStrdup(shape->values[node->tokenval.bindval.index]);
result = msReplaceSubstring(result, target, pszEscaped);
msFree(pszEscaped);
msFree(target);
}
node = nextNode;
@@ -673,6 +680,16 @@ char *msEvalTextExpression(expressionObj *expr, shapeObj *shape)
return result;
}
char *msEvalTextExpressionJSonEscape(expressionObj *expr, shapeObj *shape)
{
return msEvalTextExpressionInternal(expr, shape, TRUE);
}
char *msEvalTextExpression(expressionObj *expr, shapeObj *shape)
{
return msEvalTextExpressionInternal(expr, shape, FALSE);
}
char* msShapeGetLabelAnnotation(layerObj *layer, shapeObj *shape, labelObj *lbl) {
assert(shape && lbl);
if(lbl->text.string) {
Submodule msautotest updated from a65ae1 to 284308

0 comments on commit 673ad22

Please sign in to comment.