camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [3/3] camel git commit: CAMEL-9064: File language - Add functions to get extension in single mode
Date Sat, 08 Aug 2015 10:38:14 GMT
CAMEL-9064: File language - Add functions to get extension in single mode


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/93cdc379
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/93cdc379
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/93cdc379

Branch: refs/heads/camel-2.14.x
Commit: 93cdc379b873cd1c3a25d23a4ebec587aff60c53
Parents: dbfd70d
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sat Aug 8 10:22:50 2015 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Aug 8 12:45:44 2015 +0200

----------------------------------------------------------------------
 .../apache/camel/builder/ExpressionBuilder.java | 42 +++++++++++
 .../simple/ast/SimpleFunctionExpression.java    | 10 ++-
 .../java/org/apache/camel/util/FileUtil.java    | 16 ++++-
 .../language/FileLanguageExtSingleTest.java     | 73 ++++++++++++++++++++
 .../apache/camel/language/FileLanguageTest.java |  3 +
 5 files changed, 138 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/93cdc379/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
index f0333ed..d477517 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
@@ -1680,6 +1680,20 @@ public final class ExpressionBuilder {
         };
     }
 
+    public static Expression fileNameNoExtensionSingleExpression() {
+        return new ExpressionAdapter() {
+            public Object evaluate(Exchange exchange) {
+                String name = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
+                return FileUtil.stripExt(name, true);
+            }
+
+            @Override
+            public String toString() {
+                return "file:name.noext.single";
+            }
+        };
+    }
+
     public static Expression fileOnlyNameNoExtensionExpression() {
         return new ExpressionAdapter() {
             public Object evaluate(Exchange exchange) {
@@ -1694,6 +1708,20 @@ public final class ExpressionBuilder {
         };
     }
 
+    public static Expression fileOnlyNameNoExtensionSingleExpression() {
+        return new ExpressionAdapter() {
+            public Object evaluate(Exchange exchange) {
+                String name = fileOnlyNameExpression().evaluate(exchange, String.class);
+                return FileUtil.stripExt(name, true);
+            }
+
+            @Override
+            public String toString() {
+                return "file:onlyname.noext.single";
+            }
+        };
+    }
+
     public static Expression fileExtensionExpression() {
         return new ExpressionAdapter() {
             public Object evaluate(Exchange exchange) {
@@ -1708,6 +1736,20 @@ public final class ExpressionBuilder {
         };
     }
 
+    public static Expression fileExtensionSingleExpression() {
+        return new ExpressionAdapter() {
+            public Object evaluate(Exchange exchange) {
+                String name = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
+                return FileUtil.onlyExt(name, true);
+            }
+
+            @Override
+            public String toString() {
+                return "file:ext.single";
+            }
+        };
+    }
+
     public static Expression fileParentExpression() {
         return new ExpressionAdapter() {
             public Object evaluate(Exchange exchange) {

http://git-wip-us.apache.org/repos/asf/camel/blob/93cdc379/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
index d63e854..6ddd7be 100644
--- a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
+++ b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
@@ -345,14 +345,18 @@ public class SimpleFunctionExpression extends LiteralExpression {
             return ExpressionBuilder.fileNameExpression();
         } else if (ObjectHelper.equal(remainder, "name.noext")) {
             return ExpressionBuilder.fileNameNoExtensionExpression();
-        } else if (ObjectHelper.equal(remainder, "name.ext")) {
+        } else if (ObjectHelper.equal(remainder, "name.noext.single")) {
+            return ExpressionBuilder.fileNameNoExtensionSingleExpression();
+        } else if (ObjectHelper.equal(remainder, "name.ext") || ObjectHelper.equal(remainder,
"ext")) {
             return ExpressionBuilder.fileExtensionExpression();
+        } else if (ObjectHelper.equal(remainder, "name.ext.single")) {
+            return ExpressionBuilder.fileExtensionSingleExpression();
         } else if (ObjectHelper.equal(remainder, "onlyname")) {
             return ExpressionBuilder.fileOnlyNameExpression();
         } else if (ObjectHelper.equal(remainder, "onlyname.noext")) {
             return ExpressionBuilder.fileOnlyNameNoExtensionExpression();
-        } else if (ObjectHelper.equal(remainder, "ext")) {
-            return ExpressionBuilder.fileExtensionExpression();
+        } else if (ObjectHelper.equal(remainder, "onlyname.noext.single")) {
+            return ExpressionBuilder.fileOnlyNameNoExtensionSingleExpression();
         } else if (ObjectHelper.equal(remainder, "parent")) {
             return ExpressionBuilder.fileParentExpression();
         } else if (ObjectHelper.equal(remainder, "path")) {

http://git-wip-us.apache.org/repos/asf/camel/blob/93cdc379/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/FileUtil.java b/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
index 3f978ed..81585fc 100644
--- a/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
+++ b/camel-core/src/main/java/org/apache/camel/util/FileUtil.java
@@ -191,6 +191,10 @@ public final class FileUtil {
     }
 
     public static String stripExt(String name) {
+        return stripExt(name, false);
+    }
+
+    public static String stripExt(String name, boolean singleMode) {
         if (name == null) {
             return null;
         }
@@ -202,12 +206,13 @@ public final class FileUtil {
 
         if (pos > 0) {
             String onlyName = name.substring(pos + 1);
-            int pos2 = onlyName.indexOf('.');
+            int pos2 = singleMode ? onlyName.lastIndexOf('.') : onlyName.indexOf('.');
             if (pos2 > 0) {
                 return name.substring(0, pos + pos2 + 1);
             }
         } else {
-            int pos2 = name.indexOf('.');
+            // if single ext mode, then only return last extension
+            int pos2 = singleMode ? name.lastIndexOf('.') : name.indexOf('.');
             if (pos2 > 0) {
                 return name.substring(0, pos2);
             }
@@ -217,13 +222,18 @@ public final class FileUtil {
     }
 
     public static String onlyExt(String name) {
+        return onlyExt(name, false);
+    }
+
+    public static String onlyExt(String name, boolean singleMode) {
         if (name == null) {
             return null;
         }
         name = stripPath(name);
 
         // extension is the first dot, as a file may have double extension such as .tar.gz
-        int pos = name.indexOf('.');
+        // if single ext mode, then only return last extension
+        int pos = singleMode ? name.lastIndexOf('.') : name.indexOf('.');
         if (pos != -1) {
             return name.substring(pos + 1);
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/93cdc379/camel-core/src/test/java/org/apache/camel/language/FileLanguageExtSingleTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/FileLanguageExtSingleTest.java
b/camel-core/src/test/java/org/apache/camel/language/FileLanguageExtSingleTest.java
new file mode 100644
index 0000000..ccdc314
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/language/FileLanguageExtSingleTest.java
@@ -0,0 +1,73 @@
+/**
+ * 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;
+
+import java.io.File;
+import java.util.Calendar;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.LanguageTestSupport;
+import org.apache.camel.component.file.FileConsumer;
+import org.apache.camel.component.file.FileEndpoint;
+import org.apache.camel.component.file.GenericFile;
+
+/**
+ * Unit test for File Language.
+ */
+public class FileLanguageExtSingleTest extends LanguageTestSupport {
+
+    private File file;
+
+    protected String getLanguageName() {
+        return "file";
+    }
+
+    public void testFileNoSingleExt() throws Exception {
+        assertExpression("${file:name}", "test" + File.separator + "bye.def.txt");
+        assertExpression("${file:name.noext}", "test" + File.separator + "bye");
+        assertExpression("${file:name.noext.single}", "test" + File.separator + "bye.def");
+        assertExpression("${file:name.ext}", "def.txt");
+        assertExpression("${file:name.ext.single}", "txt");
+
+        assertExpression("${file:onlyname.noext}", "bye");
+        assertExpression("${file:onlyname.noext.single}", "bye.def");
+    }
+
+    public Exchange createExchange() {
+        // create the file
+        String uri = "file://target/filelanguage?fileExist=Override";
+        template.sendBodyAndHeader(uri, "Bye World", Exchange.FILE_NAME, "test/bye.def.txt");
+
+        // get the file handle
+        file = new File("target/filelanguage/test/bye.def.txt");
+        GenericFile<File> gf = FileConsumer.asGenericFile("target/filelanguage", file,
null);
+
+        FileEndpoint endpoint = getMandatoryEndpoint(uri, FileEndpoint.class);
+
+        Exchange answer = endpoint.createExchange(gf);
+        endpoint.configureMessage(gf, answer.getIn());
+
+        Calendar cal = Calendar.getInstance();
+        cal.set(1974, Calendar.APRIL, 20);
+        answer.getIn().setHeader("birthday", cal.getTime());
+
+        cal.set(2008, Calendar.AUGUST, 8);
+        answer.getOut().setHeader("special", cal.getTime());
+        return answer;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/93cdc379/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java b/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
index 407b737..1e98e1d 100644
--- a/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
+++ b/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
@@ -71,10 +71,13 @@ public class FileLanguageTest extends LanguageTestSupport {
     public void testFile() throws Exception {
         assertExpression("${file:ext}", "txt");
         assertExpression("${file:name.ext}", "txt");
+        assertExpression("${file:name.ext.single}", "txt");
         assertExpression("${file:name}", "test" + File.separator + file.getName());
         assertExpression("${file:name.noext}", "test" + File.separator + "hello");
+        assertExpression("${file:name.noext.single}", "test" + File.separator + "hello");
         assertExpression("${file:onlyname}", file.getName());
         assertExpression("${file:onlyname.noext}", "hello");
+        assertExpression("${file:onlyname.noext.single}", "hello");
         assertExpression("${file:parent}", file.getParent());
         assertExpression("${file:path}", file.getPath());
         assertExpression("${file:absolute}", FileUtil.isAbsolute(file));


Mime
View raw message