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