KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdom > contrib > schema > ValidationError


1 /*--
2
3  $Id: ValidationError.java,v 1.2 2004/02/06 09:57:49 jhunter Exp $
4
5  Copyright (C) 2003-2004 Jason Hunter & Brett McLaughlin.
6  All rights reserved.
7
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11
12  1. Redistributions of source code must retain the above copyright
13     notice, this list of conditions, and the following disclaimer.
14
15  2. Redistributions in binary form must reproduce the above copyright
16     notice, this list of conditions, and the disclaimer that follows
17     these conditions in the documentation and/or other materials
18     provided with the distribution.
19
20  3. The name "JDOM" must not be used to endorse or promote products
21     derived from this software without prior written permission. For
22     written permission, please contact <request_AT_jdom_DOT_org>.
23
24  4. Products derived from this software may not be called "JDOM", nor
25     may "JDOM" appear in their name, without prior written permission
26     from the JDOM Project Management <request_AT_jdom_DOT_org>.
27
28  In addition, we request (but do not require) that you include in the
29  end-user documentation provided with the redistribution and/or in the
30  software itself an acknowledgement equivalent to the following:
31      "This product includes software developed by the
32       JDOM Project (http://www.jdom.org/)."
33  Alternatively, the acknowledgment may be graphical using the logos
34  available at http://www.jdom.org/images/logos.
35
36  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
40  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  SUCH DAMAGE.
48
49  This software consists of voluntary contributions made by many
50  individuals on behalf of the JDOM Project and was originally
51  created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
52  Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
53  on the JDOM Project, please see <http://www.jdom.org/>.
54
55  */

56
57 package org.jdom.contrib.schema;
58
59 /**
60  * A ValidationError object encapsulates a schema validation error or
61  * warning.
62  *
63  * @author Laurent Bihanic
64  */

65 public class ValidationError {
66     /** The severity for warnings. */
67     public final static Severity WARNING = new Severity(0);
68     /** The severity for recoverable validation errors. */
69     public final static Severity ERROR = new Severity(1);
70     /** The severity for non-recoverable validation errors. */
71     public final static Severity FATAL = new Severity(2);
72
73     /**
74      * The error severity.
75      */

76     private final Severity severity;
77
78     /**
79      * The detailed error message.
80      */

81     private final String JavaDoc message;
82
83     /**
84      * The JDOM node that caused the error.
85      */

86     private final Object JavaDoc node;
87
88     /**
89      * Creates a new validation error.
90      *
91      * @param severity the error severity.
92      * @param message the detailed error message.
93      */

94     public ValidationError(Severity severity, String JavaDoc message) {
95         this(severity, message, null);
96     }
97
98     /**
99      * Creates a new validation error.
100      *
101      * @param severity the error severity.
102      * @param message the detailed error message.
103      * @param node the JDOM node that caused the error.
104      */

105     public ValidationError(Severity severity, String JavaDoc message, Object JavaDoc node) {
106         this.severity = severity;
107         this.message = message;
108         this.node = node;
109     }
110
111     /**
112      * Returns the severity of this error.
113      *
114      * @return the severity of this error.
115      */

116     public Severity getSeverity() {
117         return this.severity;
118     }
119
120     /**
121      * Returns the detailed error message.
122      *
123      * @return the detailed error message.
124      */

125     public String JavaDoc getMessage() {
126         return this.message;
127     }
128
129     /**
130      * Returns the JDOM node that caused the error.
131      *
132      * @return the JDOM node that caused the error.
133      */

134     public Object JavaDoc getNode() {
135         return this.node;
136     }
137
138     /**
139      * Returns a string representation of this error suitable for
140      * debugging and diagnosis.
141      *
142      * @return a string representation of this error.
143      *
144      * @see java.lang.Object#toString()
145      */

146     public String JavaDoc toString() {
147         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
148
149         buf.append('[');
150         if (this.severity == WARNING) {
151             buf.append("WARNING");
152         }
153         else if (this.severity == ERROR) {
154             buf.append("ERROR");
155         }
156         else if (this.severity == FATAL) {
157             buf.append("FATAL");
158         }
159         buf.append("] message: \"").append(this.getMessage());
160
161         if (this.getNode() != null) {
162             buf.append("\", location: \"").append(this.getNode().toString());
163         }
164         buf.append("\"");
165
166         return buf.toString();
167     }
168
169
170     /**
171      * Class to support type-safe enumeration design pattern to
172      * represent severity levels of validation errors.
173      */

174     public static final class Severity {
175         /** The severity of the error. */
176         private final int level;
177
178         /**
179          * Severity constructor, private on purpose.
180          *
181          * @param level the severity level.
182          */

183         protected Severity(int level) {
184             this.level = level;
185         }
186
187         /**
188          * Returns a unique identifier for this severity.
189          *
190          * @return a unique identifier for this severity.
191          *
192          * @see java.lang.Object#hashCode()
193          */

194         public int hashCode() {
195             return this.level;
196         }
197
198         /**
199          * Returns a string representation of this severity suitable
200          * for debugging and diagnosis.
201          *
202          * @return a string representation of this severity.
203          *
204          * @see java.lang.Object#toString()
205          */

206         public String JavaDoc toString() {
207             return "[" + this.getClass().getName() + "] " + this.level;
208         }
209
210         /**
211          * Tests for severity equality. This is only necessary to
212          * handle cases where two <code>Type</code> objects are loaded
213          * by different class loaders.
214          *
215          * @param o the object compared for equality to this
216          * severity.
217          *
218          * @return <code>true</code> if and only if <code>o</code>
219          * represents the same severity as this object.
220          *
221          * @see java.lang.Object#equals(Object)
222          */

223         public boolean equals(Object JavaDoc o) {
224             return ((o == this) ||
225                     ((o != null) && (this.hashCode() == o.hashCode()) &&
226                     (this.getClass().getName().equals(o.getClass().getName()))));
227         }
228     }
229 }
230
231
Popular Tags