camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1343037 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/language/simple/ camel-core/src/main/java/org/apache/camel/model/language/ camel-core/src/test/java/org/apache/camel/language/simple/ components/camel-spring/src/test/jav...
Date Sun, 27 May 2012 12:22:05 GMT
Author: davsclaus
Date: Sun May 27 12:22:05 2012
New Revision: 1343037

URL: http://svn.apache.org/viewvc?rev=1343037&view=rev
Log:
CAMEL-5294: XML DSL language expressions added trim option. This removes the whitespace noise
from XML in the expressions.

Added:
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/example/SpringExpressionTrimTest.java
      - copied, changed from r1343030, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/example/SpringXPathFilterTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/example/expressionTrim.xml
      - copied, changed from r1343030, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/example/xpathFilter.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/SimpleExpression.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java

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=1343037&r1=1343036&r2=1343037&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
Sun May 27 12:22:05 2012
@@ -127,8 +127,6 @@ public class SimpleLanguage implements L
     public Predicate createPredicate(String expression) {
         ObjectHelper.notNull(expression, "expression");
 
-        // trim the expression first
-        expression = expression.trim();
         // support old simple language syntax
         @SuppressWarnings("deprecation")
         Predicate answer = SimpleBackwardsCompatibleParser.parsePredicate(expression, allowEscape);
@@ -143,8 +141,6 @@ public class SimpleLanguage implements L
     public Expression createExpression(String expression) {
         ObjectHelper.notNull(expression, "expression");
 
-        // trim the expression first
-        expression = expression.trim();
         // support old simple language syntax
         @SuppressWarnings("deprecation")
         Expression answer = SimpleBackwardsCompatibleParser.parseExpression(expression, allowEscape);

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionDefinition.java?rev=1343037&r1=1343036&r2=1343037&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionDefinition.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/ExpressionDefinition.java
Sun May 27 12:22:05 2012
@@ -56,6 +56,8 @@ public class ExpressionDefinition implem
     private String id;
     @XmlValue
     private String expression;
+    @XmlAttribute
+    private Boolean trim;
     @XmlTransient
     private Predicate predicate;
     @XmlTransient
@@ -150,7 +152,12 @@ public class ExpressionDefinition implem
             } else if (getExpression() != null) {
                 ObjectHelper.notNull("language", getLanguage());
                 Language language = camelContext.resolveLanguage(getLanguage());
-                predicate = language.createPredicate(getExpression());
+                String exp = getExpression();
+                // trim if configured to trim
+                if (exp != null && isTrim()) {
+                    exp = exp.trim();
+                }
+                predicate = language.createPredicate(exp);
                 configurePredicate(camelContext, predicate);
             }
         }
@@ -168,7 +175,12 @@ public class ExpressionDefinition implem
             } else if (getExpression() != null) {
                 ObjectHelper.notNull("language", getLanguage());
                 Language language = camelContext.resolveLanguage(getLanguage());
-                setExpressionValue(language.createExpression(getExpression()));
+                String exp = getExpression();
+                // trim if configured to trim
+                if (exp != null && isTrim()) {
+                    exp = exp.trim();
+                }
+                setExpressionValue(language.createExpression(exp));
                 configureExpression(camelContext, getExpressionValue());
             }
         }
@@ -214,6 +226,19 @@ public class ExpressionDefinition implem
         return expressionType;
     }
 
+    public Boolean getTrim() {
+        return trim;
+    }
+
+    public void setTrim(Boolean trim) {
+        this.trim = trim;
+    }
+
+    public boolean isTrim() {
+        // trim by default
+        return trim == null || trim;
+    }
+
     /**
      * Returns some descriptive text to describe this node
      */

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/SimpleExpression.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/SimpleExpression.java?rev=1343037&r1=1343036&r2=1343037&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/SimpleExpression.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/language/SimpleExpression.java
Sun May 27 12:22:05 2012
@@ -83,7 +83,12 @@ public class SimpleExpression extends Ex
             }
         }
 
-        SimpleBuilder answer = new SimpleBuilder(getExpression());
+        String exp = getExpression();
+        if (isTrim() && exp != null) {
+            exp = exp.trim();
+        }
+
+        SimpleBuilder answer = new SimpleBuilder(exp);
         answer.setResultType(resultType);
         return answer;
     }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java?rev=1343037&r1=1343036&r2=1343037&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
