From: cantor Date: Tue, 19 Jan 2010 20:00:13 +0000 (+0000) Subject: Add method to compute non-visibly used prefixes in a tree. X-Git-Tag: 1.4.1~126 X-Git-Url: http://www.project-moonshot.org/gitweb/?p=shibboleth%2Fxmltooling.git;a=commitdiff_plain;h=f77e468810dc1693ef396c03168e44d2768ec397 Add method to compute non-visibly used prefixes in a tree. git-svn-id: https://svn.middleware.georgetown.edu/cpp-xmltooling/branches/REL_1@712 de75baf8-a10c-0410-a50a-987c0e22f00f --- diff --git a/xmltooling/util/XMLHelper.cpp b/xmltooling/util/XMLHelper.cpp index 73e6681..ee502a4 100644 --- a/xmltooling/util/XMLHelper.cpp +++ b/xmltooling/util/XMLHelper.cpp @@ -38,13 +38,7 @@ static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull }; bool XMLHelper::hasXSIType(const DOMElement* e) { - if (e) { - if (e->hasAttributeNS(xmlconstants::XSI_NS, type)) { - return true; - } - } - - return false; + return (e && e->hasAttributeNS(xmlconstants::XSI_NS, type)); } xmltooling::QName* XMLHelper::getXSIType(const DOMElement* e) @@ -125,6 +119,33 @@ XMLObject* XMLHelper::getXMLObjectById(XMLObject& tree, const XMLCh* id) return NULL; } +void XMLHelper::getNonVisiblyUsedPrefixes(const XMLObject& tree, set& prefixes) +{ + set child_prefixes; + const list& children = tree.getOrderedChildren(); + for (list::const_iterator i = children.begin(); i != children.end(); ++i) { + if (*i) + getNonVisiblyUsedPrefixes(*(*i), child_prefixes); + } + const set& nsset = tree.getNamespaces(); + for (set::const_iterator ns = nsset.begin(); ns != nsset.end(); ++ns) { + // Check for xmlns:xml. + if (XMLString::equals(ns->getNamespacePrefix(), xmlconstants::XML_PREFIX) && XMLString::equals(ns->getNamespaceURI(), xmlconstants::XML_NS)) + continue; + switch (ns->usage()) { + case Namespace::Indeterminate: + break; + case Namespace::VisiblyUsed: + child_prefixes.erase(ns->getNamespacePrefix() ? ns->getNamespacePrefix() : &chNull); + break; + case Namespace::NonVisiblyUsed: + prefixes.insert(ns->getNamespacePrefix() ? ns->getNamespacePrefix() : &chNull); + break; + } + } + prefixes.insert(child_prefixes.begin(), child_prefixes.end()); +} + xmltooling::QName* XMLHelper::getNodeQName(const DOMNode* domNode) { if (domNode) diff --git a/xmltooling/util/XMLHelper.h b/xmltooling/util/XMLHelper.h index 98dff81..9fe1e62 100644 --- a/xmltooling/util/XMLHelper.h +++ b/xmltooling/util/XMLHelper.h @@ -1,5 +1,5 @@ /* - * Copyright 2001-2009 Internet2 + * Copyright 2001-2010 Internet2 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ #include +#include #include #include @@ -134,6 +135,14 @@ namespace xmltooling { static XMLObject* getXMLObjectById(XMLObject& tree, const XMLCh* id); /** + * Returns a list of non-visibly-used namespace prefixes found in a tree. + * + * @param tree root of tree to search + * @param prefixes container to store prefix list + */ + static void getNonVisiblyUsedPrefixes(const XMLObject& tree, std::set& prefixes); + + /** * Gets the QName for the given DOM node. * * @param domNode the DOM node