1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jdo.model;
18
19 import org.apache.jdo.util.I18NHelper;
20
21 /***
22 * This exception indicates a problem during model validation.
23 *
24 * @author Michael Bouschen
25 * @since JDO 1.0.1
26 */
27 public class ModelValidationException
28 extends ModelException
29 {
30 /*** Constant representing an error. */
31 public static final int ERROR = 0;
32
33 /*** Constant representing a warning. */
34 public static final int WARNING = 1;
35
36 /***
37 * This field holds the type -- one of {@link #ERROR} or {@link #WARNING}
38 */
39 private int type;
40
41 /***
42 * This field holds the offending object -- the one being validated
43 * when the problem occurred
44 */
45 private Object offendingObject;
46
47 /*** I18N support */
48 private static I18NHelper msg =
49 I18NHelper.getInstance(ModelValidationException.class);
50
51 /***
52 * Creates new <code>ModelValidationException</code> of type
53 * {@link #ERROR} with <code>null</code> as the offending object and
54 * no detail message.
55 */
56 public ModelValidationException()
57 {
58 this(ERROR, null, null);
59 }
60
61 /***
62 * Constructs a <code>ModelValidationException</code> of type
63 * {@link #ERROR} with <code>null</code> as the offending object and
64 * with the specified detail message.
65 * @param message the detail message.
66 */
67 public ModelValidationException(String message)
68 {
69 this(ERROR, null, message);
70 }
71
72 /***
73 * Constructs a <code>ModelValidationException</code> of type
74 * {@link #ERROR} with the specified offending object and no
75 * detail message.
76 * @param offendingObject the offending object.
77 */
78 public ModelValidationException (Object offendingObject)
79 {
80 this(ERROR, offendingObject, null);
81 }
82
83 /***
84 * Constructs a <code>ModelValidationException</code> of type
85 * {@link #ERROR} with the specified offending object and detail
86 * message .
87 * @param offendingObject the offending object.
88 * @param message the detail message.
89 */
90 public ModelValidationException (Object offendingObject, String message)
91 {
92 this(ERROR, offendingObject, message);
93 }
94
95 /***
96 * Constructs a <code>ModelValidationException</code> of the specified
97 * type with the specified detail message and offending object.
98 * @param errorType the type -- one of {@link #ERROR} or
99 * {@link #WARNING}.
100 * @param offendingObject the offending object.
101 * @param message the detail message.
102 */
103 public ModelValidationException(int errorType, Object offendingObject,
104 String message)
105 {
106 super(message);
107 this.type = errorType;
108 this.offendingObject = offendingObject;
109 }
110
111 /***
112 * Get the offending object -- the one being validated when the problem
113 * occurred.
114 */
115 public Object getOffendingObject ()
116 {
117 return offendingObject;
118 }
119
120 /***
121 * Get the type -- one of {@link #ERROR} or {@link #WARNING}.
122 */
123 public int getType()
124 {
125 return type;
126 }
127
128 /***
129 * Returns the error message string of this throwable object.
130 * @return the error message string of this
131 * <code>ModelValidationException</code>, prepended with the warning string
132 * if the type is {@link #WARNING}
133 *
134 */
135 public String getMessage ()
136 {
137 String message = super.getMessage();
138 if ((WARNING == getType()) &&
139 (message != null) && (message.length() > 0)) {
140 message = msg.msg("MSG_OffendingObject") + message;
141 }
142 return message;
143 }
144 /***
145 * The <code>String</code> representation includes the name of the class,
146 * the descriptive comment (if any),
147 * and the <code>String</code> representation of the cause (if any).
148 * @return the <code>String</code>.
149 */
150 public String toString()
151 {
152 StringBuffer sb = new StringBuffer();
153 sb.append(super.toString());
154
155 if (offendingObject != null) {
156 sb.append("\n");
157 sb.append(msg.msg("MSG_OffendingObject"));
158 sb.append(offendingObject.toString());
159 }
160 return sb.toString();
161 }
162
163 }