camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1340733 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/language/simple/ camel-core/src/main/java/org/apache/camel/language/simple/types/ camel-core/src/test/java/org/apache/camel/language/simple/ components/camel-spring/src/t...
Date Sun, 20 May 2012 14:23:03 GMT
Author: davsclaus
Date: Sun May 20 14:23:02 2012
New Revision: 1340733

URL: http://svn.apache.org/viewvc?rev=1340733&view=rev
Log:
CAMEL-5295: Added support for escaping values with the simple language.

Added:
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/SpringSimpleNewlineTest.java
      - copied, changed from r1340682, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/SpringSimpleRegexTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/springSimpleNewline.xml
      - copied, changed from r1340682, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/springSimpleRegexContext.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/types/SimpleTokenType.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/types/TokenType.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java?rev=1340733&r1=1340732&r2=1340733&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
Sun May 20 14:23:02 2012
@@ -42,6 +42,7 @@ public final class SimpleTokenizer {
         KNOWN_TOKENS.add(new SimpleTokenType(TokenType.booleanValue, "true"));
         KNOWN_TOKENS.add(new SimpleTokenType(TokenType.booleanValue, "false"));
         KNOWN_TOKENS.add(new SimpleTokenType(TokenType.nullValue, "null"));
+        KNOWN_TOKENS.add(new SimpleTokenType(TokenType.escape, "\\"));
 
         // binary operators
         KNOWN_TOKENS.add(new SimpleTokenType(TokenType.binaryOperator, "=="));
@@ -164,6 +165,30 @@ public final class SimpleTokenizer {
             }
         }
 
