KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > spi > Validation


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21
22 package org.netbeans.modules.xml.xam.spi;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.modules.xml.xam.Model;
29 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem;
30 import org.openide.util.Lookup;
31
32
33 /**
34  * Validation clients use this interface to start validation on a model.
35  * Validator implementation can use this to optimize computing validation results
36  * by finding which models are already validated.
37  *
38  * @author Nam Nguyen
39  * @author Praveen Savur
40  *
41  */

42 public class Validation {
43     
44     private static Collection JavaDoc<Validator> validators;
45     
46     static {
47         // Lookup all available providers and create a list of providers.
48
lookupProviders();
49     }
50     
51     private List JavaDoc<ResultItem> validationResult;
52     private List JavaDoc<Model> validatedModels;
53     
54     public Validation() {
55 // System.out.println("ValidationImpl(): In constructor");
56
initialise();
57     }
58         
59     
60     
61     /**
62      * Validates the model.
63      * Note: Clients should call this method on a Validation instance only
64      * once. The same Validation instance should not be reused.
65      *
66      * @param model Contains the model for which validation has to be provided.
67      * @param validationType Type of validation: complete or partial.
68      */

69     public void validate(Model model, ValidationType validationType) {
70 // System.out.println("ValidationImpl(): validate()");
71

72         if (validatedModels.contains(model))
73             return;
74         
75         validatedModels.add(model);
76         // Call each provider and accumulate results.
77
for(Validator provider: validators) {
78             ValidationResult result = provider.validate(model, this, validationType);
79             if (result != null) {
80                 // Gather validation results.
81
validationResult.addAll(result.getValidationResult());
82
83                 // Updated validated models list.
84
validatedModels.addAll(result.getValidatedModels());
85             }
86         }
87     }
88     
89     
90     
91     /**
92      * Returns the last validationResult.
93      */

94     public List JavaDoc<ResultItem> getValidationResult() {
95         return validationResult;
96     }
97     
98     
99     /**
100      * Retuns an unmodifiable list of validated models.
101      */

102     public List JavaDoc<Model> getValidatedModels() {
103         return Collections.unmodifiableList(validatedModels);
104     }
105     
106     
107     /**
108      * The type of validation.
109      * COMPLETE indicates that the model will be recursively validated.
110      * ie., all imported models will also be validated.
111      * PARTIAL indicated that only the model will be validated and
112      * no imports will be validated.
113      */

114     public enum ValidationType {
115         COMPLETE, PARTIAL
116     }
117     
118     
119     
120     /**
121      * Initialise.
122      */

123     private void initialise() {
124         validationResult = new ArrayList JavaDoc<ResultItem>();
125         validatedModels = new ArrayList JavaDoc<Model>();
126     }
127     
128     
129     /**
130      * Get a list of all providers.
131      */

132     private static void lookupProviders() {
133
134         if(validators != null)
135             return;
136         
137         validators = new ArrayList JavaDoc<Validator>();
138         
139 // System.out.println("ValidationImpl(): lookupProviders()");
140
Lookup.Result result = Lookup.getDefault().lookup(
141                 new Lookup.Template(Validator.class));
142         
143         for(Object JavaDoc obj: result.allInstances()) {
144             Validator validator = (Validator) obj;
145             validators.add(validator);
146         }
147 // System.out.println("providers are: " + validators);
148
}
149     
150     
151
152 }
153
Popular Tags