SLING-6871: Allow tick as well as double quotes in imports in the JsonReader.
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1795918 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java b/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
index 7a2ea01..c13ac06 100644
--- a/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
+++ b/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java
@@ -22,7 +22,10 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
import java.io.StringReader;
+import java.io.StringWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
@@ -160,7 +163,7 @@
}
Map<String, Object> config = new HashMap<>();
config.put("org.apache.johnzon.supports-comments", true);
- JsonObject json = Json.createReaderFactory(config).createReader(new StringReader(jsonString)).readObject();
+ JsonObject json = Json.createReaderFactory(config).createReader(new StringReader(tickToDoubleQuote(jsonString))).readObject();
this.createNode(null, json, contentCreator);
} catch (JsonException je) {
throw (IOException) new IOException(je.getMessage()).initCause(je);
@@ -466,5 +469,39 @@
//do the work.
contentCreator.createAce(principalID, grantedPrivileges, deniedPrivileges, order);
}
+
+ private static String tickToDoubleQuote(String input) {
+ char[] output = new char[input.length()];
+ boolean quoted = false;
+ boolean escaped = false;
+ for (int i = 0; i < output.length;i++) {
+ char in = input.charAt(i);
+ if (!quoted)
+ {
+ if (in == '\'') {
+ in = '"';
+ }
+ else if (in == '"') {
+ quoted = true;
+ }
+ }
+ else {
+ if (!escaped) {
+ if (in == '"') {
+ quoted = false;
+ }
+ else if (in == '\\') {
+ escaped = true;
+ }
+ }
+ else
+ {
+ escaped = false;
+ }
+ }
+ output[i] = in;
+ }
+ return new String(output);
+ }
}
diff --git a/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java b/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java
index d7fb3f8..6aab2ac 100644
--- a/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java
+++ b/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java
@@ -341,6 +341,37 @@
}});
this.parse(json);
}
+
+ @org.junit.Test public void testCreateAclWithTickQuotes() throws Exception {
+ String json = " { " +
+ "'security:acl' : [ " +
+ " { " +
+ " 'principal' : 'username1'," +
+ " 'granted' : ['jcr:read','jcr:write']," +
+ " 'denied' : []" +
+ " }," +
+ " {" +
+ " 'principal' : 'groupname1'," +
+ " 'granted' : ['jcr:read','jcr:write']" +
+ " }," +
+ " {" +
+ " 'principal' : \"\\\"'groupname2'\"," +
+ " 'granted' : ['jcr:read']," +
+ " 'denied' : ['jcr:write']," +
+ " 'order' : 'first'" +
+ " }" +
+ "]" +
+ "}";
+ this.mockery.checking(new Expectations() {{
+ allowing(creator).createNode(null, null, null); inSequence(mySequence);
+
+ allowing(creator).createAce("username1",new String[]{"jcr:read","jcr:write"},new String[]{}, null); inSequence(mySequence);
+ allowing(creator).createAce("groupname1",new String[]{"jcr:read","jcr:write"},null, null); inSequence(mySequence);
+ allowing(creator).createAce("\"'groupname2'",new String[]{"jcr:read"},new String[]{"jcr:write"}, "first"); inSequence(mySequence);
+ allowing(creator).finishNode(); inSequence(mySequence);
+ }});
+ this.parse(json);
+ }
//---------- internal helper ----------------------------------------------