geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r411692 - in /geronimo/sandbox/gshell/trunk: gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/ gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/ gshell-testsuite/src/test/java/org/apache/geronimo/gshell/tests...
Date Mon, 05 Jun 2006 06:50:40 GMT
Author: jdillon
Date: Sun Jun  4 23:50:40 2006
New Revision: 411692

URL: http://svn.apache.org/viewvc?rev=411692&view=rev
Log:
Support for ${xxx} and $xxx variable expressions

Modified:
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/VariableExpressionParser.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/VariableExpressionParserTest.java
    geronimo/sandbox/gshell/trunk/gshell-testsuite/src/test/java/org/apache/geronimo/gshell/testsuite/MultiExpressionVariablesTest.java

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/VariableExpressionParser.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/VariableExpressionParser.java?rev=411692&r1=411691&r2=411692&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/VariableExpressionParser.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/commandline/VariableExpressionParser.java
Sun Jun  4 23:50:40 2006
@@ -36,6 +36,9 @@
  * Parser to handle ${...} expressions using
  * <a href="http://jakarta.apache.org/commons/jexl/">JEXL</a>.
  *
+ * <p>
+ * Supports complex ${xxx} and simple $xxx expressions
+ *
  * @version $Id$
  */
 public class VariableExpressionParser
@@ -152,10 +155,6 @@
         return obj;
     }
 
