2 * Copyright 2001-2010 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/base.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 NULL
67 * Returns resource held by this object.
69 * @return the resource held or NULL
76 * Returns resource held by this object and releases it to the caller.
78 * @return the resource held or NULL
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 NULL
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 NULL
135 static XMLObject* getXMLObjectById(XMLObject& tree, const XMLCh* id);
138 * Returns a list of non-visibly-used namespace prefixes found in a tree.
140 * @param tree root of tree to search
141 * @param prefixes container to store prefix list
143 static void getNonVisiblyUsedPrefixes(const XMLObject& tree, std::set<xstring>& prefixes);
146 * Gets the QName for the given DOM node.
148 * @param domNode the DOM node
149 * @return the QName for the element or null if the element was null
151 static QName* getNodeQName(const xercesc::DOMNode* domNode);
155 * Constructs a QName from an attribute's value.
157 * @param attribute the attribute with a QName value
158 * @return a QName from an attribute's value, or null if the given attribute is null
160 static QName* getAttributeValueAsQName(const xercesc::DOMAttr* attribute);
163 * Constructs a QName from a node's value.
165 * @param domNode the DOM node with a QName value
166 * @return a QName from a node's value, or null if the given node has no value
168 static QName* getNodeValueAsQName(const xercesc::DOMNode* domNode);
171 * Appends the child Element to the parent Element,
172 * importing the child Element into the parent's Document if needed.
174 * @param parentElement the parent Element
175 * @param childElement the child Element
176 * @return the child Element that was added (may be an imported copy)
178 static xercesc::DOMElement* appendChildElement(xercesc::DOMElement* parentElement, xercesc::DOMElement* childElement);
181 * Checks the qualified name of a node.
183 * @param n node to check
184 * @param ns namespace to compare with
185 * @param local local name to compare with
186 * @return true iff the node's qualified name matches the other parameters
188 static bool isNodeNamed(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* local);
191 * Returns the first matching child element of the node if any.
193 * @param n node to check
194 * @param localName local name to compare with or NULL for any match
195 * @return the first matching child node of type Element, or NULL
197 static xercesc::DOMElement* getFirstChildElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL);
200 * Returns the last matching child element of the node if any.
202 * @param n node to check
203 * @param localName local name to compare with or NULL for any match
204 * @return the last matching child node of type Element, or NULL
206 static xercesc::DOMElement* getLastChildElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL);
209 * Returns the next matching sibling element of the node if any.
211 * @param n node to check
212 * @param localName local name to compare with or NULL for any match
213 * @return the next matching sibling node of type Element, or NULL
215 static xercesc::DOMElement* getNextSiblingElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL);
218 * Returns the previous matching sibling element of the node if any.
220 * @param n node to check
221 * @param localName local name to compare with or NULL for any match
222 * @return the previous matching sibling node of type Element, or NULL
224 static xercesc::DOMElement* getPreviousSiblingElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL);
227 * Returns the first matching child element of the node if any.
229 * @param n node to check
230 * @param ns namespace to compare with
231 * @param localName local name to compare with
232 * @return the first matching child node of type Element, or NULL
234 static xercesc::DOMElement* getFirstChildElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
237 * Returns the last 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 last matching child node of type Element, or NULL
244 static xercesc::DOMElement* getLastChildElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
247 * Returns the next matching sibling 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 next matching sibling node of type Element, or NULL
254 static xercesc::DOMElement* getNextSiblingElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
257 * Returns the previous 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 previous matching sibling node of type Element, or NULL
264 static xercesc::DOMElement* getPreviousSiblingElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
267 * Returns the content of the first Text node found in the element, if any.
268 * This is roughly similar to the DOM getTextContent function, but only
269 * examples the immediate children of the element.
271 * @param e element to examine
272 * @return the content of the first Text node found, or NULL
274 static const XMLCh* getTextContent(const xercesc::DOMElement* e);
277 * Serializes the DOM node provided into a buffer using UTF-8 encoding and
278 * the default XML serializer available. No manipulation or formatting is applied.
280 * @param n node to serialize
281 * @param buf buffer to serialize element into
282 * @param pretty enable pretty printing if supported
284 static void serialize(const xercesc::DOMNode* n, std::string& buf, bool pretty=false);
287 * Serializes the DOM node provided to a stream using UTF-8 encoding and
288 * the default XML serializer available. No manipulation or formatting is applied.
290 * @param n node to serialize
291 * @param out stream to serialize element into
292 * @param pretty enable pretty printing if supported
293 * @return reference to output stream
295 static std::ostream& serialize(const xercesc::DOMNode* n, std::ostream& out, bool pretty=false);
299 * Serializes the DOM node provided to a stream using UTF-8 encoding and
300 * the default XML serializer available. No manipulation or formatting is applied.
302 * @param n node to serialize
303 * @param ostr stream to serialize element into
304 * @return reference to output stream
306 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const xercesc::DOMNode& n);
309 * Marshalls and serializes the XMLObject provided to a stream using UTF-8 encoding and
310 * the default XML serializer available. No manipulation or formatting is applied.
312 * <p>The marshaller operation takes no parameters.
314 * @param obj object to serialize
315 * @param ostr stream to serialize object into
316 * @return reference to output stream
318 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLObject& obj);
321 #endif /* __xmltooling_xmlhelper_h__ */