2 * Copyright 2001-2007 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.
20 * A helper class for working with W3C DOM objects.
23 #ifndef __xmltooling_xmlhelper_h__
24 #define __xmltooling_xmlhelper_h__
26 #include <xmltooling/XMLObject.h>
27 #include <xercesc/dom/DOM.hpp>
31 using namespace xercesc;
33 namespace xmltooling {
36 * RAII wrapper for Xerces resources.
38 template<class T> class XercesJanitor
40 MAKE_NONCOPYABLE(XercesJanitor);
46 * @param resource object to release when leaving scope
48 XercesJanitor(T* resource) : m_held(resource) {}
56 * Returns resource held by this object.
58 * @return the resource held or NULL
65 * Returns resource held by this object.
67 * @return the resource held or NULL
74 * Returns resource held by this object and releases it to the caller.
76 * @return the resource held or NULL
86 * A helper class for working with W3C DOM objects.
88 class XMLTOOL_API XMLHelper
92 * Checks if the given element has an xsi:type defined for it
94 * @param e the DOM element
95 * @return true if there is a type, false if not
97 static bool hasXSIType(const DOMElement* e);
100 * Gets the XSI type for a given element if it has one.
102 * @param e the element
103 * @return the type or null
105 static QName* getXSIType(const DOMElement* e);
108 * Gets the ID attribute of a DOM element.
110 * @param domElement the DOM element
111 * @return the ID attribute or null if there isn't one
113 static DOMAttr* getIdAttribute(const DOMElement* domElement);
116 * Attempts to locate an XMLObject from this point downward in the tree whose
117 * XML ID matches the supplied value.
119 * @param tree root of tree to search
120 * @param id ID value to locate
121 * @return XMLObject in the tree with a matching ID value, or NULL
123 static const XMLObject* getXMLObjectById(const XMLObject& tree, const XMLCh* id);
127 * Gets the QName for the given DOM node.
129 * @param domNode the DOM node
130 * @return the QName for the element or null if the element was null
132 static QName* getNodeQName(const DOMNode* domNode);
135 * Constructs a QName from an attribute's value.
137 * @param attribute the attribute with a QName value
138 * @return a QName from an attribute's value, or null if the given attribute is null
140 static QName* getAttributeValueAsQName(const DOMAttr* attribute);
143 * Appends the child Element to the parent Element,
144 * importing the child Element into the parent's Document if needed.
146 * @param parentElement the parent Element
147 * @param childElement the child Element
148 * @return the child Element that was added (may be an imported copy)
150 static DOMElement* appendChildElement(DOMElement* parentElement, DOMElement* childElement);
153 * Checks the qualified name of a node.
155 * @param n node to check
156 * @param ns namespace to compare with
157 * @param local local name to compare with
158 * @return true iff the node's qualified name matches the other parameters
160 static bool isNodeNamed(const DOMNode* n, const XMLCh* ns, const XMLCh* local) {
161 return (n && XMLString::equals(local,n->getLocalName()) && XMLString::equals(ns,n->getNamespaceURI()));
165 * Returns the first matching child element of the node if any.
167 * @param n node to check
168 * @param localName local name to compare with or NULL for any match
169 * @return the first matching child node of type Element, or NULL
171 static DOMElement* getFirstChildElement(const DOMNode* n, const XMLCh* localName=NULL);
174 * Returns the last matching child element of the node if any.
176 * @param n node to check
177 * @param localName local name to compare with or NULL for any match
178 * @return the last matching child node of type Element, or NULL
180 static DOMElement* getLastChildElement(const DOMNode* n, const XMLCh* localName=NULL);
183 * Returns the next matching sibling element of the node if any.
185 * @param n node to check
186 * @param localName local name to compare with or NULL for any match
187 * @return the next matching sibling node of type Element, or NULL
189 static DOMElement* getNextSiblingElement(const DOMNode* n, const XMLCh* localName=NULL);
192 * Returns the previous matching sibling element of the node if any.
194 * @param n node to check
195 * @param localName local name to compare with or NULL for any match
196 * @return the previous matching sibling node of type Element, or NULL
198 static DOMElement* getPreviousSiblingElement(const DOMNode* n, const XMLCh* localName=NULL);
201 * Returns the first matching child element of the node if any.
203 * @param n node to check
204 * @param ns namespace to compare with
205 * @param localName local name to compare with
206 * @return the first matching child node of type Element, or NULL
208 static DOMElement* getFirstChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
211 * Returns the last matching child element of the node if any.
213 * @param n node to check
214 * @param ns namespace to compare with
215 * @param localName local name to compare with
216 * @return the last matching child node of type Element, or NULL
218 static DOMElement* getLastChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
221 * Returns the next matching sibling element of the node if any.
223 * @param n node to check
224 * @param ns namespace to compare with
225 * @param localName local name to compare with
226 * @return the next matching sibling node of type Element, or NULL
228 static DOMElement* getNextSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
231 * Returns the previous matching sibling element of the node if any.
233 * @param n node to check
234 * @param ns namespace to compare with
235 * @param localName local name to compare with
236 * @return the previous matching sibling node of type Element, or NULL
238 static DOMElement* getPreviousSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
241 * Returns the content of the first Text node found in the element, if any.
242 * This is roughly similar to the DOM getTextContent function, but only
243 * examples the immediate children of the element.
245 * @param e element to examine
246 * @return the content of the first Text node found, or NULL
248 static const XMLCh* getTextContent(const DOMElement* e);
251 * Serializes the DOM node provided into a buffer using UTF-8 encoding and
252 * the default XML serializer available. No manipulation or formatting is applied.
254 * @param n node to serialize
255 * @param buf buffer to serialize element into
257 static void serialize(const DOMNode* n, std::string& buf);
260 * Serializes the DOM node provided to a stream using UTF-8 encoding and
261 * the default XML serializer available. No manipulation or formatting is applied.
263 * @param n node to serialize
264 * @param out stream to serialize element into
265 * @return reference to output stream
267 static std::ostream& serialize(const DOMNode* n, std::ostream& out);
271 * Serializes the DOM node provided to a stream using UTF-8 encoding and
272 * the default XML serializer available. No manipulation or formatting is applied.
274 * @param n node to serialize
275 * @param ostr stream to serialize element into
276 * @return reference to output stream
278 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const DOMNode& n);
281 * Marshalls and serializes the XMLObject provided to a stream using UTF-8 encoding and
282 * the default XML serializer available. No manipulation or formatting is applied.
284 * <p>The marshaller operation takes no parameters.
286 * @param obj object to serialize
287 * @param ostr stream to serialize object into
288 * @return reference to output stream
290 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLObject& obj);
293 #endif /* __xmltooling_xmlhelper_h__ */