2 * Copyright 2001-2006 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.
24 #include "exceptions.h"
25 #include "util/XMLHelper.h"
26 #include "util/XMLConstants.h"
28 #include <xercesc/framework/MemBufFormatTarget.hpp>
29 #include <xercesc/util/XMLUniDefs.hpp>
31 using namespace xmltooling;
33 static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };
35 bool XMLHelper::hasXSIType(const DOMElement* e)
38 if (e->hasAttributeNS(xmlconstants::XSI_NS, type)) {
46 QName* XMLHelper::getXSIType(const DOMElement* e)
48 DOMAttr* attribute = e->getAttributeNodeNS(xmlconstants::XSI_NS, type);
50 const XMLCh* attributeValue = attribute->getTextContent();
51 if (attributeValue && *attributeValue) {
53 if ((i=XMLString::indexOf(attributeValue,chColon))>0) {
54 XMLCh* prefix=new XMLCh[i+1];
55 XMLString::subString(prefix,attributeValue,0,i);
57 QName* ret=new QName(e->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
62 return new QName(e->lookupNamespaceURI(&chNull), attributeValue);
70 DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement)
72 if(!domElement->hasAttributes()) {
76 DOMNamedNodeMap* attributes = domElement->getAttributes();
78 for(XMLSize_t i = 0; i < attributes->getLength(); i++) {
79 attribute = static_cast<DOMAttr*>(attributes->item(i));
80 if(attribute->isId()) {
88 QName* XMLHelper::getNodeQName(const DOMNode* domNode)
91 return new QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());
95 QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)
101 const XMLCh* attributeValue=attribute->getTextContent();
102 if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) {
103 XMLCh* prefix=new XMLCh[i+1];
104 XMLString::subString(prefix,attributeValue,0,i);
106 QName* ret=new QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
111 return new QName(attribute->lookupNamespaceURI(NULL), attributeValue);
114 DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)
116 DOMDocument* parentDocument = parentElement->getOwnerDocument();
117 if (childElement->getOwnerDocument() != parentDocument) {
118 childElement = static_cast<DOMElement*>(parentDocument->importNode(childElement, true));
121 parentElement->appendChild(childElement);
125 const XMLCh* XMLHelper::getTextContent(const DOMElement* e)
127 DOMNode* child=e->getFirstChild();
129 if (child->getNodeType()==DOMNode::TEXT_NODE)
130 return child->getNodeValue();
131 child=child->getNextSibling();
136 DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* localName)
138 DOMNode* child = n->getFirstChild();
139 while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)
140 child = child->getNextSibling();
141 if (child && localName) {
142 if (!XMLString::equals(localName,child->getLocalName()))
143 return getNextSiblingElement(child, localName);
145 return static_cast<DOMElement*>(child);
148 DOMElement* XMLHelper::getLastChildElement(const DOMNode* n, const XMLCh* localName)
150 DOMNode* child = n->getLastChild();
151 while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)
152 child = child->getPreviousSibling();
153 if (child && localName) {
154 if (!XMLString::equals(localName,child->getLocalName()))
155 return getPreviousSiblingElement(child, localName);
157 return static_cast<DOMElement*>(child);
160 DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
162 DOMElement* e = getFirstChildElement(n, localName);
163 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
164 e = getNextSiblingElement(e, localName);
168 DOMElement* XMLHelper::getLastChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
170 DOMElement* e = getLastChildElement(n, localName);
171 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
172 e = getPreviousSiblingElement(e, localName);
176 DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n, const XMLCh* localName)
178 DOMNode* sib = n->getNextSibling();
179 while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)
180 sib = sib->getNextSibling();
181 if (sib && localName) {
182 if (!XMLString::equals(localName,sib->getLocalName()))
183 return getNextSiblingElement(sib, localName);
185 return static_cast<DOMElement*>(sib);
188 DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* localName)
190 DOMNode* sib = n->getPreviousSibling();
191 while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)
192 sib = sib->getPreviousSibling();
193 if (sib && localName) {
194 if (!XMLString::equals(localName,sib->getLocalName()))
195 return getPreviousSiblingElement(sib, localName);
197 return static_cast<DOMElement*>(sib);
200 DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
202 DOMElement* e = getNextSiblingElement(n, localName);
203 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
204 e = getNextSiblingElement(e, localName);
208 DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
210 DOMElement* e = getPreviousSiblingElement(n, localName);
211 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
212 e = getPreviousSiblingElement(e, localName);
216 void XMLHelper::serialize(const DOMElement* e, std::string& buf)
218 static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
219 static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
220 DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
221 DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
222 XercesJanitor<DOMWriter> janitor(serializer);
223 serializer->setEncoding(UTF8);
224 MemBufFormatTarget target;
225 if (!serializer->writeNode(&target,*e))
226 throw XMLParserException("unable to serialize XML");
228 buf.append(reinterpret_cast<const char*>(target.getRawBuffer()),target.getLen());