1 28 29 package org.jibx.binding.model; 30 31 import java.util.ArrayList ; 32 33 42 43 public class ValidationContext extends TreeContext 44 { 45 46 private int m_warningCount; 47 48 49 private int m_errorCount; 50 51 52 private int m_fatalCount; 53 54 55 private ArrayList m_problemList; 56 57 60 public ValidationContext(IClassLocator iloc) { 61 super(iloc); 62 m_problemList = new ArrayList (); 63 } 64 65 71 public void prevalidate(BindingElement root) { 72 PrevalidationVisitor visitor = new PrevalidationVisitor(); 73 tourTree(root, visitor); 74 } 75 76 82 public void validate(BindingElement root) { 83 ValidationVisitor visitor = new ValidationVisitor(); 84 tourTree(root, visitor); 85 } 86 87 92 public int getWarningCount() { 93 return m_warningCount; 94 } 95 96 101 public int getErrorCount() { 102 return m_errorCount; 103 } 104 105 110 public int getFatalCount() { 111 return m_fatalCount; 112 } 113 114 122 public void addWarning(String msg) { 123 addWarning(msg, peekElement()); 124 } 125 126 133 public void addWarning(String msg, Object obj) { 134 addProblem(new ValidationProblem 135 (ValidationProblem.WARNING_LEVEL, msg, obj)); 136 } 137 138 146 public void addError(String msg) { 147 addError(msg, peekElement()); 148 } 149 150 157 public void addError(String msg, Object obj) { 158 addProblem(new ValidationProblem 159 (ValidationProblem.ERROR_LEVEL, msg, obj)); 160 } 161 162 170 public void addFatal(String msg) { 171 addFatal(msg, peekElement()); 172 } 173 174 182 public void addFatal(String msg, Object obj) { 183 addProblem(new ValidationProblem 184 (ValidationProblem.FATAL_LEVEL, msg, obj)); 185 } 186 187 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 217 public ArrayList getProblems() { 218 return m_problemList; 219 } 220 221 226 private class PrevalidationVisitor extends ModelVisitor 227 { 228 231 public boolean visit(ElementBase node) { 232 try { 233 node.prevalidate(ValidationContext.this); 234 } catch (Throwable t) { 235 addFatal("Error during validation: " + t.getMessage()); 236 t.printStackTrace(); 237 return false; 238 } 239 return true; 240 } 241 } 242 243 248 private class ValidationVisitor extends ModelVisitor 249 { 250 253 public void exit(ElementBase node) { 254 try { 255 node.validate(ValidationContext.this); 256 } catch (Throwable t) { 257 addFatal("Error during validation: " + t.getMessage()); 258 t.printStackTrace(); 259 } 260 } 261 } 262 } | Popular Tags |