myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hno...@apache.org
Subject [myfaces-tobago] branch master updated: TOBAGO-1892 JsonUtils doesn't encode quotation marks
Date Wed, 18 Apr 2018 12:28:25 GMT
This is an automated email from the ASF dual-hosted git repository.

hnoeth pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces-tobago.git


The following commit(s) were added to refs/heads/master by this push:
     new 88f4f04  TOBAGO-1892 JsonUtils doesn't encode quotation marks
88f4f04 is described below

commit 88f4f04c67508c10d491c5c475d0b9eef0270e14
Author: Henning Noeth <hnoeth@apache.org>
AuthorDate: Wed Apr 18 14:28:17 2018 +0200

    TOBAGO-1892 JsonUtils doesn't encode quotation marks
    
    * Encode methods have now an escape attribute. If true, quotation marks will be escaped.
    * escape attribute is
    ** 'false' for SheetRenderer
    ** 'true' for SuggestRenderer
    * added test for JsonUtils.encode string array
---
 .../internal/renderkit/renderer/SheetRenderer.java |  2 +-
 .../renderkit/renderer/SuggestRenderer.java        |  4 +--
 .../myfaces/tobago/internal/util/JsonUtils.java    | 34 +++++++++++++++-------
 .../tobago/internal/util/JsonUtilsUnitTest.java    |  4 ++-
 4 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/SheetRenderer.java
b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/SheetRenderer.java
index 495c933..5afde82 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/SheetRenderer.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/SheetRenderer.java
@@ -334,7 +334,7 @@ public class SheetRenderer extends RendererBase {
       }
 
       encodeHiddenInput(writer,
-          JsonUtils.encode(encodedRendered.toArray(new String[encodedRendered.size()])),
+          JsonUtils.encode(encodedRendered.toArray(new String[encodedRendered.size()]), false),
           sheetId + SUFFIX_COLUMN_RENDERED);
     }
 
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/SuggestRenderer.java
b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/SuggestRenderer.java
index 8692e67..6af65d3 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/SuggestRenderer.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/renderkit/renderer/SuggestRenderer.java
@@ -123,10 +123,10 @@ public class SuggestRenderer extends RendererBase {
     writer.writeAttribute(DataAttributes.SUGGEST_MAX_ITEMS, suggest.getMaximumItems());
     writer.writeAttribute(DataAttributes.SUGGEST_UPDATE, suggest.isUpdate());
     writer.writeAttribute(DataAttributes.SUGGEST_TOTAL_COUNT, totalCount);
-    writer.writeAttribute(DataAttributes.SUGGEST_DATA, JsonUtils.encode(array), true);
+    writer.writeAttribute(DataAttributes.SUGGEST_DATA, JsonUtils.encode(array, true), true);
 
     if (LOG.isDebugEnabled()) {
-      LOG.debug("suggest list: " + JsonUtils.encode(array));
+      LOG.debug("suggest list: " + JsonUtils.encode(array, true));
     }
 
     writer.endElement(HtmlElements.INPUT);
diff --git a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/JsonUtils.java
b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/JsonUtils.java
index 70f6e4f..c2205df 100644
--- a/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/JsonUtils.java
+++ b/tobago-core/src/main/java/org/apache/myfaces/tobago/internal/util/JsonUtils.java
@@ -42,15 +42,16 @@ public class JsonUtils {
   private JsonUtils() {
   }
 
-  private static void encode(final StringBuilder builder, final String name, final String[]
value) {
+  private static void encode(final StringBuilder builder, final String name, final String[]
value,
+      final boolean escape) {
     builder.append("\"");
     builder.append(name);
     builder.append("\":");
-    encode(builder, value);
+    encode(builder, value, escape);
     builder.append(",");
   }
 
-  public static void encode(final StringBuilder builder, final String[] value) {
+  public static void encode(final StringBuilder builder, final String[] value, final boolean
escape) {
     builder.append("[");
     boolean colon = false;
     for (final String item : value) {
@@ -58,7 +59,18 @@ public class JsonUtils {
         builder.append(",");
       }
       builder.append("\"");
-      builder.append(item);
+      if (escape) {
+        for (int i = 0; i < item.length(); i++) {
+          final char c = item.charAt(i);
+          if (c == '\"') {
+            builder.append("\\\"");
+          } else {
+            builder.append(c);
+          }
+        }
+      } else {
+        builder.append(item);
+      }
       builder.append("\"");
       colon = true;
     }
@@ -223,11 +235,11 @@ public class JsonUtils {
     builder.append("{");
     final int initialLength = builder.length();
 
-    encode(builder, "monthNames", dateTimeI18n.getMonthNames());
-    encode(builder, "monthNamesShort", dateTimeI18n.getMonthNamesShort());
-    encode(builder, "dayNames", dateTimeI18n.getDayNames());
-    encode(builder, "dayNamesShort", dateTimeI18n.getDayNamesShort());
-    encode(builder, "dayNamesMin", dateTimeI18n.getDayNamesMin());
+    encode(builder, "monthNames", dateTimeI18n.getMonthNames(), false);
+    encode(builder, "monthNamesShort", dateTimeI18n.getMonthNamesShort(), false);
+    encode(builder, "dayNames", dateTimeI18n.getDayNames(), false);
+    encode(builder, "dayNamesShort", dateTimeI18n.getDayNamesShort(), false);
+    encode(builder, "dayNamesMin", dateTimeI18n.getDayNamesMin(), false);
     encode(builder, "firstDay", dateTimeI18n.getFirstDay());
 
     if (builder.length() - initialLength > 0) {
@@ -238,12 +250,12 @@ public class JsonUtils {
     return builder.toString();
   }
 
-  public static String encode(final String[] strings) {
+  public static String encode(final String[] strings, final boolean escape) {
     if (strings == null) {
       return null;
     }
     final StringBuilder builder = new StringBuilder();
-    encode(builder, strings);
+    encode(builder, strings, escape);
     return builder.toString();
   }
 
diff --git a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/JsonUtilsUnitTest.java
b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/JsonUtilsUnitTest.java
index 406f4ca..f70a139 100644
--- a/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/JsonUtilsUnitTest.java
+++ b/tobago-core/src/test/java/org/apache/myfaces/tobago/internal/util/JsonUtilsUnitTest.java
@@ -156,7 +156,9 @@ public class JsonUtilsUnitTest extends AbstractTobagoTestBase {
 
   @Test
   public void encodeStringArray() {
-    Assert.assertEquals("[\"A-rập Xê-út (Tiếng A-rập)\"]", JsonUtils.encode(new String[]{"A-rập
Xê-út (Tiếng A-rập)"}));
+    Assert.assertEquals("[\"A-rập Xê-út (Tiếng A-rập)\"]", JsonUtils.encode(new String[]{"A-rập
Xê-út (Tiếng A-rập)"}, false));
+    Assert.assertEquals("[\"foo\"bar\"]", JsonUtils.encode(new String[]{"foo\"bar"}, false));
+    Assert.assertEquals("[\"foo\\\"bar\"]", JsonUtils.encode(new String[]{"foo\"bar"}, true));
   }
 
   @Test

-- 
To stop receiving notification emails like this one, please contact
hnoeth@apache.org.

Mime
View raw message