2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * @file xmltooling/util/XMLHelper.h
24 * A helper class for working with W3C DOM objects.
27 #ifndef __xmltooling_xmlhelper_h__
28 #define __xmltooling_xmlhelper_h__
30 #include <xmltooling/unicode.h>
34 #include <xercesc/dom/DOM.hpp>
36 namespace xmltooling {
38 class XMLTOOL_API QName;
39 class XMLTOOL_API XMLObject;
42 * RAII wrapper for Xerces resources.
44 template<class T> class XercesJanitor
46 MAKE_NONCOPYABLE(XercesJanitor);
52 * @param resource object to release when leaving scope
54 XercesJanitor(T* resource) : m_held(resource) {}
62 * Returns resource held by this object.
64 * @return the resource held or nullptr
71 * Returns resource held by this object.
73 * @return the resource held or nullptr
80 * Returns resource held by this object and releases it to the caller.
82 * @return the resource held or nullptr
92 * A helper class for working with W3C DOM objects.
94 class XMLTOOL_API XMLHelper
98 * Checks if the given element has an xsi:type defined for it
100 * @param e the DOM element
101 * @return true if there is a type, false if not
103 static bool hasXSIType(const xercesc::DOMElement* e);
106 * Gets the XSI type for a given element if it has one.
108 * @param e the element
109 * @return the type or null
111 static QName* getXSIType(const xercesc::DOMElement* e);
114 * Gets the ID attribute of a DOM element.
116 * @param domElement the DOM element
117 * @return the ID attribute or null if there isn't one
119 static xercesc::DOMAttr* getIdAttribute(const xercesc::DOMElement* domElement);
122 * Attempts to locate an XMLObject from this point downward in the tree whose
123 * XML ID matches the supplied value.
125 * @param tree root of tree to search
126 * @param id ID value to locate
127 * @return XMLObject in the tree with a matching ID value, or nullptr
129 static const XMLObject* getXMLObjectById(const XMLObject& tree, const XMLCh* id);
132 * Attempts to locate an XMLObject from this point downward in the tree whose
133 * XML ID matches the supplied value.
135 * @param tree root of tree to search
136 * @param id ID value to locate
137 * @return XMLObject in the tree with a matching ID value, or nullptr
139 static XMLObject* getXMLObjectById(XMLObject& tree, const XMLCh* id);
142 * Returns the set of non-visibly-used namespace declarations found in a tree.
143 * <p>Each member of the set is a prefix/URI pair.
145 * @param tree root of tree to search
146 * @param prefixes container to store declarations
148 static void getNonVisiblyUsedPrefixes(const XMLObject& tree, std::map<xstring,xstring>& prefixes);
151 * Gets the QName for the given DOM node.
153 * @param domNode the DOM node
154 * @return the QName for the element or null if the element was null
156 static QName* getNodeQName(const xercesc::DOMNode* domNode);
160 * Constructs a QName from an attribute's value.
162 * @param attribute the attribute with a QName value
163 * @return a QName from an attribute's value, or null if the given attribute is null
165 static QName* getAttributeValueAsQName(const xercesc::DOMAttr* attribute);
168 * Constructs a QName from a node's value.
170 * @param domNode the DOM node with a QName value
171 * @return a QName from a node's value, or null if the given node has no value
173 static QName* getNodeValueAsQName(const xercesc::DOMNode* domNode);
176 * Returns a boolean based on a node's value.
178 * @param domNode the DOM node with a boolean (1/0/true/false) value
179 * @param def value to return if the node is null/missing
180 * @return a bool value based on the node's value, or a default value
182 static bool getNodeValueAsBool(const xercesc::DOMNode* domNode, bool def);
185 * Appends the child Element to the parent Element,
186 * importing the child Element into the parent's Document if needed.
188 * @param parentElement the parent Element
189 * @param childElement the child Element
190 * @return the child Element that was added (may be an imported copy)
192 static xercesc::DOMElement* appendChildElement(xercesc::DOMElement* parentElement, xercesc::DOMElement* childElement);
195 * Checks the qualified name of a node.
197 * @param n node to check
198 * @param ns namespace to compare with
199 * @param local local name to compare with
200 * @return true iff the node's qualified name matches the other parameters
202 static bool isNodeNamed(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* local);
205 * Returns the first matching child element of the node if any.
207 * @param n node to check
208 * @param localName local name to compare with or nullptr for any match
209 * @return the first matching child node of type Element, or nullptr
211 static xercesc::DOMElement* getFirstChildElement(const xercesc::DOMNode* n, const XMLCh* localName=nullptr);
214 * Returns the last matching child element of the node if any.
216 * @param n node to check
217 * @param localName local name to compare with or nullptr for any match
218 * @return the last matching child node of type Element, or nullptr
220 static xercesc::DOMElement* getLastChildElement(const xercesc::DOMNode* n, const XMLCh* localName=nullptr);
223 * Returns the next matching sibling element of the node if any.
225 * @param n node to check
226 * @param localName local name to compare with or nullptr for any match
227 * @return the next matching sibling node of type Element, or nullptr
229 static xercesc::DOMElement* getNextSiblingElement(const xercesc::DOMNode* n, const XMLCh* localName=nullptr);
232 * Returns the previous matching sibling element of the node if any.
234 * @param n node to check
235 * @param localName local name to compare with or nullptr for any match
236 * @return the previous matching sibling node of type Element, or nullptr
238 static xercesc::DOMElement* getPreviousSiblingElement(const xercesc::DOMNode* n, const XMLCh* localName=nullptr);
241 * Returns the first matching child element of the node if any.
243 * @param n node to check
244 * @param ns namespace to compare with
245 * @param localName local name to compare with
246 * @return the first matching child node of type Element, or nullptr
248 static xercesc::DOMElement* getFirstChildElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
251 * Returns the last matching child element of the node if any.
253 * @param n node to check
254 * @param ns namespace to compare with
255 * @param localName local name to compare with
256 * @return the last matching child node of type Element, or nullptr
258 static xercesc::DOMElement* getLastChildElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
261 * Returns the next matching sibling element of the node if any.
263 * @param n node to check
264 * @param ns namespace to compare with
265 * @param localName local name to compare with
266 * @return the next matching sibling node of type Element, or nullptr
268 static xercesc::DOMElement* getNextSiblingElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
271 * Returns the previous matching sibling element of the node if any.
273 * @param n node to check
274 * @param ns namespace to compare with
275 * @param localName local name to compare with
276 * @return the previous matching sibling node of type Element, or nullptr
278 static xercesc::DOMElement* getPreviousSiblingElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
281 * Returns the content of the first Text node found in the element, if any.
282 * This is roughly similar to the DOM getTextContent function, but only
283 * examines the immediate children of the element.
285 * @param e element to examine
286 * @return the content of the first Text node found, or nullptr
288 static const XMLCh* getTextContent(const xercesc::DOMElement* e);
291 * Returns the content of the specified attribute node as a string,
292 * or the default value, if the attribute is not present.
294 * @param e element to examine (may be nullptr)
295 * @param defValue default value to return
296 * @param localName local name of attribute
297 * @param ns namespace of attribute
298 * @return the specified attribute's value, or the specified default
300 static std::string getAttrString(
301 const xercesc::DOMElement* e, const char* defValue, const XMLCh* localName, const XMLCh* ns=nullptr
305 * Returns the content of the specified attribute node as an integer,
306 * or the default value, if the attribute is not present.
308 * @param e element to examine (may be nullptr)
309 * @param defValue default value to return
310 * @param localName local name of attribute
311 * @param ns namespace of attribute
312 * @return the specified attribute's value, or the specified default
314 static int getAttrInt(
315 const xercesc::DOMElement* e, int defValue, const XMLCh* localName, const XMLCh* ns=nullptr
319 * Returns the content of the specified attribute node as a boolean,
320 * or the default value, if the attribute is not present.
322 * @param e element to examine (may be nullptr)
323 * @param defValue default value to return
324 * @param localName local name of attribute
325 * @param ns namespace of attribute
326 * @return the specified attribute's value, or the specified default
328 static bool getAttrBool(
329 const xercesc::DOMElement* e, bool defValue, const XMLCh* localName, const XMLCh* ns=nullptr
333 * Serializes the DOM node provided into a buffer using UTF-8 encoding and
334 * the default XML serializer available. No manipulation or formatting is applied.
336 * @param n node to serialize
337 * @param buf buffer to serialize element into
338 * @param pretty enable pretty printing if supported
340 static void serialize(const xercesc::DOMNode* n, std::string& buf, bool pretty=false);
343 * Serializes the DOM node provided to a stream using UTF-8 encoding and
344 * the default XML serializer available. No manipulation or formatting is applied.
346 * @param n node to serialize
347 * @param out stream to serialize element into
348 * @param pretty enable pretty printing if supported
349 * @return reference to output stream
351 static std::ostream& serialize(const xercesc::DOMNode* n, std::ostream& out, bool pretty=false);
355 * Serializes the DOM node provided to a stream using UTF-8 encoding and
356 * the default XML serializer available. No manipulation or formatting is applied.
358 * @param n node to serialize
359 * @param ostr stream to serialize element into
360 * @return reference to output stream
362 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const xercesc::DOMNode& n);
365 * Marshalls and serializes the XMLObject provided to a stream using UTF-8 encoding and
366 * the default XML serializer available. No manipulation or formatting is applied.
368 * <p>The marshaller operation takes no parameters.
370 * @param obj object to serialize
371 * @param ostr stream to serialize object into
372 * @return reference to output stream
374 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLObject& obj);
377 #endif /* __xmltooling_xmlhelper_h__ */