arabica/include/DOM/Simple/AttrNSImpl.hpp

133 lines
4.4 KiB
C++
Raw Normal View History

2002-06-21 13:16:28 +02:00
#ifndef JEZUK_SimpleDOM_ATTRNSIMPL_H
#define JEZUK_SimpleDOM_ATTRNSIMPL_H
2007-09-05 00:55:47 +02:00
#include <DOM/Simple/AttrImpl.hpp>
#include <DOM/Simple/Helpers.hpp>
2002-06-21 13:16:28 +02:00
2007-09-05 13:47:13 +02:00
namespace Arabica
{
2002-06-21 13:16:28 +02:00
namespace SimpleDOM
{
template<class stringT, class string_adaptorT>
class AttrNSImpl : public AttrImpl<stringT, string_adaptorT>
{
public:
typedef typename string_adaptorT::size_type size_type;
typedef AttrImpl<stringT, string_adaptorT> AttrImplT;
typedef DOM::Node_impl<stringT, string_adaptorT> DOMNode_implT;
2002-06-21 13:16:28 +02:00
AttrNSImpl(DocumentImpl<stringT, string_adaptorT>* ownerDoc,
const stringT& namespaceURI,
bool hasNamespaceURI,
const stringT& qualifiedName) :
AttrImplT(ownerDoc, qualifiedName),
prefix_(&ownerDoc->empty_string()),
hasNamespaceURI_(false)
2002-06-21 13:16:28 +02:00
{
bool hasPrefix = false;
2005-12-09 16:20:08 +01:00
stringT const* prefix_for_checking = (&ownerDoc->empty_string());
2005-09-30 23:36:11 +02:00
size_type index = string_adaptorT::find(qualifiedName, string_adaptorT::construct_from_utf8(":"));
2002-06-21 13:16:28 +02:00
2006-06-08 11:51:18 +02:00
if(index == string_adaptorT::npos())
2002-06-21 13:16:28 +02:00
{ //qualifiedName contains no ':'
localName_ = AttrImplT::ownerDoc_->stringPool(qualifiedName);
if(*localName_ == string_adaptorT::construct_from_utf8("xmlns"))
2002-06-21 13:16:28 +02:00
{
hasPrefix = true;
2005-12-09 16:20:08 +01:00
prefix_for_checking = localName_;
2005-09-30 23:36:11 +02:00
} // if ...
2002-06-21 13:16:28 +02:00
}
else
{
hasPrefix = true;
prefix_ = AttrImplT::ownerDoc_->stringPool(string_adaptorT::substr(qualifiedName, 0, index));
localName_ = AttrImplT::ownerDoc_->stringPool(string_adaptorT::substr(qualifiedName, index+1));
2005-12-09 16:20:08 +01:00
prefix_for_checking = prefix_;
2005-09-30 23:36:11 +02:00
} // if(index == string_adaptorT::npos)
2002-06-21 13:16:28 +02:00
std::pair<bool, stringT> mappedURI =
checkPrefixAndNamespace<stringT, string_adaptorT>(hasPrefix, *prefix_for_checking, hasNamespaceURI, namespaceURI, DOM::Node_base::ATTRIBUTE_NODE);
2002-06-21 13:16:28 +02:00
hasNamespaceURI_ = mappedURI.first;
namespaceURI_ = AttrImplT::ownerDoc_->stringPool(mappedURI.second);
2002-06-21 13:16:28 +02:00
} // AttrImpl
virtual ~AttrNSImpl() { }
///////////////////////////////////////////////////////
// DOM::Node methods
virtual DOMNode_implT* cloneNode(bool deep) const
2002-06-21 13:16:28 +02:00
{
AttrNSImpl* clone = dynamic_cast<AttrNSImpl*>(AttrImplT::ownerDoc_->createAttributeNS(*namespaceURI_, *AttrImplT::name_));
AttrImplT::cloneChildren(clone);
clone->setSpecified(AttrImplT::getSpecified());
2002-06-21 13:16:28 +02:00
return clone;
} // cloneNode
2005-12-09 16:20:08 +01:00
virtual const stringT& getNamespaceURI() const
2002-06-21 13:16:28 +02:00
{
return *namespaceURI_;
2002-06-21 13:16:28 +02:00
} // getNamespaceURI
2005-12-09 16:20:08 +01:00
virtual const stringT& getPrefix() const
2002-06-21 13:16:28 +02:00
{
2005-12-09 16:20:08 +01:00
return *prefix_;
2002-06-21 13:16:28 +02:00
} // getPrefix
virtual void setPrefix(const stringT& prefix)
{
if(hasNamespaceURI_ == false)
throw DOM::DOMException(DOM::DOMException::NAMESPACE_ERR);
if(string_adaptorT::empty(prefix))
2002-06-21 13:16:28 +02:00
{
AttrImplT::name_ = localName_;
prefix_ = &AttrImplT::ownerDoc_->empty_string();
2002-06-21 13:16:28 +02:00
return;
} // empty prefix
checkPrefixAndNamespace<stringT, string_adaptorT>(true, prefix, true, *namespaceURI_, DOM::Node_base::ATTRIBUTE_NODE);
2002-06-21 13:16:28 +02:00
stringT newName = prefix;
string_adaptorT::append(newName, string_adaptorT::construct_from_utf8(":"));
string_adaptorT::append(newName, *localName_);
2005-12-09 16:20:08 +01:00
prefix_ = AttrImplT::ownerDoc_->stringPool(prefix);
AttrImplT::name_ = AttrImplT::ownerDoc_->stringPool(newName);
2002-06-21 13:16:28 +02:00
} // setPrefix
2005-12-09 16:20:08 +01:00
virtual const stringT& getLocalName() const
2002-06-21 13:16:28 +02:00
{
return *localName_;
2002-06-21 13:16:28 +02:00
} // getLocalName
// additional three methods - since C++ std::string (and by implication
// stringT) don't differenciate between a null string and an empty string,
// but the DOM recommendation does, I have to introduce these three methods
// to disambiguate. If they return false, the corresponding attribute should be
// considered null. If they return true, the attribute has been set EVEN IF
// it has been set to the empty string
virtual bool hasNamespaceURI() const
{
return hasNamespaceURI_;
} // hasNamespaceURI
virtual bool hasPrefix() const
{
return !(*prefix_ == AttrImplT::ownerDoc_->empty_string());
2002-06-21 13:16:28 +02:00
} // hasPrefix
private:
stringT const* namespaceURI_;
2005-12-09 16:20:08 +01:00
stringT const* prefix_;
stringT const* localName_;
2002-06-21 13:16:28 +02:00
bool hasNamespaceURI_;
}; // class AttrNSImpl
} // namespace SAX2DOM
2007-09-05 13:47:13 +02:00
} // namespace Arabica
2002-06-21 13:16:28 +02:00
#endif