Major revamp of credential and trust handling code, PKIX engine still needs work.
[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             ,const Credential* credential=NULL
48 #endif
49             ) const;
50
51         DOMElement* marshall(
52             DOMElement* parentElement
53 #ifndef XMLTOOLING_NO_XMLSEC
54             ,const std::vector<xmlsignature::Signature*>* sigs=NULL
55             ,const Credential* credential=NULL
56 #endif
57             ) const;
58         
59     protected:
60         AbstractXMLObjectMarshaller() {}
61
62         /**
63          * Sets the given element as the Document Element of the given Document.
64          * If the document already has a Document Element it is replaced by the given element.
65          * 
66          * @param document the document
67          * @param element the Element that will serve as the Document Element
68          */
69         void setDocumentElement(DOMDocument* document, DOMElement* element) const {
70             DOMElement* documentRoot = document->getDocumentElement();
71             if (documentRoot)
72                 document->replaceChild(element, documentRoot);
73             else
74                 document->appendChild(element);
75         }
76     
77 #ifndef XMLTOOLING_NO_XMLSEC
78         /**
79          * Marshalls the XMLObject into the given DOM Element.
80          * The DOM Element must be within a DOM tree rooted in the owning Document.
81          * 
82          * @param targetElement the Element into which the XMLObject is marshalled into
83          * @param sigs          optional array of signatures to create after marshalling          
84          * @param credential    optional credential to supply signing key and related info
85          * 
86          * @throws MarshallingException thrown if there is a problem marshalling the object
87          * @throws SignatureException thrown if a problem occurs during signature creation 
88          */
89         void marshallInto(
90             DOMElement* targetElement, const std::vector<xmlsignature::Signature*>* sigs, const Credential* credential=NULL
91             ) const;
92 #else
93         /**
94          * Marshalls the XMLObject into the given DOM Element.
95          * The DOM Element must be within a DOM tree rooted in the owning Document.
96          * 
97          * @param targetElement the Element into which the XMLObject is marshalled into
98          * 
99          * @throws MarshallingException thrown if there is a problem marshalling the object
100          */
101         void marshallInto(DOMElement* targetElement) const;
102 #endif
103     
104         /**
105          * Creates an xsi:type attribute, corresponding to the given type of the XMLObject, on the DOM element.
106          * 
107          * @param domElement the DOM element
108          * 
109          * @throws MarshallingException thrown if the type on the XMLObject is doesn't contain
110          * a local name, prefix, and namespace URI
111          */
112         void marshallElementType(DOMElement* domElement) const;
113
114         /**
115          * Creates the xmlns attributes for any namespaces set on the XMLObject.
116          * 
117          * @param domElement the DOM element the namespaces will be added to
118          */
119         void marshallNamespaces(DOMElement* domElement) const;
120     
121 #ifndef XMLTOOLING_NO_XMLSEC
122         /**
123          * Marshalls the text content and/or child elements of the XMLObject.
124          * 
125          * @param domElement the DOM element that will recieved the marshalled children
126          * @param credential    optional credential to supply signing key and related info
127          * 
128          * @throws MarshallingException thrown if there is a problem marshalling a child element
129          */
130         void marshallContent(DOMElement* domElement, const Credential* credential) const;
131 #else
132         /**
133          * Marshalls the text content and/or child elements of the XMLObject.
134          * 
135          * @param domElement the DOM element that will recieved the marshalled children
136          * 
137          * @throws MarshallingException thrown if there is a problem marshalling a child element
138          */
139         void marshallContent(DOMElement* domElement) const;
140 #endif
141
142         /**
143          * Marshalls the attributes from the XMLObject into the given DOM element.
144          * 
145          * @param domElement the DOM Element into which attributes will be marshalled
146          * 
147          * @throws MarshallingException thrown if there is a problem marshalling an attribute
148          */
149         virtual void marshallAttributes(DOMElement* domElement) const {}
150     };
151     
152 };
153
154 #if defined (_MSC_VER)
155     #pragma warning( pop )
156 #endif
157
158 #endif /* __xmltooling_xmlmarshaller_h__ */