camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1340961 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/language/simple/ main/resources/META-INF/services/org/apache/camel/language/ test/java/org/apache/camel/language/
Date Mon, 21 May 2012 10:45:26 GMT
Author: davsclaus
Date: Mon May 21 10:45:26 2012
New Revision: 1340961

URL: http://svn.apache.org/viewvc?rev=1340961&view=rev
Log:
CAMEL-5295: File language should not allow escaping as the escape character is a path separator
on windows platforms

Added:
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/BaseSimpleParser.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleBackwardsCompatibleParser.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleExpressionParser.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/SimplePredicateParser.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
    camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/file
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/BaseSimpleParser.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/BaseSimpleParser.java?rev=1340961&r1=1340960&r2=1340961&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/BaseSimpleParser.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/BaseSimpleParser.java
Mon May 21 10:45:26 2012
@@ -44,9 +44,11 @@ public abstract class BaseSimpleParser {
     protected SimpleToken token;
     protected int previousIndex;
     protected int index;
+    protected boolean allowEscape = true;
 
-    protected BaseSimpleParser(String expression) {
+    protected BaseSimpleParser(String expression, boolean allowEscape) {
         this.expression = expression;
+        this.allowEscape = allowEscape;
     }
 
     /**
@@ -55,7 +57,7 @@ public abstract class BaseSimpleParser {
      */
     protected void nextToken() {
         if (index < expression.length()) {
-            SimpleToken next = SimpleTokenizer.nextToken(expression, index);
+            SimpleToken next = SimpleTokenizer.nextToken(expression, index, allowEscape);
             // add token
             tokens.add(next);
             token = next;
@@ -76,7 +78,7 @@ public abstract class BaseSimpleParser {
      */
     protected void nextToken(TokenType... filter) {
         if (index < expression.length()) {
-            SimpleToken next = SimpleTokenizer.nextToken(expression, index, filter);
+            SimpleToken next = SimpleTokenizer.nextToken(expression, index, allowEscape,
filter);
             // add token
             tokens.add(next);
             token = next;

Added: 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=1340961&view=auto
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
(added)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
Mon May 21 10:45:26 2012
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.language.simple;
+
+/**
+ *
+ */
+public class FileLanguage extends SimpleLanguage {
+
+    public FileLanguage() {
+        // do not allow escaping, as Windows uses \ as path separator
+        setAllowEscape(false);
+    }
+
+    @Override
+    public void setAllowEscape(boolean allowEscape) {
+        if (allowEscape) {
+            throw new IllegalArgumentException("File language does not allow escape");
+        }
+        this.allowEscape = allowEscape;
+    }
+}

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleBackwardsCompatibleParser.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleBackwardsCompatibleParser.java?rev=1340961&r1=1340960&r2=1340961&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleBackwardsCompatibleParser.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleBackwardsCompatibleParser.java
Mon May 21 10:45:26 2012
@@ -43,12 +43,12 @@ public final class SimpleBackwardsCompat
         // static methods
     }
 
-    public static Expression parseExpression(String expression) {
-        return doParseExpression(expression);
+    public static Expression parseExpression(String expression, boolean allowEscape) {
+        return doParseExpression(expression, allowEscape);
     }
 
-    public static Predicate parsePredicate(String expression) {
-        Expression answer = doParseExpression(expression);
+    public static Predicate parsePredicate(String expression, boolean allowEscape) {
+        Expression answer = doParseExpression(expression, allowEscape);
         if (answer != null) {
             return ExpressionToPredicateAdapter.toPredicate(answer);
         } else {
@@ -56,10 +56,10 @@ public final class SimpleBackwardsCompat
         }
     }
 
-    private static Expression doParseExpression(String expression) {
+    private static Expression doParseExpression(String expression, boolean allowEscape) {
         // should have no function tokens
         for (int i = 0; i < expression.length(); i++) {
-            SimpleToken token = SimpleTokenizer.nextToken(expression, i, TokenType.functionStart,
TokenType.functionEnd);
+            SimpleToken token = SimpleTokenizer.nextToken(expression, i, allowEscape, TokenType.functionStart,
TokenType.functionEnd);
             if (token.getType().getType() == TokenType.functionStart || token.getType().getType()
== TokenType.functionEnd) {
                 return null;
             }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleExpressionParser.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleExpressionParser.java?rev=1340961&r1=1340960&r2=1340961&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleExpressionParser.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleExpressionParser.java
Mon May 21 10:45:26 2012
@@ -38,8 +38,13 @@ import org.apache.camel.language.simple.
  */
 public class SimpleExpressionParser extends BaseSimpleParser {
 
+    @Deprecated
     public SimpleExpressionParser(String expression) {
-        super(expression);
+        super(expression, true);
+    }
+
+    public SimpleExpressionParser(String expression, boolean allowEscape) {
+        super(expression, allowEscape);
     }
 
     public Expression parseExpression() {

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=1340961&r1=1340960&r2=1340961&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
Mon May 21 10:45:26 2012
@@ -93,7 +93,8 @@ public class SimpleLanguage implements L
     // singleton for expressions without a result type
     private static final SimpleLanguage SIMPLE = new SimpleLanguage();
 
-    private Class<?> resultType;
+    protected Class<?> resultType;
+    protected boolean allowEscape = true;
 
     /**
      * Default constructor.
@@ -109,6 +110,14 @@ public class SimpleLanguage implements L
         this.resultType = resultType;
     }
 
+    public boolean isAllowEscape() {
+        return allowEscape;
+    }
+
+    public void setAllowEscape(boolean allowEscape) {
+        this.allowEscape = allowEscape;
+    }
+
     @Override
     public boolean isSingleton() {
         // we cannot be singleton as we have state
@@ -122,10 +131,10 @@ public class SimpleLanguage implements L
         expression = expression.trim();
         // support old simple language syntax
         @SuppressWarnings("deprecation")
-        Predicate answer = SimpleBackwardsCompatibleParser.parsePredicate(expression);
+        Predicate answer = SimpleBackwardsCompatibleParser.parsePredicate(expression, allowEscape);
         if (answer == null) {
             // use the new parser
-            SimplePredicateParser parser = new SimplePredicateParser(expression);
+            SimplePredicateParser parser = new SimplePredicateParser(expression, allowEscape);
             answer = parser.parsePredicate();
         }
         return answer;
@@ -138,10 +147,10 @@ public class SimpleLanguage implements L
         expression = expression.trim();
         // support old simple language syntax
         @SuppressWarnings("deprecation")
-        Expression answer = SimpleBackwardsCompatibleParser.parseExpression(expression);
+        Expression answer = SimpleBackwardsCompatibleParser.parseExpression(expression, allowEscape);
         if (answer == null) {
             // use the new parser
-            SimpleExpressionParser parser = new SimpleExpressionParser(expression);
+            SimpleExpressionParser parser = new SimpleExpressionParser(expression, allowEscape);
             answer = parser.parseExpression();
         }
         if (resultType != null) {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimplePredicateParser.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimplePredicateParser.java?rev=1340961&r1=1340960&r2=1340961&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimplePredicateParser.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimplePredicateParser.java
Mon May 21 10:45:26 2012
@@ -51,8 +51,13 @@ import org.apache.camel.util.ExpressionT
  */
 public class SimplePredicateParser extends BaseSimpleParser {
 
+    @Deprecated
     public SimplePredicateParser(String expression) {
-        super(expression);
+        super(expression, true);
+    }
+
+    public SimplePredicateParser(String expression, boolean allowEscape) {
+        super(expression, allowEscape);
     }
 
     public Predicate parsePredicate() {

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=1340961&r1=1340960&r2=1340961&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
Mon May 21 10:45:26 2012
@@ -116,11 +116,12 @@ public final class SimpleTokenizer {
      *
      * @param expression  the input expression
      * @param index       the current index
+     * @param allowEscape whether to allow escapes
      * @param filter      defines the accepted token types to be returned (character is always
used as fallback)
      * @return the created token, will always return a token
      */
-    public static SimpleToken nextToken(String expression, int index, TokenType... filter)
{
-        return doNextToken(expression, index, filter);
+    public static SimpleToken nextToken(String expression, int index, boolean allowEscape,
TokenType... filter) {
+        return doNextToken(expression, index, allowEscape, filter);
     }
 
     /**
@@ -128,13 +129,14 @@ public final class SimpleTokenizer {
      *
      * @param expression  the input expression
      * @param index       the current index
+     * @param allowEscape whether to allow escapes
      * @return the created token, will always return a token
      */
-    public static SimpleToken nextToken(String expression, int index) {
-        return doNextToken(expression, index);
+    public static SimpleToken nextToken(String expression, int index, boolean allowEscape)
{
+        return doNextToken(expression, index, allowEscape);
     }
 
-    private static SimpleToken doNextToken(String expression, int index, TokenType... filters)
{
+    private static SimpleToken doNextToken(String expression, int index, boolean allowEscape,
TokenType... filters) {
 
         boolean numericAllowed = acceptType(TokenType.numericValue, filters);
         if (numericAllowed) {
@@ -165,7 +167,7 @@ public final class SimpleTokenizer {
             }
         }
 
-        boolean escapeAllowed = acceptType(TokenType.escape, filters);
+        boolean escapeAllowed = allowEscape && acceptType(TokenType.escape, filters);
         if (escapeAllowed) {
             StringBuilder sb = new StringBuilder();
             char ch = expression.charAt(index);

Modified: camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/file
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/file?rev=1340961&r1=1340960&r2=1340961&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/file
(original)
+++ camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/language/file
Mon May 21 10:45:26 2012
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-class=org.apache.camel.language.simple.SimpleLanguage
\ No newline at end of file
+class=org.apache.camel.language.simple.FileLanguage
\ No newline at end of file

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=1340961&r1=1340960&r2=1340961&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 Mon
May 21 10:45:26 2012
@@ -158,6 +158,11 @@ public class FileLanguageTest extends La
         assertExpression("backup-${bean:generator.generateFilename}.txt", "backup-generatorbybean.txt");
     }
 
+    public void testNoEscapeAllowed() throws Exception {
+        exchange.getIn().setHeader(Exchange.FILE_NAME, "hello.txt");
+        assertExpression("target\\newdir\\onwindows\\${file:name}", "target\\newdir\\onwindows\\hello.txt");
+    }
+
     public Exchange createExchange() {
         // create the file
         String uri = "file://target/filelanguage?fileExist=Override";



Mime
View raw message