e93a4b33a5d3e7385c08a00df18c4d4507c6c4e0
[shibboleth/cpp-xmltooling.git] / xmltooling / io / AbstractXMLObjectMarshaller.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 AbstractXMLObjectMarshaller.h\r
19  * \r
20  * A thread-safe abstract marshaller.\r
21  */\r
22 \r
23 #if !defined(__xmltooling_xmlmarshaller_h__)\r
24 #define __xmltooling_xmlmarshaller_h__\r
25 \r
26 #include <xmltooling/io/Marshaller.h>\r
27 \r
28 namespace xmltooling {\r
29 \r
30     /**\r
31      * A thread-safe abstract marshaller.\r
32      */\r
33     class XMLTOOL_API AbstractXMLObjectMarshaller : public virtual Marshaller\r
34     {\r
35     public:\r
36         virtual ~AbstractXMLObjectMarshaller() {}\r
37 \r
38         /**\r
39          * @see Marshaller::marshall(XMLObject*,DOMDocument*)\r
40          */\r
41         DOMElement* marshall(XMLObject* xmlObject, DOMDocument* document=NULL) const;\r
42 \r
43         /**\r
44          * @see Marshaller::marshall(XMLObject*,DOMElement*)\r
45          */\r
46         DOMElement* marshall(XMLObject* xmlObject, DOMElement* parentElement) const;\r
47     \r
48         \r
49     protected:\r
50         /**\r
51          * Constructor.\r
52          * \r
53          * @param targetNamespaceURI the namespace URI of either the schema type QName or element QName of the elements this\r
54          *            marshaller operates on\r
55          * @param targetLocalName the local name of either the schema type QName or element QName of the elements this\r
56          *            marshaller operates on\r
57          */\r
58         AbstractXMLObjectMarshaller(const XMLCh* targetNamespaceURI, const XMLCh* targetLocalName);\r
59 \r
60         /**\r
61          * Sets the given element as the Document Element of the given Document.\r
62          * If the document already has a Document Element it is replaced by the given element.\r
63          * \r
64          * @param document the document\r
65          * @param element the Element that will serve as the Document Element\r
66          */\r
67         void setDocumentElement(DOMDocument* document, DOMElement* element) const {\r
68             DOMElement* documentRoot = document->getDocumentElement();\r
69             if (documentRoot)\r
70                 document->replaceChild(documentRoot, element);\r
71             else\r
72                 document->appendChild(element);\r
73         }\r
74     \r
75         /**\r
76          * Marshalls the given XMLObject into the given DOM Element.\r
77          * The DOM Element must be within a DOM tree rooted in the owning Document.\r
78          * \r
79          * @param xmlObject the XMLObject to marshall\r
80          * @param targetElement the Element into which the XMLObject is marshalled into\r
81          * @throws MarshallingException thrown if there is a problem marshalling the object\r
82          */\r
83         void marshallInto(XMLObject* xmlObject, DOMElement* targetElement) const;\r
84     \r
85         /**\r
86          * Creates an xsi:type attribute, corresponding to the given type of the XMLObject, on the DOM element.\r
87          * \r
88          * @param xmlObject the XMLObject\r
89          * @param domElement the DOM element\r
90          * \r
91          * @throws MarshallingException thrown if the type on the XMLObject is doesn't contain\r
92          * a local name, prefix, and namespace URI\r
93          */\r
94         void marshallElementType(XMLObject* xmlObject, DOMElement* domElement) const;\r
95 \r
96         /**\r
97          * Creates the xmlns attributes for any namespaces set on the given XMLObject.\r
98          * \r
99          * @param xmlObject the XMLObject\r
100          * @param domElement the DOM element the namespaces will be added to\r
101          */\r
102         void marshallNamespaces(const XMLObject* xmlObject, DOMElement* domElement) const;\r
103     \r
104         /**\r
105          * Marshalls the child elements of the given XMLObject.\r
106          * \r
107          * @param xmlObject the XMLObject whose children will be marshalled\r
108          * @param domElement the DOM element that will recieved the marshalled children\r
109          * \r
110          * @throws MarshallingException thrown if there is a problem marshalling a child element\r
111          */\r
112         void marshallChildElements(const XMLObject* xmlObject, DOMElement* domElement) const;\r
113 \r
114         /**\r
115          * Marshalls the attributes from the given XMLObject into the given DOM element.\r
116          * The XMLObject passed to this method is guaranteed to be of the target name\r
117          * specified during this marshaller's construction.\r
118          * \r
119          * @param xmlObject the XMLObject being marshalled\r
120          * @param domElement the DOM Element into which attributes will be marshalled\r
121          * \r
122          * @throws UnmarshallingException thrown if there is a problem unmarshalling an attribute\r
123          */\r
124         virtual void marshallAttributes(const XMLObject* xmlObject, DOMElement* domElement) const=0;\r
125 \r
126         /**\r
127          * Marshalls data from the XMLObject into content of the DOM Element.\r
128          * \r
129          * @param xmlObject the XMLObject\r
130          * @param domElement the DOM element recieving the content\r
131          */\r
132         virtual void marshallElementContent(const XMLObject* xmlObject, DOMElement* domElement) const=0;\r
133 \r
134         void* m_log;\r
135     private:\r
136         QName m_targetQName;\r
137     };\r
138     \r
139 };\r
140 \r
141 #endif /* __xmltooling_xmlmarshaller_h__ */\r