arabica/include/DOM/Simple/AttrNSImpl.hpp

132 lines
4.3 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>
{
typedef typename string_adaptorT::size_type size_type;
2004-09-14 22:51:36 +02:00
typedef AttrImpl<stringT, string_adaptorT> AttrT;
2002-06-21 13:16:28 +02:00
public:
AttrNSImpl(DocumentImpl<stringT, string_adaptorT>* ownerDoc,
const stringT& namespaceURI,
bool hasNamespaceURI,
const stringT& qualifiedName) :
AttrImpl<stringT, string_adaptorT>(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_ = AttrT::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;
2005-12-09 16:20:08 +01:00
prefix_ = AttrT::ownerDoc_->stringPool(string_adaptorT::substr(qualifiedName, 0, index));
localName_ = AttrT::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 =
2005-12-09 16:20:08 +01:00
checkPrefixAndNamespace<stringT, string_adaptorT>(hasPrefix, *prefix_for_checking, hasNamespaceURI, namespaceURI, DOM::Node<stringT>::ATTRIBUTE_NODE);
2002-06-21 13:16:28 +02:00
hasNamespaceURI_ = mappedURI.first;
namespaceURI_ = AttrT::ownerDoc_->stringPool(mappedURI.second);
2002-06-21 13:16:28 +02:00
} // AttrImpl
virtual ~AttrNSImpl() { }
///////////////////////////////////////////////////////
// DOM::Node methods
virtual DOM::Node_impl<stringT>* cloneNode(bool deep) const
{
AttrNSImpl* clone = dynamic_cast<AttrNSImpl*>(AttrT::ownerDoc_->createAttributeNS(*namespaceURI_, *AttrT::name_));
2004-10-12 22:49:30 +02:00
AttrT::cloneChildren(clone);
clone->setSpecified(AttrT::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
{
AttrT::name_ = localName_;
2005-12-09 16:20:08 +01:00
prefix_ = &AttrT::ownerDoc_->empty_string();
2002-06-21 13:16:28 +02:00
return;
} // empty prefix
checkPrefixAndNamespace<stringT, string_adaptorT>(true, prefix, true, *namespaceURI_, DOM::Node<stringT>::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_ = AttrT::ownerDoc_->stringPool(prefix);
AttrT::name_ = AttrT::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
{
2005-12-09 16:20:08 +01:00
return !(*prefix_ == AttrT::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