KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > InvalidInputException


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

5 package com.opensymphony.workflow;
6
7 import java.util.*;
8
9
10 /**
11  * Exception to indicate the user input is invalid. Handles both general errors and errors specific to an input.
12  *
13  * @author <a HREF="mailto:plightbo@hotmail.com">Patrick Lightbody</a>
14  * @version $Revision: 1.2 $
15  */

16 public class InvalidInputException extends WorkflowException {
17     //~ Instance fields ////////////////////////////////////////////////////////
18

19     private List genericErrors = new ArrayList();
20     private Map errors = new HashMap();
21
22     //~ Constructors ///////////////////////////////////////////////////////////
23

24     public InvalidInputException() {
25         super();
26     }
27
28     /**
29      * Creates a new exception using the supplied Object
30      * as a generic base. If the object is an instance of
31      * this exception, all properties are copied to this
32      * exception. If the object is an instance of Map or
33      * String[], an errorName->errorMessage mapping will
34      * be attempted to be extracted. If the object is
35      * something else, it's toString() method will be
36      * called and added as a single generic error.
37      *
38      * @param o the object
39      */

40     public InvalidInputException(Object JavaDoc o) {
41         if (o instanceof InvalidInputException) {
42             InvalidInputException iie = (InvalidInputException) o;
43             errors = iie.errors;
44             genericErrors = iie.genericErrors;
45         } else if (o instanceof Map) {
46             errors = (Map) o;
47         } else if (o instanceof String JavaDoc[]) {
48             String JavaDoc[] stringMap = (String JavaDoc[]) o;
49             int length = stringMap.length;
50             String JavaDoc name = null;
51
52             for (int i = 0; i < length; i++) {
53                 if ((i % 2) == 0) {
54                     name = stringMap[i];
55                 } else {
56                     addError(name, stringMap[i]);
57                 }
58             }
59         } else {
60             addError(o.toString());
61         }
62     }
63
64     /**
65      * Creates a new exception with an associated generic error.
66      *
67      * @param error a generic error message
68      */

69     public InvalidInputException(String JavaDoc error) {
70         super(error);
71         addError(error);
72     }
73
74     /**
75      * Creates a new exception with an error specific to an input.
76      *
77      * @param name the input name that contains the error
78      * @param error an error about the given name
79      */

80     public InvalidInputException(String JavaDoc name, String JavaDoc error) {
81         super();
82         addError(name, error);
83     }
84
85     //~ Methods ////////////////////////////////////////////////////////////////
86

87     /**
88      * Returns a map (String->String) of the input-specific errors.
89      *
90      * @return a map (String->String) of the input-specific errors
91      */

92     public Map getErrors() {
93         return errors;
94     }
95
96     /**
97      * Returns a list (String) of generic errors.
98      *
99      * @return A list (String) of generic errors
100      */

101     public List getGenericErrors() {
102         return genericErrors;
103     }
104
105     /**
106      * Adds a generic error.
107      *
108      * @param error the generic error message
109      */

110     public void addError(String JavaDoc error) {
111         genericErrors.add(error);
112     }
113
114     /**
115      * Adds an input-specific error.
116      *
117      * @param name the name of the input
118      * @param error the error message
119      */

120     public void addError(String JavaDoc name, String JavaDoc error) {
121         errors.put(name, error);
122     }
123
124     public String JavaDoc toString() {
125         return "[InvalidInputException: [Error map: [" + errors + "]] [Error list: [" + genericErrors + "]]";
126     }
127 }
128
Popular Tags