1 /*****************************************************************************/ 2 /* Software Testing Automation Framework (STAF) */ 3 /* (C) Copyright IBM Corp. 2002 */ 4 /* */ 5 /* This software is licensed under the Eclipse Public License (EPL) V1.0. */ 6 /*****************************************************************************/ 7 8 package com.ibm.staf.service.stax; 9 import java.util.List; 10 import java.util.LinkedList; 11 import java.util.Iterator; 12 13 public class STAXTryAction extends STAXActionDefaultImpl 14 { 15 static final int INIT = 0; 16 static final int CALLED_ACTION = 1; 17 static final int COMPLETE = 2; 18 19 static final String INIT_STRING = "INIT"; 20 static final String CALLED_ACTION_STRING = "CALLED_ACTION"; 21 static final String COMPLETE_STRING = "COMPLETE"; 22 static final String STATE_UNKNOWN_STRING = "UNKNOWN"; 23 STAXTryAction()24 public STAXTryAction() 25 { /* Do Nothing */ } 26 STAXTryAction(STAXAction action, List<STAXCatchAction> catchList)27 public STAXTryAction(STAXAction action, List<STAXCatchAction> catchList) 28 { 29 fAction = action; 30 fCatchList = catchList; 31 } 32 setTryAction(STAXAction tryAction)33 public void setTryAction(STAXAction tryAction) { fAction = tryAction; } 34 setCatchList(List<STAXCatchAction> catchList)35 public void setCatchList(List<STAXCatchAction> catchList) 36 { 37 fCatchList = catchList; 38 } 39 getStateAsString()40 public String getStateAsString() 41 { 42 switch (fState) 43 { 44 case INIT: 45 return INIT_STRING; 46 case CALLED_ACTION: 47 return CALLED_ACTION_STRING; 48 case COMPLETE: 49 return COMPLETE_STRING; 50 default: 51 return STATE_UNKNOWN_STRING; 52 } 53 } 54 getXMLInfo()55 public String getXMLInfo() 56 { 57 return "<try>"; 58 } 59 getInfo()60 public String getInfo() 61 { 62 return ""; 63 } 64 getDetails()65 public String getDetails() 66 { 67 return "State:" + getStateAsString() + 68 ";Action:" + fAction + 69 ";CatchList:" + fCatchList; 70 } 71 getCatchHandler(STAXExceptionCondition ex)72 STAXCatchAction getCatchHandler(STAXExceptionCondition ex) 73 { 74 String exceptionName = ex.getName(); 75 76 for (NamedCatch namedCatch : fNamedCatchList) 77 { 78 if (namedCatch.name.equals("...") || 79 namedCatch.name.equals(exceptionName) || 80 (exceptionName.startsWith(namedCatch.name) && 81 (exceptionName.charAt(namedCatch.name.length()) == '.'))) 82 { 83 return namedCatch.catchAction; 84 } 85 } 86 87 return null; 88 } 89 execute(STAXThread thread)90 public void execute(STAXThread thread) 91 { 92 if (fState == INIT) 93 { 94 String evalElem = "catch"; 95 String evalAttr = "exception"; 96 int evalIndex = 0; 97 98 fNamedCatchList = new LinkedList<NamedCatch>(); 99 100 for (STAXCatchAction catchAction : fCatchList) 101 { 102 try 103 { 104 fNamedCatchList.add( 105 new NamedCatch( 106 thread.pyStringEval( 107 catchAction.getCatchableExceptionName()), 108 catchAction)); 109 } 110 catch (STAXPythonEvaluationException e) 111 { 112 thread.popAction(); 113 114 setElementInfo(new STAXElementInfo( 115 evalElem, evalAttr, evalIndex)); 116 117 thread.setSignalMsgVar( 118 "STAXPythonEvalMsg", 119 STAXUtil.formatErrorMessage(this), e); 120 121 thread.raiseSignal("STAXPythonEvaluationError"); 122 123 return; 124 } 125 126 evalIndex++; 127 } 128 129 thread.pushAction(fAction.cloneAction()); 130 fState = CALLED_ACTION; 131 } 132 else if (fState == CALLED_ACTION) 133 { 134 fState = COMPLETE; 135 thread.popAction(); 136 } 137 } 138 handleCondition(STAXThread thread, STAXCondition cond)139 public void handleCondition(STAXThread thread, STAXCondition cond) 140 { 141 fState = COMPLETE; 142 thread.popAction(); 143 144 if (cond instanceof STAXExceptionCondition) 145 { 146 STAXExceptionCondition ex = (STAXExceptionCondition)cond; 147 STAXCatchAction handler = getCatchHandler(ex); 148 149 if (handler != null) 150 { 151 // Push the catch block action 152 153 handler = (STAXCatchAction)handler.cloneAction(); 154 handler.setException(ex); 155 156 thread.pushAction(handler); 157 } 158 } 159 160 // Remove any exception conditions from the condition stack that this 161 // try action handles. This way, there won't be any "dangling" 162 // exception conditions hanging around if a terminate block condition 163 // is added to the condition stack. 164 165 thread.visitConditions(new STAXVisitor() 166 { 167 public void visit(Object o, Iterator iter) 168 { 169 if (o instanceof STAXExceptionCondition) 170 { 171 STAXCatchAction handler = 172 getCatchHandler((STAXExceptionCondition)o); 173 174 if (handler != null) iter.remove(); 175 } 176 } 177 }); 178 } 179 cloneAction()180 public STAXAction cloneAction() 181 { 182 STAXTryAction clone = new STAXTryAction(); 183 184 clone.setElement(getElement()); 185 clone.setLineNumberMap(getLineNumberMap()); 186 clone.setXmlFile(getXmlFile()); 187 clone.setXmlMachine(getXmlMachine()); 188 189 clone.fAction = fAction; 190 clone.fCatchList = fCatchList; 191 192 return clone; 193 } 194 195 class NamedCatch 196 { NamedCatch(String name, STAXCatchAction catchAction)197 public NamedCatch(String name, STAXCatchAction catchAction) 198 { 199 this.name = name; 200 this.catchAction = catchAction; 201 } 202 203 String name; 204 STAXCatchAction catchAction; 205 } 206 207 private int fState = INIT; 208 private STAXAction fAction = null; 209 private List<STAXCatchAction> fCatchList = null; 210 private LinkedList<NamedCatch> fNamedCatchList = null; 211 } 212