Default support for arbitrary DOM objects.
[shibboleth/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          * If no match is found, the default unmarshaller is returned, if any.\r
77          * \r
78          * @param element the element for which to return an unmarshaller\r
79          * @return the unmarshaller or NULL\r
80          */\r
81         static const Unmarshaller* getUnmarshaller(const DOMElement* key);\r
82 \r
83         /**\r
84          * Retrieves the default Unmarshaller for an unknown DOM element\r
85          * \r
86          * @return the default unmarshaller or NULL\r
87          */\r
88         static const Unmarshaller* getDefaultUnmarshaller() {\r
89             return m_default;\r
90         }\r
91 \r
92         /**\r
93          * Gets an immutable list of all the unmarshallers currently registered.\r
94          * \r
95          * @return list of all the unmarshallers currently registered\r
96          */\r
97         static const std::map<QName,Unmarshaller*>& getUnmarshallers() {\r
98             return m_map;\r
99         }\r
100     \r
101         /**\r
102          * Registers a new unmarshaller for the given key.\r
103          * \r
104          * @param key the key used to retrieve this unmarshaller later\r
105          * @param unmarshaller the unmarshaller\r
106          */\r
107         static void registerUnmarshaller(const QName& key, Unmarshaller* unmarshaller) {\r
108             deregisterUnmarshaller(key);\r
109             m_map[key]=unmarshaller;\r
110         }\r
111 \r
112         /**\r
113          * Registers a new default unmarshaller\r
114          * \r
115          * @param unmarshaller the default unmarshaller\r
116          */\r
117         static void registerDefaultUnmarshaller(Unmarshaller* unmarshaller) {\r
118             deregisterDefaultUnmarshaller();\r
119             m_default=unmarshaller;\r
120         }\r
121     \r
122         /**\r
123          * Deregisters a unmarshaller.\r
124          * \r
125          * @param key the key for the unmarshaller to be deregistered\r
126          */\r
127         static void deregisterUnmarshaller(const QName& key) {\r
128             delete getUnmarshaller(key);\r
129             m_map.erase(key);\r
130         }\r
131 \r
132         /**\r
133          * Deregisters the default unmarshaller.\r
134          */\r
135         static void deregisterDefaultUnmarshaller() {\r
136             delete m_default;\r
137             m_default=NULL;\r
138         }\r
139         \r
140         /**\r
141          * Unregisters and destroys all registered unmarshallers. \r
142          */\r
143         static void destroyUnmarshallers();\r
144     \r
145     private:\r
146         static std::map<QName,Unmarshaller*> m_map;\r
147         static Unmarshaller* m_default;\r
148     };\r
149     \r
150 };\r
151 \r
152 #if defined (_MSC_VER)\r
153     #pragma warning( pop )\r
154 #endif\r
155 \r
156 #endif /* __xmltooling_unmarshaller_h__ */\r