Validator interface.
[shibboleth/cpp-xmltooling.git] / xmltooling / validation / Validator.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 Validator.h\r
19  * \r
20  * Rules checking of XMLObjects \r
21  */\r
22 \r
23 #if !defined(__xmltooling_validator_h__)\r
24 #define __xmltooling_validator_h__\r
25 \r
26 #include <map>\r
27 #include <vector>\r
28 #include <xmltooling/QName.h>\r
29 #include <xmltooling/XMLObject.h>\r
30 \r
31 using namespace xercesc;\r
32 \r
33 #if defined (_MSC_VER)\r
34     #pragma warning( push )\r
35     #pragma warning( disable : 4250 4251 )\r
36 #endif\r
37 \r
38 namespace xmltooling {\r
39 \r
40     /**\r
41      * An interface for classes that implement rules for checking the \r
42      * validity of XMLObjects.\r
43      */\r
44     class XMLTOOL_API Validator\r
45     {\r
46     MAKE_NONCOPYABLE(Validator);\r
47     public:\r
48         virtual ~Validator() {}\r
49         \r
50         /**\r
51          * Checks to see if an XMLObject is valid.\r
52          * \r
53          * @param xmlObject the XMLObject to validate\r
54 \r
55          * @throws ValidationException thrown if the element is not valid\r
56          */\r
57         virtual void validate(const XMLObject* xmlObject) const=0;\r
58 \r
59         /**\r
60          * Evaluates the registered validators against the given XMLObject and it's children.\r
61          * \r
62          * @param xmlObject the XMLObject tree to validate\r
63          * \r
64          * @throws ValidationException thrown if the element tree is not valid\r
65          */\r
66         static void checkValidity(const XMLObject* xmlObject);\r
67 \r
68         /**\r
69          * Registers a new validator for the given key.\r
70          * \r
71          * @param key       the key used to retrieve the validator\r
72          * @param validator the validator\r
73          */\r
74         static void registerValidator(const QName& key, Validator* validator) {\r
75             std::map< QName, std::vector<Validator*> >::iterator i=m_map.find(key);\r
76             if (i==m_map.end())\r
77                 m_map.insert(std::make_pair(key,std::vector<Validator*>(1,validator)));\r
78             else\r
79                 i->second.push_back(validator);\r
80         }\r
81 \r
82         /**\r
83          * Deregisters validators.\r
84          * \r
85          * @param key       the key for the validators to be deregistered\r
86          */\r
87         static void deregisterValidators(const QName& key);\r
88 \r
89         /**\r
90          * Unregisters and destroys all registered validators. \r
91          */\r
92         static void destroyValidators();\r
93 \r
94     protected:\r
95         Validator() {}\r
96     \r
97     private:\r
98         static std::map< QName, std::vector<Validator*> > m_map;\r
99     };\r
100 \r
101 };\r
102 \r
103 #if defined (_MSC_VER)\r
104     #pragma warning( pop )\r
105 #endif\r
106 \r
107 #endif /* __xmltooling_validator_h__ */\r