Update copyright.
[shibboleth/cpp-xmltooling.git] / xmltooling / io / AbstractXMLObjectMarshaller.h
1 /*
2 *  Copyright 2001-2007 Internet2
3  * 
4 * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file AbstractXMLObjectMarshaller.h
19  * 
20  * A mix-in to implement object marshalling with DOM reuse.
21  */
22
23 #if !defined(__xmltooling_xmlmarshaller_h__)
24 #define __xmltooling_xmlmarshaller_h__
25
26 #include <xmltooling/AbstractDOMCachingXMLObject.h>
27
28 #if defined (_MSC_VER)
29     #pragma warning( push )
30     #pragma warning( disable : 4250 4251 )
31 #endif
32
33 namespace xmltooling {
34
35     /**
36      * A mix-in to implement object marshalling with DOM reuse.
37      */
38     class XMLTOOL_API AbstractXMLObjectMarshaller : public virtual AbstractXMLObject
39     {
40     public:
41         virtual ~AbstractXMLObjectMarshaller() {}
42
43         DOMElement* marshall(
44             DOMDocument* document=NULL
45 #ifndef XMLTOOLING_NO_XMLSEC
46             ,const std::vector<xmlsignature::Signature*>* sigs=NULL
47 #endif
48             ) const;
49
50         DOMElement* marshall(
51             DOMElement* parentElement
52 #ifndef XMLTOOLING_NO_XMLSEC
53             ,const std::vector<xmlsignature::Signature*>* sigs=NULL
54 #endif
55             ) const;
56         
57     protected:
58         AbstractXMLObjectMarshaller() {}
59
60         /**
61          * Sets the given element as the Document Element of the given Document.
62          * If the document already has a Document Element it is replaced by the given element.
63          * 
64          * @param document the document
65          * @param element the Element that will serve as the Document Element
66          */
67         void setDocumentElement(DOMDocument* document, DOMElement* element) const {
68             DOMElement* documentRoot = document->getDocumentElement();
69             if (documentRoot)
70                 document->replaceChild(element, documentRoot);
71             else
72                 document->appendChild(element);
73         }
74     
75 #ifndef XMLTOOLING_NO_XMLSEC
76         /**
77          * Marshalls the XMLObject into the given DOM Element.
78          * The DOM Element must be within a DOM tree rooted in the owning Document.
79          * 
80          * @param targetElement the Element into which the XMLObject is marshalled into
81          * @param sigs          optional array of signatures to create after marshalling          
82          * 
83          * @throws MarshallingException thrown if there is a problem marshalling the object
84          * @throws SignatureException thrown if a problem occurs during signature creation 
85          */
86         void marshallInto(DOMElement* targetElement, const std::vector<xmlsignature::Signature*>* sigs) const;
87 #else
88         /**
89          * Marshalls the XMLObject into the given DOM Element.
90          * The DOM Element must be within a DOM tree rooted in the owning Document.
91          * 
92          * @param targetElement the Element into which the XMLObject is marshalled into
93          * 
94          * @throws MarshallingException thrown if there is a problem marshalling the object
95          */
96         void marshallInto(DOMElement* targetElement) const;
97 #endif
98     
99         /**
100          * Creates an xsi:type attribute, corresponding to the given type of the XMLObject, on the DOM element.
101          * 
102          * @param domElement the DOM element
103          * 
104          * @throws MarshallingException thrown if the type on the XMLObject is doesn't contain
105          * a local name, prefix, and namespace URI
106          */
107         void marshallElementType(DOMElement* domElement) const;
108
109         /**
110          * Creates the xmlns attributes for any namespaces set on the XMLObject.
111          * 
112          * @param domElement the DOM element the namespaces will be added to
113          */
114         void marshallNamespaces(DOMElement* domElement) const;
115     
116         /**
117          * Marshalls the text content and/or child elements of the XMLObject.
118          * 
119          * @param domElement the DOM element that will recieved the marshalled children
120          * 
121          * @throws MarshallingException thrown if there is a problem marshalling a child element
122          */
123         void marshallContent(DOMElement* domElement) const;
124
125         /**
126          * Marshalls the attributes from the XMLObject into the given DOM element.
127          * 
128          * @param domElement the DOM Element into which attributes will be marshalled
129          * 
130          * @throws MarshallingException thrown if there is a problem marshalling an attribute
131          */
132         virtual void marshallAttributes(DOMElement* domElement) const {}
133     };
134     
135 };
136
137 #if defined (_MSC_VER)
138     #pragma warning( pop )
139 #endif
140
141 #endif /* __xmltooling_xmlmarshaller_h__ */