camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [5/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:00 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/bb3e5107
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/bb3e5107
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/bb3e5107

Branch: refs/heads/camel-2.14.x
Commit: bb3e5107a69bb6e3882af9f4d7a2115edf2fa2f7
Parents: 82d7c3a
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:13 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/bb3e5107/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 4473fe7..f0333ed 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
@@ -1698,11 +1698,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/bb3e5107/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/bb3e5107/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/bb3e5107/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 89446e9..2160683 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