You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2012/01/02 08:57:24 UTC

svn commit: r1226363 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/properties/ main/java/org/apache/camel/util/ test/java/org/apache/camel/component/properties/ test/java/org/apache/camel/util/

Author: davsclaus
Date: Mon Jan  2 07:57:23 2012
New Revision: 1226363

URL: http://svn.apache.org/viewvc?rev=1226363&view=rev
Log:
CAMEL-4848: Properties component should skip prefix and suffix tokens which are quoted

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java?rev=1226363&r1=1226362&r2=1226363&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java Mon Jan  2 07:57:23 2012
@@ -20,6 +20,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Properties;
 
+import org.apache.camel.util.StringHelper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -57,8 +58,8 @@ public class DefaultPropertiesParser imp
             // okay all okay so add the replaced as visited
             visited.addAll(replaced);
 
-            // are we done yet
-            done = !answer.contains(prefixToken);
+            // we are done when we can no longer find any prefix tokens in the answer
+            done = findTokenPosition(answer, 0, prefixToken) == -1;
         }
         return answer;
     }
@@ -74,7 +75,7 @@ public class DefaultPropertiesParser imp
         int pivot = 0;
         int size = uri.length();
         while (pivot < size) {
-            int idx = uri.indexOf(prefixToken, pivot);
+            int idx = findTokenPosition(uri, pivot, prefixToken);
             if (idx < 0) {
                 sb.append(createConstantPart(uri, pivot, size));
                 break;
@@ -83,7 +84,7 @@ public class DefaultPropertiesParser imp
                     sb.append(createConstantPart(uri, pivot, idx));
                 }
                 pivot = idx + prefixToken.length();
-                int endIdx = uri.indexOf(suffixToken, pivot);
+                int endIdx = findTokenPosition(uri, pivot, suffixToken);
                 if (endIdx < 0) {
                     throw new IllegalArgumentException("Expecting " + suffixToken + " but found end of string from text: " + uri);
                 }
@@ -115,7 +116,6 @@ public class DefaultPropertiesParser imp
                         esb.append("(and original key [").append(key).append("]) ");
                     }
                     esb.append("not found in properties from text: ").append(uri);
-                    
                     throw new IllegalArgumentException(esb.toString());
                 }
                 sb.append(part);
@@ -124,6 +124,28 @@ public class DefaultPropertiesParser imp
         }
         return sb.toString();
     }
+    
+    private int findTokenPosition(String uri, int pivot, String token) {
+        int idx = uri.indexOf(token, pivot);
+        while (idx > 0) {
+            // grab part as the previous char + token + next char, to test if the token is quoted
+            String part = null;
+            int len = idx + token.length() + 1;
+            if (uri.length() >= len) {
+                part = uri.substring(idx - 1, len);
+            }
+            if (StringHelper.isQuoted(part)) {
+                // the token was quoted, so regard it as a literal
+                // and then try to find from next position
+                pivot = idx + token.length() + 1;
+                idx = uri.indexOf(token, pivot);
+            } else {
+                // found token
+                return idx;
+            }
+        }
+        return idx;
+    }
 
     private String createConstantPart(String uri, int start, int end) {
         return uri.substring(start, end);

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java?rev=1226363&r1=1226362&r2=1226363&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/StringHelper.java Mon Jan  2 07:57:23 2012
@@ -89,6 +89,21 @@ public final class StringHelper {
         }
         return s;
     }
+    
+    public static boolean isQuoted(String s) {
+        if (ObjectHelper.isEmpty(s)) {
+            return false;
+        }
+
+        if (s.startsWith("'") && s.endsWith("'")) {
+            return true;
+        }
+        if (s.startsWith("\"") && s.endsWith("\"")) {
+            return true;
+        }
+
+        return false;
+    }
 
     /**
      * Encodes the text into safe XML by replacing < > and & with XML tokens

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java?rev=1226363&r1=1226362&r2=1226363&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java Mon Jan  2 07:57:23 2012
@@ -464,6 +464,16 @@ public class PropertiesComponentTest ext
 
         assertMockEndpointsSatisfied();
     }
+    
+    public void testQuotedPrefix() throws Exception {
+       assertEquals("mock", context.resolvePropertyPlaceholders("{{cool.mock}}"));
+       assertEquals("'{{' + something + '}}'", context.resolvePropertyPlaceholders("'{{' + something + '}}'"));
+       assertEquals("\"{{\" + something + \"}}\"", context.resolvePropertyPlaceholders("\"{{\" + something + \"}}\""));
+       assertEquals("mock'", context.resolvePropertyPlaceholders("{{cool.mock}}'"));
+       assertEquals("mock\"", context.resolvePropertyPlaceholders("{{cool.mock}}\""));
+       assertEquals("'mock", context.resolvePropertyPlaceholders("'{{cool.mock}}"));
+       assertEquals("\"mock", context.resolvePropertyPlaceholders("\"{{cool.mock}}"));
+    }
 
     @Override
     protected CamelContext createCamelContext() throws Exception {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java?rev=1226363&r1=1226362&r2=1226363&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/util/StringHelperTest.java Mon Jan  2 07:57:23 2012
@@ -95,5 +95,23 @@ public class StringHelperTest extends Te
         assertEquals(true, StringHelper.hasStartToken("${body}", "foo"));
         assertEquals(true, StringHelper.hasStartToken("$foo{body}", "foo"));
     }
+    
+    public void testIsQuoted() throws Exception {
+        assertEquals(false, StringHelper.isQuoted(null));
+        assertEquals(false, StringHelper.isQuoted(""));
+        assertEquals(false, StringHelper.isQuoted(" "));
+        assertEquals(false, StringHelper.isQuoted("abc"));
+        assertEquals(false, StringHelper.isQuoted("abc'"));
+        assertEquals(false, StringHelper.isQuoted("\"abc'"));
+        assertEquals(false, StringHelper.isQuoted("abc\""));
+        assertEquals(false, StringHelper.isQuoted("'abc\""));
+        assertEquals(false, StringHelper.isQuoted("\"abc'"));
+        assertEquals(false, StringHelper.isQuoted("abc'def'"));
+        assertEquals(false, StringHelper.isQuoted("abc'def'ghi"));
+        assertEquals(false, StringHelper.isQuoted("'def'ghi"));
+
+        assertEquals(true, StringHelper.isQuoted("'abc'"));
+        assertEquals(true, StringHelper.isQuoted("\"abc\""));
+    }
 
 }