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