Added ValidatorSuite around groups of static validators.
[shibboleth/cpp-xmltooling.git] / xmltooling / validation / ValidatorSuite.h
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * @file ValidatorSuite.h\r
19  * \r
20  * Groups of rule checkers of XMLObjects based on type or element name. \r
21  */\r
22 \r
23 #ifndef __xmltooling_valsuite_h__\r
24 #define __xmltooling_valsuite_h__\r
25 \r
26 #include <map>\r
27 #include <vector>\r
28 #include <xmltooling/QName.h>\r
29 #include <xmltooling/validation/Validator.h>\r
30 \r
31 #if defined (_MSC_VER)\r
32     #pragma warning( push )\r
33     #pragma warning( disable : 4250 4251 )\r
34 #endif\r
35 \r
36 namespace xmltooling {\r
37 \r
38     /**\r
39      * A collection of validators that can be applied to an XMLObject and its children. These collections can represent\r
40      * usage specific checks, such as those outlined in schemas or profiles of specific XML specifications.\r
41      * \r
42      * Registered Validators must be stateless and cloneable. Validators are fetched based on schema type and\r
43      * element name, in that order.\r
44      */\r
45     class XMLTOOL_API ValidatorSuite\r
46     {\r
47     MAKE_NONCOPYABLE(ValidatorSuite);\r
48     public:\r
49         /**\r
50          * Creates a new suite.\r
51          * \r
52          * @param id    an identifier for the suite\r
53          */\r
54         ValidatorSuite(const char* id) : m_id(id) {}\r
55         \r
56         ~ValidatorSuite() {\r
57             destroyValidators();\r
58         }\r
59 \r
60         /**\r
61          * Gets a unique ID for this suite.\r
62          * \r
63          * @return a unique ID for this suite\r
64          */\r
65         const char* getId() {\r
66             return m_id.c_str();\r
67         }\r
68 \r
69         /**\r
70          * Evaluates the registered validators against the given XMLObject and it's children.\r
71          * \r
72          * @param xmlObject the XMLObject tree to validate\r
73          * \r
74          * @throws ValidationException thrown if the element tree is not valid\r
75          */\r
76         void validate(const XMLObject* xmlObject) const;\r
77 \r
78         /**\r
79          * Registers a new validator for the given key.\r
80          * \r
81          * @param key       the key used to retrieve the validator\r
82          * @param validator the validator\r
83          */\r
84         void registerValidator(const QName& key, Validator* validator) {\r
85             std::map< QName, std::vector<Validator*> >::iterator i=m_map.find(key);\r
86             if (i==m_map.end())\r
87                 m_map.insert(std::make_pair(key,std::vector<Validator*>(1,validator)));\r
88             else\r
89                 i->second.push_back(validator);\r
90         }\r
91 \r
92         /**\r
93          * Deregisters validators.\r
94          * \r
95          * @param key       the key for the validators to be deregistered\r
96          */\r
97         void deregisterValidators(const QName& key);\r
98 \r
99         /**\r
100          * Unregisters and destroys all registered validators. \r
101          */\r
102         void destroyValidators();\r
103 \r
104     private:\r
105         std::string m_id;\r
106         std::map< QName, std::vector<Validator*> > m_map;\r
107     };\r
108 \r
109 };\r
110 \r
111 #if defined (_MSC_VER)\r
112     #pragma warning( pop )\r
113 #endif\r
114 \r
115 #endif /* __xmltooling_valsuite_h__ */\r