Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 23380D82E for ; Thu, 16 May 2013 01:52:57 +0000 (UTC) Received: (qmail 47440 invoked by uid 500); 16 May 2013 01:52:57 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 47395 invoked by uid 500); 16 May 2013 01:52:57 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 47388 invoked by uid 99); 16 May 2013 01:52:57 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 May 2013 01:52:57 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id AA20B12C39; Thu, 16 May 2013 01:52:56 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: ningjiang@apache.org To: commits@camel.apache.org Message-Id: <8dea1630444f47409833462f2cabe603@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: git commit: CAMEL-6363 Allow to specify the string-template delimiters with thanks to Antoine Date: Thu, 16 May 2013 01:52:56 +0000 (UTC) Updated Branches: refs/heads/camel-2.11.x 799736421 -> a2b1df78e CAMEL-6363 Allow to specify the string-template delimiters with thanks to Antoine Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a2b1df78 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a2b1df78 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a2b1df78 Branch: refs/heads/camel-2.11.x Commit: a2b1df78e7d5db653878a7e65cd916c55a26e116 Parents: 7997364 Author: Willem Jiang Authored: Thu May 16 09:49:14 2013 +0800 Committer: Willem Jiang Committed: Thu May 16 09:51:47 2013 +0800 ---------------------------------------------------------------------- .../stringtemplate/StringTemplateEndpoint.java | 26 +++++- .../StringTemplateCustomDelimiterTest.java | 63 +++++++++++++++ .../stringtemplate/custom-delimiter-brace.tm | 17 ++++ .../stringtemplate/custom-delimiter-dollar.tm | 17 ++++ 4 files changed, 119 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a2b1df78/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java b/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java index 61f6086..a5d6837 100644 --- a/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java +++ b/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java @@ -27,11 +27,14 @@ import org.apache.camel.component.ResourceEndpoint; import org.apache.camel.util.ExchangeHelper; import org.stringtemplate.v4.NoIndentWriter; import org.stringtemplate.v4.ST; +import org.stringtemplate.v4.STGroup; /** - * @version + * @version */ public class StringTemplateEndpoint extends ResourceEndpoint { + private char delimiterStart = STGroup.defaultGroup.delimiterStartChar; + private char delimiterStop = STGroup.defaultGroup.delimiterStopChar; public StringTemplateEndpoint() { } @@ -50,6 +53,22 @@ public class StringTemplateEndpoint extends ResourceEndpoint { return ExchangePattern.InOut; } + public char getDelimiterStart() { + return delimiterStart; + } + + public void setDelimiterStart(char delimiterStart) { + this.delimiterStart = delimiterStart; + } + + public char getDelimiterStop() { + return delimiterStop; + } + + public void setDelimiterStop(char delimiterStop) { + this.delimiterStop = delimiterStop; + } + @Override protected void onExchange(Exchange exchange) throws Exception { StringWriter buffer = new StringWriter(); @@ -57,7 +76,7 @@ public class StringTemplateEndpoint extends ResourceEndpoint { // getResourceAsInputStream also considers the content cache String text = exchange.getContext().getTypeConverter().mandatoryConvertTo(String.class, getResourceAsInputStream()); - ST template = new ST(text); + ST template = new ST(text, delimiterStart, delimiterStop); for (Map.Entry entry : variableMap.entrySet()) { template.add(entry.getKey(), entry.getValue()); } @@ -71,5 +90,4 @@ public class StringTemplateEndpoint extends ResourceEndpoint { out.setHeader(StringTemplateConstants.STRINGTEMPLATE_RESOURCE_URI, getResourceUri()); out.setAttachments(exchange.getIn().getAttachments()); } - -} \ No newline at end of file +} http://git-wip-us.apache.org/repos/asf/camel/blob/a2b1df78/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateCustomDelimiterTest.java ---------------------------------------------------------------------- diff --git a/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateCustomDelimiterTest.java b/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateCustomDelimiterTest.java new file mode 100644 index 0000000..9560f95 --- /dev/null +++ b/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateCustomDelimiterTest.java @@ -0,0 +1,63 @@ +/** + * 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.component.stringtemplate; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class StringTemplateCustomDelimiterTest extends CamelTestSupport { + private static final String DIRECT_BRACE = "direct:brace"; + private static final String DIRECT_DOLLAR = "direct:dollar"; + + @Test + public void testWithBraceDelimiter() { + Exchange response = template.request(DIRECT_BRACE, new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody("Yay !"); + } + }); + + assertEquals("With brace delimiter Yay !", response.getOut().getBody().toString().trim()); + } + + @Test + public void testWithDollarDelimiter() { + Exchange response = template.request(DIRECT_DOLLAR, new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody("Yay !"); + } + }); + + assertEquals("With identical dollar delimiter Yay !", response.getOut().getBody().toString().trim()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from(DIRECT_BRACE).to("string-template:org/apache/camel/component/stringtemplate/custom-delimiter-brace.tm?delimiterStart={&delimiterStop=}"); + from(DIRECT_DOLLAR).to("string-template:org/apache/camel/component/stringtemplate/custom-delimiter-dollar.tm?delimiterStart=$&delimiterStop=$"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a2b1df78/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/custom-delimiter-brace.tm ---------------------------------------------------------------------- diff --git a/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/custom-delimiter-brace.tm b/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/custom-delimiter-brace.tm new file mode 100644 index 0000000..b07efd8 --- /dev/null +++ b/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/custom-delimiter-brace.tm @@ -0,0 +1,17 @@ +{! ------------------------------------------------------------------------ +## 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. +## ------------------------------------------------------------------------ !} +With brace delimiter {body} http://git-wip-us.apache.org/repos/asf/camel/blob/a2b1df78/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/custom-delimiter-dollar.tm ---------------------------------------------------------------------- diff --git a/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/custom-delimiter-dollar.tm b/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/custom-delimiter-dollar.tm new file mode 100644 index 0000000..84fb234 --- /dev/null +++ b/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/custom-delimiter-dollar.tm @@ -0,0 +1,17 @@ +$! ------------------------------------------------------------------------ +## 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. +## ------------------------------------------------------------------------ !$ +With identical dollar delimiter $body$