Multi-line svn commit, see body.
[shibboleth/cpp-xmltooling.git] / xmltooling / XMLObject.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 xmltooling/XMLObject.h
19  * 
20  * Abstract interface to objects that can be manipulated in and out of XML form. 
21  */
22
23 #ifndef __xmltooling_xmlobj_h__
24 #define __xmltooling_xmlobj_h__
25
26 #include <xmltooling/QName.h>
27 #include <xmltooling/Namespace.h>
28 #include <xmltooling/util/XMLConstants.h>
29
30 #include <set>
31 #include <list>
32 #include <vector>
33 #include <xercesc/dom/DOM.hpp>
34
35 #ifndef XMLTOOLING_NO_XMLSEC
36 namespace xmlsignature {
37     class XMLTOOL_API Signature;
38 };
39 #endif
40
41 #if defined (_MSC_VER)
42     #pragma warning( push )
43     #pragma warning( disable : 4250 4251 )
44 #endif
45
46 namespace xmltooling {
47
48 #ifndef XMLTOOLING_NO_XMLSEC
49     class XMLTOOL_API Credential;
50 #endif
51
52     /**
53      * Object that represents an XML Element that has been unmarshalled into this C++ object.
54      */
55     class XMLTOOL_API XMLObject
56     {
57     public:
58         virtual ~XMLObject() {}
59         
60         /**
61          * Creates a copy of the object, along with all of its children.
62          * 
63          * The new object tree will be completely distinct and independent of
64          * the original in all respects.
65          */
66         virtual XMLObject* clone() const=0;
67         
68         /**
69          * Specialized function for detaching a child object from its parent
70          * <strong>while disposing of the parent</strong>.
71          *
72          * This is not a generic way of detaching any child object, but only of
73          * pruning a single child from the root of an XMLObject tree. If the
74          * detached XMLObject's parent is itself a child, an exception will be
75          * thrown. It's mainly useful for turning a child into the new root of
76          * the tree without having to clone the child.
77          */
78         virtual void detach()=0;
79
80         /**
81          * Gets the QName for this element.  This QName <strong>MUST</strong> 
82          * contain the namespace URI, namespace prefix, and local element name.
83          * 
84          * @return constant reference to the QName for this object
85          */
86         virtual const QName& getElementQName() const=0;
87         
88         /**
89          * Gets the namespaces that are scoped to this element.
90          * 
91          * The caller MUST NOT modify the set returned, but may use any
92          * non-modifying operations or algorithms on it. Iterators will
93          * remain valid unless the set member referenced is removed using
94          * the removeNamespace method.
95          * 
96          * @return the namespaces that are scoped to this element
97          */
98         virtual const std::set<Namespace>& getNamespaces() const=0;
99         
100         /**
101          * Adds a namespace to the ones already scoped to this element
102          * 
103          * @param ns the namespace to add
104          */
105         virtual void addNamespace(const Namespace& ns) const=0;
106         
107         /**
108          * Removes a namespace from this element
109          * 
110          * @param ns the namespace to remove
111          */
112         virtual void removeNamespace(const Namespace& ns)=0;
113         
114         /**
115          * Gets the XML schema type of this element.  This translates to contents the xsi:type
116          * attribute for the element.
117          * 
118          * @return XML schema type of this element
119          */
120         virtual const QName* getSchemaType() const=0;
121         
122         /**
123          * Gets the value of the ID attribute set on this object, if any.
124          * 
125          * @return an ID value or NULL 
126          */
127         virtual const XMLCh* getXMLID() const=0;
128
129         /**
130          * Returns the xsi:nil property of the object, or false if not set.
131          * 
132          * @return      the xsi:nil property
133          */
134         bool nil() const {
135             switch (getNil()) {
136                 case xmlconstants::XML_BOOL_TRUE:
137                 case xmlconstants::XML_BOOL_ONE:
138                     return true;
139                 case xmlconstants::XML_BOOL_FALSE:
140                 case xmlconstants::XML_BOOL_ZERO:
141                 default:
142                     return false; 
143             }
144         }
145
146         /**
147          * Returns the xsi:nil property as an explicit enumerated value.
148          * 
149          * @return the xsi:nil property
150          */
151         virtual xmlconstants::xmltooling_bool_t getNil() const=0;
152         
153         /**
154          * Sets the xsi:nil property using an enumerated value.
155          * 
156          * @param value value to set
157          */
158         virtual void nil(xmlconstants::xmltooling_bool_t value)=0;
159         
160         /**
161          * Sets the xsi:nil property.
162          * 
163          * @param value value to set
164          */
165         void nil(bool value) {
166             nil(value ? xmlconstants::XML_BOOL_ONE : xmlconstants::XML_BOOL_ZERO);
167         }
168         
169         /**
170          * Sets the xsi:nil property using a string constant.
171          * 
172          * @param value value to set
173          */
174         void setNil(const XMLCh* value);
175         
176         /**
177          * Checks to see if this object has a parent.
178          * 
179          * @return true if the object has a parent, false if not
180          */
181         virtual bool hasParent() const=0;
182         
183         /**
184          * Gets the parent of this element or null if there is no parent.
185          * 
186          * @return the parent of this element or null
187          */
188         virtual XMLObject* getParent() const=0;
189         
190         /**
191          * Sets the parent of this element.
192          * 
193          * @param parent the parent of this element
194          */
195         virtual void setParent(XMLObject* parent)=0;
196         
197         /**
198          * Checks if this XMLObject has children.
199          * 
200          * @return true if this XMLObject has children, false if not
201          */
202         virtual bool hasChildren() const=0;
203         
204         /**
205          * Returns an unmodifiable list of child objects in the order that they
206          * should appear in the serialized representation.
207          * 
208          * The validity of the returned list is not maintained if any non-const
209          * operations are performed on the parent object. 
210          * 
211          * @return the list of children
212          */
213         virtual const std::list<XMLObject*>& getOrderedChildren() const=0;
214
215         /**
216          * Used by a child's detach method to isolate the child from
217          * this parent object in preparation for destroying the parent
218          * (this object).
219          * 
220          * @param child the child object to remove
221          */
222         virtual void removeChild(XMLObject* child)=0;
223
224         /**
225          * Returns the text content at the specified position relative to
226          * any child elements. A zero represents leading text, 1 comes after
227          * the first child, and so forth.
228          *
229          * @param position  the relative child element position of the text  
230          * @return the designated text value
231          */
232         virtual const XMLCh* getTextContent(unsigned int position=0) const=0;
233
234         /**
235          * Sets (or clears) text content relative to a child element's position. 
236          * 
237          * @param value         value to set, or NULL to clear
238          * @param position      position relative to child element 
239          */
240         virtual void setTextContent(const XMLCh* value, unsigned int position=0)=0;
241
242         /**
243          * Gets the DOM representation of this XMLObject, if one exists.
244          * 
245          * @return the DOM representation of this XMLObject
246          */
247         virtual xercesc::DOMElement* getDOM() const=0;
248         
249         /**
250          * Sets the DOM representation of this XMLObject.
251          * 
252          * @param dom       DOM representation of this XMLObject
253          * @param bindDocument  true if the object should take ownership of the associated Document
254          */
255         virtual void setDOM(xercesc::DOMElement* dom, bool bindDocument=false) const=0;
256     
257         /**
258          * Assigns ownership of a DOM document to the XMLObject.
259          * This binds the lifetime of the document to the lifetime of the object.
260          * 
261          * @param doc DOM document bound to this object 
262          */
263         virtual void setDocument(xercesc::DOMDocument* doc) const=0;
264
265         /**
266          * Releases the DOM representation of this XMLObject, if there is one.
267          */
268         virtual void releaseDOM() const=0;
269         
270         /**
271          * Releases the DOM representation of this XMLObject's parent.
272          * 
273          * @param propagateRelease true if all ancestors of this element should release their DOM
274          */
275         virtual void releaseParentDOM(bool propagateRelease=true) const=0;
276         
277         /**
278          * Releases the DOM representation of this XMLObject's children.
279          * 
280          * @param propagateRelease true if all descendants of this element should release their DOM
281          */
282         virtual void releaseChildrenDOM(bool propagateRelease=true) const=0;
283
284         /**
285          * A convenience method that is equal to calling releaseDOM() then releaseParentDOM(true).
286          */
287         void releaseThisandParentDOM() const {
288             if (getDOM()) {
289                 releaseDOM();
290                 releaseParentDOM(true);
291             }
292         }
293     
294         /**
295          * A convenience method that is equal to calling releaseChildrenDOM(true) then releaseDOM().
296          */
297         void releaseThisAndChildrenDOM() const {
298             if (getDOM()) {
299                 releaseChildrenDOM(true);
300                 releaseDOM();
301             }
302         }
303
304         /**
305          * Marshalls the XMLObject, and its children, into a DOM element.
306          * If a document is supplied, then it will be used to create the resulting elements.
307          * If the document does not have a Document Element set, then the resulting
308          * element will be set as the Document Element. If no document is supplied, then
309          * a new document will be created and bound to the lifetime of the root object being
310          * marshalled, unless an existing DOM can be reused without creating a new document. 
311          * 
312          * @param document  the DOM document the marshalled element will be placed in, or NULL
313          * @param sigs      ordered array of signatures to create after marshalling is complete
314          * @param credential    optional credential to supply signing key and related info
315          * @return the DOM element representing this XMLObject
316          * 
317          * @throws MarshallingException thrown if there is a problem marshalling the given object
318          * @throws SignatureException thrown if a problem occurs during signature creation 
319          */
320         virtual xercesc::DOMElement* marshall(
321             xercesc::DOMDocument* document=NULL
322 #ifndef XMLTOOLING_NO_XMLSEC
323             ,const std::vector<xmlsignature::Signature*>* sigs=NULL
324             ,const Credential* credential=NULL
325 #endif
326             ) const=0;
327         
328         /**
329          * Marshalls the XMLObject and appends it as a child of the given parent element.
330          * 
331          * <strong>NOTE:</strong> The given Element must be within a DOM tree rooted in 
332          * the Document owning the given Element.
333          * 
334          * @param parentElement the parent element to append the resulting DOM tree
335          * @param sigs          ordered array of signatures to create after marshalling is complete
336          * @param credential    optional credential to supply signing key and related info
337          * @return the marshalled element tree
338
339          * @throws MarshallingException thrown if the given XMLObject can not be marshalled.
340          * @throws SignatureException thrown if a problem occurs during signature creation 
341          */
342         virtual xercesc::DOMElement* marshall(
343             xercesc::DOMElement* parentElement
344 #ifndef XMLTOOLING_NO_XMLSEC
345             ,const std::vector<xmlsignature::Signature*>* sigs=NULL
346             ,const Credential* credential=NULL
347 #endif
348             ) const=0;
349
350         /**
351          * Unmarshalls the given W3C DOM element into the XMLObject.
352          * The root of a given XML construct should be unmarshalled with the bindDocument parameter
353          * set to true.
354          * 
355          * @param element       the DOM element to unmarshall
356          * @param bindDocument  true iff the resulting XMLObject should take ownership of the DOM's Document 
357          * 
358          * @return the unmarshalled XMLObject
359          * 
360          * @throws UnmarshallingException thrown if an error occurs unmarshalling the DOM element into the XMLObject
361          */
362         virtual XMLObject* unmarshall(xercesc::DOMElement* element, bool bindDocument=false)=0;
363
364     protected:
365         XMLObject() {}
366     private:
367         XMLObject& operator=(const XMLObject& src);
368     };
369
370 };
371
372 #if defined (_MSC_VER)
373     #pragma warning( pop )
374 #endif
375
376 #endif /* __xmltooling_xmlobj_h__ */