e65f49f1340ca84e1e940aa046bb53754d7fea6c
[shibboleth/cpp-xmltooling.git] / xmltooling / io / Unmarshaller.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 Unmarshaller.h\r
19  * \r
20  * Transforms DOM trees into XMLObjects\r
21  */\r
22 \r
23 #if !defined(__xmltooling_unmarshaller_h__)\r
24 #define __xmltooling_unmarshaller_h__\r
25 \r
26 #include <map>\r
27 #include <xercesc/dom/DOM.hpp>\r
28 #include <xmltooling/XMLObject.h>\r
29 \r
30 using namespace xercesc;\r
31 \r
32 #if defined (_MSC_VER)\r
33     #pragma warning( push )\r
34     #pragma warning( disable : 4250 4251 )\r
35 #endif\r
36 \r
37 namespace xmltooling {\r
38 \r
39     /**\r
40      * Unmarshallers are used to unmarshall a W3C DOM element into an XMLObject.\r
41      */\r
42     class XMLTOOL_API Unmarshaller\r
43     {\r
44     MAKE_NONCOPYABLE(Unmarshaller);\r
45     public:\r
46         Unmarshaller() {}\r
47         virtual ~Unmarshaller() {}\r
48         \r
49         /**\r
50          * Unmarshalls the given W3C DOM element into an XMLObject.\r
51          * The root of a given XML construct should be unmarshalled with the bindDocument parameter\r
52          * set to true.\r
53          * \r
54          * @param element       the DOM element to unmarshall\r
55          * @param bindDocument  true iff the resulting XMLObject should take ownership of the DOM's Document \r
56          * \r
57          * @return the unmarshalled XMLObject\r
58          * \r
59          * @throws UnmarshallingException thrown if an error occurs unmarshalling the DOM element into the XMLObject\r
60          */\r
61         virtual XMLObject* unmarshall(DOMElement* element, bool bindDocument=false) const=0;\r
62 \r
63         /**\r
64          * Retrieves an unmarshaller using the key it was registered with.\r
65          * \r
66          * @param key the key used to register the unmarshaller\r
67          * @return the unmarshaller\r
68          */\r
69         static const Unmarshaller* getUnmarshaller(const QName& key) {\r
70             std::map<QName,Unmarshaller*>::const_iterator i=m_map.find(key);\r
71             return (i==m_map.end()) ? NULL : i->second;\r
72         }\r
73     \r
74         /**\r
75          * Retrieves an Unmarshaller for a DOM element\r
76          * \r
77          * @param element the element for which to return an unmarshaller\r
78          * @return the unmarshaller or NULL\r
79          */\r
80         static const Unmarshaller* getUnmarshaller(const DOMElement* key);\r
81 \r
82         /**\r
83          * Retrieves the default Unmarshaller for an unknown DOM element\r
84          * \r
85          * @return the default unmarshaller or NULL\r
86          */\r
87         static const Unmarshaller* getDefaultUnmarshaller() {\r
88             return m_default;\r
89         }\r
90 \r
91         /**\r
92          * Gets an immutable list of all the unmarshallers currently registered.\r
93          * \r
94          * @return list of all the unmarshallers currently registered\r
95          */\r
96         static const std::map<QName,Unmarshaller*>& getUnmarshallers() {\r
97             return m_map;\r
98         }\r
99     \r
100         /**\r
101          * Registers a new unmarshaller for the given key.\r
102          * \r
103          * @param key the key used to retrieve this unmarshaller later\r
104          * @param unmarshaller the unmarshaller\r
105          */\r
106         static void registerUnmarshaller(const QName& key, Unmarshaller* unmarshaller) {\r
107             deregisterUnmarshaller(key);\r
108             m_map[key]=unmarshaller;\r
109         }\r
110 \r
111         /**\r
112          * Registers a new default unmarshaller\r
113          * \r
114          * @param unmarshaller the default unmarshaller\r
115          */\r
116         static void registerDefaultUnmarshaller(Unmarshaller* unmarshaller) {\r
117             deregisterDefaultUnmarshaller();\r
118             m_default=unmarshaller;\r
119         }\r
120     \r
121         /**\r
122          * Deregisters a unmarshaller.\r
123          * \r
124          * @param key the key for the unmarshaller to be deregistered\r
125          */\r
126         static void deregisterUnmarshaller(const QName& key) {\r
127             delete getUnmarshaller(key);\r
128             m_map.erase(key);\r
129         }\r
130 \r
131         /**\r
132          * Deregisters the default unmarshaller.\r
133          */\r
134         static void deregisterDefaultUnmarshaller() {\r
135             delete m_default;\r
136             m_default=NULL;\r
137         }\r
138         \r
139         /**\r
140          * Unregisters and destroys all registered unmarshallers. \r
141          */\r
142         static void destroyUnmarshallers();\r
143     \r
144     private:\r
145         static std::map<QName,Unmarshaller*> m_map;\r
146         static Unmarshaller* m_default;\r
147     };\r
148     \r
149 };\r
150 \r
151 #if defined (_MSC_VER)\r
152     #pragma warning( pop )\r
153 #endif\r
154 \r
155 #endif /* __xmltooling_unmarshaller_h__ */\r