KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jibx > binding > model > ValidationProblem


1 /*
2 Copyright (c) 2004-2005, Dennis M. Sosnoski
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8  * Redistributions of source code must retain the above copyright notice, this
9    list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice,
11    this list of conditions and the following disclaimer in the documentation
12    and/or other materials provided with the distribution.
13  * Neither the name of JiBX nor the names of its contributors may be used
14    to endorse or promote products derived from this software without specific
15    prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */

28
29 package org.jibx.binding.model;
30
31 import org.jibx.runtime.ITrackSource;
32 import org.jibx.runtime.ValidationException;
33
34 /**
35  * Problem reported by model validation. Provides the details for a specific
36  * problem item.
37  *
38  * @author Dennis M. Sosnoski
39  * @version 1.0
40  */

41  
42 public class ValidationProblem
43 {
44     // severity levels
45
public static final int WARNING_LEVEL = 0;
46     public static final int ERROR_LEVEL = 1;
47     public static final int FATAL_LEVEL = 2;
48     
49     /** Problem severity level. */
50     private final int m_severity;
51     
52     /** Description of problem found. */
53     private final String JavaDoc m_description;
54     
55     /** Component that reported problem. */
56     private final Object JavaDoc m_component;
57     
58     /**
59      * Full constructor.
60      *
61      * @param level severity level of problem
62      * @param msg problem description
63      * @param obj source object for validation error (may be <code>null</code>
64      * if not specific to a particular component)
65      */

66     /*package*/ ValidationProblem(int level, String JavaDoc msg, Object JavaDoc obj) {
67         m_severity = level;
68         if (obj == null) {
69             m_description = msg;
70         } else {
71             m_description = msg + " for " + componentDescription(obj);
72         }
73         m_component = obj;
74     }
75     
76     /**
77      * Create description text for a component of a binding definition.
78      *
79      * @param obj binding definition component
80      */

81     public static String JavaDoc componentDescription(Object JavaDoc obj) {
82         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
83         if (obj instanceof ElementBase) {
84             buff.append(ElementBase.ELEMENT_NAMES[((ElementBase)obj).type()]);
85             buff.append(" element");
86         } else {
87             String JavaDoc cname = obj.getClass().getName();
88             int split = cname.lastIndexOf('.');
89             if (split >= 0) {
90                 cname = cname.substring(split+1);
91             }
92             buff.append(cname);
93         }
94         if (obj instanceof ITrackSource) {
95             buff.append(" at ");
96             buff.append(ValidationException.describe(obj));
97         } else {
98             buff.append(" at unknown location");
99         }
100         return buff.toString();
101     }
102
103     /**
104      * Constructor using default (error) severity level.
105      *
106      * @param msg problem description
107      * @param obj source object for validation error
108      */

109     /*package*/ ValidationProblem(String JavaDoc msg, Object JavaDoc obj) {
110         this(ERROR_LEVEL, msg, obj);
111     }
112     
113     /**
114      * Get the main binding definition item for the problem.
115      *
116      * @return element or attribute at root of problem
117      */

118     public Object JavaDoc getComponent() {
119         return m_component;
120     }
121     
122     /**
123      * Get problem description.
124      *
125      * @return problem description
126      */

127     public String JavaDoc getDescription() {
128         return m_description;
129     }
130     
131     /**
132      * Get problem severity level.
133      *
134      * @return severity level for problem
135      */

136     public int getSeverity() {
137         return m_severity;
138     }
139 }
Popular Tags