tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From antel...@apache.org
Subject svn commit: r1041571 - /tuscany/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/Shell.java
Date Thu, 02 Dec 2010 20:33:30 GMT
Author: antelder
Date: Thu Dec  2 20:33:30 2010
New Revision: 1041571

URL: http://svn.apache.org/viewvc?rev=1041571&view=rev
Log:
Update to support entering quoted strings conatining spaces

Modified:
    tuscany/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/Shell.java

Modified: tuscany/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/Shell.java
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/Shell.java?rev=1041571&r1=1041570&r2=1041571&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/Shell.java
(original)
+++ tuscany/sca-java-2.x/trunk/modules/shell/src/main/java/org/apache/tuscany/sca/shell/Shell.java
Thu Dec  2 20:33:30 2010
@@ -319,12 +319,7 @@ public class Shell {
             while ((l = r.readLine()) != null) {
                 out.println(l);
                 String[] toks = l != null ? l.trim().split(" ") : "".split(" ");
-                List<String> toksList = new ArrayList<String>();
-                for (String s : toks) {
-                    if (s != null && s.trim().length() > 0) {
-                        toksList.add(s);
-                    }
-                }
+                List<String> toksList = getTokens(toks);
                 apply(eval(toksList));
             }
         } finally {
@@ -488,15 +483,47 @@ public class Shell {
             l = ((BufferedReader)r).readLine();
             history.add(l);
         }
+        
         String[] toks = l != null ? l.trim().split(" ") : "bye".split(" ");
+        return getTokens(toks);
+    }
+
+    /**
+     * Parse the string into tokens, which may include quoted strings
+     */
+    List<String> getTokens(String[] toks) {
         List<String> toksList = new ArrayList<String>();
-        for (String s : toks) {
-            if (s != null && s.trim().length() > 0) {
-                toksList.add(s);
+        for (int i=0; i<toks.length; i++) {
+            if (toks[i] != null && toks[i].trim().length() > 0) {
+                int j = quotedString(toks, i);
+                if (j > -1) {
+                    StringBuilder sb = new StringBuilder();
+                    sb.append(toks[i]);
+                    for (int k=i+1; k<=j; k++) {
+                        sb.append(" ");
+                        sb.append(toks[k]);
+                    }
+                    i = j;
+                    String s = sb.toString();
+                    toksList.add(s.substring(1, s.length()-1));
+                } else {
+                    toksList.add(toks[i]);
+                }
             }
         }
         return toksList;
     }
+    
+    int quotedString(String[] toks, int i) {
+        if (toks[i].startsWith("\"") || toks[i].startsWith("'")) {
+            for (int j=i+1; j<toks.length; j++) {
+                if (toks[j].endsWith(toks[i].substring(0,0))) {
+                    return j;
+                }
+            }
+        }
+        return -1;
+    }
 
     Callable<Boolean> eval(final List<String> toks) {
         final String op = toks.size() > 0 ? toks.get(0) : "";



Mime
View raw message