KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > validator > ValidationErr


1 package org.sapia.validator;
2
3 import java.io.PrintWriter JavaDoc;
4 import java.io.StringWriter JavaDoc;
5
6 /**
7  * An instance of this class represents a validation error.
8  *
9  * @author Yanick Duchesne
10  * <dl>
11  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
12  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
13  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
14  * </dl>
15  */

16 public class ValidationErr {
17   private String JavaDoc _id;
18   private Object JavaDoc _msg;
19   
20   /**
21    * Constructor for ValidationErr.
22    */

23   ValidationErr(String JavaDoc id, Throwable JavaDoc message) {
24     _id = id;
25     _msg = message;
26   }
27
28   /**
29    * Constructor for ValidationErr.
30    */

31   ValidationErr(String JavaDoc id, String JavaDoc message) {
32     _id = id;
33     _msg = message;
34   }
35
36   /**
37    * Returns the identifier of the rule that failed.
38    *
39    * @return the identifier of a <code>Rule</code>.
40    */

41   public String JavaDoc getId() {
42     return _id;
43   }
44
45   /**
46    * Returns the message of this error. If this instance encapsulates
47    * a <code>Throwable</code>, then that instance's message is returned.
48    *
49    * @return a message, or <code>null</code> if no message was set.
50    */

51   public String JavaDoc getMsg() {
52     if(_msg != null){
53       if(_msg instanceof Throwable JavaDoc){
54         return ((Throwable JavaDoc)_msg).getMessage();
55       }
56       return (String JavaDoc)_msg;
57     }
58     else{
59       return null;
60     }
61   }
62
63   /**
64    * Returns the <code>Throwable</code> of this instance, if
65    * the latter was indeed constructed with a Throwable.
66    *
67    * @return a <code>Throwable</code>.
68    */

69   public Throwable JavaDoc getThrowable() {
70     return (Throwable JavaDoc) _msg;
71   }
72
73   /**
74    * Returns <code>true</code> if this instance was constructed with
75    * a <code>Throwable</code>.
76    *
77    * @return <code>true</code> if this instance was constructed with
78    * a <code>Throwable</code>.
79    */

80   public boolean isThrowable() {
81     return _msg instanceof Throwable JavaDoc;
82   }
83   
84   /**
85    * Returns the stack trace of the <code>Throwable</code> that this instance
86    * encapsulates. Client applications should ensure that the internal error object
87    * is indeed a <code>Throwable</code> by calling the <code>isThrowable()</code> method
88    * on this instance.
89    *
90    * @see #isThrowable().
91    *
92    * @return the stacktrace of this instance's internal <code>Throwable</code>,
93    * as a string.
94    */

95   public String JavaDoc getStackTrace(){
96     if(!isThrowable()){
97       throw new IllegalStateException JavaDoc("Internal error is not an instance of " + Throwable JavaDoc.class.getName());
98       
99     }
100     StringWriter JavaDoc sw = new StringWriter JavaDoc();
101     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw, true);
102     Throwable JavaDoc t = (Throwable JavaDoc)_msg;
103     t.printStackTrace(pw);
104     pw.close();
105     return sw.toString();
106   }
107 }
108
Popular Tags