KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
32
33 /**
34  * Tracks the validation state. This includes the current validation phase, as
35  * well as order-dependent state information collected while walking the tree
36  * structure of a binding model. Collects all errors and warnings and maintains
37  * a summary of the severity of the problems found.
38  *
39  * @author Dennis M. Sosnoski
40  * @version 1.0
41  */

42  
43 public class ValidationContext extends TreeContext
44 {
45     /** Number of warnings reported. */
46     private int m_warningCount;
47     
48     /** Number of errors reported. */
49     private int m_errorCount;
50     
51     /** Number of fatals reported. */
52     private int m_fatalCount;
53     
54     /** List of problem items reported by validation. */
55     private ArrayList JavaDoc m_problemList;
56     
57     /**
58      * Constructor.
59      */

60     public ValidationContext(IClassLocator iloc) {
61         super(iloc);
62         m_problemList = new ArrayList JavaDoc();
63     }
64     
65     /**
66      * Prevalidate binding model tree. This calls the prevalidate method for
67      * each element in the tree, in preorder traversal order.
68      *
69      * @param root binding node of tree to be prevalidated
70      */

71     public void prevalidate(BindingElement root) {
72         PrevalidationVisitor visitor = new PrevalidationVisitor();
73         tourTree(root, visitor);
74     }
75     
76     /**
77      * Validate binding model tree. This calls the validate method for each
78      * element in the tree, in postorder traversal order.
79      *
80      * @param root binding node of tree to be prevalidated
81      */

82     public void validate(BindingElement root) {
83         ValidationVisitor visitor = new ValidationVisitor();
84         tourTree(root, visitor);
85     }
86     
87     /**
88      * Get number of warning problems reported.
89      *
90      * @return warning problem count
91      */

92     public int getWarningCount() {
93         return m_warningCount;
94     }
95     
96     /**
97      * Get number of error problems reported.
98      *
99      * @return error problem count
100      */

101     public int getErrorCount() {
102         return m_errorCount;
103     }
104     
105     /**
106      * Get number of fatal problems reported.
107      *
108      * @return fatal problem count
109      */

110     public int getFatalCount() {
111         return m_fatalCount;
112     }
113     
114     /**
115      * Add warning item for current element. Adds a warning item to the problem
116      * list, which is a possible problem that still allows reasonable operation.
117      * This form of the call can only be used during a tree tour being
118      * controlled by this context.
119      *
120      * @param msg problem description
121      */

122     public void addWarning(String JavaDoc msg) {
123         addWarning(msg, peekElement());
124     }
125     
126     /**
127      * Add warning item. Adds a warning item to the problem list, which is a
128      * possible problem that still allows reasonable operation.
129      *
130      * @param msg problem description
131      * @param obj source object for validation error
132      */

133     public void addWarning(String JavaDoc msg, Object JavaDoc obj) {
134         addProblem(new ValidationProblem
135             (ValidationProblem.WARNING_LEVEL, msg, obj));
136     }
137     
138     /**
139      * Add error item for current element. Adds an error item to the problem
140      * list, which is a definite problem that still allows validation to
141      * proceed. This form of the call can only be used during a tree tour being
142      * controlled by this context.
143      *
144      * @param msg problem description
145      */

146     public void addError(String JavaDoc msg) {
147         addError(msg, peekElement());
148     }
149     
150     /**
151      * Add error item. Adds an error item to the problem list, which is a
152      * definite problem that still allows validation to proceed.
153      *
154      * @param msg problem description
155      * @param obj source object for validation error
156      */

157     public void addError(String JavaDoc msg, Object JavaDoc obj) {
158         addProblem(new ValidationProblem
159             (ValidationProblem.ERROR_LEVEL, msg, obj));
160     }
161     
162     /**
163      * Add fatal item for current element. Adds a fatal item to the problem
164      * list, which is a severe problem that blocks further validation within the
165      * tree branch involved. This form of the call can only be used during a
166      * tree tour being controlled by this context.
167      *
168      * @param msg problem description
169      */

170     public void addFatal(String JavaDoc msg) {
171         addFatal(msg, peekElement());
172     }
173     
174     /**
175      * Add fatal item. Adds a fatal item to the problem list, which is a severe
176      * problem that blocks further validation within the tree branch involved.
177      * The object associated with a fatal error should always be an element.
178      *
179      * @param msg problem description
180      * @param obj source object for validation error (should be an element)
181      */

182     public void addFatal(String JavaDoc msg, Object JavaDoc obj) {
183         addProblem(new ValidationProblem
184             (ValidationProblem.FATAL_LEVEL, msg, obj));
185     }
186     
187     /**
188      * Add problem report. The problem is added and counted as appropriate.
189      *
190      * @param problem details of problem report
191      */

192     public void addProblem(ValidationProblem problem) {
193         m_problemList.add(problem);
194         switch (problem.getSeverity()) {
195             
196             case ValidationProblem.ERROR_LEVEL:
197                 m_errorCount++;
198                 break;
199                 
200             case ValidationProblem.FATAL_LEVEL:
201                 m_fatalCount++;
202                 addSkip(problem.getComponent());
203                 break;
204             
205             case ValidationProblem.WARNING_LEVEL:
206                 m_warningCount++;
207                 break;
208                 
209         }
210     }
211     
212     /**
213      * Get list of problems.
214      *
215      * @return problem list
216      */

217     public ArrayList JavaDoc getProblems() {
218         return m_problemList;
219     }
220     
221     /**
222      * Inner class for handling prevalidation. This visitor implementation just
223      * calls the {@link org.jibx.binding.model#prevalidate} method for each
224      * element visited in preorder.
225      */

226     private class PrevalidationVisitor extends ModelVisitor
227     {
228         /* (non-Javadoc)
229          * @see org.jibx.binding.model.ModelVisitor#visit(org.jibx.binding.model.ElementBase)
230          */

231         public boolean visit(ElementBase node) {
232             try {
233                 node.prevalidate(ValidationContext.this);
234             } catch (Throwable JavaDoc t) {
235                 addFatal("Error during validation: " + t.getMessage());
236                 t.printStackTrace();
237                 return false;
238             }
239             return true;
240         }
241     }
242     
243     /**
244      * Inner class for handling validation. This visitor implementation just
245      * calls the {@link org.jibx.binding.model#validate} method for each
246      * element visited in postorder.
247      */

248     private class ValidationVisitor extends ModelVisitor
249     {
250         /* (non-Javadoc)
251          * @see org.jibx.binding.model.ModelVisitor#exit(org.jibx.binding.model.ElementBase)
252          */

253         public void exit(ElementBase node) {
254             try {
255                 node.validate(ValidationContext.this);
256             } catch (Throwable JavaDoc t) {
257                 addFatal("Error during validation: " + t.getMessage());
258                 t.printStackTrace();
259             }
260         }
261     }
262 }
Popular Tags