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