-    private static final String PREFIX = "${";
-
-    private static final String SUFFIX = "}";
-
     public String parse(final String input) throws SyntaxException {
         if (input == null) {
             throw new NullArgumentException("input");
@@ -168,24 +167,46 @@
 
         StringBuffer buff = new StringBuffer();
 
-        int cur = 0;
-        int prefixLoc = 0;
-        int suffixLoc = 0;
+        int current = 0;
+
+        while (current < input.length()) {
+            boolean complex = false;
 
-        while (cur < input.length()) {
-            prefixLoc = input.indexOf(PREFIX, cur);
+            int start = input.indexOf("$", current);
 
-            if (prefixLoc < 0) {
+            if (start == -1) {
                 break;
             }
+            else if (start + 1 < input.length()) {
+                if (input.charAt(start + 1) == '{') {
+                    complex = true;
+                }
+            }
 
-            suffixLoc = input.indexOf(SUFFIX, prefixLoc);
-            if (suffixLoc < 0) {
-                throw new SyntaxException("Missing '}': " + input);
+            int end;
+            if (complex) {
+                end = input.indexOf("}", start);
+                if (end == -1) {
+                    throw new SyntaxException("Missing '}': " + input);
+                }
+            }
+            else {
+                end = input.indexOf(" ", start);
+                if (end == -1) {
+                    end = input.indexOf("\t", start);
+                    
+                    if (end == -1) {
+                        end = input.length();
+                    }
+                }
             }
 
-            String expr = input.substring(prefixLoc + 2, suffixLoc);
-            buff.append(input.substring(cur, prefixLoc));
+            String expr = input.substring(start + (complex ? 2 : 1), end);
+
+            // System.err.println("b: " + buff);
+            String tmp = input.substring(current, start);
+            // System.err.println("t: " + tmp + "<");
+            buff.append(tmp);
 
             try {
                 buff.append(evaluate(expr));
@@ -194,10 +215,21 @@
                 throw new SyntaxException("Failed to evaluate: " + expr, e);
             }
 
-            cur = suffixLoc + 1;
+            // System.err.println("s:" + start);
+            // System.err.println("e:" + end);
+            // System.err.println("c:" + current);
+
+            current = end + (complex ? 1 : 0);
         }
 
-        buff.append(input.substring(cur));
+        // System.err.println("c:" + current);
+
+        if (current < input.length()) {
+            // System.err.println("b: " + buff);
+            String tmp = input.substring(current);
+            // System.err.println("t: " + tmp);
+            buff.append(tmp);
+        }
 
         if (trace) {
             log.trace("Parsed result: " + buff);
@@ -226,7 +258,7 @@
         extends RuntimeException
     {
         ///CLOVER:OFF
-        
+
         public SyntaxException(final String msg) {
             super(msg);
         }

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/VariableExpressionParserTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/VariableExpressionParserTest.java?rev=411692&r1=411691&r2=411692&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/VariableExpressionParserTest.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/commandline/VariableExpressionParserTest.java
Sun Jun  4 23:50:40 2006
@@ -36,38 +36,38 @@
         parser = null;
     }
 
-    public void testDefault() throws Exception {
+    public void testComplexDefault() throws Exception {
         String value = "${java.home}";
         String result = parser.parse(value);
-        assertEquals(result, System.getProperty("java.home"));
+        assertEquals(System.getProperty("java.home"), result);
     }
 
-    public void testSubst() throws Exception {
+    public void testComplexSubst() throws Exception {
         String value = "BEFORE${java.home}AFTER";
         String result = parser.parse(value);
-        assertEquals(result, "BEFORE" + System.getProperty("java.home") + "AFTER");
+        assertEquals("BEFORE" + System.getProperty("java.home") + "AFTER", result);
     }
 
-    public void testVariable() throws Exception {
+    public void testComplexVariable() throws Exception {
         String myvar = "this is my variable";
         parser.setVariable("my.var", myvar);
 
         String value = "${my.var}";
         String result = parser.parse(value);
-        assertEquals(result, myvar);
+        assertEquals(myvar, result);
     }
 
-    public void testFlatVariable() throws Exception {
+    public void testComplexFlatVariable() throws Exception {
         String myvar = "this is my variable";
         parser.setVariable("my.var", myvar);
         parser.setVariable("my", "not used");
 
         String value = "${my.var}";
         String result = parser.parse(value);
-        assertEquals(result, myvar);
+        assertEquals(myvar, result);
     }
 
-    public void testSyntax() throws Exception {
+    public void testComplexSyntaxError() throws Exception {
         String value = "${java.home";
 
         try {
@@ -77,5 +77,23 @@
         catch (VariableExpressionParser.SyntaxException expected) {
             // ignore
         }
+    }
+
+    public void testSimple() throws Exception {
+        String value = "$java.home";
+        String result = parser.parse(value);
+        assertEquals(System.getProperty("java.home"), result);
+    }
+
+    public void testSimpleSubst() throws Exception {
+        String value = "BEFORE$java.home AFTER";
+        String result = parser.parse(value);
+        assertEquals("BEFORE" + System.getProperty("java.home") + " AFTER", result);
+    }
+
+    public void testSimpleSubst2() throws Exception {
+        String value = "BEFORE$java.home\tAFTER";
+        String result = parser.parse(value);
+        assertEquals("BEFORE" + System.getProperty("java.home") + "\tAFTER", result);
     }
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-testsuite/src/test/java/org/apache/geronimo/gshell/testsuite/MultiExpressionVariablesTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-testsuite/src/test/java/org/apache/geronimo/gshell/testsuite/MultiExpressionVariablesTest.java?rev=411692&r1=411691&r2=411692&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-testsuite/src/test/java/org/apache/geronimo/gshell/testsuite/MultiExpressionVariablesTest.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-testsuite/src/test/java/org/apache/geronimo/gshell/testsuite/MultiExpressionVariablesTest.java
Sun Jun  4 23:50:40 2006
@@ -47,4 +47,19 @@
         assertNotNull(line1);
         assertEquals("1", line1);
     }
+
+    public void testSimple2() throws Exception {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        IO io = new IO(System.in, out);
+
+        Shell shell = new Shell(io);
+        shell.execute("set a=1; echo $a");
+
+        ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
+
+        String line1 = reader.readLine();
+        assertNotNull(line1);
+        assertEquals("1", line1);
+    }
 }



Mime
View raw message