?? namespacestrategy.cpp
字號:
//
// NamespaceStrategy.cpp
//
// $Id: //poco/Main/XML/src/NamespaceStrategy.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 "XML/NamespaceStrategy.h"
#include "SAX/AttributesImpl.h"
#include "SAX/ContentHandler.h"
#include "XML/XMLException.h"
#include "XML/Name.h"
XML_BEGIN
const XMLString NamespaceStrategy::NOTHING;
const XMLString NamespaceStrategy::CDATA = toXMLString("CDATA");
const XMLString NamespaceStrategy::COLON = toXMLString(":");
NamespaceStrategy::~NamespaceStrategy()
{
}
void NamespaceStrategy::splitName(const XMLChar* qname, XMLString& uri, XMLString& localName)
{
for (const XMLChar* p = qname; *p; ++p)
{
if (*p == '\t')
{
uri.assign(qname, p - qname);
localName.assign(p + 1);
return;
}
}
localName = qname;
}
NoNamespacesStrategy::NoNamespacesStrategy()
{
}
NoNamespacesStrategy::~NoNamespacesStrategy()
{
}
void NoNamespacesStrategy::startElement(const XMLChar* name, const XMLChar** atts, int specifiedCount, ContentHandler* pContentHandler)
{
poco_assert_dbg (name && atts && pContentHandler);
AttributesImpl attributes;
for (int i = 0; *atts; ++i)
{
const XMLChar* attrName = *atts++;
const XMLChar* attrValue = *atts++;
attributes.addAttribute(NOTHING, NOTHING, attrName, CDATA, attrValue, i < specifiedCount);
}
pContentHandler->startElement(NOTHING, NOTHING, name, attributes);
}
void NoNamespacesStrategy::endElement(const XMLChar* name, ContentHandler* pContentHandler)
{
poco_assert_dbg (name && pContentHandler);
pContentHandler->endElement(NOTHING, NOTHING, name);
}
void NoNamespacesStrategy::startPrefixMapping(const XMLChar* prefix, const XMLChar* uri, ContentHandler* pContentHandler)
{
}
void NoNamespacesStrategy::endPrefixMapping(const XMLChar* prefix, ContentHandler* pContentHandler)
{
}
NoNamespacePrefixesStrategy::NoNamespacePrefixesStrategy()
{
}
NoNamespacePrefixesStrategy::~NoNamespacePrefixesStrategy()
{
}
void NoNamespacePrefixesStrategy::startElement(const XMLChar* name, const XMLChar** atts, int specifiedCount, ContentHandler* pContentHandler)
{
poco_assert_dbg (name && atts && pContentHandler);
AttributesImpl attributes;
for (int i = 0; *atts; ++i)
{
const XMLChar* attrName = *atts++;
const XMLChar* attrValue = *atts++;
XMLString attrURI;
XMLString attrLocal;
splitName(attrName, attrURI, attrLocal);
attributes.addAttribute(attrURI, attrLocal, NOTHING, CDATA, attrValue, i < specifiedCount);
}
XMLString uri;
XMLString local;
splitName(name, uri, local);
pContentHandler->startElement(uri, local, NOTHING, attributes);
}
void NoNamespacePrefixesStrategy::endElement(const XMLChar* name, ContentHandler* pContentHandler)
{
poco_assert_dbg (name && pContentHandler);
XMLString uri;
XMLString local;
splitName(name, uri, local);
pContentHandler->endElement(uri, local, NOTHING);
}
void NoNamespacePrefixesStrategy::startPrefixMapping(const XMLChar* prefix, const XMLChar* uri, ContentHandler* pContentHandler)
{
poco_assert_dbg (pContentHandler);
pContentHandler->startPrefixMapping((prefix ? XMLString(prefix) : NOTHING), (uri ? XMLString(uri) : NOTHING));
}
void NoNamespacePrefixesStrategy::endPrefixMapping(const XMLChar* prefix, ContentHandler* pContentHandler)
{
poco_assert_dbg (pContentHandler);
pContentHandler->endPrefixMapping(prefix ? XMLString(prefix) : NOTHING);
}
const XMLString NamespacePrefixesStrategy::XMLNS = toXMLString("xmlns");
NamespacePrefixesStrategy::NamespacePrefixesStrategy()
{
}
NamespacePrefixesStrategy::~NamespacePrefixesStrategy()
{
}
void NamespacePrefixesStrategy::startElement(const XMLChar* name, const XMLChar** atts, int specifiedCount, ContentHandler* pContentHandler)
{
poco_assert_dbg (name && atts && pContentHandler);
_namespaces.pushContext();
for (const XMLChar** p = atts; *p;)
{
XMLString qname = *p++;
XMLString value = *p++;
XMLString prefix;
XMLString localName;
XMLString namespaceURI;
Name::split(qname, prefix, localName);
if (localName == XMLNS)
declarePrefix(NOTHING, value, pContentHandler);
else if (prefix == XMLNS)
declarePrefix(localName, value, pContentHandler);
}
AttributesImpl attributes;
int i = 0;
for (const XMLChar** p = atts; *p; ++i)
{
XMLString qname = *p++;
XMLString value = *p++;
XMLString localName;
XMLString namespaceURI;
if (!_namespaces.processName(qname, namespaceURI, localName, true))
throw XMLException("Undeclared namespace prefix", qname);
attributes.addAttribute(namespaceURI, localName, qname, CDATA, value, i < specifiedCount);
}
XMLString qname = name;
XMLString namespaceURI;
XMLString localName;
if (!_namespaces.processName(qname, namespaceURI, localName, false))
throw XMLException("Undeclared namespace prefix", qname);
pContentHandler->startElement(namespaceURI, localName, qname, attributes);
}
void NamespacePrefixesStrategy::endElement(const XMLChar* name, ContentHandler* pContentHandler)
{
poco_assert_dbg (name && pContentHandler);
XMLString qname = name;
XMLString namespaceURI;
XMLString localName;
if (!_namespaces.processName(qname, namespaceURI, localName, false))
throw XMLException("Undeclared namespace prefix", qname);
pContentHandler->endElement(namespaceURI, localName, qname);
std::set<XMLString> prefixes;
_namespaces.getDeclaredPrefixes(prefixes);
for (std::set<XMLString>::const_iterator it = prefixes.begin(); it != prefixes.end(); ++it)
pContentHandler->endPrefixMapping(*it);
_namespaces.popContext();
}
void NamespacePrefixesStrategy::startPrefixMapping(const XMLChar* prefix, const XMLChar* uri, ContentHandler* pContentHandler)
{
}
void NamespacePrefixesStrategy::endPrefixMapping(const XMLChar* prefix, ContentHandler* pContentHandler)
{
}
void NamespacePrefixesStrategy::declarePrefix(const XMLString& prefix, const XMLString& namespaceURI, ContentHandler* pContentHandler)
{
_namespaces.declarePrefix(prefix, namespaceURI);
pContentHandler->startPrefixMapping(prefix, namespaceURI);
}
XML_END
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -