Update copyright.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / XMLHelper.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 XMLHelper.h
19  * 
20  * A helper class for working with W3C DOM objects. 
21  */
22
23 #ifndef __xmltooling_xmlhelper_h__
24 #define __xmltooling_xmlhelper_h__
25
26 #include <xmltooling/XMLObject.h>
27 #include <xercesc/dom/DOM.hpp>
28
29 #include <iostream>
30
31 using namespace xercesc;
32
33 namespace xmltooling {
34     
35     /**
36      * RAII wrapper for Xerces resources.
37      */
38     template<class T> class XercesJanitor
39     {
40         MAKE_NONCOPYABLE(XercesJanitor);
41         T* m_held;
42     public:
43         /**
44          * Constructor
45          * 
46          * @param resource  object to release when leaving scope
47          */
48         XercesJanitor(T* resource) : m_held(resource) {}
49         
50         ~XercesJanitor() {
51             if (m_held)
52                 m_held->release();
53         }
54         
55         /**
56          * Returns resource held by this object and releases it to the caller.
57          * 
58          * @return  the resource held or NULL
59          */
60         T* release() {
61             T* ret=m_held;
62             m_held=NULL;
63             return ret;
64         }
65     };
66     
67     /**
68      * A helper class for working with W3C DOM objects. 
69      */
70     class XMLTOOL_API XMLHelper
71     {
72     public:
73         /**
74          * Checks if the given element has an xsi:type defined for it
75          * 
76          * @param e the DOM element
77          * @return true if there is a type, false if not
78          */
79         static bool hasXSIType(const DOMElement* e);
80
81         /**
82          * Gets the XSI type for a given element if it has one.
83          * 
84          * @param e the element
85          * @return the type or null
86          */
87         static QName* getXSIType(const DOMElement* e);
88
89         /**
90          * Gets the ID attribute of a DOM element.
91          * 
92          * @param domElement the DOM element
93          * @return the ID attribute or null if there isn't one
94          */
95         static DOMAttr* getIdAttribute(const DOMElement* domElement);
96
97         /**
98          * Attempts to locate an XMLObject from this point downward in the tree whose
99          * XML ID matches the supplied value.
100          * 
101          * @param tree  root of tree to search
102          * @param id    ID value to locate
103          * @return XMLObject in the tree with a matching ID value, or NULL
104          */
105         static const XMLObject* getXMLObjectById(const XMLObject& tree, const XMLCh* id);
106         
107
108         /**
109          * Gets the QName for the given DOM node.
110          * 
111          * @param domNode the DOM node
112          * @return the QName for the element or null if the element was null
113          */
114         static QName* getNodeQName(const DOMNode* domNode);
115
116         /**
117          * Constructs a QName from an attribute's value.
118          * 
119          * @param attribute the attribute with a QName value
120          * @return a QName from an attribute's value, or null if the given attribute is null
121          */
122         static QName* getAttributeValueAsQName(const DOMAttr* attribute);
123
124         /**
125          * Appends the child Element to the parent Element,
126          * importing the child Element into the parent's Document if needed.
127          * 
128          * @param parentElement the parent Element
129          * @param childElement the child Element
130          * @return the child Element that was added (may be an imported copy)
131          */
132         static DOMElement* appendChildElement(DOMElement* parentElement, DOMElement* childElement);
133         
134         /**
135          * Checks the qualified name of a node.
136          * 
137          * @param n     node to check
138          * @param ns    namespace to compare with
139          * @param local local name to compare with
140          * @return  true iff the node's qualified name matches the other parameters
141          */
142         static bool isNodeNamed(const DOMNode* n, const XMLCh* ns, const XMLCh* local) {
143             return (n && XMLString::equals(local,n->getLocalName()) && XMLString::equals(ns,n->getNamespaceURI()));
144         }
145
146         /**
147          * Returns the first matching child element of the node if any.
148          * 
149          * @param n         node to check
150          * @param localName local name to compare with or NULL for any match
151          * @return  the first matching child node of type Element, or NULL
152          */
153         static DOMElement* getFirstChildElement(const DOMNode* n, const XMLCh* localName=NULL);
154         
155         /**
156          * Returns the last matching child element of the node if any.
157          * 
158          * @param n     node to check
159          * @param localName local name to compare with or NULL for any match
160          * @return  the last matching child node of type Element, or NULL
161          */
162         static DOMElement* getLastChildElement(const DOMNode* n, const XMLCh* localName=NULL);
163         
164         /**
165          * Returns the next matching sibling element of the node if any.
166          * 
167          * @param n     node to check
168          * @param localName local name to compare with or NULL for any match
169          * @return  the next matching sibling node of type Element, or NULL
170          */
171         static DOMElement* getNextSiblingElement(const DOMNode* n, const XMLCh* localName=NULL);
172         
173         /**
174          * Returns the previous matching sibling element of the node if any.
175          * 
176          * @param n     node to check
177          * @param localName local name to compare with or NULL for any match
178          * @return  the previous matching sibling node of type Element, or NULL
179          */
180         static DOMElement* getPreviousSiblingElement(const DOMNode* n, const XMLCh* localName=NULL);
181         
182         /**
183          * Returns the first matching child element of the node if any.
184          * 
185          * @param n         node to check
186          * @param ns        namespace to compare with
187          * @param localName local name to compare with
188          * @return  the first matching child node of type Element, or NULL
189          */
190         static DOMElement* getFirstChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
191         
192         /**
193          * Returns the last matching child element of the node if any.
194          * 
195          * @param n         node to check
196          * @param ns        namespace to compare with
197          * @param localName local name to compare with
198          * @return  the last matching child node of type Element, or NULL
199          */
200         static DOMElement* getLastChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
201         
202         /**
203          * Returns the next matching sibling element of the node if any.
204          * 
205          * @param n         node to check
206          * @param ns        namespace to compare with
207          * @param localName local name to compare with
208          * @return  the next matching sibling node of type Element, or NULL
209          */
210         static DOMElement* getNextSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
211         
212         /**
213          * Returns the previous matching sibling element of the node if any.
214          * 
215          * @param n         node to check
216          * @param ns        namespace to compare with
217          * @param localName local name to compare with
218          * @return  the previous matching sibling node of type Element, or NULL
219          */
220         static DOMElement* getPreviousSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
221
222         /**
223          * Returns the content of the first Text node found in the element, if any.
224          * This is roughly similar to the DOM getTextContent function, but only
225          * examples the immediate children of the element.
226          *
227          * @param e     element to examine
228          * @return the content of the first Text node found, or NULL
229          */
230         static const XMLCh* getTextContent(const DOMElement* e);
231
232         /**
233          * Serializes the DOM node provided into a buffer using UTF-8 encoding and
234          * the default XML serializer available. No manipulation or formatting is applied.
235          * 
236          * @param n     node to serialize
237          * @param buf   buffer to serialize element into
238          */
239         static void serialize(const DOMNode* n, std::string& buf);
240
241         /**
242          * Serializes the DOM node provided to a stream using UTF-8 encoding and
243          * the default XML serializer available. No manipulation or formatting is applied.
244          * 
245          * @param n     node to serialize
246          * @param out   stream to serialize element into
247          * @return reference to output stream
248          */
249         static std::ostream& serialize(const DOMNode* n, std::ostream& out);
250     };
251
252     /**
253      * Serializes the DOM node provided to a stream using UTF-8 encoding and
254      * the default XML serializer available. No manipulation or formatting is applied.
255      * 
256      * @param n      node to serialize
257      * @param ostr   stream to serialize element into
258      * @return reference to output stream
259      */
260     extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const DOMNode& n);
261
262     /**
263      * Marshalls and serializes the XMLObject provided to a stream using UTF-8 encoding and
264      * the default XML serializer available. No manipulation or formatting is applied.
265      * 
266      * <p>The marshaller operation takes no parameters.
267      * 
268      * @param obj    object to serialize
269      * @param ostr   stream to serialize object into
270      * @return reference to output stream
271      */
272     extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLObject& obj);
273 };
274
275 #endif /* __xmltooling_xmlhelper_h__ */