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
27 #include <xercesc/dom/DOM.hpp>
\r
28 #include <xmltooling/XMLObject.h>
\r
30 using namespace xercesc;
\r
32 #if defined (_MSC_VER)
\r
33 #pragma warning( push )
\r
34 #pragma warning( disable : 4250 4251 )
\r
37 namespace xmltooling {
\r
40 * Marshallers are used to marshall an XMLObject into a W3C DOM element.
\r
42 class XMLTOOL_API Marshaller
\r
44 MAKE_NONCOPYABLE(Marshaller);
\r
47 virtual ~Marshaller() {}
\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
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
62 virtual DOMElement* marshall(XMLObject* xmlObject, DOMDocument* document=NULL) const=0;
\r
65 * Marshall the given XMLObject and append it as a child of the given parent element.
\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
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
75 virtual DOMElement* marshall(XMLObject* xmlObject, DOMElement* parentElement) const=0;
\r
78 * Retrieves a Marshaller using the key it was registered with.
\r
80 * @param key the key used to register the marshaller
\r
81 * @return the marshaller or NULL
\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
89 * Retrieves a Marshaller for an XML object
\r
90 * If no match is found, the default marshaller is returned, if any.
\r
92 * @param xmlObject the object for which to return a marshaller
\r
93 * @return the marshaller or NULL
\r
95 static const Marshaller* getMarshaller(const XMLObject* xmlObject);
\r
98 * Retrieves default Marshaller for DOM elements
\r
100 * @return the default marshaller or NULL
\r
102 static const Marshaller* getDefaultMarshaller() {
\r
107 * Gets an immutable list of all the marshallers currently registered.
\r
109 * @return list of all the marshallers currently registered
\r
111 static const std::map<QName,Marshaller*>& getMarshallers() {
\r
116 * Registers a new marshaller for the given key.
\r
118 * @param key the key used to retrieve this marshaller later
\r
119 * @param marshaller the marshaller
\r
121 static void registerMarshaller(const QName& key, Marshaller* marshaller) {
\r
122 deregisterMarshaller(key);
\r
123 m_map[key]=marshaller;
\r
127 * Registers default marshaller
\r
129 * @param marshaller the default marshaller
\r
131 static void registerDefaultMarshaller(Marshaller* marshaller) {
\r
132 deregisterDefaultMarshaller();
\r
133 m_default=marshaller;
\r
137 * Deregisters a marshaller.
\r
139 * @param key the key for the marshaller to be deregistered
\r
141 static void deregisterMarshaller(const QName& key) {
\r
142 delete getMarshaller(key);
\r
147 * Deregisters default marshaller.
\r
149 static void deregisterDefaultMarshaller() {
\r
155 * Unregisters and destroys all registered marshallers.
\r
157 static void destroyMarshallers();
\r
160 static std::map<QName,Marshaller*> m_map;
\r
161 static Marshaller* m_default;
\r
166 #if defined (_MSC_VER)
\r
167 #pragma warning( pop )
\r
170 #endif /* __xmltooling_marshaller_h__ */
\r