001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow;
006:
007: import java.util.*;
008:
009: /**
010: * Exception to indicate the user input is invalid. Handles both general errors and errors specific to an input.
011: *
012: * @author <a href="mailto:plightbo@hotmail.com">Patrick Lightbody</a>
013: * @version $Revision: 1.2 $
014: */
015: public class InvalidInputException extends WorkflowException {
016: //~ Instance fields ////////////////////////////////////////////////////////
017:
018: private List genericErrors = new ArrayList();
019: private Map errors = new HashMap();
020:
021: //~ Constructors ///////////////////////////////////////////////////////////
022:
023: public InvalidInputException() {
024: super ();
025: }
026:
027: /**
028: * Creates a new exception using the supplied Object
029: * as a generic base. If the object is an instance of
030: * this exception, all properties are copied to this
031: * exception. If the object is an instance of Map or
032: * String[], an errorName->errorMessage mapping will
033: * be attempted to be extracted. If the object is
034: * something else, it's toString() method will be
035: * called and added as a single generic error.
036: *
037: * @param o the object
038: */
039: public InvalidInputException(Object o) {
040: if (o instanceof InvalidInputException) {
041: InvalidInputException iie = (InvalidInputException) o;
042: errors = iie.errors;
043: genericErrors = iie.genericErrors;
044: } else if (o instanceof Map) {
045: errors = (Map) o;
046: } else if (o instanceof String[]) {
047: String[] stringMap = (String[]) o;
048: int length = stringMap.length;
049: String name = null;
050:
051: for (int i = 0; i < length; i++) {
052: if ((i % 2) == 0) {
053: name = stringMap[i];
054: } else {
055: addError(name, stringMap[i]);
056: }
057: }
058: } else {
059: addError(o.toString());
060: }
061: }
062:
063: /**
064: * Creates a new exception with an associated generic error.
065: *
066: * @param error a generic error message
067: */
068: public InvalidInputException(String error) {
069: super (error);
070: addError(error);
071: }
072:
073: /**
074: * Creates a new exception with an error specific to an input.
075: *
076: * @param name the input name that contains the error
077: * @param error an error about the given name
078: */
079: public InvalidInputException(String name, String error) {
080: super ();
081: addError(name, error);
082: }
083:
084: //~ Methods ////////////////////////////////////////////////////////////////
085:
086: /**
087: * Returns a map (String->String) of the input-specific errors.
088: *
089: * @return a map (String->String) of the input-specific errors
090: */
091: public Map getErrors() {
092: return errors;
093: }
094:
095: /**
096: * Returns a list (String) of generic errors.
097: *
098: * @return A list (String) of generic errors
099: */
100: public List getGenericErrors() {
101: return genericErrors;
102: }
103:
104: /**
105: * Adds a generic error.
106: *
107: * @param error the generic error message
108: */
109: public void addError(String error) {
110: genericErrors.add(error);
111: }
112:
113: /**
114: * Adds an input-specific error.
115: *
116: * @param name the name of the input
117: * @param error the error message
118: */
119: public void addError(String name, String error) {
120: errors.put(name, error);
121: }
122:
123: public String toString() {
124: return "[InvalidInputException: [Error map: [" + errors
125: + "]] [Error list: [" + genericErrors + "]]";
126: }
127: }
|