2 * Copyright 2001-2010 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"
26 #include "XMLObject.h"
27 #include "util/XMLHelper.h"
28 #include "util/XMLConstants.h"
30 #include <xercesc/framework/MemBufFormatTarget.hpp>
31 #include <xercesc/util/XMLUniDefs.hpp>
33 using namespace xmltooling;
34 using namespace xercesc;
37 static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };
39 bool XMLHelper::hasXSIType(const DOMElement* e)
41 return (e && e->hasAttributeNS(xmlconstants::XSI_NS, type));
44 xmltooling::QName* XMLHelper::getXSIType(const DOMElement* e)
46 DOMAttr* attribute = e->getAttributeNodeNS(xmlconstants::XSI_NS, type);
48 const XMLCh* attributeValue = attribute->getTextContent();
49 if (attributeValue && *attributeValue) {
51 if ((i=XMLString::indexOf(attributeValue,chColon))>0) {
52 XMLCh* prefix=new XMLCh[i+1];
53 XMLString::subString(prefix,attributeValue,0,i);
55 xmltooling::QName* ret=new xmltooling::QName(e->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
60 return new xmltooling::QName(e->lookupNamespaceURI(nullptr), attributeValue);
68 DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement)
70 if(!domElement->hasAttributes()) {
74 DOMNamedNodeMap* attributes = domElement->getAttributes();
76 for(XMLSize_t i = 0; i < attributes->getLength(); i++) {
77 attribute = static_cast<DOMAttr*>(attributes->item(i));
78 if(attribute->isId()) {
86 const XMLObject* XMLHelper::getXMLObjectById(const XMLObject& tree, const XMLCh* id)
88 if (XMLString::equals(id, tree.getXMLID()))
92 const list<XMLObject*>& children = tree.getOrderedChildren();
93 for (list<XMLObject*>::const_iterator i=children.begin(); i!=children.end(); ++i) {
95 ret = getXMLObjectById(*(*i), id);
104 XMLObject* XMLHelper::getXMLObjectById(XMLObject& tree, const XMLCh* id)
106 if (XMLString::equals(id, tree.getXMLID()))
110 const list<XMLObject*>& children = tree.getOrderedChildren();
111 for (list<XMLObject*>::const_iterator i=children.begin(); i!=children.end(); ++i) {
113 ret = getXMLObjectById(*(*i), id);
122 void XMLHelper::getNonVisiblyUsedPrefixes(const XMLObject& tree, set<xstring>& prefixes)
124 set<xstring> child_prefixes;
125 const list<XMLObject*>& children = tree.getOrderedChildren();
126 for (list<XMLObject*>::const_iterator i = children.begin(); i != children.end(); ++i) {
128 getNonVisiblyUsedPrefixes(*(*i), child_prefixes);
130 const set<Namespace>& nsset = tree.getNamespaces();
131 for (set<Namespace>::const_iterator ns = nsset.begin(); ns != nsset.end(); ++ns) {
132 // Check for xmlns:xml.
133 if (XMLString::equals(ns->getNamespacePrefix(), xmlconstants::XML_PREFIX) && XMLString::equals(ns->getNamespaceURI(), xmlconstants::XML_NS))
135 switch (ns->usage()) {
136 case Namespace::Indeterminate:
138 case Namespace::VisiblyUsed:
139 child_prefixes.erase(ns->getNamespacePrefix() ? ns->getNamespacePrefix() : &chNull);
141 case Namespace::NonVisiblyUsed:
142 prefixes.insert(ns->getNamespacePrefix() ? ns->getNamespacePrefix() : &chNull);
146 prefixes.insert(child_prefixes.begin(), child_prefixes.end());
149 xmltooling::QName* XMLHelper::getNodeQName(const DOMNode* domNode)
152 return new xmltooling::QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());
156 xmltooling::QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)
158 return getNodeValueAsQName(attribute);
161 xmltooling::QName* XMLHelper::getNodeValueAsQName(const DOMNode* domNode)
167 const XMLCh* value=domNode->getTextContent();
168 if (value && (i=XMLString::indexOf(value,chColon))>0) {
169 XMLCh* prefix=new XMLCh[i+1];
170 XMLString::subString(prefix,value,0,i);
172 xmltooling::QName* ret=new xmltooling::QName(domNode->lookupNamespaceURI(prefix), value + i + 1, prefix);
177 return new xmltooling::QName(domNode->lookupNamespaceURI(nullptr), value);
180 bool XMLHelper::getNodeValueAsBool(const xercesc::DOMNode* domNode, bool def)
184 const XMLCh* value = domNode->getNodeValue();
185 if (!value || !*value)
187 if (*value == chLatin_t || *value == chDigit_1)
189 else if (*value == chLatin_f || *value == chDigit_0)
194 DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)
196 DOMDocument* parentDocument = parentElement->getOwnerDocument();
197 if (childElement->getOwnerDocument() != parentDocument) {
198 childElement = static_cast<DOMElement*>(parentDocument->importNode(childElement, true));
201 parentElement->appendChild(childElement);
205 bool XMLHelper::isNodeNamed(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* local)
207 return (n && XMLString::equals(local,n->getLocalName()) && XMLString::equals(ns,n->getNamespaceURI()));
210 const XMLCh* XMLHelper::getTextContent(const DOMElement* e)
212 DOMNode* child=e->getFirstChild();
214 if (child->getNodeType()==DOMNode::TEXT_NODE)
215 return child->getNodeValue();
216 child=child->getNextSibling();
221 DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* localName)
223 DOMNode* child = n->getFirstChild();
224 while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)
225 child = child->getNextSibling();
226 if (child && localName) {
227 if (!XMLString::equals(localName,child->getLocalName()))
228 return getNextSiblingElement(child, localName);
230 return static_cast<DOMElement*>(child);
233 DOMElement* XMLHelper::getLastChildElement(const DOMNode* n, const XMLCh* localName)
235 DOMNode* child = n->getLastChild();
236 while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)
237 child = child->getPreviousSibling();
238 if (child && localName) {
239 if (!XMLString::equals(localName,child->getLocalName()))
240 return getPreviousSiblingElement(child, localName);
242 return static_cast<DOMElement*>(child);
245 DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
247 DOMElement* e = getFirstChildElement(n, localName);
248 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
249 e = getNextSiblingElement(e, localName);
253 DOMElement* XMLHelper::getLastChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
255 DOMElement* e = getLastChildElement(n, localName);
256 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
257 e = getPreviousSiblingElement(e, localName);
261 DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n, const XMLCh* localName)
263 DOMNode* sib = n->getNextSibling();
264 while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)
265 sib = sib->getNextSibling();
266 if (sib && localName) {
267 if (!XMLString::equals(localName,sib->getLocalName()))
268 return getNextSiblingElement(sib, localName);
270 return static_cast<DOMElement*>(sib);
273 DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* localName)
275 DOMNode* sib = n->getPreviousSibling();
276 while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)
277 sib = sib->getPreviousSibling();
278 if (sib && localName) {
279 if (!XMLString::equals(localName,sib->getLocalName()))
280 return getPreviousSiblingElement(sib, localName);
282 return static_cast<DOMElement*>(sib);
285 DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
287 DOMElement* e = getNextSiblingElement(n, localName);
288 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
289 e = getNextSiblingElement(e, localName);
293 DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
295 DOMElement* e = getPreviousSiblingElement(n, localName);
296 while (e && !XMLString::equals(e->getNamespaceURI(),ns))
297 e = getPreviousSiblingElement(e, localName);
301 void XMLHelper::serialize(const DOMNode* n, std::string& buf, bool pretty)
303 static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
304 static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDash, chDigit_8, chNull };
306 MemBufFormatTarget target;
307 DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
309 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
310 DOMLSSerializer* serializer = static_cast<DOMImplementationLS*>(impl)->createLSSerializer();
311 XercesJanitor<DOMLSSerializer> janitor(serializer);
312 if (pretty && serializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
313 serializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
314 DOMLSOutput *theOutput = static_cast<DOMImplementationLS*>(impl)->createLSOutput();
315 XercesJanitor<DOMLSOutput> j_theOutput(theOutput);
316 theOutput->setEncoding(UTF8);
317 theOutput->setByteStream(&target);
318 if (!serializer->write(n, theOutput))
319 throw XMLParserException("unable to serialize XML");
321 DOMWriter* serializer = static_cast<DOMImplementationLS*>(impl)->createDOMWriter();
322 XercesJanitor<DOMWriter> janitor(serializer);
323 serializer->setEncoding(UTF8);
324 if (pretty && serializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
325 serializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
326 if (!serializer->writeNode(&target, *n))
327 throw XMLParserException("unable to serialize XML");
331 buf.append(reinterpret_cast<const char*>(target.getRawBuffer()),target.getLen());
335 class StreamFormatTarget : public XMLFormatTarget
338 StreamFormatTarget(std::ostream& out) : m_out(out) {}
339 ~StreamFormatTarget() {}
341 void writeChars(const XMLByte *const toWrite, const xsecsize_t count, XMLFormatter *const formatter) {
342 m_out.write(reinterpret_cast<const char*>(toWrite),count);
354 ostream& XMLHelper::serialize(const DOMNode* n, ostream& out, bool pretty)
356 static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
357 static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDash, chDigit_8, chNull };
359 StreamFormatTarget target(out);
360 DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
362 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
363 DOMLSSerializer* serializer = static_cast<DOMImplementationLS*>(impl)->createLSSerializer();
364 XercesJanitor<DOMLSSerializer> janitor(serializer);
365 if (pretty && serializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
366 serializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
367 DOMLSOutput *theOutput = static_cast<DOMImplementationLS*>(impl)->createLSOutput();
368 XercesJanitor<DOMLSOutput> j_theOutput(theOutput);
369 theOutput->setEncoding(UTF8);
370 theOutput->setByteStream(&target);
371 if (!serializer->write(n, theOutput))
372 throw XMLParserException("unable to serialize XML");
374 DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
375 XercesJanitor<DOMWriter> janitor(serializer);
376 serializer->setEncoding(UTF8);
377 if (pretty && serializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
378 serializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
379 if (!serializer->writeNode(&target,*n))
380 throw XMLParserException("unable to serialize XML");
386 ostream& xmltooling::operator<<(ostream& ostr, const DOMNode& node)
388 return XMLHelper::serialize(&node, ostr);
391 ostream& xmltooling::operator<<(ostream& ostr, const XMLObject& obj)
393 return ostr << *(obj.marshall());