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