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.
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/XMLObject.h>
27 #include <xercesc/dom/DOM.hpp>
31 namespace xmltooling {
34 * RAII wrapper for Xerces resources.
36 template<class T> class XercesJanitor
38 MAKE_NONCOPYABLE(XercesJanitor);
44 * @param resource object to release when leaving scope
46 XercesJanitor(T* resource) : m_held(resource) {}
54 * Returns resource held by this object.
56 * @return the resource held or NULL
63 * Returns resource held by this object.
65 * @return the resource held or NULL
72 * Returns resource held by this object and releases it to the caller.
74 * @return the resource held or NULL
84 * A helper class for working with W3C DOM objects.
86 class XMLTOOL_API XMLHelper
90 * Checks if the given element has an xsi:type defined for it
92 * @param e the DOM element
93 * @return true if there is a type, false if not
95 static bool hasXSIType(const xercesc::DOMElement* e);
98 * Gets the XSI type for a given element if it has one.
100 * @param e the element
101 * @return the type or null
103 static QName* getXSIType(const xercesc::DOMElement* e);
106 * Gets the ID attribute of a DOM element.
108 * @param domElement the DOM element
109 * @return the ID attribute or null if there isn't one
111 static xercesc::DOMAttr* getIdAttribute(const xercesc::DOMElement* domElement);
114 * Attempts to locate an XMLObject from this point downward in the tree whose
115 * XML ID matches the supplied value.
117 * @param tree root of tree to search
118 * @param id ID value to locate
119 * @return XMLObject in the tree with a matching ID value, or NULL
121 static const XMLObject* getXMLObjectById(const XMLObject& tree, const XMLCh* id);
124 * Attempts to locate an XMLObject from this point downward in the tree whose
125 * XML ID matches the supplied value.
127 * @param tree root of tree to search
128 * @param id ID value to locate
129 * @return XMLObject in the tree with a matching ID value, or NULL
131 static XMLObject* getXMLObjectById(XMLObject& tree, const XMLCh* id);
134 * Gets the QName for the given DOM node.
136 * @param domNode the DOM node
137 * @return the QName for the element or null if the element was null
139 static QName* getNodeQName(const xercesc::DOMNode* domNode);
142 * Constructs a QName from an attribute's value.
144 * @param attribute the attribute with a QName value
145 * @return a QName from an attribute's value, or null if the given attribute is null
147 static QName* getAttributeValueAsQName(const xercesc::DOMAttr* attribute);
150 * Appends the child Element to the parent Element,
151 * importing the child Element into the parent's Document if needed.
153 * @param parentElement the parent Element
154 * @param childElement the child Element
155 * @return the child Element that was added (may be an imported copy)
157 static xercesc::DOMElement* appendChildElement(xercesc::DOMElement* parentElement, xercesc::DOMElement* childElement);
160 * Checks the qualified name of a node.
162 * @param n node to check
163 * @param ns namespace to compare with
164 * @param local local name to compare with
165 * @return true iff the node's qualified name matches the other parameters
167 static bool isNodeNamed(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* local) {
168 return (n && xercesc::XMLString::equals(local,n->getLocalName()) && xercesc::XMLString::equals(ns,n->getNamespaceURI()));
172 * Returns the first matching child element of the node if any.
174 * @param n node to check
175 * @param localName local name to compare with or NULL for any match
176 * @return the first matching child node of type Element, or NULL
178 static xercesc::DOMElement* getFirstChildElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL);
181 * Returns the last matching child element of the node if any.
183 * @param n node to check
184 * @param localName local name to compare with or NULL for any match
185 * @return the last matching child node of type Element, or NULL
187 static xercesc::DOMElement* getLastChildElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL);
190 * Returns the next matching sibling element of the node if any.
192 * @param n node to check
193 * @param localName local name to compare with or NULL for any match
194 * @return the next matching sibling node of type Element, or NULL
196 static xercesc::DOMElement* getNextSiblingElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL);
199 * Returns the previous matching sibling element of the node if any.
201 * @param n node to check
202 * @param localName local name to compare with or NULL for any match
203 * @return the previous matching sibling node of type Element, or NULL
205 static xercesc::DOMElement* getPreviousSiblingElement(const xercesc::DOMNode* n, const XMLCh* localName=NULL);
208 * Returns the first matching child element of the node if any.
210 * @param n node to check
211 * @param ns namespace to compare with
212 * @param localName local name to compare with
213 * @return the first matching child node of type Element, or NULL
215 static xercesc::DOMElement* getFirstChildElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
218 * Returns the last matching child element of the node if any.
220 * @param n node to check
221 * @param ns namespace to compare with
222 * @param localName local name to compare with
223 * @return the last matching child node of type Element, or NULL
225 static xercesc::DOMElement* getLastChildElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
228 * Returns the next matching sibling element of the node if any.
230 * @param n node to check
231 * @param ns namespace to compare with
232 * @param localName local name to compare with
233 * @return the next matching sibling node of type Element, or NULL
235 static xercesc::DOMElement* getNextSiblingElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
238 * Returns the previous matching sibling element of the node if any.
240 * @param n node to check
241 * @param ns namespace to compare with
242 * @param localName local name to compare with
243 * @return the previous matching sibling node of type Element, or NULL
245 static xercesc::DOMElement* getPreviousSiblingElement(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* localName);
248 * Returns the content of the first Text node found in the element, if any.
249 * This is roughly similar to the DOM getTextContent function, but only
250 * examples the immediate children of the element.
252 * @param e element to examine
253 * @return the content of the first Text node found, or NULL
255 static const XMLCh* getTextContent(const xercesc::DOMElement* e);
258 * Serializes the DOM node provided into a buffer using UTF-8 encoding and
259 * the default XML serializer available. No manipulation or formatting is applied.
261 * @param n node to serialize
262 * @param buf buffer to serialize element into
263 * @param pretty enable pretty printing if supported
265 static void serialize(const xercesc::DOMNode* n, std::string& buf, bool pretty=false);
268 * Serializes the DOM node provided to a stream using UTF-8 encoding and
269 * the default XML serializer available. No manipulation or formatting is applied.
271 * @param n node to serialize
272 * @param out stream to serialize element into
273 * @param pretty enable pretty printing if supported
274 * @return reference to output stream
276 static std::ostream& serialize(const xercesc::DOMNode* n, std::ostream& out, bool pretty=false);
280 * Serializes the DOM node provided to a stream using UTF-8 encoding and
281 * the default XML serializer available. No manipulation or formatting is applied.
283 * @param n node to serialize
284 * @param ostr stream to serialize element into
285 * @return reference to output stream
287 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const xercesc::DOMNode& n);
290 * Marshalls and serializes the XMLObject provided to a stream using UTF-8 encoding and
291 * the default XML serializer available. No manipulation or formatting is applied.
293 * <p>The marshaller operation takes no parameters.
295 * @param obj object to serialize
296 * @param ostr stream to serialize object into
297 * @return reference to output stream
299 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLObject& obj);
302 #endif /* __xmltooling_xmlhelper_h__ */