CAMEL-10136: Add tokenize group in ValueBuilder
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/2929ee72 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/2929ee72 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/2929ee72 Branch: refs/heads/camel-2.17.x Commit: 2929ee72b37116d6f9a6f3412e3e75beaa454738 Parents: 0d4511a Author: Claus Ibsen <davscl...@apache.org> Authored: Wed Jul 27 09:45:08 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Jul 27 09:45:36 2016 +0200 ---------------------------------------------------------------------- .../org/apache/camel/builder/ValueBuilder.java | 10 ++++ .../processor/SplitTokenizerGroupTest.java | 59 ++++++++++++++++++++ 2 files changed, 69 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/2929ee72/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java index a8faf61..91b5fcd 100644 --- a/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java +++ b/camel-core/src/main/java/org/apache/camel/builder/ValueBuilder.java @@ -184,6 +184,16 @@ public class ValueBuilder implements Expression, Predicate { return onNewValueBuilder(newExp); } + public ValueBuilder tokenize(String token, int group, boolean skipFirst) { + Expression newExp = ExpressionBuilder.tokenizeExpression(expression, token); + if (group == 0 && skipFirst) { + // wrap in skip first (if group then it has its own skip first logic) + newExp = ExpressionBuilder.skipFirstExpression(newExp); + } + newExp = ExpressionBuilder.groupIteratorExpression(newExp, token, group, skipFirst); + return onNewValueBuilder(newExp); + } + public ValueBuilder tokenizeXML(String tagName, String inheritNamespaceTagName) { Expression newExp = ExpressionBuilder.tokenizeXMLExpression(tagName, inheritNamespaceTagName); return onNewValueBuilder(newExp); http://git-wip-us.apache.org/repos/asf/camel/blob/2929ee72/camel-core/src/test/java/org/apache/camel/processor/SplitTokenizerGroupTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/processor/SplitTokenizerGroupTest.java b/camel-core/src/test/java/org/apache/camel/processor/SplitTokenizerGroupTest.java new file mode 100644 index 0000000..22962b4 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/processor/SplitTokenizerGroupTest.java @@ -0,0 +1,59 @@ +/** + * 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.processor; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; + +public class SplitTokenizerGroupTest extends ContextTestSupport { + + public void testSplitTokenizerA() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:split"); + mock.expectedBodiesReceived("Claus,James", "Willem"); + + template.sendBody("direct:a", "Claus,James,Willem"); + + assertMockEndpointsSatisfied(); + } + + public void testSplitTokenizerB() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:split"); + mock.expectedBodiesReceived("James,Willem"); + + template.sendBody("direct:b", "Claus,James,Willem".getBytes()); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + + from("direct:a") + .split().tokenize(",", 2) + .to("mock:split"); + + from("direct:b") + .split(bodyAs(String.class).tokenize(",", 2, true)) + .to("mock:split"); + } + }; + } +}