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 and releases it to the caller.
58 * @return the resource held or NULL
68 * A helper class for working with W3C DOM objects.
70 class XMLTOOL_API XMLHelper
74 * Checks if the given element has an xsi:type defined for it
76 * @param e the DOM element
77 * @return true if there is a type, false if not
79 static bool hasXSIType(const DOMElement* e);
82 * Gets the XSI type for a given element if it has one.
84 * @param e the element
85 * @return the type or null
87 static QName* getXSIType(const DOMElement* e);
90 * Gets the ID attribute of a DOM element.
92 * @param domElement the DOM element
93 * @return the ID attribute or null if there isn't one
95 static DOMAttr* getIdAttribute(const DOMElement* domElement);
98 * Attempts to locate an XMLObject from this point downward in the tree whose
99 * XML ID matches the supplied value.
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
105 static const XMLObject* getXMLObjectById(const XMLObject& tree, const XMLCh* id);
109 * Gets the QName for the given DOM node.
111 * @param domNode the DOM node
112 * @return the QName for the element or null if the element was null
114 static QName* getNodeQName(const DOMNode* domNode);
117 * Constructs a QName from an attribute's value.
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
122 static QName* getAttributeValueAsQName(const DOMAttr* attribute);
125 * Appends the child Element to the parent Element,
126 * importing the child Element into the parent's Document if needed.
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)
132 static DOMElement* appendChildElement(DOMElement* parentElement, DOMElement* childElement);
135 * Checks the qualified name of a node.
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
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()));
147 * Returns the first matching child element of the node if any.
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
153 static DOMElement* getFirstChildElement(const DOMNode* n, const XMLCh* localName=NULL);
156 * Returns the last matching child element of the node if any.
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
162 static DOMElement* getLastChildElement(const DOMNode* n, const XMLCh* localName=NULL);
165 * Returns the next matching sibling 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 next matching sibling node of type Element, or NULL
171 static DOMElement* getNextSiblingElement(const DOMNode* n, const XMLCh* localName=NULL);
174 * Returns the previous matching sibling 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 previous matching sibling node of type Element, or NULL
180 static DOMElement* getPreviousSiblingElement(const DOMNode* n, const XMLCh* localName=NULL);
183 * Returns the first matching child element of the node if any.
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
190 static DOMElement* getFirstChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
193 * Returns the last matching child element of the node if any.
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
200 static DOMElement* getLastChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
203 * Returns the next matching sibling element of the node if any.
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
210 static DOMElement* getNextSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
213 * Returns the previous matching sibling element of the node if any.
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
220 static DOMElement* getPreviousSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
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.
227 * @param e element to examine
228 * @return the content of the first Text node found, or NULL
230 static const XMLCh* getTextContent(const DOMElement* e);
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.
236 * @param n node to serialize
237 * @param buf buffer to serialize element into
239 static void serialize(const DOMNode* n, std::string& buf);
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.
245 * @param n node to serialize
246 * @param out stream to serialize element into
247 * @return reference to output stream
249 static std::ostream& serialize(const DOMNode* n, std::ostream& out);
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.
256 * @param n node to serialize
257 * @param ostr stream to serialize element into
258 * @return reference to output stream
260 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const DOMNode& n);
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.
266 * <p>The marshaller operation takes no parameters.
268 * @param obj object to serialize
269 * @param ostr stream to serialize object into
270 * @return reference to output stream
272 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLObject& obj);
275 #endif /* __xmltooling_xmlhelper_h__ */