001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow.loader;
006:
007: import org.w3c.dom.Element;
008:
009: import java.io.PrintWriter;
010:
011: import java.util.*;
012:
013: /**
014: * Desrives a function that can be applied to a workflow step.
015: *
016: * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a>
017: */
018: public class FunctionDescriptor extends AbstractDescriptor {
019: //~ Instance fields ////////////////////////////////////////////////////////
020:
021: protected Map args = new HashMap();
022:
023: /**
024: * The name field helps the editor identify the condition template used.
025: */
026: protected String name;
027: protected String type;
028:
029: //~ Constructors ///////////////////////////////////////////////////////////
030:
031: /**
032: * @deprecated use {@link DescriptorFactory} instead
033: */
034: FunctionDescriptor() {
035: }
036:
037: /**
038: * @deprecated use {@link DescriptorFactory} instead
039: */
040: FunctionDescriptor(Element function) {
041: init(function);
042: }
043:
044: //~ Methods ////////////////////////////////////////////////////////////////
045:
046: public Map getArgs() {
047: return args;
048: }
049:
050: public void setName(String name) {
051: this .name = name;
052: }
053:
054: public String getName() {
055: return name;
056: }
057:
058: public void setType(String type) {
059: this .type = type;
060: }
061:
062: public String getType() {
063: return type;
064: }
065:
066: public void writeXML(PrintWriter out, int indent) {
067: XMLUtil.printIndent(out, indent++);
068: out.println("<function "
069: + (hasId() ? ("id=\"" + getId() + "\" ") : "")
070: + (((name != null) && (name.length() > 0)) ? ("name=\""
071: + XMLUtil.encode(getName()) + "\" ") : "")
072: + "type=\"" + type + "\">");
073:
074: Iterator iter = args.entrySet().iterator();
075:
076: while (iter.hasNext()) {
077: Map.Entry entry = (Map.Entry) iter.next();
078: XMLUtil.printIndent(out, indent);
079: out.print("<arg name=\"");
080: out.print(entry.getKey());
081: out.print("\">");
082:
083: if ("beanshell".equals(type) || "bsf".equals(type)) {
084: out.print("<![CDATA[");
085: out.print(entry.getValue());
086: out.print("]]>");
087: } else {
088: out.print(XMLUtil.encode(entry.getValue()));
089: }
090:
091: out.println("</arg>");
092: }
093:
094: XMLUtil.printIndent(out, --indent);
095: out.println("</function>");
096: }
097:
098: protected void init(Element function) {
099: type = function.getAttribute("type");
100:
101: try {
102: setId(Integer.parseInt(function.getAttribute("id")));
103: } catch (NumberFormatException e) {
104: }
105:
106: if (function.getAttribute("name") != null) {
107: name = function.getAttribute("name");
108: }
109:
110: List args = XMLUtil.getChildElements(function, "arg");
111:
112: for (int l = 0; l < args.size(); l++) {
113: Element arg = (Element) args.get(l);
114: String value = XMLUtil.getText(arg);
115:
116: this .args.put(arg.getAttribute("name"), value);
117: }
118: }
119: }
|