namespacesupport.cpp

来自「This software aims to create an applet a」· C++ 代码 · 共 218 行

CPP
218
字号
//
// NamespaceSupport.cpp
//
// $Id: //poco/Main/XML/src/NamespaceSupport.cpp#5 $
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
//    how to obtain complete source code for this software and any
//    accompanying software that uses this software.  The source code
//    must either be included in the distribution or be available for no
//    more than the cost of distribution plus a nominal fee, and must be
//    freely redistributable under reasonable conditions.  For an
//    executable file, complete source code means the source code for all
//    modules it contains.  It does not include source code for modules or
//    files that typically accompany the major components of the operating
//    system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//


#include "SAX/NamespaceSupport.h"
#include "XML/Name.h"


XML_BEGIN


const XMLString NamespaceSupport::EMPTY_STRING;
const XMLString NamespaceSupport::XML_NAMESPACE          = toXMLString("http://www.w3.org/XML/1998/namespace");
const XMLString NamespaceSupport::XML_NAMESPACE_PREFIX   = toXMLString("xml");
const XMLString NamespaceSupport::XMLNS_NAMESPACE        = toXMLString("http://www.w3.org/xmlns/2000/");
const XMLString NamespaceSupport::XMLNS_NAMESPACE_PREFIX = toXMLString("xmlns");


NamespaceSupport::NamespaceSupport()
{
	reset();
}


NamespaceSupport::~NamespaceSupport()
{
}


bool NamespaceSupport::declarePrefix(const XMLString& prefix, const XMLString& namespaceURI)
{
	poco_assert (_contexts.size() > 0);

	Context& ctx = _contexts.back();
	if (ctx.find(prefix) == ctx.end())
	{
		ctx.insert(Context::value_type(prefix, namespaceURI));
		return true;
	}
	else return false;
}


bool NamespaceSupport::undeclarePrefix(const XMLString& prefix)
{
	for (ContextVec::reverse_iterator rit = _contexts.rbegin(); rit != _contexts.rend(); ++rit)
	{
		Context::iterator it = rit->find(prefix);
		if (it != rit->end())
		{
			rit->erase(it);
			return true;
		}
	}
	return false;
}


void NamespaceSupport::getDeclaredPrefixes(PrefixSet& prefixes) const
{
	prefixes.clear();
	const Context& ctx = _contexts.back();
	for (Context::const_iterator it = ctx.begin(); it != ctx.end(); ++it)
		prefixes.insert(it->first);
}


const XMLString& NamespaceSupport::getPrefix(const XMLString& namespaceURI) const
{
	for (ContextVec::const_reverse_iterator rit = _contexts.rbegin(); rit != _contexts.rend(); ++rit)
	{
		for (Context::const_iterator it = rit->begin(); it != rit->end(); ++it)
		{
			if (it->second == namespaceURI) 
				return it->first;
		}
	}
	return EMPTY_STRING;
}


bool NamespaceSupport::isMapped(const XMLString& namespaceURI) const
{
	for (ContextVec::const_reverse_iterator rit = _contexts.rbegin(); rit != _contexts.rend(); ++rit)
	{
		for (Context::const_iterator it = rit->begin(); it != rit->end(); ++it)
		{
			if (it->second == namespaceURI) 
				return true;
		}
	}
	return false;
}


void NamespaceSupport::getPrefixes(PrefixSet& prefixes) const
{
	prefixes.clear();
	for (ContextVec::const_reverse_iterator rit = _contexts.rbegin(); rit != _contexts.rend(); ++rit)
	{
		for (Context::const_iterator it = rit->begin(); it != rit->end(); ++it)
		{
			const XMLString& prefix = it->first;
			if (!prefix.empty() && prefixes.find(prefix) == prefixes.end()) 
				prefixes.insert(it->first);
		}
	}
}


void NamespaceSupport::getPrefixes(const XMLString& namespaceURI, PrefixSet& prefixes) const
{
	prefixes.clear();
	for (ContextVec::const_reverse_iterator rit = _contexts.rbegin(); rit != _contexts.rend(); ++rit)
	{
		for (Context::const_iterator it = rit->begin(); it != rit->end(); ++it)
		{
			const XMLString& prefix = it->first;
			if (it->second == namespaceURI && !prefix.empty() && prefixes.find(prefix) == prefixes.end()) 
				prefixes.insert(it->first);
		}
	}
}


const XMLString& NamespaceSupport::getURI(const XMLString& prefix) const
{
	for (ContextVec::const_reverse_iterator rit = _contexts.rbegin(); rit != _contexts.rend(); ++rit)
	{
		Context::const_iterator it = rit->find(prefix);
		if (it != rit->end()) 
			return it->second;
	}
	return EMPTY_STRING;
}


void NamespaceSupport::pushContext()
{
	_contexts.push_back(Context());
}


void NamespaceSupport::popContext()
{
	_contexts.pop_back();
}


bool NamespaceSupport::processName(const XMLString& qname, XMLString& namespaceURI, XMLString& localName, bool isAttribute) const
{
	XMLString prefix;
	Name::split(qname, prefix, localName);
	if (prefix.empty() && isAttribute)
	{
		namespaceURI.clear();
		return true;
	}
	else
	{
		namespaceURI = getURI(prefix);
		return !namespaceURI.empty() || prefix.empty();
	}
}


void NamespaceSupport::reset()
{
	_contexts.clear();
	pushContext();
	declarePrefix(XML_NAMESPACE_PREFIX, XML_NAMESPACE);
	declarePrefix(XMLNS_NAMESPACE_PREFIX, XMLNS_NAMESPACE);
}


XML_END

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?