Integrated credential resolver API with signing context.
[shibboleth/cpp-xmltooling.git] / xmltooling / XMLObject.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 XMLObject.h\r
19  * \r
20  * Abstract interface to objects that can be manipulated in and out of XML form. \r
21  */\r
22 \r
23 #ifndef __xmltooling_xmlobj_h__\r
24 #define __xmltooling_xmlobj_h__\r
25 \r
26 #include <set>\r
27 #include <list>\r
28 #include <vector>\r
29 #include <xercesc/dom/DOM.hpp>\r
30 #include <xmltooling/QName.h>\r
31 #include <xmltooling/Namespace.h>\r
32 \r
33 using namespace xercesc;\r
34 \r
35 #ifndef XMLTOOLING_NO_XMLSEC\r
36 namespace xmlsignature {\r
37     class XMLTOOL_API Signature;\r
38     class XMLTOOL_API SigningContext;\r
39 };\r
40 #endif\r
41 \r
42 #if defined (_MSC_VER)\r
43     #pragma warning( push )\r
44     #pragma warning( disable : 4250 4251 )\r
45 #endif\r
46 \r
47 namespace xmltooling {\r
48 \r
49     /**\r
50      * Supplies additional information to the marshalling process.\r
51      * Currently this only consists of signature related information.\r
52      */\r
53     class XMLTOOL_API MarshallingContext\r
54     {\r
55         MAKE_NONCOPYABLE(MarshallingContext);\r
56     public:\r
57         /**\r
58          * Default constructor.\r
59          */\r
60         MarshallingContext() {}\r
61         ~MarshallingContext() {}\r
62 \r
63 #ifndef XMLTOOLING_NO_XMLSEC\r
64         /**\r
65          * Builds a marshalling context with an initial signature/context pair.\r
66          * \r
67          * @param sig   a signature object\r
68          * @param ctx   the signing context to associate with the signature \r
69          */\r
70         MarshallingContext(xmlsignature::Signature* sig, xmlsignature::SigningContext* ctx) {\r
71             m_signingContexts.push_back(std::make_pair(sig,ctx));\r
72         }\r
73         \r
74         /** Array of signing contexts, keyed off of the associated Signature */\r
75         std::vector< std::pair<xmlsignature::Signature*,xmlsignature::SigningContext*> > m_signingContexts;\r
76 #endif\r
77     };\r
78 \r
79     /**\r
80      * Object that represents an XML Element that has been unmarshalled into this C++ object.\r
81      */\r
82     class XMLTOOL_API XMLObject\r
83     {\r
84     public:\r
85         virtual ~XMLObject() {}\r
86         \r
87         /**\r
88          * Creates a copy of the object, along with all of its children.\r
89          * \r
90          * The new object tree will be completely distinct and independent of\r
91          * the original in all respects.\r
92          */\r
93         virtual XMLObject* clone() const=0;\r
94         \r
95         /**\r
96          * Gets the QName for this element.  This QName <strong>MUST</strong> \r
97          * contain the namespace URI, namespace prefix, and local element name.\r
98          * \r
99          * @return constant reference to the QName for this object\r
100          */\r
101         virtual const QName& getElementQName() const=0;\r
102         \r
103         /**\r
104          * Gets the namespaces that are scoped to this element.\r
105          * \r
106          * The caller MUST NOT modify the set returned, but may use any\r
107          * non-modifying operations or algorithms on it. Iterators will\r
108          * remain valid unless the set member referenced is removed using\r
109          * the removeNamespace method.\r
110          * \r
111          * @return the namespaces that are scoped to this element\r
112          */\r
113         virtual const std::set<Namespace>& getNamespaces() const=0;\r
114         \r
115         /**\r
116          * Adds a namespace to the ones already scoped to this element\r
117          * \r
118          * @param ns the namespace to add\r
119          */\r
120         virtual void addNamespace(const Namespace& ns) const=0;\r
121         \r
122         /**\r
123          * Removes a namespace from this element\r
124          * \r
125          * @param ns the namespace to remove\r
126          */\r
127         virtual void removeNamespace(const Namespace& ns)=0;\r
128         \r
129         /**\r
130          * Gets the XML schema type of this element.  This translates to contents the xsi:type\r
131          * attribute for the element.\r
132          * \r
133          * @return XML schema type of this element\r
134          */\r
135         virtual const QName* getSchemaType() const=0;\r
136         \r
137         /**\r
138          * Checks to see if this object has a parent.\r
139          * \r
140          * @return true if the object has a parent, false if not\r
141          */\r
142         virtual bool hasParent() const=0;\r
143         \r
144         /**\r
145          * Gets the parent of this element or null if there is no parent.\r
146          * \r
147          * @return the parent of this element or null\r
148          */\r
149         virtual XMLObject* getParent() const=0;\r
150         \r
151         /**\r
152          * Sets the parent of this element.\r
153          * \r
154          * @param parent the parent of this element\r
155          */\r
156         virtual void setParent(XMLObject* parent)=0;\r
157         \r
158         /**\r
159          * Checks if this XMLObject has children.\r
160          * \r
161          * @return true if this XMLObject has children, false if not\r
162          */\r
163         virtual bool hasChildren() const=0;\r
164         \r
165         /**\r
166          * Returns an unmodifiable list of child objects in the order that they\r
167          * should appear in the serialized representation.\r
168          * \r
169          * The validity of the returned list is not maintained if any non-const\r
170          * operations are performed on the parent object. \r
171          * \r
172          * @return the list of children\r
173          */\r
174         virtual const std::list<XMLObject*>& getOrderedChildren() const=0;\r
175 \r
176         /**\r
177          * Gets the DOM representation of this XMLObject, if one exists.\r
178          * \r
179          * @return the DOM representation of this XMLObject\r
180          */\r
181         virtual DOMElement* getDOM() const=0;\r
182         \r
183         /**\r
184          * Sets the DOM representation of this XMLObject.\r
185          * \r
186          * @param dom       DOM representation of this XMLObject\r
187          * @param bindDocument  true if the object should take ownership of the associated Document\r
188          */\r
189         virtual void setDOM(DOMElement* dom, bool bindDocument=false) const=0;\r
190     \r
191         /**\r
192          * Assigns ownership of a DOM document to the XMLObject.\r
193          * This binds the lifetime of the document to the lifetime of the object.\r
194          * \r
195          * @param doc DOM document bound to this object \r
196          */\r
197         virtual void setDocument(DOMDocument* doc) const=0;\r
198 \r
199         /**\r
200          * Releases the DOM representation of this XMLObject, if there is one.\r
201          */\r
202         virtual void releaseDOM() const=0;\r
203         \r
204         /**\r
205          * Releases the DOM representation of this XMLObject's parent.\r
206          * \r
207          * @param propagateRelease true if all ancestors of this element should release their DOM\r
208          */\r
209         virtual void releaseParentDOM(bool propagateRelease=true) const=0;\r
210         \r
211         /**\r
212          * Releases the DOM representation of this XMLObject's children.\r
213          * \r
214          * @param propagateRelease true if all descendants of this element should release their DOM\r
215          */\r
216         virtual void releaseChildrenDOM(bool propagateRelease=true) const=0;\r
217 \r
218         /**\r
219          * A convenience method that is equal to calling releaseDOM() then releaseParentDOM(true).\r
220          */\r
221         void releaseThisandParentDOM() const {\r
222             if (getDOM()) {\r
223                 releaseDOM();\r
224                 releaseParentDOM(true);\r
225             }\r
226         }\r
227     \r
228         /**\r
229          * A convenience method that is equal to calling releaseChildrenDOM(true) then releaseDOM().\r
230          */\r
231         void releaseThisAndChildrenDOM() const {\r
232             if (getDOM()) {\r
233                 releaseChildrenDOM(true);\r
234                 releaseDOM();\r
235             }\r
236         }\r
237 \r
238         /**\r
239          * Marshalls the XMLObject, and its children, into a DOM element.\r
240          * If a document is supplied, then it will be used to create the resulting elements.\r
241          * If the document does not have a Document Element set, then the resulting\r
242          * element will be set as the Document Element. If no document is supplied, then\r
243          * a new document will be created and bound to the lifetime of the root object being\r
244          * marshalled, unless an existing DOM can be reused without creating a new document. \r
245          * \r
246          * @param document  the DOM document the marshalled element will be placed in, or NULL\r
247          * @param ctx       optional marshalling context\r
248          * @return the DOM element representing this XMLObject\r
249          * \r
250          * @throws MarshallingException thrown if there is a problem marshalling the given object\r
251          * @throws SignatureException thrown if a problem occurs during signature creation \r
252          */\r
253         virtual DOMElement* marshall(DOMDocument* document=NULL, MarshallingContext* ctx=NULL) const=0;\r
254         \r
255         /**\r
256          * Marshalls the XMLObject and appends it as a child of the given parent element.\r
257          * \r
258          * <strong>NOTE:</strong> The given Element must be within a DOM tree rooted in \r
259          * the Document owning the given Element.\r
260          * \r
261          * @param parentElement the parent element to append the resulting DOM tree\r
262          * @param ctx       optional marshalling context\r
263          * @return the marshalled element tree\r
264 \r
265          * @throws MarshallingException thrown if the given XMLObject can not be marshalled.\r
266          * @throws SignatureException thrown if a problem occurs during signature creation \r
267          */\r
268         virtual DOMElement* marshall(DOMElement* parentElement, MarshallingContext* ctx=NULL) const=0;\r
269 \r
270         /**\r
271          * Unmarshalls the given W3C DOM element into the XMLObject.\r
272          * The root of a given XML construct should be unmarshalled with the bindDocument parameter\r
273          * set to true.\r
274          * \r
275          * @param element       the DOM element to unmarshall\r
276          * @param bindDocument  true iff the resulting XMLObject should take ownership of the DOM's Document \r
277          * \r
278          * @return the unmarshalled XMLObject\r
279          * \r
280          * @throws UnmarshallingException thrown if an error occurs unmarshalling the DOM element into the XMLObject\r
281          */\r
282         virtual XMLObject* unmarshall(DOMElement* element, bool bindDocument=false)=0;\r
283 \r
284     protected:\r
285         XMLObject() {}\r
286     private:\r
287         XMLObject& operator=(const XMLObject& src);\r
288     };\r
289 \r
290 #if defined (_MSC_VER)\r
291     #pragma warning( pop )\r
292 #endif\r
293 \r
294 };\r
295 \r
296 #endif /* __xmltooling_xmlobj_h__ */\r