Author: pauls Date: Tue May 23 15:10:54 2017 New Revision: 1795918 URL: http://svn.apache.org/viewvc?rev=1795918&view=rev Log: SLING-6871: Allow tick as well as double quotes in imports in the JsonReader.
Modified: sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java Modified: sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java?rev=1795918&r1=1795917&r2=1795918&view=diff ============================================================================== --- sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java (original) +++ sling/trunk/bundles/jcr/contentloader/src/main/java/org/apache/sling/jcr/contentloader/internal/readers/JsonReader.java Tue May 23 15:10:54 2017 @@ -22,7 +22,10 @@ import java.io.BufferedInputStream; 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 @@ public class JsonReader implements Conte } 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 @@ public class JsonReader implements Conte //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); + } } Modified: sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java?rev=1795918&r1=1795917&r2=1795918&view=diff ============================================================================== --- sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java (original) +++ sling/trunk/bundles/jcr/contentloader/src/test/java/org/apache/sling/jcr/contentloader/internal/JsonReaderTest.java Tue May 23 15:10:54 2017 @@ -341,6 +341,37 @@ public class JsonReaderTest { }}); 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 ----------------------------------------------