Return-Path: Delivered-To: apmail-geronimo-scm-archive@www.apache.org Received: (qmail 35251 invoked from network); 5 Jun 2006 06:51:04 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 5 Jun 2006 06:51:04 -0000 Received: (qmail 18090 invoked by uid 500); 5 Jun 2006 06:51:04 -0000 Delivered-To: apmail-geronimo-scm-archive@geronimo.apache.org Received: (qmail 18080 invoked by uid 500); 5 Jun 2006 06:51:03 -0000 Mailing-List: contact scm-help@geronimo.apache.org; run by ezmlm Precedence: bulk list-help: list-unsubscribe: List-Post: Reply-To: dev@geronimo.apache.org List-Id: Delivered-To: mailing list scm@geronimo.apache.org Received: (qmail 18069 invoked by uid 99); 5 Jun 2006 06:51:03 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 04 Jun 2006 23:51:03 -0700 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: local policy) Received: from [140.211.166.113] (HELO eris.apache.org) (140.211.166.113) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 04 Jun 2006 23:51:02 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id E3E9E1A983A; Sun, 4 Jun 2006 23:50:41 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: scm@geronimo.apache.org From: jdillon@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20060605065041.E3E9E1A983A@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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 * JEXL. * + *

+ * 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); + } }