Author: rgs
Date: Sun Nov  8 22:25:17 2015
New Revision: 1713308

URL: http://svn.apache.org/viewvc?rev=1713308&view=rev
Log:
ZOOKEEPER-1853: zkCli.sh can't issue a CREATE command containing
spaces in the data (Jun Gong via rgs)

Modified:
    zookeeper/branches/branch-3.4/CHANGES.txt
    
zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/ZooKeeperMain.java
    
zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/ZooKeeperTest.java

Modified: zookeeper/branches/branch-3.4/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/CHANGES.txt?rev=1713308&r1=1713307&r2=1713308&view=diff
==============================================================================
--- zookeeper/branches/branch-3.4/CHANGES.txt (original)
+++ zookeeper/branches/branch-3.4/CHANGES.txt Sun Nov  8 22:25:17 2015
@@ -141,6 +141,9 @@ BUGFIXES:
   ZOOKEEPER-2142: JMX ObjectName is incorrect for observers (Edward Ribeiro
   via michim)
 
+  ZOOKEEPER-1853: zkCli.sh can't issue a CREATE command containing
+  spaces in the data (Jun Gong via rgs)
+
 IMPROVEMENTS:
 
   ZOOKEEPER-1575. adding .gitattributes to prevent CRLF and LF mismatches for

Modified: 
zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/ZooKeeperMain.java
URL: 
http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/ZooKeeperMain.java?rev=1713308&r1=1713307&r2=1713308&view=diff
==============================================================================
--- 
zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/ZooKeeperMain.java
 (original)
+++ 
zookeeper/branches/branch-3.4/src/java/main/org/apache/zookeeper/ZooKeeperMain.java
 Sun Nov  8 22:25:17 2015
@@ -41,6 +41,8 @@ import org.apache.zookeeper.data.ACL;
 import org.apache.zookeeper.data.Id;
 import org.apache.zookeeper.data.Stat;
 import java.util.StringTokenizer;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 /**
  * The command line client to ZooKeeper.
@@ -153,6 +155,8 @@ public class ZooKeeperMain {
         private Map<String,String> options = new HashMap<String,String>();
         private List<String> cmdArgs = null;
         private String command = null;
+        public static final Pattern ARGS_PATTERN = 
Pattern.compile("\\s*([^\"\']\\S*|\"[^\"]*\"|'[^']*')\\s*");
+        public static final Pattern QUOTED_PATTERN = 
Pattern.compile("^([\'\"])(.*)(\\1)$");
 
         public MyCommandOptions() {
           options.put("server", "localhost:2181");
@@ -224,18 +228,22 @@ public class ZooKeeperMain {
          * @return true if parsing succeeded.
          */
         public boolean parseCommand( String cmdstring ) {
-            StringTokenizer cmdTokens = new StringTokenizer(cmdstring, " ");   
       
-            String[] args = new String[cmdTokens.countTokens()];
-            int tokenIndex = 0;
-            while (cmdTokens.hasMoreTokens()) {
-                args[tokenIndex] = cmdTokens.nextToken();
-                tokenIndex++;
+            Matcher matcher = ARGS_PATTERN.matcher(cmdstring);
+
+            List<String> args = new LinkedList<String>();
+            while (matcher.find()) {
+                String value = matcher.group(1);
+                if (QUOTED_PATTERN.matcher(value).matches()) {
+                    // Strip off the surrounding quotes
+                    value = value.substring(1, value.length() - 1);
+                }
+                args.add(value);
             }
-            if (args.length == 0){
+            if (args.isEmpty()){
                 return false;
             }
-            command = args[0];
-            cmdArgs = Arrays.asList(args);
+            command = args.get(0);
+            cmdArgs = args;
             return true;
         }
     }

Modified: 
zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/ZooKeeperTest.java
URL: 
http://svn.apache.org/viewvc/zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/ZooKeeperTest.java?rev=1713308&r1=1713307&r2=1713308&view=diff
==============================================================================
--- 
zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/ZooKeeperTest.java
 (original)
+++ 
zookeeper/branches/branch-3.4/src/java/test/org/apache/zookeeper/ZooKeeperTest.java
 Sun Nov  8 22:25:17 2015
@@ -201,4 +201,58 @@ public class ZooKeeperTest extends Clien
             }
     }
 
+     @Test
+    public void testParseWithQuotes() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        for (String quoteChar : new String[] {"'", "\""}) {
+            String cmdstring = String.format("create /node %1$squoted 
data%1$s", quoteChar);
+            zkMain.cl.parseCommand(cmdstring);
+            Assert.assertEquals("quotes combine arguments", 
zkMain.cl.getNumArguments(), 3);
+            Assert.assertEquals("create is not taken as first argument", 
zkMain.cl.getCmdArgument(0), "create");
+            Assert.assertEquals("/node is not taken as second argument", 
zkMain.cl.getCmdArgument(1), "/node");
+            Assert.assertEquals("quoted data is not taken as third argument", 
zkMain.cl.getCmdArgument(2), "quoted data");
+        }
+    }
+
+    @Test
+    public void testParseWithMixedQuotes() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        for (String[] quoteChars : new String[][] {{"'", "\""}, {"\"", "'"}}) {
+            String outerQuotes = quoteChars[0];
+            String innerQuotes = quoteChars[1];
+            String cmdstring = String.format("create /node %1$s%2$squoted 
data%2$s%1$s", outerQuotes, innerQuotes);
+            zkMain.cl.parseCommand(cmdstring);
+            Assert.assertEquals("quotes combine arguments", 
zkMain.cl.getNumArguments(), 3);
+            Assert.assertEquals("create is not taken as first argument", 
zkMain.cl.getCmdArgument(0), "create");
+            Assert.assertEquals("/node is not taken as second argument", 
zkMain.cl.getCmdArgument(1), "/node");
+            Assert.assertEquals("quoted data is not taken as third argument", 
zkMain.cl.getCmdArgument(2), innerQuotes + "quoted data" + innerQuotes);
+        }
+    }
+
+    @Test
+    public void testParseWithEmptyQuotes() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        String cmdstring = "create /node ''";
+        zkMain.cl.parseCommand(cmdstring);
+        Assert.assertEquals("empty quotes should produce arguments", 
zkMain.cl.getNumArguments(), 3);
+        Assert.assertEquals("create is not taken as first argument", 
zkMain.cl.getCmdArgument(0), "create");
+        Assert.assertEquals("/node is not taken as second argument", 
zkMain.cl.getCmdArgument(1), "/node");
+        Assert.assertEquals("empty string is not taken as third argument", 
zkMain.cl.getCmdArgument(2), "");
+    }
+
+    @Test
+    public void testParseWithMultipleQuotes() throws Exception {
+        final ZooKeeper zk = createClient();
+        ZooKeeperMain zkMain = new ZooKeeperMain(zk);
+        String cmdstring = "create /node '' ''";
+        zkMain.cl.parseCommand(cmdstring);
+        Assert.assertEquals("expected 5 arguments", 
zkMain.cl.getNumArguments(), 4);
+        Assert.assertEquals("create is not taken as first argument", 
zkMain.cl.getCmdArgument(0), "create");
+        Assert.assertEquals("/node is not taken as second argument", 
zkMain.cl.getCmdArgument(1), "/node");
+        Assert.assertEquals("empty string is not taken as third argument", 
zkMain.cl.getCmdArgument(2), "");
+        Assert.assertEquals("empty string is not taken as fourth argument", 
zkMain.cl.getCmdArgument(3), "");
+    }
 }


Reply via email to