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: * A validator is a helper used to verify values in the input map that is
015: * provided to every action call.
016: *
017: * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a>
018: */
019: public class ValidatorDescriptor extends AbstractDescriptor {
020: //~ Instance fields ////////////////////////////////////////////////////////
021:
022: protected Map args = new HashMap();
023: protected String name;
024: protected String type;
025:
026: //~ Constructors ///////////////////////////////////////////////////////////
027:
028: /**
029: * @deprecated use {@link DescriptorFactory} instead
030: */
031: ValidatorDescriptor() {
032: }
033:
034: /**
035: * @deprecated use {@link DescriptorFactory} instead
036: */
037: ValidatorDescriptor(Element validator) {
038: init(validator);
039: }
040:
041: //~ Methods ////////////////////////////////////////////////////////////////
042:
043: public Map getArgs() {
044: return args;
045: }
046:
047: public void setName(String name) {
048: this .name = name;
049: }
050:
051: public String getName() {
052: return name;
053: }
054:
055: public void setType(String type) {
056: this .type = type;
057: }
058:
059: public String getType() {
060: return type;
061: }
062:
063: public void writeXML(PrintWriter out, int indent) {
064: XMLUtil.printIndent(out, indent++);
065: out.println("<validator "
066: + (hasId() ? ("id=\"" + getId() + "\" ") : "")
067: + ((name != null) ? ("name=\""
068: + XMLUtil.encode(getName()) + "\" ") : "")
069: + "type=\"" + type + "\">");
070:
071: Iterator iter = args.entrySet().iterator();
072:
073: while (iter.hasNext()) {
074: Map.Entry entry = (Map.Entry) iter.next();
075: XMLUtil.printIndent(out, indent);
076: out.print("<arg name=\"");
077: out.print(entry.getKey());
078: out.print("\">");
079:
080: if ("beanshell".equals(type) || "bsf".equals(type)) {
081: out.print("<![CDATA[");
082: out.print(entry.getValue());
083: out.print("]]>");
084: } else {
085: out.print(XMLUtil.encode(entry.getValue()));
086: }
087:
088: out.println("</arg>");
089: }
090:
091: XMLUtil.printIndent(out, --indent);
092: out.println("</validator>");
093: }
094:
095: protected void init(Element validator) {
096: type = validator.getAttribute("type");
097: name = validator.getAttribute("name");
098:
099: try {
100: setId(Integer.parseInt(validator.getAttribute("id")));
101: } catch (NumberFormatException e) {
102: }
103:
104: this .args = new HashMap();
105:
106: List args = XMLUtil.getChildElements(validator, "arg");
107:
108: for (int l = 0; l < args.size(); l++) {
109: Element arg = (Element) args.get(l);
110: this .args.put(arg.getAttribute("name"), XMLUtil
111: .getText(arg));
112: }
113: }
114: }
|