camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [8/8] camel git commit: CAMEL-8484: File language - Should support file extensions with multiple dots such as tar.gz
Date Sat, 14 Mar 2015 07:16:03 GMT
CAMEL-8484: File language - Should support file extensions with multiple dots such as tar.gz


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

Branch: refs/heads/camel-2.15.x
Commit: cfe8fb54998d5be08e74c298f454e2e75f72deac
Parents: 20ae653
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sat Mar 14 07:41:16 2015 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Mar 14 08:17:30 2015 +0100

----------------------------------------------------------------------
 .../apache/camel/builder/ExpressionBuilder.java  |  6 +-----
 .../java/org/apache/camel/util/FileUtil.java     | 19 ++++++++++++++++++-
 .../apache/camel/language/FileLanguageTest.java  | 16 ++++++++++++++++
 .../java/org/apache/camel/util/FileUtilTest.java |  9 +++++++++
 4 files changed, 44 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/cfe8fb54/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 a5539e2..a31e895 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
@@ -1724,11 +1724,7 @@ public final class ExpressionBuilder {
         return new ExpressionAdapter() {
             public Object evaluate(Exchange exchange) {
                 String name = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
-                if (name != null) {
-                    return name.substring(name.lastIndexOf('.') + 1);
-                } else {
-                    return null;
-                }
+                return FileUtil.onlyExt(name);
             }
 
             @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/cfe8fb54/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 e59b575..d3db672 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
@@ -194,13 +194,30 @@ public final class FileUtil {
         if (name == null) {
             return null;
         }
-        int pos = name.lastIndexOf('.');
+        name = stripPath(name);
+
+        // extension is the first dot, as a file may have double extension such as .tar.gz
+        int pos = name.indexOf('.');
         if (pos != -1) {
             return name.substring(0, pos);
         }
         return name;
     }
 
+    public static String onlyExt(String name) {
+        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 (pos != -1) {
+            return name.substring(pos + 1);
+        }
+        return null;
+    }
+
     /**
      * Returns only the leading path (returns <tt>null</tt> if no path)
      */

http://git-wip-us.apache.org/repos/asf/camel/blob/cfe8fb54/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 3208835..407b737 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
@@ -162,6 +162,22 @@ public class FileLanguageTest extends LanguageTestSupport {
         assertExpression("target\\newdir\\onwindows\\${file:name}", "target\\newdir\\onwindows\\hello.txt");
     }
 
+    public void testFileNameDoubleExtension() throws Exception {
+        file = new File("target/filelanguage/test/bigfile.tar.gz");
+
+        String uri = "file://target/filelanguage?fileExist=Override";
+        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());
+
+        assertEquals("bigfile.tar.gz", file.getName());
+        assertExpression(answer, "${file:onlyname}", "bigfile.tar.gz");
+        assertExpression(answer, "${file:ext}", "tar.gz");
+    }
+
     public Exchange createExchange() {
         // create the file
         String uri = "file://target/filelanguage?fileExist=Override";

http://git-wip-us.apache.org/repos/asf/camel/blob/cfe8fb54/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java b/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
index 1f84aad..d386bbc 100644
--- a/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
+++ b/camel-core/src/test/java/org/apache/camel/util/FileUtilTest.java
@@ -110,6 +110,15 @@ public class FileUtilTest extends TestCase {
         assertEquals("/foo/bar", FileUtil.stripExt("/foo/bar.xml"));
     }
 
+    public void testOnlyExt() {
+        assertEquals(null, FileUtil.onlyExt(null));
+        assertEquals(null, FileUtil.onlyExt("foo"));
+        assertEquals("xml", FileUtil.onlyExt("foo.xml"));
+        assertEquals("xml", FileUtil.onlyExt("/foo/bar.xml"));
+        assertEquals("tar.gz", FileUtil.onlyExt("/foo/bigfile.tar.gz"));
+        assertEquals("tar.gz", FileUtil.onlyExt("/foo.bar/bigfile.tar.gz"));
+    }
+
     public void testOnlyPath() {
         assertEquals(null, FileUtil.onlyPath(null));
         assertEquals(null, FileUtil.onlyPath("foo"));


Mime
View raw message