2 * Copyright 2001-2006 Internet2
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
18 * @file Marshaller.h
\r
20 * Transforms XMLObjects into DOM trees
\r
23 #if !defined(__xmltooling_marshaller_h__)
\r
24 #define __xmltooling_marshaller_h__
\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
34 using namespace xercesc;
\r
36 #if defined (_MSC_VER)
\r
37 #pragma warning( push )
\r
38 #pragma warning( disable : 4250 4251 )
\r
41 namespace xmltooling {
\r
44 * Supplies additional information to the marshalling process.
\r
45 * Currently this only consists of signature related information.
\r
47 class XMLTOOL_API MarshallingContext
\r
49 MAKE_NONCOPYABLE(MarshallingContext);
\r
51 MarshallingContext() {}
\r
52 ~MarshallingContext() {}
\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
59 /** Array of signing contexts, keyed off of the associated Signature */
\r
60 std::vector< std::pair<Signature*,const SigningContext*> > m_signingContexts;
\r
65 * Marshallers are used to marshall an XMLObject into a W3C DOM element.
\r
67 class XMLTOOL_API Marshaller
\r
69 MAKE_NONCOPYABLE(Marshaller);
\r
72 virtual ~Marshaller() {}
\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
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
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
90 virtual DOMElement* marshall(XMLObject* xmlObject, DOMDocument* document=NULL, MarshallingContext* ctx=NULL) const=0;
\r
93 * Marshall the given XMLObject and append it as a child of the given parent element.
\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
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
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
106 virtual DOMElement* marshall(XMLObject* xmlObject, DOMElement* parentElement, MarshallingContext* ctx=NULL) const=0;
\r
109 * Retrieves a Marshaller using the key it was registered with.
\r
111 * @param key the key used to register the marshaller
\r
112 * @return the marshaller or NULL
\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
120 * Retrieves a Marshaller for an XML object
\r
121 * If no match is found, the default marshaller is returned, if any.
\r
123 * @param xmlObject the object for which to return a marshaller
\r
124 * @return the marshaller or NULL
\r
126 static const Marshaller* getMarshaller(const XMLObject* xmlObject);
\r
129 * Retrieves default Marshaller for DOM elements
\r
131 * @return the default marshaller or NULL
\r
133 static const Marshaller* getDefaultMarshaller() {
\r
138 * Gets an immutable list of all the marshallers currently registered.
\r
140 * @return list of all the marshallers currently registered
\r
142 static const std::map<QName,Marshaller*>& getMarshallers() {
\r
147 * Registers a new marshaller for the given key.
\r
149 * @param key the key used to retrieve this marshaller later
\r
150 * @param marshaller the marshaller
\r
152 static void registerMarshaller(const QName& key, Marshaller* marshaller) {
\r
153 deregisterMarshaller(key);
\r
154 m_map[key]=marshaller;
\r
158 * Registers default marshaller
\r
160 * @param marshaller the default marshaller
\r
162 static void registerDefaultMarshaller(Marshaller* marshaller) {
\r
163 deregisterDefaultMarshaller();
\r
164 m_default=marshaller;
\r
168 * Deregisters a marshaller.
\r
170 * @param key the key for the marshaller to be deregistered
\r
172 static void deregisterMarshaller(const QName& key) {
\r
173 delete getMarshaller(key);
\r
178 * Deregisters default marshaller.
\r
180 static void deregisterDefaultMarshaller() {
\r
186 * Unregisters and destroys all registered marshallers.
\r
188 static void destroyMarshallers();
\r
191 static std::map<QName,Marshaller*> m_map;
\r
192 static Marshaller* m_default;
\r
197 #if defined (_MSC_VER)
\r
198 #pragma warning( pop )
\r
201 #endif /* __xmltooling_marshaller_h__ */
\r