camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r772927 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ main/java/org/apache/camel/language/simple/ test/java/org/apache/camel/language/
Date Fri, 08 May 2009 10:31:11 GMT
Author: davsclaus
Date: Fri May  8 10:31:10 2009
New Revision: 772927

URL: http://svn.apache.org/viewvc?rev=772927&view=rev
Log:
CAMEL-1582: Improved syntax validation for file language, so it reports error if syntax is
invalid.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FileExpressionBuilder.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguageSupport.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FileExpressionBuilder.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FileExpressionBuilder.java?rev=772927&r1=772926&r2=772927&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FileExpressionBuilder.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/builder/FileExpressionBuilder.java
Fri May  8 10:31:10 2009
@@ -240,14 +240,8 @@
             public Object evaluate(Exchange exchange) {
                 // must call evaluate to return the nested language evaluate when evaluating
                 // stacked expressions
-                try {
-                    Language simple = exchange.getContext().resolveLanguage("simple");
-                    return simple.createExpression(expression).evaluate(exchange, Object.class);
-                } catch (ExpressionIllegalSyntaxException e) {
-                    // fallback to constant so end users can enter a fixed filename
-                    Language constant = exchange.getContext().resolveLanguage("constant");
-                    return constant.createExpression(expression).evaluate(exchange, Object.class);
-                }
+                Language simple = exchange.getContext().resolveLanguage("simple");
+                return simple.createExpression(expression).evaluate(exchange, Object.class);
             }
 
             @Override

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java?rev=772927&r1=772926&r2=772927&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
Fri May  8 10:31:10 2009
@@ -54,12 +54,14 @@
  */
 public class FileLanguage extends SimpleLanguageSupport {
 
+    private static final SimpleLanguage SIMPLE = new SimpleLanguage();
+
     public static Expression file(String expression) {
         FileLanguage language = new FileLanguage();
         return language.createExpression(expression);
     }
 
-    protected Expression createSimpleExpression(String expression) {
+    protected Expression createSimpleExpression(String expression, boolean strict) {
 
         // file: prefix
         String remainder = ifStartsWithReturnRemainder("file:", expression);
@@ -102,7 +104,7 @@
         }
 
         // fallback to simple language if not file specific
-        return FileExpressionBuilder.simpleExpression(expression);
+        return SIMPLE.createSimpleExpression(expression, strict);
     }
 
     public boolean isSingleton() {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java?rev=772927&r1=772926&r2=772927&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
Fri May  8 10:31:10 2009
@@ -52,7 +52,7 @@
         return language.createExpression(expression);
     }
 
-    protected Expression createSimpleExpression(String expression) {
+    protected Expression createSimpleExpression(String expression, boolean strict) {
         if (ObjectHelper.isEqualToAny(expression, "body", "in.body")) {
             return ExpressionBuilder.bodyExpression();
         } else if (ObjectHelper.equal(expression, "out.body")) {
@@ -117,7 +117,11 @@
             return ExpressionBuilder.beanExpression(remainder);
         }
 
-        throw new ExpressionIllegalSyntaxException(expression);
+        if (strict) {
+            throw new ExpressionIllegalSyntaxException(expression);
+        } else {
+            return ExpressionBuilder.constantExpression(expression);
+        }
     }
 
     public boolean isSingleton() {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguageSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguageSupport.java?rev=772927&r1=772926&r2=772927&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguageSupport.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguageSupport.java
Fri May  8 10:31:10 2009
@@ -52,24 +52,24 @@
         Matcher matcher = PATTERN.matcher(expression);
         if (matcher.matches()) {
             if (log.isDebugEnabled()) {
-                log.debug("Expression is evaluated as operator expression: " + expression);
+                log.debug("Expression is evaluated as simple expression wiht operator: "
+ expression);
             }
             return createOperatorExpression(matcher, expression);
         } else if (expression.indexOf("${") >= 0) {
             if (log.isDebugEnabled()) {
-                log.debug("Expression is evaluated as complex expression: " + expression);
+                log.debug("Expression is evaluated as simple (strict) expression: " + expression);
             }
             return createComplexConcatExpression(expression);
         } else {
             if (log.isDebugEnabled()) {
-                log.debug("Expression is evaluated as simple expression: " + expression);
+                log.debug("Expression is evaluated as simple (non strict) expression: " +
expression);
             }
-            return createSimpleExpression(expression);
+            return createSimpleExpression(expression, false);
         }
     }
 
     private Expression createOperatorExpression(final Matcher matcher, final String expression)
{
-        final Expression left = createSimpleExpression(matcher.group(1));
+        final Expression left = createSimpleExpression(matcher.group(1), true);
         final SimpleLangaugeOperator operator = asOperator(matcher.group(2));
 
         // the right hand side expression can either be a constant expression wiht ' '
@@ -91,7 +91,7 @@
             }
             String simple = ObjectHelper.between(text, "${", "}");
 
-            right = simple != null ? createSimpleExpression(simple) : createConstantExpression(constant);
+            right = simple != null ? createSimpleExpression(simple, true) : createConstantExpression(constant);
             // to support numeric comparions using > and < operators we must convert
the right hand side
             // to the same type as the left
             rightConverted = ExpressionBuilder.convertToExpression(right, left);
@@ -176,7 +176,7 @@
                 }
                 String simpleText = expression.substring(pivot, endIdx);
 
-                Expression simpleExpression = createSimpleExpression(simpleText);
+                Expression simpleExpression = createSimpleExpression(simpleText, true);
                 results.add(simpleExpression);
                 pivot = endIdx + 1;
             }
