1 5 package com.opensymphony.workflow.loader; 6 7 import com.opensymphony.workflow.InvalidWorkflowDescriptorException; 8 import com.opensymphony.workflow.util.Validatable; 9 10 import org.w3c.dom.Element ; 11 import org.w3c.dom.Node ; 12 import org.w3c.dom.NodeList ; 13 14 import java.io.PrintWriter ; 15 16 import java.util.*; 17 18 19 23 public class ActionDescriptor extends AbstractDescriptor implements Validatable { 24 26 protected List conditionalResults = new ArrayList(); 27 protected List postFunctions = new ArrayList(); 28 protected List preFunctions = new ArrayList(); 29 protected List validators = new ArrayList(); 30 protected Map metaAttributes = new HashMap(); 31 protected RestrictionDescriptor restriction; 32 protected ResultDescriptor unconditionalResult; 33 protected String name; 34 protected String view; 35 protected boolean autoExecute = false; 36 protected boolean finish = false; 37 38 40 public ActionDescriptor() { 41 } 42 43 public ActionDescriptor(Element action) { 44 init(action); 45 } 46 47 49 public void setAutoExecute(boolean autoExecute) { 50 this.autoExecute = autoExecute; 51 } 52 53 public boolean getAutoExecute() { 54 return autoExecute; 55 } 56 57 public List getConditionalResults() { 58 return conditionalResults; 59 } 60 61 public void setFinish(boolean finish) { 62 this.finish = finish; 63 } 64 65 public boolean isFinish() { 66 return finish; 67 } 68 69 public void setMetaAttributes(Map metaAttributes) { 70 this.metaAttributes = metaAttributes; 71 } 72 73 public Map getMetaAttributes() { 74 return metaAttributes; 75 } 76 77 public void setName(String name) { 78 this.name = name; 79 } 80 81 public String getName() { 82 return name; 83 } 84 85 public List getPostFunctions() { 86 return postFunctions; 87 } 88 89 public List getPreFunctions() { 90 return preFunctions; 91 } 92 93 public void setRestriction(RestrictionDescriptor restriction) { 94 this.restriction = restriction; 95 } 96 97 public RestrictionDescriptor getRestriction() { 98 return restriction; 99 } 100 101 public void setUnconditionalResult(ResultDescriptor unconditionalResult) { 102 this.unconditionalResult = unconditionalResult; 103 } 104 105 public ResultDescriptor getUnconditionalResult() { 106 return unconditionalResult; 107 } 108 109 public List getValidators() { 110 return validators; 111 } 112 113 public void setView(String view) { 114 this.view = view; 115 } 116 117 public String getView() { 118 return view; 119 } 120 121 public String toString() { 122 StringBuffer sb = new StringBuffer (); 123 124 if (name != null) { 125 sb.append(name); 126 } 127 128 if ((view != null) && (view.length() > 0)) { 129 sb.append(" (").append(view).append(")"); 130 } 131 132 return sb.toString(); 133 } 134 135 public void validate() throws InvalidWorkflowDescriptorException { 136 ValidationHelper.validate(preFunctions); 137 ValidationHelper.validate(postFunctions); 138 ValidationHelper.validate(validators); 139 ValidationHelper.validate(conditionalResults); 140 141 if (restriction != null) { 142 restriction.validate(); 143 } 144 145 if (unconditionalResult != null) { 146 unconditionalResult.validate(); 147 } 148 } 149 150 public void writeXML(PrintWriter out, int indent) { 151 XMLUtil.printIndent(out, indent++); 152 153 StringBuffer buf = new StringBuffer ("<action id=\""); 154 buf.append(getId()); 155 buf.append("\""); 156 157 if ((name != null) && (name.length() > 0)) { 158 buf.append(" name=\""); 159 buf.append(name); 160 buf.append("\""); 161 } 162 163 if ((view != null) && (view.length() > 0)) { 164 buf.append(" view=\""); 165 buf.append(view); 166 buf.append("\""); 167 } 168 169 if (finish) { 170 buf.append(" finish=\"true\""); 171 } 172 173 if (autoExecute) { 174 buf.append(" auto=\"true\""); 175 } 176 177 buf.append(">"); 178 out.println(buf.toString()); 179 180 Iterator iter = metaAttributes.entrySet().iterator(); 181 182 while (iter.hasNext()) { 183 Map.Entry entry = (Map.Entry) iter.next(); 184 XMLUtil.printIndent(out, indent); 185 out.print("<meta name=\""); 186 out.print(entry.getKey()); 187 out.print("\">"); 188 out.print(entry.getValue()); 189 out.println("</meta>"); 190 } 191 192 if (restriction != null) { 193 restriction.writeXML(out, indent); 194 } 195 196 if (validators.size() > 0) { 197 XMLUtil.printIndent(out, indent++); 198 out.println("<validators>"); 199 200 for (int i = 0; i < validators.size(); i++) { 201 ValidatorDescriptor validator = (ValidatorDescriptor) validators.get(i); 202 validator.writeXML(out, indent); 203 } 204 205 XMLUtil.printIndent(out, --indent); 206 out.println("</validators>"); 207 } 208 209 if (preFunctions.size() > 0) { 210 XMLUtil.printIndent(out, indent++); 211 out.println("<pre-functions>"); 212 213 for (int i = 0; i < preFunctions.size(); i++) { 214 FunctionDescriptor function = (FunctionDescriptor) preFunctions.get(i); 215 function.writeXML(out, indent); 216 } 217 218 XMLUtil.printIndent(out, --indent); 219 out.println("</pre-functions>"); 220 } 221 222 XMLUtil.printIndent(out, indent++); 223 out.println("<results>"); 224 225 for (int i = 0; i < conditionalResults.size(); i++) { 226 ConditionalResultDescriptor result = (ConditionalResultDescriptor) conditionalResults.get(i); 227 result.writeXML(out, indent); 228 } 229 230 if (unconditionalResult != null) { 231 unconditionalResult.writeXML(out, indent); 232 } 233 234 XMLUtil.printIndent(out, --indent); 235 out.println("</results>"); 236 237 if (postFunctions.size() > 0) { 238 XMLUtil.printIndent(out, indent++); 239 out.println("<post-functions>"); 240 241 for (int i = 0; i < postFunctions.size(); i++) { 242 FunctionDescriptor function = (FunctionDescriptor) postFunctions.get(i); 243 function.writeXML(out, indent); 244 } 245 246 XMLUtil.printIndent(out, --indent); 247 out.println("</post-functions>"); 248 } 249 250 XMLUtil.printIndent(out, --indent); 251 out.println("</action>"); 252 } 253 254 protected void init(Element action) { 255 try { 256 setId(Integer.parseInt(action.getAttribute("id"))); 257 } catch (Exception ex) { 258 throw new IllegalArgumentException ("Invalid action id value " + action.getAttribute("id")); 259 } 260 261 this.name = action.getAttribute("name"); 262 this.view = action.getAttribute("view"); 263 this.autoExecute = "true".equals(action.getAttribute("auto")); 264 this.finish = "true".equals(action.getAttribute("finish")); 265 266 NodeList children = action.getChildNodes(); 267 268 for (int i = 0; i < children.getLength(); i++) { 269 Node child = (Node ) children.item(i); 270 271 if (child.getNodeName().equals("meta")) { 272 Element meta = (Element ) child; 273 String value = XMLUtil.getText(meta); 274 this.metaAttributes.put(meta.getAttribute("name"), value); 275 } 276 } 277 278 Element v = XMLUtil.getChildElement(action, "validators"); 280 281 if (v != null) { 282 List validators = XMLUtil.getChildElements(v, "validator"); 283 284 for (int k = 0; k < validators.size(); k++) { 285 Element validator = (Element ) validators.get(k); 286 ValidatorDescriptor validatorDescriptor = new ValidatorDescriptor(validator); 287 validatorDescriptor.setParent(this); 288 this.validators.add(validatorDescriptor); 289 } 290 } 291 292 Element pre = XMLUtil.getChildElement(action, "pre-functions"); 294 295 if (pre != null) { 296 List preFunctions = XMLUtil.getChildElements(pre, "function"); 297 298 for (int k = 0; k < preFunctions.size(); k++) { 299 Element preFunction = (Element ) preFunctions.get(k); 300 FunctionDescriptor functionDescriptor = new FunctionDescriptor(preFunction); 301 functionDescriptor.setParent(this); 302 this.preFunctions.add(functionDescriptor); 303 } 304 } 305 306 Element resultsElememt = XMLUtil.getChildElement(action, "results"); 308 List results = XMLUtil.getChildElements(resultsElememt, "result"); 309 310 for (int k = 0; k < results.size(); k++) { 311 Element result = (Element ) results.get(k); 312 ConditionalResultDescriptor conditionalResultDescriptor = new ConditionalResultDescriptor(result); 313 conditionalResultDescriptor.setParent(this); 314 this.conditionalResults.add(conditionalResultDescriptor); 315 } 316 317 Element unconditionalResult = XMLUtil.getChildElement(resultsElememt, "unconditional-result"); 318 this.unconditionalResult = new ResultDescriptor(unconditionalResult); 319 this.unconditionalResult.setParent(this); 320 321 Element post = XMLUtil.getChildElement(action, "post-functions"); 323 324 if (post != null) { 325 List postFunctions = XMLUtil.getChildElements(post, "function"); 326 327 for (int k = 0; k < postFunctions.size(); k++) { 328 Element postFunction = (Element ) postFunctions.get(k); 329 FunctionDescriptor functionDescriptor = new FunctionDescriptor(postFunction); 330 functionDescriptor.setParent(this); 331 this.postFunctions.add(functionDescriptor); 332 } 333 } 334 335 Element restrictElement = XMLUtil.getChildElement(action, "restrict-to"); 337 338 if (restrictElement != null) { 339 restriction = new RestrictionDescriptor(restrictElement); 340 341 if (restriction.getConditions().size() == 0) { 342 restriction = null; 343 } else { 344 restriction.setParent(this); 345 } 346 } 347 } 348 } 349 | Popular Tags |