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;
35 static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };
37 bool XMLHelper::hasXSIType(const DOMElement* e)
40 if (e->hasAttributeNS(xmlconstants::XSI_NS, type)) {
48 QName* XMLHelper::getXSIType(const DOMElement* e)
50 DOMAttr* attribute = e->getAttributeNodeNS(xmlconstants::XSI_NS, type);
52 const XMLCh* attributeValue = attribute->getTextContent();
53 if (attributeValue && *attributeValue) {
55 if ((i=XMLString::indexOf(attributeValue,chColon))>0) {
56 XMLCh* prefix=new XMLCh[i+1];
57 XMLString::subString(prefix,attributeValue,0,i);
59 QName* ret=new QName(e->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
64 return new QName(e->lookupNamespaceURI(&chNull), attributeValue);
72 DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement)
74 if(!domElement->hasAttributes()) {
78 DOMNamedNodeMap* attributes = domElement->getAttributes();
80 for(XMLSize_t i = 0; i < attributes->getLength(); i++) {
81 attribute = static_cast<DOMAttr*>(attributes->item(i));
82 if(attribute->isId()) {
90 const XMLObject* XMLHelper::getXMLObjectById(const XMLObject& tree, const XMLCh* id)
92 if (XMLString::equals(id, tree.getXMLID()))
96 const list<XMLObject*>& children = tree.getOrderedChildren();
97 for (list<XMLObject*>::const_iterator i=children.begin(); i!=children.end(); ++i) {
99 ret = getXMLObjectById(*(*i), id);
108 QName* XMLHelper::getNodeQName(const DOMNode* domNode)
111 return new QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());
115 QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)
121 const XMLCh* attributeValue=attribute->getTextContent();
122 if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) {
123 XMLCh* prefix=new XMLCh[i+1];
124 XMLString::subString(prefix,attributeValue,0,i);
126 QName* ret=new QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
131 return new QName(attribute->lookupNamespaceURI(NULL), attributeValue);
134 DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)
136 DOMDocument* parentDocument = parentElement->getOwnerDocument();
137 if (childElement->getOwnerDocument() != parentDocument) {
138 childElement = static_cast<DOMElement*>(parentDocument->importNode(childElement, true));
141 parentElement->appendChild(childElement);
145 const XMLCh* XMLHelper::getTextContent(const DOMElement* e)
147 DOMNode* child=e->getFirstChild();
149 if (child->getNodeType()==DOMNode::TEXT_NODE)
150 return child->getNodeValue();
151 child=child->getNextSibling();
156 DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* localName)
158 DOMNode* child = n->getFirstChild();
159 while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)
160 child = child->getNextSibling();
161 if (child && localName) {
162 if (!XMLString::equals(localName,child->getLocalName()))
163 return getNextSiblingElement(child, localName);
165 return static_cast<DOMElement*>(child);
168 DOMElement* XMLHelper::getLastChildElement(const DOMNode* n, const XMLCh* localName)
170 DOMNode* child = n->getLastChild();
171 while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)
172 child = child->getPreviousSibling();
173 if (child && localName) {
174 if (!XMLString::equals(localName,child->getLocalName()))
175 return getPreviousSiblingElement(child, localName);
177 return static_cast<DOMElement*>(child);
180 DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
182 DOMElement* e = getFirstChildElement(n, localName);
183 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
184 e = getNextSiblingElement(e, localName);
188 DOMElement* XMLHelper::getLastChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
190 DOMElement* e = getLastChildElement(n, localName);
191 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
192 e = getPreviousSiblingElement(e, localName);
196 DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n, const XMLCh* localName)
198 DOMNode* sib = n->getNextSibling();
199 while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)
200 sib = sib->getNextSibling();
201 if (sib && localName) {
202 if (!XMLString::equals(localName,sib->getLocalName()))
203 return getNextSiblingElement(sib, localName);
205 return static_cast<DOMElement*>(sib);
208 DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* localName)
210 DOMNode* sib = n->getPreviousSibling();
211 while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)
212 sib = sib->getPreviousSibling();
213 if (sib && localName) {
214 if (!XMLString::equals(localName,sib->getLocalName()))
215 return getPreviousSiblingElement(sib, localName);
217 return static_cast<DOMElement*>(sib);
220 DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
222 DOMElement* e = getNextSiblingElement(n, localName);
223 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
224 e = getNextSiblingElement(e, localName);
228 DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
230 DOMElement* e = getPreviousSiblingElement(n, localName);
231 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
232 e = getPreviousSiblingElement(e, localName);
236 void XMLHelper::serialize(const DOMNode* n, std::string& buf)
238 static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
239 static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
240 DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
241 DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
242 XercesJanitor<DOMWriter> janitor(serializer);
243 serializer->setEncoding(UTF8);
244 MemBufFormatTarget target;
245 if (!serializer->writeNode(&target,*n))
246 throw XMLParserException("unable to serialize XML");
248 buf.append(reinterpret_cast<const char*>(target.getRawBuffer()),target.getLen());
252 class StreamFormatTarget : public XMLFormatTarget
255 StreamFormatTarget(std::ostream& out) : m_out(out) {}
256 ~StreamFormatTarget() {}
258 void writeChars(const XMLByte *const toWrite, const unsigned int count, XMLFormatter *const formatter) {
259 m_out.write(reinterpret_cast<const char*>(toWrite),count);
271 void XMLHelper::serialize(const DOMNode* n, std::ostream& out)
273 static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
274 static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
275 DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
276 DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
277 XercesJanitor<DOMWriter> janitor(serializer);
278 serializer->setEncoding(UTF8);
279 StreamFormatTarget target(out);
280 if (!serializer->writeNode(&target,*n))
281 throw XMLParserException("unable to serialize XML");
284 ostream& xmltooling::operator<<(ostream& ostr, const DOMNode& node)
286 XMLHelper::serialize(&node, ostr);
290 ostream& xmltooling::operator<<(ostream& ostr, const XMLObject& obj)
292 return ostr << obj.marshall();