Manage disposal of new objects when exceptions are thrown.
[shibboleth/cpp-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          * Marshalls an object, and its children, into a DOM element.\r
51          * If a document is supplied, then it will be used to create the resulting elements.\r
52          * If the document does not have a Document Element set, then the resulting\r
53          * element will be set as the Document Element. If no document is supplied, then\r
54          * a new document will be created and bound to the lifetime of the root object being\r
55          * marshalled, unless an existing DOM can be reused without creating a new document. \r
56          * \r
57          * @param xmlObject the object to marshall\r
58          * @param document the DOM document the marshalled element will be placed in, or NULL\r
59          * @return the DOM element representing this XMLObject\r
60          * @throws MarshallingException thrown if there is a problem marshalling the given object\r
61          */\r
62         virtual DOMElement* marshall(XMLObject* xmlObject, DOMDocument* document=NULL) const=0;\r
63         \r
64         /**\r
65          * Marshall the given XMLObject and append it as a child of the given parent element.\r
66          * \r
67          * <strong>NOTE:</strong> The given Element must be within a DOM tree rooted in \r
68          * the Document owning the given Element.\r
69          * \r
70          * @param xmlObject the XMLObject to be marshalled\r
71          * @param parentElement the parent element to append the resulting DOM tree\r
72          * @return the marshalled element tree\r
73          * @throws MarshallingException thrown if the given XMLObject can not be marshalled.\r
74          */\r
75         virtual DOMElement* marshall(XMLObject* xmlObject, DOMElement* parentElement) const=0;\r
76 \r
77         /**\r
78          * Retrieves a Marshaller using the key it was registered with.\r
79          * \r
80          * @param key the key used to register the marshaller\r
81          * @return the marshaller or NULL\r
82          */\r
83         static const Marshaller* getMarshaller(const QName& key) {\r
84             std::map<QName,Marshaller*>::const_iterator i=m_map.find(key);\r
85             return (i==m_map.end()) ? NULL : i->second;\r
86         }\r
87 \r
88         /**\r
89          * Retrieves a Marshaller for an XML object\r
90          * If no match is found, the default marshaller is returned, if any.\r
91          * \r
92          * @param xmlObject the object for which to return a marshaller\r
93          * @return the marshaller or NULL\r
94          */\r
95         static const Marshaller* getMarshaller(const XMLObject* xmlObject);\r
96 \r
97         /**\r
98          * Retrieves default Marshaller for DOM elements\r
99          * \r
100          * @return the default marshaller or NULL\r
101          */\r
102         static const Marshaller* getDefaultMarshaller() {\r
103             return m_default;\r
104         }\r
105     \r
106         /**\r
107          * Gets an immutable list of all the marshallers currently registered.\r
108          * \r
109          * @return list of all the marshallers currently registered\r
110          */\r
111         static const std::map<QName,Marshaller*>& getMarshallers() {\r
112             return m_map;\r
113         }\r
114     \r
115         /**\r
116          * Registers a new marshaller for the given key.\r
117          * \r
118          * @param key the key used to retrieve this marshaller later\r
119          * @param marshaller the marshaller\r
120          */\r
121         static void registerMarshaller(const QName& key, Marshaller* marshaller) {\r
122             deregisterMarshaller(key);\r
123             m_map[key]=marshaller;\r
124         }\r
125 \r
126         /**\r
127          * Registers default marshaller\r
128          * \r
129          * @param marshaller the default marshaller\r
130          */\r
131         static void registerDefaultMarshaller(Marshaller* marshaller) {\r
132             deregisterDefaultMarshaller();\r
133             m_default=marshaller;\r
134         }\r
135 \r
136         /**\r
137          * Deregisters a marshaller.\r
138          * \r
139          * @param key the key for the marshaller to be deregistered\r
140          */\r
141         static void deregisterMarshaller(const QName& key) {\r
142             delete getMarshaller(key);\r
143             m_map.erase(key);\r
144         }\r
145 \r
146         /**\r
147          * Deregisters default marshaller.\r
148          */\r
149         static void deregisterDefaultMarshaller() {\r
150             delete m_default;\r
151             m_default=NULL;\r
152         }\r
153 \r
154         /**\r
155          * Unregisters and destroys all registered marshallers. \r
156          */\r
157         static void destroyMarshallers();\r
158     \r
159     private:\r
160         static std::map<QName,Marshaller*> m_map;\r
161         static Marshaller* m_default;\r
162     };\r
163     \r
164 };\r
165 \r
166 #if defined (_MSC_VER)\r
167     #pragma warning( pop )\r
168 #endif\r
169 \r
170 #endif /* __xmltooling_marshaller_h__ */\r