1 56 57 package org.objectstyle.cayenne.project.validator; 58 59 import java.util.ArrayList ; 60 import java.util.Iterator ; 61 import java.util.List ; 62 63 import org.objectstyle.cayenne.conf.ConfigStatus; 64 import org.objectstyle.cayenne.project.Project; 65 import org.objectstyle.cayenne.project.ProjectPath; 66 67 72 public class Validator { 73 protected Project project; 74 protected List validationResults = new ArrayList (); 75 protected int maxSeverity; 76 77 82 public Validator(Project project) { 83 this.project = project; 84 } 85 86 92 public Validator(Project project, ConfigStatus status) { 93 this(project); 94 95 if (status.hasFailures()) { 96 ProjectPath path = new ProjectPath(project); 97 98 Iterator it = status.getOtherFailures().iterator(); 99 while (it.hasNext()) { 100 registerError((String ) it.next(), path); 101 } 102 103 it = status.getFailedMaps().keySet().iterator(); 104 while (it.hasNext()) { 105 registerError("Map failed to load: " + it.next(), path); 106 } 107 108 it = status.getFailedAdapters().keySet().iterator(); 109 while (it.hasNext()) { 110 registerError("Adapter failed to load: " + it.next(), path); 111 } 112 113 it = status.getFailedDataSources().keySet().iterator(); 114 while (it.hasNext()) { 115 registerError("DataSource failed to load: " + it.next(), path); 116 } 117 118 it = status.getFailedMapRefs().iterator(); 119 while (it.hasNext()) { 120 registerError("Map reference failed to load: " + it.next(), path); 121 } 122 } 123 } 124 125 129 public Project getProject() { 130 return project; 131 } 132 133 137 protected void reset() { 138 if (validationResults != null) { 139 validationResults = new ArrayList (); 140 } 141 maxSeverity = ValidationInfo.VALID; 142 } 143 144 148 public int getMaxSeverity() { 149 return maxSeverity; 150 } 151 152 158 public void registerValidated( 159 int severity, 160 String message, 161 ProjectPath treeNodePath) { 162 ValidationInfo result = new ValidationInfo(severity, message, treeNodePath); 163 validationResults.add(result); 164 if (maxSeverity < severity) { 165 maxSeverity = severity; 166 } 167 } 168 169 public void registerError(String message, ProjectPath treeNodePath) { 170 registerValidated(ValidationInfo.ERROR, message, treeNodePath); 171 } 172 173 public void registerWarning(String message, ProjectPath treeNodePath) { 174 registerValidated(ValidationInfo.WARNING, message, treeNodePath); 175 } 176 177 178 public List validationResults() { 179 return validationResults; 180 } 181 182 189 public synchronized int validate() { 190 return validate(project.treeNodes()); 191 } 192 193 200 public synchronized int validate(Iterator treeNodes) { 201 reset(); 202 203 while (treeNodes.hasNext()) { 204 TreeNodeValidator.validate((ProjectPath) treeNodes.next(), this); 205 } 206 207 return getMaxSeverity(); 208 } 209 } | Popular Tags |