|
From: JP <j-...@us...> - 2007-03-21 01:59:23
|
Update of /cvsroot/swixat/swixat/src/main/java/org/swixat/framework/action In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv11964/src/main/java/org/swixat/framework/action Modified Files: Tag: B0_6_0 HandleScriptFunctionAction.java Log Message: Modified script function action to support xpath arguments. Xpath args can also be combined with literals. Example: <button id="myButton" actionCommand='someScriptFunction($myButton, "string literal", $myButton/text)'/> Index: HandleScriptFunctionAction.java =================================================================== RCS file: /cvsroot/swixat/swixat/src/main/java/org/swixat/framework/action/HandleScriptFunctionAction.java,v retrieving revision 1.2 retrieving revision 1.2.2.1 diff -C2 -d -r1.2 -r1.2.2.1 *** HandleScriptFunctionAction.java 2 Oct 2006 21:48:35 -0000 1.2 --- HandleScriptFunctionAction.java 21 Mar 2007 01:58:47 -0000 1.2.2.1 *************** *** 31,38 **** --- 31,47 ---- import org.swixat.SwiXAT; + import org.swixat.util.SwiXUtil; import org.swixat.framework.AbstractFrame; + import org.swixat.framework.XPathUtil; import org.swixat.model.ContextBinder; import org.swixat.script.ScriptException; import org.swixat.script.Scripting; + import org.apache.commons.logging.Log; + import org.apache.commons.logging.LogFactory; + import org.apache.commons.jxpath.JXPathContext; + + import java.util.regex.Pattern; + import java.util.regex.Matcher; + import java.util.Arrays; *************** *** 44,52 **** public class HandleScriptFunctionAction implements Action { /* (non-Javadoc) * @see org.swixat.framework.action.Action#accept(org.swixat.framework.AbstractFrame, java.lang.String, java.lang.Object) */ public boolean accept(Object caller, AbstractFrame frame, String command, Object arg) { ! return (command.trim().endsWith(")")) || (command.trim().endsWith(";")); } --- 53,74 ---- public class HandleScriptFunctionAction implements Action { + + /** + * Logger for this class + */ + private static final Log log = LogFactory.getLog(HandleScriptFunctionAction.class); + + /** + * pattern used to parse script function name and arguments + * example: scriptFunctionName($resource/xpath, "String arg") + */ + protected static final Pattern handleScriptFunctionPattern = + Pattern.compile("(\\w+)\\s*\\((.*)\\)\\s*;?"); + /* (non-Javadoc) * @see org.swixat.framework.action.Action#accept(org.swixat.framework.AbstractFrame, java.lang.String, java.lang.Object) */ public boolean accept(Object caller, AbstractFrame frame, String command, Object arg) { ! return handleScriptFunctionPattern.matcher(command.trim()).matches(); } *************** *** 55,73 **** */ public Object doCommand(Object caller, AbstractFrame frame, String command, Object arg) { Object retValue = null; try { Scripting i = frame.getScriptEngine(); frame.getActualContext().setArgument(arg); ! i.setVariable("currentContext", frame.getActualContext()); i.setVariable("interpreter", i); i.setVariable("context", ContextBinder.getInstance()); i.setVariable("application", SwiXAT.getFactory().getBean("application")); ! retValue = i.eval(command); if (retValue == null) { ! retValue = Integer.valueOf(0); } } catch (ScriptException se) { ! se.printStackTrace(); retValue = Integer.valueOf(1); } --- 77,136 ---- */ public Object doCommand(Object caller, AbstractFrame frame, String command, Object arg) { + Object retValue = null; + String functionName = null; + String[] args = new String[0]; + String argList = ""; + JXPathContext engine = null; + + // parse out the function name and, if they exist, the arguments + Matcher matcher = handleScriptFunctionPattern.matcher(command.trim()); + if (matcher.matches()) { + functionName = matcher.group(1); + args = SwiXUtil.split(matcher.group(2), ","); + } + try { + Scripting i = frame.getScriptEngine(); + frame.getActualContext().setArgument(arg); ! i.setVariable("currentContext", frame.getActualContext()); i.setVariable("interpreter", i); i.setVariable("context", ContextBinder.getInstance()); i.setVariable("application", SwiXAT.getFactory().getBean("application")); ! ! // only initialize an xpath context if we have arguments ! if (args.length > 0) { ! engine = JXPathContext.newContext(frame.getXmlEngine().getSharedContext(), null); ! engine.getVariables().declareVariable("e", arg); ! engine.getVariables().declareVariable("_arg", arg); ! } ! ! // process arguments ! for (int a=0; a<args.length; a++) { ! if (a>0) { ! argList += ", "; ! } ! String fnArg = args[a].trim(); ! if (fnArg.startsWith("$")) { // process arg as an xpath expression ! Object result = XPathUtil.applyXPath(fnArg, null, engine, false); ! String argname = "_fn_arg_" + a; ! i.setVariable(argname, result); ! argList += argname; ! } else { ! argList += fnArg; ! } ! } ! ! log.debug("Calling script function [" + functionName + "] with argList [" + argList + "] and args " + Arrays.toString(args)); ! ! retValue = i.eval(functionName + "(" + argList + ")"); if (retValue == null) { ! retValue = Integer.valueOf(0); } } catch (ScriptException se) { ! log.error("Script error", se); retValue = Integer.valueOf(1); } *************** *** 75,78 **** --- 138,142 ---- } + /* (non-Javadoc) * @see org.swixat.framework.action.Action#checkAction(org.swixat.framework.AbstractFrame, java.lang.String, java.lang.Object) |