From 3f34c742109a7773703fbf9beab9d34722586c05 Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Tue, 19 Jan 2010 19:02:32 +0000 Subject: [PATCH] Reimplement namespace visibility as a three-way setting. --- .../AbstractAttributeExtensibleXMLObject.cpp | 5 ++-- xmltooling/AbstractXMLObject.cpp | 23 ++++++++++------ xmltooling/Namespace.cpp | 6 ++-- xmltooling/Namespace.h | 32 ++++++++++++++-------- xmltooling/io/AbstractXMLObjectMarshaller.cpp | 8 +++--- xmltooling/io/AbstractXMLObjectUnmarshaller.cpp | 10 +++---- xmltoolingtest/NonVisibleNamespaceTest.h | 10 +++---- 7 files changed, 55 insertions(+), 39 deletions(-) diff --git a/xmltooling/AbstractAttributeExtensibleXMLObject.cpp b/xmltooling/AbstractAttributeExtensibleXMLObject.cpp index 44b07bc..b7b9ccd 100644 --- a/xmltooling/AbstractAttributeExtensibleXMLObject.cpp +++ b/xmltooling/AbstractAttributeExtensibleXMLObject.cpp @@ -138,7 +138,7 @@ void AbstractAttributeExtensibleXMLObject::setAttribute(const QName& qualifiedNa m_attributeMap[qualifiedName]=XMLString::replicate(value); if (ID) m_idAttribute = m_attributeMap.find(qualifiedName); - Namespace newNamespace(qualifiedName.getNamespaceURI(), qualifiedName.getPrefix()); + Namespace newNamespace(qualifiedName.getNamespaceURI(), qualifiedName.getPrefix(), false, Namespace::VisiblyUsed); addNamespace(newNamespace); } } @@ -157,8 +157,7 @@ void AttributeExtensibleXMLObject::setAttribute(const QName& qualifiedName, cons setAttribute(qualifiedName, value.getLocalPart()); } - // Attach a non-visibly used namespace. - Namespace newNamespace(value.getNamespaceURI(), value.getPrefix(), false, false); + Namespace newNamespace(value.getNamespaceURI(), value.getPrefix(), false, Namespace::NonVisiblyUsed); addNamespace(newNamespace); } diff --git a/xmltooling/AbstractXMLObject.cpp b/xmltooling/AbstractXMLObject.cpp index 9944a96..3f6726a 100644 --- a/xmltooling/AbstractXMLObject.cpp +++ b/xmltooling/AbstractXMLObject.cpp @@ -57,11 +57,10 @@ AbstractXMLObject::AbstractXMLObject(const XMLCh* nsURI, const XMLCh* localName, m_schemaLocation(NULL), m_noNamespaceSchemaLocation(NULL), m_nil(xmlconstants::XML_BOOL_NULL), m_parent(NULL), m_elementQname(nsURI, localName, prefix), m_typeQname(NULL) { - addNamespace(Namespace(nsURI, prefix)); + addNamespace(Namespace(nsURI, prefix, false, Namespace::VisiblyUsed)); if (schemaType) { m_typeQname = new QName(*schemaType); - // Attach a non-visibly used namespace. - addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix(), false, false)); + addNamespace(Namespace(m_typeQname->getNamespaceURI(), m_typeQname->getPrefix(), false, Namespace::NonVisiblyUsed)); } } @@ -137,8 +136,17 @@ void AbstractXMLObject::addNamespace(const Namespace& ns) const else { if (ns.alwaysDeclare()) const_cast(*i).setAlwaysDeclare(true); - if (ns.visiblyUsed()) - const_cast(*i).setVisiblyUsed(true); + switch (ns.usage()) { + case Namespace::Indeterminate: + break; + case Namespace::VisiblyUsed: + const_cast(*i).setUsage(Namespace::VisiblyUsed); + break; + case Namespace::NonVisiblyUsed: + if (i->usage() == Namespace::Indeterminate) + const_cast(*i).setUsage(Namespace::NonVisiblyUsed); + break; + } } } @@ -201,8 +209,7 @@ QName* AbstractXMLObject::prepareForAssignment(QName* oldValue, const QName* new if (!oldValue) { if (newValue) { releaseThisandParentDOM(); - // Attach a non-visibly used namespace. - addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, false)); + addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, Namespace::NonVisiblyUsed)); return new QName(*newValue); } return NULL; @@ -212,7 +219,7 @@ QName* AbstractXMLObject::prepareForAssignment(QName* oldValue, const QName* new releaseThisandParentDOM(); if (newValue) { // Attach a non-visibly used namespace. - addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, false)); + addNamespace(Namespace(newValue->getNamespaceURI(), newValue->getPrefix(), false, Namespace::NonVisiblyUsed)); return new QName(*newValue); } return NULL; diff --git a/xmltooling/Namespace.cpp b/xmltooling/Namespace.cpp index 179579b..ae388db 100644 --- a/xmltooling/Namespace.cpp +++ b/xmltooling/Namespace.cpp @@ -17,7 +17,7 @@ /** * Namespace.cpp * - * Representing XML namespace attributes + * Representing XML namespace attributes. */ #include "internal.h" @@ -27,8 +27,8 @@ using namespace xmltooling; using xercesc::XMLString; -Namespace::Namespace(const XMLCh* uri, const XMLCh* prefix, bool alwaysDeclare, bool visiblyUsed) - : m_pinned(alwaysDeclare), m_visiblyUsed(visiblyUsed) +Namespace::Namespace(const XMLCh* uri, const XMLCh* prefix, bool alwaysDeclare, namespace_usage_t usage) + : m_pinned(alwaysDeclare), m_usage(usage) { setNamespaceURI(uri); setNamespacePrefix(prefix); diff --git a/xmltooling/Namespace.h b/xmltooling/Namespace.h index 8ac6382..d711cdd 100644 --- a/xmltooling/Namespace.h +++ b/xmltooling/Namespace.h @@ -17,7 +17,7 @@ /** * @file xmltooling/Namespace.h * - * Representing XML namespace attributes + * Representing XML namespace attributes. */ #if !defined(__xmltooling_namespace_h__) @@ -33,19 +33,28 @@ namespace xmltooling { #endif /** - * A data structure for encapsulating XML Namespace attributes + * A data structure for encapsulating XML Namespace attributes. */ class XMLTOOL_API Namespace { public: /** + * Tri-state indicator of namespace usage. + */ + enum namespace_usage_t { + Indeterminate, + NonVisiblyUsed, + VisiblyUsed + }; + + /** * Constructor * @param uri namespace URI * @param prefix namespace prefix (without the colon) * @param alwaysDeclare true iff the namespace should always be declared regardless of in-scope declarations - * @param visiblyUsed true iff the namespace is visibly used by an XMLObject its attached to + * @param usage indicates usage of namespace in the context of an XMLObject */ - Namespace(const XMLCh* uri=NULL, const XMLCh* prefix=NULL, bool alwaysDeclare=false, bool visiblyUsed=true); + Namespace(const XMLCh* uri=NULL, const XMLCh* prefix=NULL, bool alwaysDeclare=false, namespace_usage_t usage=Indeterminate); ~Namespace(); @@ -68,10 +77,10 @@ namespace xmltooling { const bool alwaysDeclare() const { return m_pinned; } /** - * Returns true iff the namespace is visibly used by an XMLObject its attached to - * @return the visiblyUsed setting + * Returns the usage of the namespace by an XMLObject + * @return the usage setting */ - const bool visiblyUsed() const { return m_visiblyUsed; } + const namespace_usage_t usage() const { return m_usage; } /** * Sets the namespace prefix @@ -92,13 +101,14 @@ namespace xmltooling { void setAlwaysDeclare(bool alwaysDeclare) { m_pinned = alwaysDeclare; } /** - * Sets the visiblyUsed property - * @param visiblyUsed true iff the namespace is visibly used by an XMLObject its attached to + * Sets the usage property + * @param usage usage of the namespace by an XMLObject */ - void setVisiblyUsed(bool visiblyUsed) { m_visiblyUsed = visiblyUsed; } + void setUsage(namespace_usage_t usage) { m_usage = usage; } private: - bool m_pinned,m_visiblyUsed; + bool m_pinned; + namespace_usage_t m_usage; xstring m_uri; xstring m_prefix; }; diff --git a/xmltooling/io/AbstractXMLObjectMarshaller.cpp b/xmltooling/io/AbstractXMLObjectMarshaller.cpp index 3a6064e..39b515c 100644 --- a/xmltooling/io/AbstractXMLObjectMarshaller.cpp +++ b/xmltooling/io/AbstractXMLObjectMarshaller.cpp @@ -228,8 +228,8 @@ void AbstractXMLObjectMarshaller::marshallInto( targetElement->setAttributeNS(XSI_NS, _nil, xmlconstants::XML_ZERO); break; } - m_log.debug("adding XSI namespace to list of namespaces used by XMLObject"); - addNamespace(Namespace(XSI_NS, XSI_PREFIX)); + m_log.debug("adding XSI namespace to list of namespaces visibly used by XMLObject"); + addNamespace(Namespace(XSI_NS, XSI_PREFIX, false, Namespace::VisiblyUsed)); } marshallElementType(targetElement); @@ -275,8 +275,8 @@ void AbstractXMLObjectMarshaller::marshallElementType(DOMElement* domElement) co if (xsivalue != typeLocalName) XMLString::release(&xsivalue); - m_log.debug("adding XSI namespace to list of namespaces used by XMLObject"); - addNamespace(Namespace(XSI_NS, XSI_PREFIX)); + m_log.debug("adding XSI namespace to list of namespaces visibly used by XMLObject"); + addNamespace(Namespace(XSI_NS, XSI_PREFIX, false, Namespace::VisiblyUsed)); } } diff --git a/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp b/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp index cf58a54..2297b6a 100644 --- a/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp +++ b/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp @@ -106,14 +106,14 @@ void AbstractXMLObjectUnmarshaller::unmarshallAttributes(const DOMElement* domEl if (XMLString::equals(nsuri,XMLNS_NS)) { if (XMLString::equals(attribute->getLocalName(),XMLNS_PREFIX)) { m_log.debug("found default namespace declaration, adding it to the list of namespaces on the XMLObject"); - addNamespace(Namespace(attribute->getValue(), NULL, true, false)); + addNamespace(Namespace(attribute->getValue(), NULL, true)); } else if (XMLString::equals(attribute->getLocalName(),XML_PREFIX) && XMLString::equals(attribute->getNodeValue(),XML_NS)) { m_log.debug("found standard xml prefix declaration, ignoring as superfluous"); } else { m_log.debug("found namespace declaration, adding it to the list of namespaces on the XMLObject"); - addNamespace(Namespace(attribute->getValue(), attribute->getLocalName(), true, false)); + addNamespace(Namespace(attribute->getValue(), attribute->getLocalName(), true)); } continue; } @@ -147,11 +147,11 @@ void AbstractXMLObjectUnmarshaller::unmarshallAttributes(const DOMElement* domEl continue; } // Note that the prefix is visibly used. - addNamespace(Namespace(nsuri, attribute->getPrefix())); + addNamespace(Namespace(nsuri, attribute->getPrefix(), false, Namespace::VisiblyUsed)); } else if (nsuri && !XMLString::equals(nsuri,XML_NS)) { - m_log.debug("found namespace-qualified attribute, adding prefix to the list of namespaces on the XMLObject"); - addNamespace(Namespace(nsuri, attribute->getPrefix())); + m_log.debug("found namespace-qualified attribute, adding prefix to the list of visible namespaces on the XMLObject"); + addNamespace(Namespace(nsuri, attribute->getPrefix(), false, Namespace::VisiblyUsed)); } m_log.debug("processing generic attribute"); diff --git a/xmltoolingtest/NonVisibleNamespaceTest.h b/xmltoolingtest/NonVisibleNamespaceTest.h index a1eeaa7..0d4624e 100644 --- a/xmltoolingtest/NonVisibleNamespaceTest.h +++ b/xmltoolingtest/NonVisibleNamespaceTest.h @@ -53,15 +53,15 @@ public: bool cond1=false, cond2=false, cond3 = false; for (set::const_iterator ns = namespaces.begin(); ns != namespaces.end(); ++ns) { if (XMLString::equals(ns->getNamespacePrefix(), SimpleXMLObject::NAMESPACE_PREFIX)) { - TSM_ASSERT("'test' namespace was visibly used", !ns->visiblyUsed()); + TSM_ASSERT("'test' namespace was visibly used", ns->usage() != Namespace::VisiblyUsed); cond1 = true; } else if (XMLString::equals(ns->getNamespacePrefix(), TEST2_PREFIX)) { - TSM_ASSERT("'test2' namespace was visibly used", !ns->visiblyUsed()); + TSM_ASSERT("'test2' namespace was visibly used", ns->usage() != Namespace::VisiblyUsed); cond2 = true; } else if (XMLString::equals(ns->getNamespacePrefix(), &chNull)) { - TSM_ASSERT("Default namespace was not visibly used", ns->visiblyUsed()); + TSM_ASSERT("Default namespace was not visibly used", ns->usage() == Namespace::VisiblyUsed); cond3 = true; } } @@ -88,11 +88,11 @@ public: bool cond1=false, cond2=false, cond3=false; for (set::const_iterator ns = namespaces.begin(); ns != namespaces.end(); ++ns) { if (XMLString::equals(ns->getNamespacePrefix(), SimpleXMLObject::NAMESPACE_PREFIX)) { - TSM_ASSERT("'test' namespace was visibly used", !ns->visiblyUsed()); + TSM_ASSERT("'test' namespace was visibly used", ns->usage() != Namespace::VisiblyUsed); cond1 = true; } else if (XMLString::equals(ns->getNamespacePrefix(), &chNull)) { - TSM_ASSERT("Default namespace was not visibly used", ns->visiblyUsed()); + TSM_ASSERT("Default namespace was not visibly used", ns->usage() == Namespace::VisiblyUsed); cond2 = true; } } -- 2.1.4