First cut at signing support.
[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 <vector>\r
28 #include <xercesc/dom/DOM.hpp>\r
29 #include <xmltooling/XMLObject.h>\r
30 #ifndef XMLTOOLING_NO_XMLSEC\r
31     #include <xmltooling/signature/Signature.h>\r
32 #endif\r
33 \r
34 using namespace xercesc;\r
35 \r
36 #if defined (_MSC_VER)\r
37     #pragma warning( push )\r
38     #pragma warning( disable : 4250 4251 )\r
39 #endif\r
40 \r
41 namespace xmltooling {\r
42 \r
43     /**\r
44      * Supplies additional information to the marshalling process.\r
45      * Currently this only consists of signature related information.\r
46      */\r
47     class XMLTOOL_API MarshallingContext\r
48     {\r
49         MAKE_NONCOPYABLE(MarshallingContext);\r
50     public:\r
51         MarshallingContext() {}\r
52         ~MarshallingContext() {}\r
53 \r
54 #ifndef XMLTOOLING_NO_XMLSEC\r
55         MarshallingContext(Signature* sig, const SigningContext* ctx) {\r
56             m_signingContexts.push_back(std::make_pair(sig,ctx));\r
57         }\r
58         \r
59         /** Array of signing contexts, keyed off of the associated Signature */\r
60         std::vector< std::pair<Signature*,const SigningContext*> > m_signingContexts;\r
61 #endif\r
62     };\r
63 \r
64     /**\r
65      * Marshallers are used to marshall an XMLObject into a W3C DOM element.\r
66      */\r
67     class XMLTOOL_API Marshaller\r
68     {\r
69     MAKE_NONCOPYABLE(Marshaller);\r
70     public:\r
71         Marshaller() {}\r
72         virtual ~Marshaller() {}\r
73         \r
74         /**\r
75          * Marshalls an object, and its children, into a DOM element.\r
76          * If a document is supplied, then it will be used to create the resulting elements.\r
77          * If the document does not have a Document Element set, then the resulting\r
78          * element will be set as the Document Element. If no document is supplied, then\r
79          * a new document will be created and bound to the lifetime of the root object being\r
80          * marshalled, unless an existing DOM can be reused without creating a new document. \r
81          * \r
82          * @param xmlObject the object to marshall\r
83          * @param document  the DOM document the marshalled element will be placed in, or NULL\r
84          * @param ctx       optional marshalling context\r
85          * @return the DOM element representing this XMLObject\r
86          * \r
87          * @throws MarshallingException thrown if there is a problem marshalling the given object\r
88          * @throws SignatureException thrown if a problem occurs during signature creation \r
89          */\r
90         virtual DOMElement* marshall(XMLObject* xmlObject, DOMDocument* document=NULL, MarshallingContext* ctx=NULL) const=0;\r
91         \r
92         /**\r
93          * Marshall the given XMLObject and append it as a child of the given parent element.\r
94          * \r
95          * <strong>NOTE:</strong> The given Element must be within a DOM tree rooted in \r
96          * the Document owning the given Element.\r
97          * \r
98          * @param xmlObject the XMLObject to be marshalled\r
99          * @param parentElement the parent element to append the resulting DOM tree\r
100          * @param ctx       optional marshalling context\r
101          * @return the marshalled element tree\r
102 \r
103          * @throws MarshallingException thrown if the given XMLObject can not be marshalled.\r
104          * @throws SignatureException thrown if a problem occurs during signature creation \r
105          */\r
106         virtual DOMElement* marshall(XMLObject* xmlObject, DOMElement* parentElement, MarshallingContext* ctx=NULL) const=0;\r
107 \r
108         /**\r
109          * Retrieves a Marshaller using the key it was registered with.\r
110          * \r
111          * @param key the key used to register the marshaller\r
112          * @return the marshaller or NULL\r
113          */\r
114         static const Marshaller* getMarshaller(const QName& key) {\r
115             std::map<QName,Marshaller*>::const_iterator i=m_map.find(key);\r
116             return (i==m_map.end()) ? NULL : i->second;\r
117         }\r
118 \r
119         /**\r
120          * Retrieves a Marshaller for an XML object\r
121          * If no match is found, the default marshaller is returned, if any.\r
122          * \r
123          * @param xmlObject the object for which to return a marshaller\r
124          * @return the marshaller or NULL\r
125          */\r
126         static const Marshaller* getMarshaller(const XMLObject* xmlObject);\r
127 \r
128         /**\r
129          * Retrieves default Marshaller for DOM elements\r
130          * \r
131          * @return the default marshaller or NULL\r
132          */\r
133         static const Marshaller* getDefaultMarshaller() {\r
134             return m_default;\r
135         }\r
136     \r
137         /**\r
138          * Gets an immutable list of all the marshallers currently registered.\r
139          * \r
140          * @return list of all the marshallers currently registered\r
141          */\r
142         static const std::map<QName,Marshaller*>& getMarshallers() {\r
143             return m_map;\r
144         }\r
145     \r
146         /**\r
147          * Registers a new marshaller for the given key.\r
148          * \r
149          * @param key the key used to retrieve this marshaller later\r
150          * @param marshaller the marshaller\r
151          */\r
152         static void registerMarshaller(const QName& key, Marshaller* marshaller) {\r
153             deregisterMarshaller(key);\r
154             m_map[key]=marshaller;\r
155         }\r
156 \r
157         /**\r
158          * Registers default marshaller\r
159          * \r
160          * @param marshaller the default marshaller\r
161          */\r
162         static void registerDefaultMarshaller(Marshaller* marshaller) {\r
163             deregisterDefaultMarshaller();\r
164             m_default=marshaller;\r
165         }\r
166 \r
167         /**\r
168          * Deregisters a marshaller.\r
169          * \r
170          * @param key the key for the marshaller to be deregistered\r
171          */\r
172         static void deregisterMarshaller(const QName& key) {\r
173             delete getMarshaller(key);\r
174             m_map.erase(key);\r
175         }\r
176 \r
177         /**\r
178          * Deregisters default marshaller.\r
179          */\r
180         static void deregisterDefaultMarshaller() {\r
181             delete m_default;\r
182             m_default=NULL;\r
183         }\r
184 \r
185         /**\r
186          * Unregisters and destroys all registered marshallers. \r
187          */\r
188         static void destroyMarshallers();\r
189     \r
190     private:\r
191         static std::map<QName,Marshaller*> m_map;\r
192         static Marshaller* m_default;\r
193     };\r
194     \r
195 };\r
196 \r
197 #if defined (_MSC_VER)\r
198     #pragma warning( pop )\r
199 #endif\r
200 \r
201 #endif /* __xmltooling_marshaller_h__ */\r