6962bb81bc8a9500c2693b53e100399cf0682ea7
[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          * Returns a copy of the validator.\r
59          *\r
60          * @return the new validator\r
61          */\r
62         virtual Validator* clone() const=0;\r
63 \r
64         /**\r
65          * Evaluates the registered validators against the given XMLObject and it's children.\r
66          * \r
67          * @param xmlObject the XMLObject tree to validate\r
68          * \r
69          * @throws ValidationException thrown if the element tree is not valid\r
70          */\r
71         static void checkValidity(const XMLObject* xmlObject);\r
72 \r
73         /**\r
74          * Registers a new validator for the given key.\r
75          * \r
76          * @param key       the key used to retrieve the validator\r
77          * @param validator the validator\r
78          */\r
79         static void registerValidator(const QName& key, Validator* validator) {\r
80             std::map< QName, std::vector<Validator*> >::iterator i=m_map.find(key);\r
81             if (i==m_map.end())\r
82                 m_map.insert(std::make_pair(key,std::vector<Validator*>(1,validator)));\r
83             else\r
84                 i->second.push_back(validator);\r
85         }\r
86 \r
87         /**\r
88          * Deregisters validators.\r
89          * \r
90          * @param key       the key for the validators to be deregistered\r
91          */\r
92         static void deregisterValidators(const QName& key);\r
93 \r
94         /**\r
95          * Unregisters and destroys all registered validators. \r
96          */\r
97         static void destroyValidators();\r
98 \r
99     protected:\r
100         Validator() {}\r
101     \r
102     private:\r
103         static std::map< QName, std::vector<Validator*> > m_map;\r
104     };\r
105 \r
106 };\r
107 \r
108 #if defined (_MSC_VER)\r
109     #pragma warning( pop )\r
110 #endif\r
111 \r
112 #endif /* __xmltooling_validator_h__ */\r