Default support for arbitrary DOM objects.
[shibboleth/xmltooling.git] / xmltooling / io / Marshaller.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 Marshaller.h\r
19  * \r
20  * Transforms XMLObjects into DOM trees\r
21  */\r
22 \r
23 #if !defined(__xmltooling_marshaller_h__)\r
24 #define __xmltooling_marshaller_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      * Marshallers are used to marshall an XMLObject into a W3C DOM element.\r
41      */\r
42     class XMLTOOL_API Marshaller\r
43     {\r
44     MAKE_NONCOPYABLE(Marshaller);\r
45     public:\r
46         Marshaller() {}\r
47         virtual ~Marshaller() {}\r
48         \r
49         /**\r
50          * Marshall this element, and its children, into a W3C DOM element rooted in a given document.\r
51          * If no document is provided, then the marshaller will create one, and bind it to the\r
52          * object being marshalled.\r
53          * \r
54          * @param xmlObject the object to marshall\r
55          * @param document the DOM document the marshalled element will be rooted in\r
56          * \r
57          * @return the W3C DOM element representing this XML element\r
58          * \r
59          * @throws MarshallingException thrown if there is a problem marshalling the given object\r
60          */\r
61         virtual DOMElement* marshall(XMLObject* xmlObject, DOMDocument* document=NULL) const=0;\r
62 \r
63         /**\r
64          * Retrieves a Marshaller using the key it was registered with.\r
65          * \r
66          * @param key the key used to register the marshaller\r
67          * @return the marshaller or NULL\r
68          */\r
69         static const Marshaller* getMarshaller(const QName& key) {\r
70             std::map<QName,Marshaller*>::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 a Marshaller for an XML object\r
76          * If no match is found, the default marshaller is returned, if any.\r
77          * \r
78          * @param xmlObject the object for which to return a marshaller\r
79          * @return the marshaller or NULL\r
80          */\r
81         static const Marshaller* getMarshaller(const XMLObject* xmlObject);\r
82 \r
83         /**\r
84          * Retrieves default Marshaller for DOM elements\r
85          * \r
86          * @return the default marshaller or NULL\r
87          */\r
88         static const Marshaller* getDefaultMarshaller() {\r
89             return m_default;\r
90         }\r
91     \r
92         /**\r
93          * Gets an immutable list of all the marshallers currently registered.\r
94          * \r
95          * @return list of all the marshallers currently registered\r
96          */\r
97         static const std::map<QName,Marshaller*>& getMarshallers() {\r
98             return m_map;\r
99         }\r
100     \r
101         /**\r
102          * Registers a new marshaller for the given key.\r
103          * \r
104          * @param key the key used to retrieve this marshaller later\r
105          * @param marshaller the marshaller\r
106          */\r
107         static void registerMarshaller(const QName& key, Marshaller* marshaller) {\r
108             deregisterMarshaller(key);\r
109             m_map[key]=marshaller;\r
110         }\r
111 \r
112         /**\r
113          * Registers default marshaller\r
114          * \r
115          * @param marshaller the default marshaller\r
116          */\r
117         static void registerDefaultMarshaller(Marshaller* marshaller) {\r
118             deregisterDefaultMarshaller();\r
119             m_default=marshaller;\r
120         }\r
121 \r
122         /**\r
123          * Deregisters a marshaller.\r
124          * \r
125          * @param key the key for the marshaller to be deregistered\r
126          */\r
127         static void deregisterMarshaller(const QName& key) {\r
128             delete getMarshaller(key);\r
129             m_map.erase(key);\r
130         }\r
131 \r
132         /**\r
133          * Deregisters default marshaller.\r
134          */\r
135         static void deregisterDefaultMarshaller() {\r
136             delete m_default;\r
137             m_default=NULL;\r
138         }\r
139 \r
140         /**\r
141          * Unregisters and destroys all registered marshallers. \r
142          */\r
143         static void destroyMarshallers();\r
144     \r
145     private:\r
146         static std::map<QName,Marshaller*> m_map;\r
147         static Marshaller* 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_marshaller_h__ */\r