@@ -196,9 +196,12 @@
      * Creates the simple expression based on the extracted content from the ${ } place holders
      *
      * @param expression  the content between ${ and }
+     * @param strict whether it is strict mode or not, if strict it will throw a
+     * {@link org.apache.camel.ExpressionIllegalSyntaxException} if the expression was not
known.
+     * Set to <tt>false</tt> to support constant expressions
      * @return the expression
      */
-    protected abstract Expression createSimpleExpression(String expression);
+    protected abstract Expression createSimpleExpression(String expression, boolean strict);
 
     protected String ifStartsWithReturnRemainder(String prefix, String text) {
         if (text.startsWith(prefix)) {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java?rev=772927&r1=772926&r2=772927&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java Fri
May  8 10:31:10 2009
@@ -24,6 +24,7 @@
 
 import org.apache.camel.Exchange;
 import org.apache.camel.LanguageTestSupport;
+import org.apache.camel.ExpressionIllegalSyntaxException;
 import org.apache.camel.component.file.FileConsumer;
 import org.apache.camel.component.file.FileEndpoint;
 import org.apache.camel.component.file.GenericFile;
@@ -131,6 +132,34 @@
         return answer;
     }
 
+    public void testIllegalSyntax() throws Exception {
+        try {
+            // it should be with colon
+            assertExpression("${file.name}", "");
+            fail("Should have thrown an exception");
+        } catch (ExpressionIllegalSyntaxException e) {
+            assertEquals("Illegal syntax: file.name", e.getMessage());
+        }
+
+        try {
+            assertExpression("hey ${xxx} how are you?", "");
+            fail("Should have thrown an exception");
+        } catch (ExpressionIllegalSyntaxException e) {
+            assertEquals("Illegal syntax: xxx", e.getMessage());
+        }
+
+        try {
+            assertExpression("${xxx}", "");
+            fail("Should have thrown an exception");
+        } catch (ExpressionIllegalSyntaxException e) {
+            assertEquals("Illegal syntax: xxx", e.getMessage());
+        }
+    }
+
+    public void testConstantFilename() throws Exception {
+        assertExpression("hello.txt", "hello.txt");
+    }
+
     public class MyFileNameGenerator {
         public String generateFilename(Exchange exchange) {
             return "generatorbybean";

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java?rev=772927&r1=772926&r2=772927&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/SimpleTest.java Fri May
 8 10:31:10 2009
@@ -26,12 +26,7 @@
 public class SimpleTest extends LanguageTestSupport {
 
     public void testConstantExpression() throws Exception {
-        try {
-            assertExpression("Hello World", "Hello World");
-            fail("Should have thrown an Exception");
-        } catch (ExpressionIllegalSyntaxException e) {
-            // constants is not supported
-        }
+        assertExpression("Hello World", "Hello World");
     }
 
     public void testSimpleExpressions() throws Exception {
@@ -81,6 +76,22 @@
         assertExpression("Hello ${exception.message} World", "Hello Just testing World");
     }
 
+    public void testIllegalSyntax() throws Exception {
+        try {
+            assertExpression("hey ${xxx} how are you?", "");
+            fail("Should have thrown an exception");
+        } catch (ExpressionIllegalSyntaxException e) {
+            assertEquals("Illegal syntax: xxx", e.getMessage());
+        }
+
+        try {
+            assertExpression("${xxx}", "");
+            fail("Should have thrown an exception");
+        } catch (ExpressionIllegalSyntaxException e) {
+            assertEquals("Illegal syntax: xxx", e.getMessage());
+        }
+    }
+
     protected String getLanguageName() {
         return "simple";
     }



Mime
View raw message