1 19 20 21 package org.apache.cayenne.project.validator; 22 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 import org.apache.cayenne.conf.ConfigStatus; 28 import org.apache.cayenne.project.Project; 29 import org.apache.cayenne.project.ProjectPath; 30 31 36 public class Validator { 37 protected Project project; 38 protected List validationResults = new ArrayList (); 39 protected int maxSeverity; 40 41 46 public Validator(Project project) { 47 this.project = project; 48 } 49 50 56 public Validator(Project project, ConfigStatus status) { 57 this(project); 58 59 if (status.hasFailures()) { 60 ProjectPath path = new ProjectPath(project); 61 62 Iterator it = status.getOtherFailures().iterator(); 63 while (it.hasNext()) { 64 registerError((String ) it.next(), path); 65 } 66 67 it = status.getFailedMaps().keySet().iterator(); 68 while (it.hasNext()) { 69 registerError("Map failed to load: " + it.next(), path); 70 } 71 72 it = status.getFailedAdapters().keySet().iterator(); 73 while (it.hasNext()) { 74 registerError("Adapter failed to load: " + it.next(), path); 75 } 76 77 it = status.getFailedDataSources().keySet().iterator(); 78 while (it.hasNext()) { 79 registerError("DataSource failed to load: " + it.next(), path); 80 } 81 82 it = status.getFailedMapRefs().iterator(); 83 while (it.hasNext()) { 84 registerError("Map reference failed to load: " + it.next(), path); 85 } 86 } 87 } 88 89 93 public Project getProject() { 94 return project; 95 } 96 97 101 protected void reset() { 102 if (validationResults != null) { 103 validationResults = new ArrayList (); 104 } 105 maxSeverity = ValidationInfo.VALID; 106 } 107 108 112 public int getMaxSeverity() { 113 return maxSeverity; 114 } 115 116 122 public void registerValidated( 123 int severity, 124 String message, 125 ProjectPath treeNodePath) { 126 ValidationInfo result = new ValidationInfo(severity, message, treeNodePath); 127 validationResults.add(result); 128 if (maxSeverity < severity) { 129 maxSeverity = severity; 130 } 131 } 132 133 public void registerError(String message, ProjectPath treeNodePath) { 134 registerValidated(ValidationInfo.ERROR, message, treeNodePath); 135 } 136 137 public void registerWarning(String message, ProjectPath treeNodePath) { 138 registerValidated(ValidationInfo.WARNING, message, treeNodePath); 139 } 140 141 142 public List validationResults() { 143 return validationResults; 144 } 145 146 153 public synchronized int validate() { 154 return validate(project.treeNodes()); 155 } 156 157 164 public synchronized int validate(Iterator treeNodes) { 165 reset(); 166 167 while (treeNodes.hasNext()) { 168 TreeNodeValidator.validate((ProjectPath) treeNodes.next(), this); 169 } 170 171 return getMaxSeverity(); 172 } 173 } 174 | Popular Tags |