+        boolean escapeAllowed = acceptType(TokenType.escape, filters);
+        if (escapeAllowed) {
+            StringBuilder sb = new StringBuilder();
+            char ch = expression.charAt(index);
+            boolean escaped = '\\' == ch;
+            if (escaped && index < expression.length()) {
+                // grab next character to escape
+                char next = expression.charAt(++index);
+                // special for new line, tabs and carriage return
+                if ('n' == next) {
+                    sb.append("\n");
+                } else if ('t' == next) {
+                    sb.append("\t");
+                } else if ('r' == next) {
+                    sb.append("\r");
+                } else {
+                    // append the next
+                    sb.append(next);
+                }
+                // force 2 as length
+                return new SimpleToken(new SimpleTokenType(TokenType.character, sb.toString()),
index, 2);
+            }
+        }
+
         // it could be any of the known tokens
         String text = expression.substring(index);
         for (SimpleTokenType token : KNOWN_TOKENS) {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/types/SimpleTokenType.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/types/SimpleTokenType.java?rev=1340733&r1=1340732&r2=1340733&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/types/SimpleTokenType.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/types/SimpleTokenType.java
Sun May 20 14:23:02 2012
@@ -62,6 +62,13 @@ public class SimpleTokenType {
     }
 
     /**
+     * Whether the type is escape
+     */
+    public boolean isEscape() {
+        return type == TokenType.escape;
+    }
+
+    /**
      * Whether the type is single quote
      */
     public boolean isSingleQuote() {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/types/TokenType.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/types/TokenType.java?rev=1340733&r1=1340732&r2=1340733&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/types/TokenType.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/types/TokenType.java
Sun May 20 14:23:02 2012
@@ -24,6 +24,7 @@ public enum TokenType {
     whiteSpace, character,
     booleanValue, numericValue, nullValue,
     singleQuote, doubleQuote,
+    escape,
     functionStart, functionEnd,
     binaryOperator, unaryOperator, logicalOperator,
     eol

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java?rev=1340733&r1=1340732&r2=1340733&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
Sun May 20 14:23:02 2012
@@ -1088,7 +1088,7 @@ public class SimpleTest extends Language
 
     public void testSlashBeforeHeader() throws Exception {
         assertExpression("foo/${header.foo}", "foo/abc");
-        assertExpression("foo\\${header.foo}", "foo\\abc");
+        assertExpression("foo\\\\${header.foo}", "foo\\abc");
     }
 
     public void testJSonLike() throws Exception {
@@ -1108,6 +1108,26 @@ public class SimpleTest extends Language
         assertExpression("{{${body}-${body}}}", "{{Something-Something}}");
     }
 
+    public void testEscape() throws Exception {
+        exchange.getIn().setBody("Something");
+
+        // slash foo
+        assertExpression("\\\\foo", "\\foo");
+
+        assertExpression("\\n${body}", "\nSomething");
+        assertExpression("\\t${body}", "\tSomething");
+        assertExpression("\\r${body}", "\rSomething");
+        assertExpression("\\n\\r${body}", "\n\rSomething");
+        assertExpression("\\n${body}\\n", "\nSomething\n");
+        assertExpression("\\t${body}\\t", "\tSomething\t");
+        assertExpression("\\r${body}\\r", "\rSomething\r");
+        assertExpression("\\n\\r${body}\\n\\r", "\n\rSomething\n\r");
+
+        assertExpression("\\$${body}", "$Something");
+        assertExpression("\\$\\{${body}\\}", "${Something}");
+        assertExpression("\\$\\{body\\}", "${body}");
+    }
+
     protected String getLanguageName() {
         return "simple";
     }

Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/SpringSimpleNewlineTest.java
(from r1340682, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/SpringSimpleRegexTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/SpringSimpleNewlineTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/SpringSimpleNewlineTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/SpringSimpleRegexTest.java&r1=1340682&r2=1340733&rev=1340733&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/SpringSimpleRegexTest.java
(original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/language/SpringSimpleNewlineTest.java
Sun May 20 14:23:02 2012
@@ -20,19 +20,16 @@ import org.apache.camel.spring.SpringTes
 import org.springframework.context.support.AbstractXmlApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
-public class SpringSimpleRegexTest extends SpringTestSupport {
+public class SpringSimpleNewlineTest extends SpringTestSupport {
 
     @Override
     protected AbstractXmlApplicationContext createApplicationContext() {
-        return new ClassPathXmlApplicationContext("org/apache/camel/language/springSimpleRegexContext.xml");
+        return new ClassPathXmlApplicationContext("org/apache/camel/language/springSimpleNewline.xml");
     }
     
-    public void testSimpleRegex() {
-        String result = template.requestBody("direct:start", "Something is wrong", String.class);
-        assertEquals("Let's keep looking.", result);
-
-        result = template.requestBody("direct:start", "12.34.5678", String.class);
-        assertEquals("Found the result.", result);
+    public void testSimpleNewline() {
+        String result = template.requestBody("direct:start", "Camel", String.class);
+        assertEquals("Body is\non new line Camel\n", result);
     }
 
 }

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/springSimpleNewline.xml
(from r1340682, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/springSimpleRegexContext.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/springSimpleNewline.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/springSimpleNewline.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/springSimpleRegexContext.xml&r1=1340682&r2=1340733&rev=1340733&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/springSimpleRegexContext.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/language/springSimpleNewline.xml
Sun May 20 14:23:02 2012
@@ -24,20 +24,12 @@
 
   <camelContext xmlns="http://camel.apache.org/schema/spring">
       <route>
-	      <from uri="direct:start" />
-	      <choice>
-	          <when>
-	              <simple>${body} regex '^\d{2}\.\d{2}\.\d{4}$'</simple>
-	                  <setBody>
-	                      <constant>Found the result.</constant>
-	                  </setBody>
-	           </when>
-	           <otherwise>
-	              <setBody>
-	                  <constant>Let's keep looking.</constant>
-	              </setBody>
-	           </otherwise>
-	      </choice>
+	      <from uri="direct:start"/>
+        <transform>
+          <simple>
+            Body is\non new line ${body}\n
+          </simple>
+        </transform>
       </route>
   </camelContext>
 



Mime
View raw message