2 * Copyright 2001-2011 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file xmltooling/util/XMLHelper.h
20 * A helper class for working with W3C DOM objects.
23 #ifndef __xmltooling_xmlhelper_h__
24 #define __xmltooling_xmlhelper_h__
26 #include <xmltooling/unicode.h>
30 #include <xercesc/dom/DOM.hpp>
32 namespace xmltooling {
34 class XMLTOOL_API QName;
35 class XMLTOOL_API XMLObject;
38 * RAII wrapper for Xerces resources.
40 template<class T> class XercesJanitor
42 MAKE_NONCOPYABLE(XercesJanitor);
48 * @param resource object to release when leaving scope
50 XercesJanitor(T* resource) : m_held(resource) {}
58 * Returns resource held by this object.
60 * @return the resource held or nullptr
67 * Returns resource held by this object.
69 * @return the resource held or nullptr
76 * Returns resource held by this object and releases it to the caller.
78 * @return the resource held or nullptr
88 * A helper class for working with W3C DOM objects.
90 class XMLTOOL_API XMLHelper
94 * Checks if the given element has an xsi:type defined for it
96 * @param e the DOM element
97 * @return true if there is a type, false if not
99 static bool hasXSIType(const xercesc::DOMElement* e);
102 * Gets the XSI type for a given element if it has one.
104 * @param e the element
105 * @return the type or null
107 static QName* getXSIType(const xercesc::DOMElement* e);
110 * Gets the ID attribute of a DOM element.
112 * @param domElement the DOM element
113 * @return the ID attribute or null if there isn't one
115 static xercesc::DOMAttr* getIdAttribute(const xercesc::DOMElement* domElement);
118 * Attempts to locate an XMLObject from this point downward in the tree whose
119 * XML ID matches the supplied value.
121 * @param tree root of tree to search
122 * @param id ID value to locate
123 * @return XMLObject in the tree with a matching ID value, or nullptr
125 static const XMLObject* getXMLObjectById(const XMLObject& tree, const XMLCh* id);
128 * Attempts to locate an XMLObject from this point downward in the tree whose
129 * XML ID matches the supplied value.
131 * @param tree root of tree to search
132 * @param id ID value to locate
133 * @return XMLObject in the tree with a matching ID value, or nullptr
135 static XMLObject* getXMLObjectById(XMLObject& tree, const XMLCh* id);
138 * Returns the set of non-visibly-used namespace declarations found in a tree.
139 * <p>Each member of the set is a prefix/URI pair.
141 * @param tree root of tree to search
142 * @param prefixes container to store declarations
144 static void getNonVisiblyUsedPrefixes(const XMLObject& tree, std::map<xstring,xstring>& prefixes);
147 * Gets the QName for the given DOM node.
149 * @param domNode the DOM node
150 * @return the QName for the element or null if the element was null
152 static QName* getNodeQName(const xercesc::DOMNode* domNode);
156 * Constructs a QName from an attribute's value.
158 * @param attribute the attribute with a QName value
159 * @return a QName from an attribute's value, or null if the given attribute is null
161 static QName* getAttributeValueAsQName(const xercesc::DOMAttr* attribute);
164 * Constructs a QName from a node's value.
166 * @param domNode the DOM node with a QName value
167 * @return a QName from a node's value, or null if the given node has no value
169 static QName* getNodeValueAsQName(const xercesc::DOMNode* domNode);
172 * Returns a boolean based on a node's value.
174 * @param domNode the DOM node with a boolean (1/0/true/false) value
175 * @param def value to return if the node is null/missing
176 * @return a bool value based on the node's value, or a default value
178 static bool getNodeValueAsBool(const xercesc::DOMNode* domNode, bool def);
181 * Appends the child Element to the parent Element,
182 * importing the child Element into the parent's Document if needed.
184 * @param parentElement the parent Element
185 * @param childElement the child Element
186 * @return the child Element that was added (may be an imported copy)
188 static xercesc::DOMElement* appendChildElement(xercesc::DOMElement* parentElement, xercesc::DOMElement* childElement);
191 * Checks the qualified name of a node.
193 * @param n node to check
194 * @param ns namespace to compare with
195 * @param local local name to compare with
196 * @return true iff the node's qualified name matches the other parameters
198 static bool isNodeNamed(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* local);
201 * Returns the first matching child element of the node if any.
203 * @param n node to check
204 * @param localName local name to compare with or nullptr for any match
205 * @return the first matching child node of type Element, or nullptr
207 static xercesc::DOMElement* getFirstChildElement(const xercesc::DOMNode* n, const XMLCh* localName=nullptr);
210 * Returns the last matching child element of the node if any.
212 * @param n node to check
213 * @param localName local name to compare with or nullptr for any match
214 * @return the last matching child node of type Element, or nullptr
216 static xercesc::DOMElement* getLastChildElement(const xercesc::DOMNode* n, const XMLCh* localName=nullptr);
219 * Returns the next matching sibling element of the node if any.
221 * @param n node to check
222 * @param localName local name to compare with or nullptr for any match
223 * @return the next matching sibling node of type Element, or nullptr
225 static xercesc::DOMElement* getNextSiblingElement(const xercesc::DOMNode* n, const XMLCh* localName=nullptr);
228 * Returns the previous matching sibling element of the node if any.
230 * @param n node to check
231 * @param localName local name to compare with or nullptr for any match
232 * @return the previous matching sibling node of type Element, or nullptr
234 static xercesc::DOMElement* getPreviousSiblingElement(const xercesc::DOMNode* n, const XMLCh* localName=nullptr);
237 * Returns the first matching child element of the node if any.
239 * @param n node to check
240 * @param ns namespace to compare with
241 * @param localName local name to compare with
242 * @return the first matching child node of type Element, or nullptr
244 static xercesc::DOMElement* getFirstChildElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
247 * Returns the last matching child element of the node if any.
249 * @param n node to check
250 * @param ns namespace to compare with
251 * @param localName local name to compare with
252 * @return the last matching child node of type Element, or nullptr
254 static xercesc::DOMElement* getLastChildElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
257 * Returns the next matching sibling element of the node if any.
259 * @param n node to check
260 * @param ns namespace to compare with
261 * @param localName local name to compare with
262 * @return the next matching sibling node of type Element, or nullptr
264 static xercesc::DOMElement* getNextSiblingElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
267 * Returns the previous matching sibling element of the node if any.
269 * @param n node to check
270 * @param ns namespace to compare with
271 * @param localName local name to compare with
272 * @return the previous matching sibling node of type Element, or nullptr
274 static xercesc::DOMElement* getPreviousSiblingElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
277 * Returns the content of the first Text node found in the element, if any.
278 * This is roughly similar to the DOM getTextContent function, but only
279 * examines the immediate children of the element.
281 * @param e element to examine
282 * @return the content of the first Text node found, or nullptr
284 static const XMLCh* getTextContent(const xercesc::DOMElement* e);
287 * Returns the content of the specified attribute node as a string,
288 * or the default value, if the attribute is not present.
290 * @param e element to examine (may be nullptr)
291 * @param defValue default value to return
292 * @param localName local name of attribute
293 * @param ns namespace of attribute
294 * @return the specified attribute's value, or the specified default
296 static std::string getAttrString(
297 const xercesc::DOMElement* e, const char* defValue, const XMLCh* localName, const XMLCh* ns=nullptr
301 * Returns the content of the specified attribute node as an integer,
302 * or the default value, if the attribute is not present.
304 * @param e element to examine (may be nullptr)
305 * @param defValue default value to return
306 * @param localName local name of attribute
307 * @param ns namespace of attribute
308 * @return the specified attribute's value, or the specified default
310 static int getAttrInt(
311 const xercesc::DOMElement* e, int defValue, const XMLCh* localName, const XMLCh* ns=nullptr
315 * Returns the content of the specified attribute node as a boolean,
316 * or the default value, if the attribute is not present.
318 * @param e element to examine (may be nullptr)
319 * @param defValue default value to return
320 * @param localName local name of attribute
321 * @param ns namespace of attribute
322 * @return the specified attribute's value, or the specified default
324 static bool getAttrBool(
325 const xercesc::DOMElement* e, bool defValue, const XMLCh* localName, const XMLCh* ns=nullptr
329 * Serializes the DOM node provided into a buffer using UTF-8 encoding and
330 * the default XML serializer available. No manipulation or formatting is applied.
332 * @param n node to serialize
333 * @param buf buffer to serialize element into
334 * @param pretty enable pretty printing if supported
336 static void serialize(const xercesc::DOMNode* n, std::string& buf, bool pretty=false);
339 * Serializes the DOM node provided to a stream using UTF-8 encoding and
340 * the default XML serializer available. No manipulation or formatting is applied.
342 * @param n node to serialize
343 * @param out stream to serialize element into
344 * @param pretty enable pretty printing if supported
345 * @return reference to output stream
347 static std::ostream& serialize(const xercesc::DOMNode* n, std::ostream& out, bool pretty=false);
351 * Serializes the DOM node provided to a stream using UTF-8 encoding and
352 * the default XML serializer available. No manipulation or formatting is applied.
354 * @param n node to serialize
355 * @param ostr stream to serialize element into
356 * @return reference to output stream
358 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const xercesc::DOMNode& n);
361 * Marshalls and serializes the XMLObject provided to a stream using UTF-8 encoding and
362 * the default XML serializer available. No manipulation or formatting is applied.
364 * <p>The marshaller operation takes no parameters.
366 * @param obj object to serialize
367 * @param ostr stream to serialize object into
368 * @return reference to output stream
370 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLObject& obj);
373 #endif /* __xmltooling_xmlhelper_h__ */