Sun May 27 12:22:05 2012
@@ -77,7 +77,7 @@ public class SimpleTest extends Language
 
     public void testEmptyExpression() throws Exception {
         assertExpression("", "");
-        assertExpression(" ", "");
+        assertExpression(" ", " ");
         try {
             assertExpression(null, null);
             fail("Should have thrown exception");
@@ -126,10 +126,10 @@ public class SimpleTest extends Language
     }
     
     public void testTrimSimpleExpressions() throws Exception {
-        assertExpression(" \texchangeId\n", exchange.getExchangeId());
-        assertExpression("\nid\r", exchange.getIn().getMessageId());
-        assertExpression("\t\r body", "<hello id='m123'>world!</hello>");
-        assertExpression("\nin.body\r", "<hello id='m123'>world!</hello>");
+        assertExpression(" \texchangeId\n".trim(), exchange.getExchangeId());
+        assertExpression("\nid\r".trim(), exchange.getIn().getMessageId());
+        assertExpression("\t\r body".trim(), "<hello id='m123'>world!</hello>");
+        assertExpression("\nin.body\r".trim(), "<hello id='m123'>world!</hello>");
     }
 
     public void testSimpleThreadName() throws Exception {

Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/example/SpringExpressionTrimTest.java
(from r1343030, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/example/SpringXPathFilterTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/example/SpringExpressionTrimTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/example/SpringExpressionTrimTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/example/SpringXPathFilterTest.java&r1=1343030&r2=1343037&rev=1343037&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/example/SpringXPathFilterTest.java
(original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/example/SpringExpressionTrimTest.java
Sun May 27 12:22:05 2012
@@ -16,24 +16,34 @@
  */
 package org.apache.camel.spring.example;
 
-import org.apache.camel.CamelContext;
-import org.apache.camel.processor.XPathFilterTest;
-import org.apache.camel.spring.SpringCamelContext;
+import org.apache.camel.spring.SpringTestSupport;
+import org.springframework.context.support.AbstractXmlApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 /**
  * @version 
  */
-public class SpringXPathFilterTest extends XPathFilterTest {
+public class SpringExpressionTrimTest extends SpringTestSupport {
 
     @Override
-    public boolean isUseRouteBuilder() {
-        return false;
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/spring/example/expressionTrim.xml");
     }
 
-    @Override
-    protected CamelContext createCamelContext() throws Exception {
-        return SpringCamelContext.springCamelContext(new ClassPathXmlApplicationContext("org/apache/camel/spring/example/xpathFilter.xml"));
+    public void testDefault() throws Exception {
+        String out = template.requestBody("direct:a", "World", String.class);
+        assertEquals("Hello World", out);
+    }
+
+    public void testTrim() throws Exception {
+        String out = template.requestBody("direct:b", "World", String.class);
+        assertEquals("Hello World", out);
+    }
+
+    public void testNoTrim() throws Exception {
+        String out = template.requestBody("direct:c", "World", String.class);
+        // the xml has whitespace noise which is not trimmed
+        assertEquals("\n          Hello World\n        ", out);
     }
 
 }

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/example/expressionTrim.xml
(from r1343030, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/example/xpathFilter.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/example/expressionTrim.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/example/expressionTrim.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/example/xpathFilter.xml&r1=1343030&r2=1343037&rev=1343037&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/example/xpathFilter.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/example/expressionTrim.xml
Sun May 27 12:22:05 2012
@@ -22,18 +22,33 @@
        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-  <!-- START SNIPPET: example -->
   <camelContext xmlns="http://camel.apache.org/schema/spring">
     <route>
-      <from uri="direct:start"/>
-      <filter>
-        <xpath>
-          /person[@name='James']
-        </xpath>
-        <to uri="mock:result"/>
-      </filter>
+      <from uri="direct:a"/>
+      <transform>
+        <simple>
+          Hello ${body}
+        </simple>
+      </transform>
+    </route>
+
+    <route>
+      <from uri="direct:b"/>
+      <transform>
+        <simple trim="true">
+          Hello ${body}
+        </simple>
+      </transform>
+    </route>
+
+    <route>
+      <from uri="direct:c"/>
+      <transform>
+        <simple trim="false">
+          Hello ${body}
+        </simple>
+      </transform>
     </route>
   </camelContext>
-  <!-- END SNIPPET: example -->
 
 </beans>



Mime
View raw message