2 * Copyright 2001-2006 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
20 * A helper class for working with W3C DOM objects.
\r
23 #ifndef __xmltooling_xmlhelper_h__
\r
24 #define __xmltooling_xmlhelper_h__
\r
26 #include <xmltooling/QName.h>
\r
27 #include <xercesc/dom/DOM.hpp>
\r
29 using namespace xercesc;
\r
31 namespace xmltooling {
\r
34 * RAII wrapper for Xerces resources.
\r
36 template<class T> class XercesJanitor
\r
38 MAKE_NONCOPYABLE(XercesJanitor);
\r
41 XercesJanitor(T* resource) : m_held(resource) {}
\r
49 * Returns resource held by this object and releases it to the caller.
\r
51 * @return the resource held or NULL
\r
61 * A helper class for working with W3C DOM objects.
\r
63 class XMLTOOL_API XMLHelper
\r
67 * Checks if the given element has an xsi:type defined for it
\r
69 * @param e the DOM element
\r
70 * @return true if there is a type, false if not
\r
72 static bool hasXSIType(const DOMElement* e);
\r
75 * Gets the XSI type for a given element if it has one.
\r
77 * @param e the element
\r
78 * @return the type or null
\r
80 static QName* getXSIType(const DOMElement* e);
\r
83 * Gets the ID attribute of a DOM element.
\r
85 * @param domElement the DOM element
\r
86 * @return the ID attribute or null if there isn't one
\r
88 static DOMAttr* getIdAttribute(const DOMElement* domElement);
\r
91 * Gets the QName for the given DOM node.
\r
93 * @param domNode the DOM node
\r
94 * @return the QName for the element or null if the element was null
\r
96 static QName* getNodeQName(const DOMNode* domNode);
\r
99 * Constructs a QName from an attributes value.
\r
101 * @param attribute the attribute with a QName value
\r
102 * @return a QName from an attributes value, or null if the given attribute is null
\r
104 static QName* getAttributeValueAsQName(const DOMAttr* attribute);
\r
107 * Appends the child Element to the parent Element,
\r
108 * importing the child Element into the parent's Document if needed.
\r
110 * @param parentElement the parent Element
\r
111 * @param childElement the child Element
\r
112 * @return the child Element that was added (may be an imported copy)
\r
114 static DOMElement* appendChildElement(DOMElement* parentElement, DOMElement* childElement);
\r
117 * Checks the qualified name of a node.
\r
119 * @param n node to check
\r
120 * @param ns namespace to compare with
\r
121 * @param local local name to compare with
\r
122 * @return true iff the node's qualified name matches the other parameters
\r
124 static bool isNodeNamed(const DOMNode* n, const XMLCh* ns, const XMLCh* local) {
\r
125 return (n && XMLString::equals(local,n->getLocalName()) && XMLString::equals(ns,n->getNamespaceURI()));
\r
129 * Returns the first matching child element of the node if any.
\r
131 * @param n node to check
\r
132 * @param localName local name to compare with or NULL for any match
\r
133 * @return the first matching child node of type Element, or NULL
\r
135 static DOMElement* getFirstChildElement(const DOMNode* n, const XMLCh* localName=NULL);
\r
138 * Returns the last matching child element of the node if any.
\r
140 * @param n node to check
\r
141 * @param localName local name to compare with or NULL for any match
\r
142 * @return the last matching child node of type Element, or NULL
\r
144 static DOMElement* getLastChildElement(const DOMNode* n, const XMLCh* localName=NULL);
\r
147 * Returns the next matching sibling element of the node if any.
\r
149 * @param n node to check
\r
150 * @param localName local name to compare with or NULL for any match
\r
151 * @return the next matching sibling node of type Element, or NULL
\r
153 static DOMElement* getNextSiblingElement(const DOMNode* n, const XMLCh* localName=NULL);
\r
156 * Returns the previous matching sibling element of the node if any.
\r
158 * @param n node to check
\r
159 * @param localName local name to compare with or NULL for any match
\r
160 * @return the previous matching sibling node of type Element, or NULL
\r
162 static DOMElement* getPreviousSiblingElement(const DOMNode* n, const XMLCh* localName=NULL);
\r
165 * Returns the first matching child element of the node if any.
\r
167 * @param n node to check
\r
168 * @param ns namespace to compare with
\r
169 * @param localName local name to compare with
\r
170 * @return the first matching child node of type Element, or NULL
\r
172 static DOMElement* getFirstChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
\r
175 * Returns the last matching child element of the node if any.
\r
177 * @param n node to check
\r
178 * @param ns namespace to compare with
\r
179 * @param localName local name to compare with
\r
180 * @return the last matching child node of type Element, or NULL
\r
182 static DOMElement* getLastChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
\r
185 * Returns the next matching sibling element of the node if any.
\r
187 * @param n node to check
\r
188 * @param ns namespace to compare with
\r
189 * @param localName local name to compare with
\r
190 * @return the next matching sibling node of type Element, or NULL
\r
192 static DOMElement* getNextSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
\r
195 * Returns the previous matching sibling element of the node if any.
\r
197 * @param n node to check
\r
198 * @param ns namespace to compare with
\r
199 * @param localName local name to compare with
\r
200 * @return the previous matching sibling node of type Element, or NULL
\r
202 static DOMElement* getPreviousSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName);
\r
205 * Returns the content of the first Text node found in the element, if any.
\r
206 * This is roughly similar to the DOM getTextContent function, but only
\r
207 * examples the immediate children of the element.
\r
209 * @param e element to examine
\r
210 * @return the content of the first Text node found, or NULL
\r
212 static const XMLCh* getTextContent(const DOMElement* e);
\r
215 * Serializes the DOM Element provided into a buffer using UTF-8 encoding and
\r
216 * the default XML serializer available. No manipulation or formatting is applied.
\r
218 * @param e element to serialize
\r
219 * @param buf buffer to serialize element into
\r
221 static void serialize(const DOMElement* e, std::string& buf);
\r
226 #endif /* __xmltooling_xmlhelper_h__ */
\r