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