Author: mfortun Date: 2011-07-26 13:43:51 +0200 (Tue, 26 Jul 2011) New Revision: 1089 Url: http://nuiton.org/repositories/revision/wikitty/1089 Log: * correct engine for method invoke Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/engine/HtmlScriptEngine.java Modified: trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/engine/HtmlScriptEngine.java =================================================================== --- trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/engine/HtmlScriptEngine.java 2011-07-25 15:24:41 UTC (rev 1088) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/engine/HtmlScriptEngine.java 2011-07-26 11:43:51 UTC (rev 1089) @@ -14,6 +14,7 @@ import javax.script.SimpleBindings; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; +import org.nuiton.util.ArrayUtil; import org.nuiton.util.StringUtil; public class HtmlScriptEngine implements ScriptEngine { @@ -29,10 +30,11 @@ * usefull to match element contained inside the binding map that on which * method are called. like : objectName.getName() objectName.setName("truc") */ - static public String REGEX_BIND_ELEMENT_METHOD = "\\.\\w+\\(\\w*\\)"; + static public String REGEX_BIND_ELEMENT_METHOD = "\\.\\w+\\([\\w\"\\d]*\\)"; static public String REGEX_END = "$"; - static public String REGEX_EMPTY = " *"; + static public String REGEX_EMPTY = " +"; static public String REGEX_EMPTY_END = REGEX_EMPTY + REGEX_END; + static public String REGEX_STRING_ARG = "\"[\\w\\d]*\""; protected ScriptEngineFactory factory; protected ScriptContext context; @@ -54,8 +56,8 @@ try { return this.eval(IOUtils.toString(reader), context); } catch (IOException e) { - //TODO mfortun-2011-07-25 handle this, change ? - return StringUtils.EMPTY; + // TODO mfortun-2011-07-25 handle this, change ? + return StringUtils.EMPTY; } } @@ -85,8 +87,8 @@ try { return this.eval(IOUtils.toString(reader), n); } catch (IOException e) { - //TODO mfortun-2011-07-25 handle this, change ? - return StringUtils.EMPTY; + // TODO mfortun-2011-07-25 handle this, change ? + return StringUtils.EMPTY; } } @@ -147,26 +149,31 @@ for (int i = 0; i < hmtl.length - 1; i++) { int begin = code.indexOf(hmtl[i]); - begin = begin + hmtl[i].length() + 1; + begin = begin + hmtl[i].length(); int end = code.indexOf(hmtl[i + 1]); - end = end - 1; + // end = end; String codeToExecude = code.substring(begin, end); codeToExecude = codeToExecude.trim(); + String resultCode = invokeFromName(oo, codeToExecude, binds); // replace at position by result - pageModified += " " + resultCode + " "; + pageModified += resultCode; pageModified += hmtl[i + 1]; } - pageModified.replaceAll(bindingElement + REGEX_END, oo.toString()); - pageModified.replaceAll(bindingElement + REGEX_EMPTY_END, - oo.toString() + " "); + pageModified = pageModified.replaceAll(bindingElement + REGEX_END + + "|" + REGEX_EMPTY + bindingElement + REGEX_EMPTY, + oo.toString()); + // pageModified = pageModified.replaceAll(bindingElement + + // REGEX_EMPTY_END, + // oo.toString() + " "); + result = pageModified; /* @@ -186,17 +193,19 @@ protected String invokeFromName(Object oo, String invokation, Bindings binds) { - String result = StringUtils.EMPTY; - int dotPosition = invokation.indexOf("."); int openBracketPosition = invokation.indexOf("("); int closingBracketPosition = invokation.indexOf(")"); + // if no method found just let the same string in place + String result = invokation; + // .substring(0,dotPosition);; + // String methodName = invokation.substring(dotPosition + 1, - openBracketPosition - 1); + openBracketPosition); String params = invokation.substring(openBracketPosition + 1, - closingBracketPosition - 1); + closingBracketPosition); String[] parsedParam = StringUtil.split(params, ","); @@ -209,6 +218,13 @@ * check if argument is contained inside binding */ + if (argument.matches(REGEX_STRING_ARG)){ + value = argument.substring(1, argument.length()-1); + listParam.add(value); + listType.add(String.class); + continue; + } + if (binds.containsKey(argument)) { value = binds.get(argument); listParam.add(value); @@ -235,14 +251,17 @@ } Object[] args = listParam.toArray(); - Class<?>[] types = (Class<?>[]) listType.toArray(); + Class<?>[] types = ArrayUtil.toArray(listType, Class.class); + try { Method methodBindin = oo.getClass().getMethod(methodName, types); + result = methodBindin.invoke(oo, args).toString(); } catch (Exception e) { + e.printStackTrace(); // TODO mfortun-2011-07-25 handle exception } @@ -250,7 +269,4 @@ } - - - }