18d90569b0a0bcf85be0900d1e88b2efb50abdc1
[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/AbstractDOMCachingXMLObject.h>\r
27 \r
28 #if defined (_MSC_VER)\r
29     #pragma warning( push )\r
30     #pragma warning( disable : 4250 4251 )\r
31 #endif\r
32 \r
33 namespace xmltooling {\r
34 \r
35     /**\r
36      * A thread-safe abstract marshaller.\r
37      */\r
38     class XMLTOOL_API AbstractXMLObjectMarshaller : public virtual AbstractXMLObject\r
39     {\r
40     public:\r
41         virtual ~AbstractXMLObjectMarshaller() {}\r
42 \r
43         DOMElement* marshall(\r
44             DOMDocument* document=NULL\r
45 #ifndef XMLTOOLING_NO_XMLSEC\r
46             ,const std::vector<xmlsignature::Signature*>* sigs=NULL\r
47 #endif\r
48             ) const;\r
49 \r
50         DOMElement* marshall(\r
51             DOMElement* parentElement\r
52 #ifndef XMLTOOLING_NO_XMLSEC\r
53             ,const std::vector<xmlsignature::Signature*>* sigs=NULL\r
54 #endif\r
55             ) const;\r
56         \r
57     protected:\r
58         AbstractXMLObjectMarshaller() {}\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(element, documentRoot);\r
71             else\r
72                 document->appendChild(element);\r
73         }\r
74     \r
75         /**\r
76          * Marshalls the 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 targetElement the Element into which the XMLObject is marshalled into\r
80          * @param ctx           optional marshalling context\r
81          * \r
82          * @throws MarshallingException thrown if there is a problem marshalling the object\r
83          * @throws SignatureException thrown if a problem occurs during signature creation \r
84          */\r
85         void marshallInto(\r
86             DOMElement* targetElement\r
87 #ifndef XMLTOOLING_NO_XMLSEC\r
88             ,const std::vector<xmlsignature::Signature*>* sigs\r
89 #endif\r
90             ) const;\r
91     \r
92         /**\r
93          * Creates an xsi:type attribute, corresponding to the given type of the XMLObject, on the DOM element.\r
94          * \r
95          * @param domElement the DOM element\r
96          * \r
97          * @throws MarshallingException thrown if the type on the XMLObject is doesn't contain\r
98          * a local name, prefix, and namespace URI\r
99          */\r
100         void marshallElementType(DOMElement* domElement) const;\r
101 \r
102         /**\r
103          * Creates the xmlns attributes for any namespaces set on the XMLObject.\r
104          * \r
105          * @param domElement the DOM element the namespaces will be added to\r
106          */\r
107         void marshallNamespaces(DOMElement* domElement) const;\r
108     \r
109         /**\r
110          * Marshalls the child elements of the XMLObject.\r
111          * \r
112          * @param domElement the DOM element that will recieved the marshalled children\r
113          * \r
114          * @throws MarshallingException thrown if there is a problem marshalling a child element\r
115          */\r
116         void marshallChildElements(DOMElement* domElement) const;\r
117 \r
118         /**\r
119          * Marshalls the attributes from the XMLObject into the given DOM element.\r
120          * \r
121          * @param domElement the DOM Element into which attributes will be marshalled\r
122          * \r
123          * @throws MarshallingException thrown if there is a problem marshalling an attribute\r
124          */\r
125         virtual void marshallAttributes(DOMElement* domElement) const {}\r
126 \r
127         /**\r
128          * Marshalls data from the XMLObject into content of the DOM Element.\r
129          * \r
130          * @param domElement the DOM element recieving the content\r
131          */\r
132         virtual void marshallElementContent(DOMElement* domElement) const {}\r
133     };\r
134     \r
135 };\r
136 \r
137 #if defined (_MSC_VER)\r
138     #pragma warning( pop )\r
139 #endif\r
140 \r
141 #endif /* __xmltooling_xmlmarshaller_h__ */\r