KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > project > validator > Validator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20
21 package org.apache.cayenne.project.validator;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.cayenne.conf.ConfigStatus;
28 import org.apache.cayenne.project.Project;
29 import org.apache.cayenne.project.ProjectPath;
30
31 /**
32  * Validator is used to validate Cayenne projects.
33  *
34  * @author Andrus Adamchik
35  */

36 public class Validator {
37     protected Project project;
38     protected List JavaDoc validationResults = new ArrayList JavaDoc();
39     protected int maxSeverity;
40
41     /**
42      * Creates a new validator initialized with the project.
43      *
44      * @param project
45      */

46     public Validator(Project project) {
47         this.project = project;
48     }
49
50     /**
51      * Initializes validator with the project loading config status.
52      *
53      * @param project
54      * @param status
55      */

56     public Validator(Project project, ConfigStatus status) {
57         this(project);
58
59         if (status.hasFailures()) {
60             ProjectPath path = new ProjectPath(project);
61
62             Iterator JavaDoc it = status.getOtherFailures().iterator();
63             while (it.hasNext()) {
64                 registerError((String JavaDoc) 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     /**
90      * Returns the project.
91      * @return Project
92      */

93     public Project getProject() {
94         return project;
95     }
96
97     /**
98      * Resets internal state.
99      * Called internally before starting validation.
100      */

101     protected void reset() {
102         if (validationResults != null) {
103             validationResults = new ArrayList JavaDoc();
104         }
105         maxSeverity = ValidationInfo.VALID;
106     }
107
108     /**
109      * Returns maximum severity level encountered during
110      * the last validation run.
111      */

112     public int getMaxSeverity() {
113         return maxSeverity;
114     }
115
116     /**
117      * Registers validation result.
118      * Increases internally stored max severity if
119      * <code>result</code> parameter has a higher severity then the current value.
120      * Leaves current value unchanged otherwise.
121      */

122     public void registerValidated(
123         int severity,
124         String JavaDoc 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 JavaDoc message, ProjectPath treeNodePath) {
134         registerValidated(ValidationInfo.ERROR, message, treeNodePath);
135     }
136
137     public void registerWarning(String JavaDoc message, ProjectPath treeNodePath) {
138         registerValidated(ValidationInfo.WARNING, message, treeNodePath);
139     }
140
141     /** Return collection of ValidationInfo objects from last validation. */
142     public List JavaDoc validationResults() {
143         return validationResults;
144     }
145
146     /**
147      * Validates all project elements.
148      *
149      * @return ValidationInfo.VALID if no errors were found,
150      * or an error code of the error with the highest severity
151      * if there were errors.
152      */

153     public synchronized int validate() {
154         return validate(project.treeNodes());
155     }
156
157     /**
158      * Validates a set of tree nodes passed as an iterator.
159      *
160      * @return ValidationInfo.VALID if no errors were found,
161      * or an error code of the error with the highest severity
162      * if there were errors.
163      */

164     public synchronized int validate(Iterator JavaDoc 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