arabica/include/DOM/Simple/ElementNSImpl.hpp

127 lines
4.1 KiB
C++
Raw Normal View History

2002-06-21 13:16:28 +02:00
#ifndef JEZUK_SimpleDOM_ELEMENTNSIMPL_H
#define JEZUK_SimpleDOM_ELEMENTNSIMPL_H
2007-09-05 00:55:47 +02:00
#include <DOM/Simple/ElementImpl.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 ElementNSImpl : public ElementImpl<stringT, string_adaptorT>
{
public:
typedef typename string_adaptorT::size_type size_type;
2004-09-14 22:51:36 +02:00
typedef ElementImpl<stringT, string_adaptorT> ElementImplT;
typedef DOM::Node_impl<stringT, string_adaptorT> DOMNode_implT;
2002-06-21 13:16:28 +02:00
ElementNSImpl(DocumentImpl<stringT, string_adaptorT>* ownerDoc,
const stringT& namespaceURI,
bool hasNamespaceURI,
const stringT& qualifiedName) :
ElementImplT(ownerDoc, qualifiedName),
prefix_(&ownerDoc->empty_string()),
hasNamespaceURI_(false)
2002-06-21 13:16:28 +02:00
{
bool hasPrefix = false;
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())
{ //qualifiedName contains no ':'
localName_ = ElementImplT::ownerDoc_->stringPool(qualifiedName);
2002-06-21 13:16:28 +02:00
}
else
{
hasPrefix = true;
prefix_ = ElementImplT::ownerDoc_->stringPool(string_adaptorT::substr(qualifiedName, 0, index));
localName_ = ElementImplT::ownerDoc_->stringPool(string_adaptorT::substr(qualifiedName, index+1));
2002-06-21 13:16:28 +02:00
}
std::pair<bool, stringT> mappedURI =
checkPrefixAndNamespace<stringT, string_adaptorT>(hasPrefix, *prefix_, hasNamespaceURI, namespaceURI, DOM::Node_base::ELEMENT_NODE);
2002-06-21 13:16:28 +02:00
hasNamespaceURI_ = mappedURI.first;
namespaceURI_ = ElementImplT::ownerDoc_->stringPool(mappedURI.second);
2002-06-21 13:16:28 +02:00
} // ElementImpl
virtual ~ElementNSImpl() { }
///////////////////////////////////////////////////////
// DOM::Node methods
virtual DOMNode_implT* cloneNode(bool deep) const
2002-06-21 13:16:28 +02:00
{
ElementNSImpl* clone = dynamic_cast<ElementNSImpl*>(ElementImplT::ownerDoc_->createElementNS(*namespaceURI_, ElementImplT::getNodeName()));
2004-10-12 22:49:30 +02:00
ElementImplT::cloneChildren(clone, deep);
2002-06-21 13:16:28 +02:00
return clone;
} // cloneNode
virtual const stringT& getNamespaceURI() const
2002-06-21 13:16:28 +02:00
{
return *namespaceURI_;
2002-06-21 13:16:28 +02:00
} // getNamespaceURI
virtual const stringT& getPrefix() const
2002-06-21 13:16:28 +02:00
{
return *prefix_;
2002-06-21 13:16:28 +02:00
} // getPrefix
virtual void setPrefix(const stringT& prefix)
{
ElementImplT::throwIfReadOnly();
2002-06-21 13:16:28 +02:00
if(hasNamespaceURI_ == false)
throw DOM::DOMException(DOM::DOMException::NAMESPACE_ERR);
if(string_adaptorT::empty(prefix))
2002-06-21 13:16:28 +02:00
{
ElementImplT::tagName_ = localName_;
prefix_ = &ElementImplT::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::ELEMENT_NODE);
2002-06-21 13:16:28 +02:00
stringT newTagName = prefix;
string_adaptorT::append(newTagName, string_adaptorT::construct_from_utf8(":"));
string_adaptorT::append(newTagName, *localName_);
prefix_ = ElementImplT::ownerDoc_->stringPool(prefix);
ElementImplT::tagName_ = ElementImplT::ownerDoc_->stringPool(newTagName);
2002-06-21 13:16:28 +02:00
} // setPrefix
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_ == ElementImplT::ownerDoc_->empty_string());
2002-06-21 13:16:28 +02:00
} // hasPrefix
private:
stringT const* namespaceURI_;
stringT const* prefix_;
stringT const* localName_;
2002-06-21 13:16:28 +02:00
bool hasNamespaceURI_;
}; // class ElementNSImpl
2002-06-21 13:16:28 +02:00
} // namespace SAX2DOM
2007-09-05 13:47:13 +02:00
} // namespace Arabica
2002-06-21 13:16:28 +02:00
#endif