KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > loader > FunctionDescriptor


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.loader;
6
7 import org.w3c.dom.Element JavaDoc;
8 import org.w3c.dom.NodeList JavaDoc;
9
10 import java.io.PrintWriter JavaDoc;
11
12 import java.util.*;
13
14
15 /**
16  * Desrives a function that can be applied to a workflow step.
17  *
18  * @author <a HREF="mailto:plightbo@hotmail.com">Pat Lightbody</a>
19  * @version $Revision: 1.8 $
20  */

21 public class FunctionDescriptor extends AbstractDescriptor {
22     //~ Instance fields ////////////////////////////////////////////////////////
23

24     protected Map args = new HashMap();
25
26     /**
27      * The name field helps the editor identify the condition template used.
28      */

29     protected String JavaDoc name;
30     protected String JavaDoc type;
31
32     //~ Constructors ///////////////////////////////////////////////////////////
33

34     public FunctionDescriptor() {
35     }
36
37     public FunctionDescriptor(Element JavaDoc function) {
38         init(function);
39     }
40
41     //~ Methods ////////////////////////////////////////////////////////////////
42

43     public Map getArgs() {
44         return args;
45     }
46
47     public void setName(String JavaDoc name) {
48         this.name = name;
49     }
50
51     public String JavaDoc getName() {
52         return name;
53     }
54
55     public void setType(String JavaDoc type) {
56         this.type = type;
57     }
58
59     public String JavaDoc getType() {
60         return type;
61     }
62
63     public void writeXML(PrintWriter JavaDoc out, int indent) {
64         XMLUtil.printIndent(out, indent++);
65         out.println("<function " + (hasId() ? ("id=\"" + getId() + "\" ") : "") + (((name != null) && (name.length() > 0)) ? ("name=\"" + getName() + "\" ") : "") + "type=\"" + type + "\">");
66
67         Iterator iter = args.entrySet().iterator();
68
69         while (iter.hasNext()) {
70             Map.Entry entry = (Map.Entry) iter.next();
71             XMLUtil.printIndent(out, indent);
72             out.print("<arg name=\"");
73             out.print(entry.getKey());
74             out.print("\">");
75
76             if ("beanshell".equals(type) || "bsf".equals(type)) {
77                 out.print("<![CDATA[");
78                 out.print(entry.getValue());
79                 out.print("]]>");
80             } else {
81                 out.print(XMLUtil.encode(entry.getValue()));
82             }
83
84             out.println("</arg>");
85         }
86
87         XMLUtil.printIndent(out, --indent);
88         out.println("</function>");
89     }
90
91     protected void init(Element JavaDoc function) {
92         type = function.getAttribute("type");
93
94         try {
95             setId(Integer.parseInt(function.getAttribute("id")));
96         } catch (NumberFormatException JavaDoc e) {
97         }
98
99         if (function.getAttribute("name") != null) {
100             name = function.getAttribute("name");
101         }
102
103         List JavaDoc args = XMLUtil.getChildElements(function, "arg");
104
105         for (int l = 0; l < args.size(); l++) {
106             Element JavaDoc arg = (Element JavaDoc) args.get(l);
107             String JavaDoc value = XMLUtil.getText(arg);
108
109             this.args.put(arg.getAttribute("name"), value);
110         }
111     }
112 }
113
Popular Tags