From 71c55750d1eef8fb98c6614d3ebf5e2996622976 Mon Sep 17 00:00:00 2001 From: synapticloop Date: Tue, 7 Feb 2017 19:42:16 +0000 Subject: [PATCH] added in substring and startswith string functions --- README.md | 10 ++--- build.gradle | 2 +- .../function/string/FunctionStartsWith.java | 41 +++++++++++++++++++ .../function/string/FunctionSubString.java | 34 +++++++++++++-- .../templar/utils/TemplarContext.java | 9 ++-- .../string/FunctionSubStringTest.java | 28 +++++++++++++ 6 files changed, 111 insertions(+), 13 deletions(-) create mode 100644 src/main/java/synapticloop/templar/function/string/FunctionStartsWith.java create mode 100644 src/test/java/synapticloop/templar/function/string/FunctionSubStringTest.java diff --git a/README.md b/README.md index f44f29d..3c77338 100644 --- a/README.md +++ b/README.md @@ -216,9 +216,9 @@ repositories { ``` dependencies { - runtime(group: 'synapticloop', name: 'templar', version: '1.4.1', ext: 'jar') + runtime(group: 'synapticloop', name: 'templar', version: '1.4.2', ext: 'jar') - compile(group: 'synapticloop', name: 'templar', version: '1.4.1', ext: 'jar') + compile(group: 'synapticloop', name: 'templar', version: '1.4.2', ext: 'jar') } ``` @@ -226,9 +226,9 @@ or, more simply for versions of gradle greater than 2.1 ``` dependencies { - runtime 'synapticloop:templar:1.4.1' + runtime 'synapticloop:templar:1.4.2' - compile 'synapticloop:templar:1.4.1' + compile 'synapticloop:templar:1.4.2' } ``` @@ -238,7 +238,7 @@ dependencies { synapticloop templar - 1.4.1 + 1.4.2 jar ``` diff --git a/build.gradle b/build.gradle index 57b8dd0..195c121 100644 --- a/build.gradle +++ b/build.gradle @@ -20,7 +20,7 @@ plugins { group = 'synapticloop' archivesBaseName = 'templar' description = """Templar templating engine""" -version = '1.4.1' +version = '1.4.2' sourceCompatibility = 1.7 targetCompatibility = 1.7 diff --git a/src/main/java/synapticloop/templar/function/string/FunctionStartsWith.java b/src/main/java/synapticloop/templar/function/string/FunctionStartsWith.java new file mode 100644 index 0000000..d986209 --- /dev/null +++ b/src/main/java/synapticloop/templar/function/string/FunctionStartsWith.java @@ -0,0 +1,41 @@ +package synapticloop.templar.function.string; + +import synapticloop.templar.exception.FunctionException; +import synapticloop.templar.function.Function; +import synapticloop.templar.helper.ObjectHelper; +import synapticloop.templar.utils.TemplarContext; + +/* + * Copyright (c) 2017 Synapticloop. + * + * All rights reserved. + * + * This code may contain contributions from other parties which, where + * applicable, will be listed in the default build file for the project + * ~and/or~ in a file named CONTRIBUTORS.txt in the root of the project. + * + * This source code and any derived binaries are covered by the terms and + * conditions of the Licence agreement ("the Licence"). You may not use this + * source code or any derived binaries except in compliance with the Licence. + * A copy of the Licence is available in the file named LICENSE.txt shipped with + * this source code or binaries. + */ + +public class FunctionStartsWith extends Function { + + public FunctionStartsWith() { + super(2); + } + + @Override + protected Object evaluateFunction(Object[] args, TemplarContext templarContext) throws FunctionException { + Object argZero = ObjectHelper.evaluateObjectToDefault(args[0], templarContext); + Object argOne = ObjectHelper.evaluateObjectToDefault(args[1], templarContext); + if(null == argZero || null == argOne) { + throw new FunctionException("Could not evaluate arguments to String, arguments were: " + args[0] + ", " + args[1] + ", values: " + argZero + ", " + argOne); + } + + return(argOne.toString().startsWith(argZero.toString())); + } + +} diff --git a/src/main/java/synapticloop/templar/function/string/FunctionSubString.java b/src/main/java/synapticloop/templar/function/string/FunctionSubString.java index 02fb56e..e4a5e95 100644 --- a/src/main/java/synapticloop/templar/function/string/FunctionSubString.java +++ b/src/main/java/synapticloop/templar/function/string/FunctionSubString.java @@ -2,19 +2,45 @@ import synapticloop.templar.exception.FunctionException; import synapticloop.templar.function.Function; +import synapticloop.templar.helper.ObjectHelper; import synapticloop.templar.utils.TemplarContext; public class FunctionSubString extends Function { public FunctionSubString() { - super(3); + super(2, 3); } @Override protected Object evaluateFunction(Object[] args, TemplarContext templarContext) throws FunctionException { - // arguments can be either 2 or 3 - // TODO Auto-generated method stub - return null; + Object argZero = ObjectHelper.evaluateObjectToDefault(args[0], templarContext); + Object argOne = ObjectHelper.evaluateObjectToDefault(args[1], templarContext); + + int from = 0; + int to = 0; + + try { + from = Integer.parseInt(argOne.toString()); + } catch(NumberFormatException ex) { + throw new FunctionException("Could not evaluate arguments to Integer, arguments was: " + args[1] + ", value: " + argOne); + } + + Object argTwo = null; + if(args.length == 3) { + argTwo = ObjectHelper.evaluateObjectToDefault(args[2], templarContext); + try { + to = Integer.parseInt(argTwo.toString()); + } catch(NumberFormatException ex) { + throw new FunctionException("Could not evaluate arguments to Integer, arguments was: " + args[2] + ", value: " + argTwo); + } + } + + // at this point we should be good to go + if(args.length == 3) { + return(argZero.toString().substring(from, to)); + } else { + return(argZero.toString().substring(from)); + } } } diff --git a/src/main/java/synapticloop/templar/utils/TemplarContext.java b/src/main/java/synapticloop/templar/utils/TemplarContext.java index 862b817..1bee802 100644 --- a/src/main/java/synapticloop/templar/utils/TemplarContext.java +++ b/src/main/java/synapticloop/templar/utils/TemplarContext.java @@ -48,6 +48,8 @@ import synapticloop.templar.function.math.FunctionPower; import synapticloop.templar.function.math.FunctionSubtract; import synapticloop.templar.function.string.FunctionIndexOf; +import synapticloop.templar.function.string.FunctionStartsWith; +import synapticloop.templar.function.string.FunctionSubString; import synapticloop.templar.function.string.FunctionToJson; import synapticloop.templar.function.util.FunctionInstanceOf; @@ -104,9 +106,10 @@ public class TemplarContext { // string functionMap.put("indexOf", new FunctionIndexOf()); // get the index of strings functionMap.put("toJson", new FunctionToJson()); // convert a string into a JSON object - // NIY -// functionMap.put("substring", new FunctionSubString()); // get the substring -} + functionMap.put("startsWith", new FunctionStartsWith()); // determine whether a string starts with another string + functionMap.put("substring", new FunctionSubString()); // return a substring of a string + + } private static Map functionAliasMap = new HashMap(); static { diff --git a/src/test/java/synapticloop/templar/function/string/FunctionSubStringTest.java b/src/test/java/synapticloop/templar/function/string/FunctionSubStringTest.java new file mode 100644 index 0000000..df8b348 --- /dev/null +++ b/src/test/java/synapticloop/templar/function/string/FunctionSubStringTest.java @@ -0,0 +1,28 @@ +package synapticloop.templar.function.string; +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +import synapticloop.templar.exception.FunctionException; + +public class FunctionSubStringTest { + private FunctionSubString functionSubString = null; + + @Before + public void setup() { + functionSubString = new FunctionSubString(); + } + + @Test + public void testSubstring() throws FunctionException { + Object evaluate = functionSubString.evaluate("substring", new Object[] { "\"hello\"", "\"1\"" }, null); + assertEquals("ello", evaluate); + } + + @Test + public void testSubstringRange() throws FunctionException { + Object evaluate = functionSubString.evaluate("substring", new Object[] { "\"hello\"", "\"1\"", "\"3\"" }, null); + assertEquals("el", evaluate); + } +}