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