r1086 - trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/engine
Author: mfortun Date: 2011-07-25 15:08:27 +0200 (Mon, 25 Jul 2011) New Revision: 1086 Url: http://nuiton.org/repositories/revision/wikitty/1086 Log: * dummy evaluate algorithm 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 08:54:20 UTC (rev 1085) +++ trunk/wikitty-publication/src/main/java/org/nuiton/wikitty/publication/engine/HtmlScriptEngine.java 2011-07-25 13:08:27 UTC (rev 1086) @@ -1,56 +1,47 @@ package org.nuiton.wikitty.publication.engine; import java.io.Reader; +import java.lang.reflect.Method; +import java.util.LinkedList; +import java.util.List; import java.util.Map; -import java.util.StringTokenizer; - import javax.script.Bindings; import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineFactory; import javax.script.ScriptException; import javax.script.SimpleBindings; +import org.apache.commons.lang.StringUtils; +import org.nuiton.util.StringUtil; +public class HtmlScriptEngine implements ScriptEngine { -public class HtmlScriptEngine implements ScriptEngine{ - /* * algo super simple en fait. * - * On lit le code. - * pour tout ce qui est dans les clés des bindings on fait l'invocation - * java qui va avec. - * + * On lit le code. pour tout ce qui est dans les clés des bindings on fait + * l'invocation java qui va avec. */ - - - + /** * usefull to match element contained inside the binding map that on which - * method are called. like : - * objectName.getName() - * objectName.setName("truc") + * method are called. like : objectName.getName() objectName.setName("truc") */ - static public String REGEX_BIND_ELEMENT_METHOD = "\\..*\\(.*\\)"; - static public String REGEX_END="$"; - static public String REGEX_EMPTY_END=" *"+REGEX_END; + static public String REGEX_BIND_ELEMENT_METHOD = "\\.\\w+\\(\\w*\\)"; + static public String REGEX_END = "$"; + static public String REGEX_EMPTY_END = " *" + REGEX_END; - protected ScriptEngineFactory factory; protected ScriptContext context; protected Map<String, Object> attributes; - - - - - + @Override public Object eval(String script, ScriptContext context) throws ScriptException { // TODO mfortun throw new UnsupportedOperationException("not yet implemented"); - //return null; - + // return null; + } @Override @@ -58,45 +49,45 @@ throws ScriptException { // TODO mfortun throw new UnsupportedOperationException("not yet implemented"); - //return null; - + // return null; + } @Override public Object eval(String script) throws ScriptException { // TODO mfortun throw new UnsupportedOperationException("not yet implemented"); - //return null; - + // return null; + } @Override public Object eval(Reader reader) throws ScriptException { // TODO mfortun throw new UnsupportedOperationException("not yet implemented"); - //return null; - + // return null; + } @Override public Object eval(String script, Bindings n) throws ScriptException { // TODO mfortun throw new UnsupportedOperationException("not yet implemented"); - //return null; - + // return null; + } @Override public Object eval(Reader reader, Bindings n) throws ScriptException { // TODO mfortun throw new UnsupportedOperationException("not yet implemented"); - //return null; - + // return null; + } @Override public void put(String key, Object value) { - attributes.put(key, value); + attributes.put(key, value); } @Override @@ -126,69 +117,132 @@ @Override public void setContext(ScriptContext context) { - this.context=context; + this.context = context; } @Override public ScriptEngineFactory getFactory() { - if (factory==null){ + if (factory == null) { factory = new HtmlScriptEngineFactory(); } return factory; } - - protected String evaluateCodeInsideHtml(String code, Bindings binds){ - String result = new String (code); - - for (String key: binds.keySet()){ + protected String evaluateCodeInsideHtml(String code, Bindings binds) { + String result = new String(code); - Object oo = binds.get(key); - - String [] hmtl = code.split(key+REGEX_BIND_ELEMENT_METHOD); - + for (String bindingElement : binds.keySet()) { - for(int i=0; i<hmtl.length-1; i++){ + Object oo = binds.get(bindingElement); + + String[] hmtl = result.split(bindingElement + + REGEX_BIND_ELEMENT_METHOD); + + String pageModified = hmtl[0]; + for (int i = 0; i < hmtl.length - 1; i++) { + int begin = code.indexOf(hmtl[i]); - begin = begin+hmtl[i].length()+1; - - int end = code.indexOf(hmtl[i+1]); - end=end-1; - + begin = begin + hmtl[i].length() + 1; + + int end = code.indexOf(hmtl[i + 1]); + end = end - 1; + String codeToExecude = code.substring(begin, end); + codeToExecude = codeToExecude.trim(); - String resultCode=""; - + String resultCode = invokeFromName(oo, codeToExecude, binds); + // replace at position by result - - - + pageModified += " " + resultCode + " "; + + pageModified += hmtl[i + 1]; } - code.replaceAll(key+REGEX_END, oo.toString()); - code.replaceAll(key+REGEX_EMPTY_END, oo.toString()+" "); - - - + pageModified.replaceAll(bindingElement + REGEX_END, oo.toString()); + pageModified.replaceAll(bindingElement + REGEX_EMPTY_END, + oo.toString() + " "); + + result = pageModified; + /* - * Voir si on match aussi les trucs plus générique avec - * déclaration de classe et tout le tintouint + * Voir si on match aussi les trucs plus générique avec déclaration + * de classe et tout le tintouint * - * Si ça match le nom et avec le nom associé à la regex method - * cette derniuère est prioritaire. - * Et on éxécute la méthode sur l'objet. + * Si ça match le nom et avec le nom associé à la regex method cette + * derniuère est prioritaire. Et on éxécute la méthode sur l'objet. * - * Si ça match seulement avec le nom alors on fait + * Si ça match seulement avec le nom alors on fait * getValue().tostring - * - * - * */ - - - + } return result; } - + + 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(")"); + + String methodName = invokation.substring(dotPosition + 1, + openBracketPosition - 1); + + String params = invokation.substring(openBracketPosition + 1, + closingBracketPosition - 1); + + String[] parsedParam = StringUtil.split(params, ","); + + List<Object> listParam = new LinkedList<Object>(); + List<Class<?>> listType = new LinkedList<Class<?>>(); + + for (String argument : parsedParam) { + Object value = argument; + /* + * check if argument is contained inside binding + */ + + if (binds.containsKey(argument)) { + value = binds.get(argument); + listParam.add(value); + listType.add(value.getClass()); + continue; + } + // dummy algo, try to determine type of the argument + // if exception throwned try another type of object + try { + value = Double.parseDouble(argument); + } catch (Exception e) { + try { + value = Integer.parseInt(argument); + } catch (Exception ee) { + try { + value = Boolean.parseBoolean(argument); + } catch (Exception eee) { + + } + } + } + listParam.add(value); + listType.add(value.getClass()); + } + + Object[] args = listParam.toArray(); + Class<?>[] types = (Class<?>[]) listType.toArray(); + + try { + + Method methodBindin = oo.getClass().getMethod(methodName, types); + result = methodBindin.invoke(oo, args).toString(); + + } catch (Exception e) { + // TODO mfortun-2011-07-25 handle exception + } + + return result; + + } + }
participants (1)
-
mfortun@users.nuiton.org