📄 staxcallaction.java
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2002 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/package com.ibm.staf.service.stax;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import java.util.Hashtable;import java.util.Iterator;import org.python.core.*;public class STAXCallAction implements STAXAction{ // Ways arguments are passed on a Call public static final int CALL_ONE_ARG = 1; // <call> element public static final int CALL_LIST_ARGS = 2; // <call-with-list> element public static final int CALL_MAP_ARGS = 3; // <call-with-map> element public STAXCallAction() { /* Do Nothing */ } public STAXCallAction(String name, String args) { fUnevalName = name; fName = name; fArgs = args; } public String getFunction() { return fName; } public void setFunction(String function) { fUnevalName = function; fName = function; } public int getCallType() { return fCallType; } public void setCallType(int callType) { fCallType = callType; } public String getArg() { return fArgs; } public void setArgs(String args) { fArgs = args; } public ArrayList getArgList() { return fArgList; } public void addListArg(String arg) { fArgList.add(arg); } public HashMap getArgMap() { return fArgMap; } public void addMapArg(String key, String value) { fArgMap.put(key, value); } public String getXMLInfo() { StringBuffer result = new StringBuffer(); if (fCallType == CALL_ONE_ARG) { if (fArgs == null) { result.append("<call function=\"").append(fUnevalName). append("\"/>"); } else { result.append("<call function=\"").append(fUnevalName). append("\">").append(fArgs).append("</call>"); } } else if (fCallType == CALL_LIST_ARGS) { if (fArgList.size() == 0) { result.append("<call-with-list function=\""). append(fUnevalName).append("\"/>"); } else { result.append("<call-with-list function=\""). append(fUnevalName).append("\">\n"); for (int i = 0; i < fArgList.size(); i++) { if (fArgList.get(i) == null) { result.append(" <call-list-arg/>\n"); } else { result.append(" <call-list-arg>"). append(fArgList.get(i)). append("</call-list-arg>\n"); } } result.append("</call-with-list>"); } } else if (fCallType == CALL_MAP_ARGS) { if (fArgMap.size() == 0) { result.append("<call-with-map function=\""). append(fUnevalName).append("\"/>"); } else { result.append("<call-with-map function=\""). append(fUnevalName).append("\">\n"); Iterator it = fArgMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry)it.next(); String key = (String)e.getKey(); if (key == null) { key = ""; } if (e.getValue() == null) { result.append(" <call-map-arg name=\""). append(key).append("\"/>\n"); } else { result.append(" <call-map-arg name=\""). append(key).append("\">").append(e.getValue()). append("</call-map-arg>\n"); } } result.append("</call-with-map>"); } } return result.toString(); } public String getInfo() { return fName; } public String getDetails() { StringBuffer result = new StringBuffer(); result.append("Name:").append(fName). append(";CallType:").append(fCallType).append(";Arguments:"); if (fCallType == CALL_ONE_ARG) { result.append(fArgs); } else if (fCallType == CALL_LIST_ARGS) { result.append(fArgList); } else if (fCallType == CALL_MAP_ARGS) { result.append(fArgMap); } return result.toString(); } public void execute(STAXThread thread) { thread.popAction(); // Evaluate the function name try { fName = thread.pyStringEval(fUnevalName); } catch (STAXPythonEvaluationException e) { thread.setSignalMsgVar("STAXPythonEvalMsg", getXMLInfo(), e); thread.raiseSignal("STAXPythonEvaluationError"); return; } // Verify function called is defined STAXAction function = thread.getJob().getFunction(fName); if (function == null) { String elementInfo = getXMLInfo() + "\n\n Function does not exist: " + fName; thread.setSignalMsgVar("STAXFunctionDoesNotExistMsg", elementInfo); thread.raiseSignal("STAXFunctionDoesNotExist"); return; } // if fArgs is blank, set to null if (fArgs != null && fArgs.equals("")) fArgs = null; // Put the arguments into a Python Object PyObject args = null; try { if (fCallType == CALL_ONE_ARG) { // Evaluate fArgs data and put into a Python object if (fArgs != null) { args = thread.pyObjectEval(fArgs); } } else if (fCallType == CALL_LIST_ARGS) { // Evaluate fArgList data and put into a PyList object args = new PyList(); String value; for (int i = 0; i < fArgList.size(); i++) { value = (String)fArgList.get(i); // If value is null or blank, set to None if (fArgList.get(i) == null || fArgList.get(i).equals("")) value = "None"; ((PyList)args).append( Py.java2py(thread.pyObjectEval(value))); } } else if (fCallType == CALL_MAP_ARGS) { // Evaluate fArgMap data and put in a Python dictionary object Map evalArgMap = new HashMap(); Iterator it = fArgMap.entrySet().iterator(); String key; String value; while (it.hasNext()) { Map.Entry e = (Map.Entry)it.next(); key = (String)e.getKey(); value = (String)e.getValue(); // If value or key is null or blank, set to None if (key == null || key.equals("")) key = "None"; if (value == null || value.equals("")) value = "None"; evalArgMap.put(Py.java2py(thread.pyObjectEval(key)), Py.java2py(thread.pyObjectEval(value))); } args = new PyDictionary(new Hashtable(evalArgMap)); } else { // Should never happen System.out.println("STAXCallAction: Invalid call type=" + fCallType); return; } } catch (STAXPythonEvaluationException e) { String elementInfo = "Python evaluation error in function " + "arguments value."; if (!fUnevalName.equals("'" + fName + "'")) { elementInfo += "\nActual function called: " + fName; } elementInfo += "\n\n" + getXMLInfo(); thread.setSignalMsgVar("STAXPythonEvalMsg", elementInfo, e); thread.raiseSignal("STAXPythonEvaluationError"); return; } // Clone the function before setting arguments to avoid a race // condition STAXAction cloneFunction = function.cloneAction(); ((STAXFunctionAction)cloneFunction).setCallArgs(args); ((STAXFunctionAction)cloneFunction).setCallXMLInfo(getXMLInfo()); // Put call of function on the action stack thread.pushAction(cloneFunction); } public void handleCondition(STAXThread thread, STAXCondition cond) { thread.popAction(); } public STAXAction cloneAction() { STAXCallAction clone = new STAXCallAction(); clone.fUnevalName = fUnevalName; clone.fName = fName; clone.fArgs = fArgs; clone.fCallType = fCallType; clone.fArgList = (ArrayList)fArgList.clone(); clone.fArgMap = (HashMap)fArgMap.clone(); return clone; } private String fUnevalName; private String fName; private String fArgs = null; private int fCallType = 1; private ArrayList fArgList = new ArrayList(); private HashMap fArgMap = new HashMap();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -