This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git
The following commit(s) were added to refs/heads/master by this push:
new d641cd2 FELIX-6371 : JSONConfigurationWriter does not escape
backslash and other chars
d641cd2 is described below
commit d641cd29703ee6fff418f42c0dd807bc2a87a358
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Thu Jan 14 13:29:37 2021 +0100
FELIX-6371 : JSONConfigurationWriter does not escape backslash and other
chars
---
inventory/changelog.txt | 8 +++-
.../impl/helper/JSONConfigurationWriter.java | 15 +++++---
.../impl/helper/JSONConfigurationWriterTest.java | 44 ++++++++++++++++++++++
3 files changed, 60 insertions(+), 7 deletions(-)
diff --git a/inventory/changelog.txt b/inventory/changelog.txt
index fb8d64d..918448f 100644
--- a/inventory/changelog.txt
+++ b/inventory/changelog.txt
@@ -1,3 +1,9 @@
+Changes in 1.1.0
+----------------
+** Bug
+ * [FELIX-6371] - JSONConfigurationWriter does not escape backslash and
other chars
+
+
Changes in 1.0.6
----------------
** Bug
@@ -37,4 +43,4 @@ Initial Release 1.0.0
** Task
* [FELIX-3874] - Create new status printer module
- * [FELIX-3970] - Remove unneeded hashCode and equals methods from
PrinterMode
\ No newline at end of file
+ * [FELIX-3970] - Remove unneeded hashCode and equals methods from
PrinterMode
diff --git
a/inventory/src/main/java/org/apache/felix/inventory/impl/helper/JSONConfigurationWriter.java
b/inventory/src/main/java/org/apache/felix/inventory/impl/helper/JSONConfigurationWriter.java
index b21547b..5900f0d 100644
---
a/inventory/src/main/java/org/apache/felix/inventory/impl/helper/JSONConfigurationWriter.java
+++
b/inventory/src/main/java/org/apache/felix/inventory/impl/helper/JSONConfigurationWriter.java
@@ -27,7 +27,6 @@ import java.util.StringTokenizer;
*/
public class JSONConfigurationWriter extends ConfigurationWriter
{
-
private boolean wrapJSON;
private boolean startLine;
@@ -71,6 +70,7 @@ public class JSONConfigurationWriter extends
ConfigurationWriter
// IE has an issue with white-space:pre in our case so, we write
// <br/> instead of [CR]LF to get the line break. This also works
// in other browsers.
+ @Override
public void println()
{
if (wrapJSON)
@@ -93,6 +93,7 @@ public class JSONConfigurationWriter extends
ConfigurationWriter
// delegation to the write() method. So we need to override this, to
// make
// sure, that everything is escaped correctly
+ @Override
public void print(final String str)
{
final char[] chars = str.toCharArray();
@@ -103,6 +104,7 @@ public class JSONConfigurationWriter extends
ConfigurationWriter
// always delegate to write(char[], int, int) otherwise in some VM
// it cause endless cycle and StackOverflowError
+ @Override
public void write(final int character)
{
synchronized (oneChar)
@@ -112,8 +114,8 @@ public class JSONConfigurationWriter extends
ConfigurationWriter
}
}
- // write the characters unmodified unless filtering is enabled in
- // which case the writeFiltered(String) method is called for filtering
+ // write the characters unmodified unless filtering is enabled
+ @Override
public void write(char[] chars, int off, int len)
{
if (this.wrapJSON)
@@ -125,14 +127,14 @@ public class JSONConfigurationWriter extends
ConfigurationWriter
}
String v = new String(chars, off, len);
- StringTokenizer st = new StringTokenizer(v, "\r\n\"", true);
+ StringTokenizer st = new StringTokenizer(v, "\r\n\"\\\t\b\f",
true);
while (st.hasMoreTokens())
{
String t = st.nextToken();
if (t.length() == 1)
{
char c = t.charAt(0);
- if (c == '\r')
+ if (c == '\r' || c == '\f' || c == '\t' || c == '\b')
{
// ignore
}
@@ -141,7 +143,7 @@ public class JSONConfigurationWriter extends
ConfigurationWriter
this.println();
this.startLine();
}
- else if (c == '"')
+ else if (c == '"' || c == '\\')
{
super.write('\\');
super.write(c);
@@ -165,6 +167,7 @@ public class JSONConfigurationWriter extends
ConfigurationWriter
// write the string unmodified unless filtering is enabled in
// which case the writeFiltered(String) method is called for filtering
+ @Override
public void write(final String string, final int off, final int len)
{
write(string.toCharArray(), off, len);
diff --git
a/inventory/src/test/java/org/apache/felix/inventory/impl/helper/JSONConfigurationWriterTest.java
b/inventory/src/test/java/org/apache/felix/inventory/impl/helper/JSONConfigurationWriterTest.java
new file mode 100644
index 0000000..029bd09
--- /dev/null
+++
b/inventory/src/test/java/org/apache/felix/inventory/impl/helper/JSONConfigurationWriterTest.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.inventory.impl.helper;
+
+import java.io.StringWriter;
+
+import org.junit.Assert;
+
+import junit.framework.TestCase;
+
+public class JSONConfigurationWriterTest extends TestCase {
+
+ public void test_escaping() {
+ final StringWriter out = new StringWriter();
+ final JSONConfigurationWriter w = new JSONConfigurationWriter(out);
+ w.startJSONWrapper();
+ w.write("abcd\\\n1\t2\f3\b4\"5end");
+ w.endJSONWrapper();
+ w.close();
+
+ final String expected = "[" + System.lineSeparator()
+ + " \"abcd\\\\\"," + System.lineSeparator()
+ + " \"1234\\\"5end\"]" + System.lineSeparator();
+
+ Assert.assertEquals("Escaped JSON", expected, out.toString());
+
+ }
+}