2002-06-21 13:16:28 +02:00
|
|
|
#ifndef JEZUK_SimpleDOM_NAMEDNODEMAPIMPL_H
|
|
|
|
#define JEZUK_SimpleDOM_NAMEDNODEMAPIMPL_H
|
|
|
|
|
2007-09-05 00:55:47 +02:00
|
|
|
#include <DOM/Simple/NodeImpl.hpp>
|
|
|
|
#include <DOM/NamedNodeMap.hpp>
|
2002-06-21 13:16:28 +02:00
|
|
|
#include <deque>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
|
|
|
|
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>
|
2007-09-05 13:47:13 +02:00
|
|
|
class nameIs : public std::unary_function<Arabica::SimpleDOM::NodeImpl<stringT, string_adaptorT>*, bool>
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
nameIs(const stringT& name) : name_(name) { }
|
2007-09-05 13:47:13 +02:00
|
|
|
bool operator()(const Arabica::SimpleDOM::NodeImpl<stringT, string_adaptorT>* node) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
return (node->getNodeName() == name_);
|
|
|
|
} // operator()
|
|
|
|
|
|
|
|
private:
|
|
|
|
stringT name_;
|
|
|
|
}; // class nameIs
|
|
|
|
|
|
|
|
template<class stringT, class string_adaptorT>
|
|
|
|
class namespaceAndNameIs : public std::unary_function<NodeImpl<stringT, string_adaptorT>*, bool>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
namespaceAndNameIs(const stringT& namespaceURI, const stringT& localName) :
|
|
|
|
namespaceURI_(namespaceURI),
|
|
|
|
localName_(localName) { }
|
|
|
|
|
|
|
|
bool operator()(const NodeImpl<stringT, string_adaptorT>* node) const
|
|
|
|
{
|
2005-09-08 23:43:21 +02:00
|
|
|
if(string_adaptorT::empty(namespaceURI_))
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if((node->hasNamespaceURI() == false) &&
|
|
|
|
((node->getLocalName() == localName_) ||
|
2005-09-08 23:43:21 +02:00
|
|
|
(string_adaptorT::empty(node->getLocalName()) && node->getNodeName() == localName_)
|
2002-06-21 13:16:28 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
return true;
|
|
|
|
} // if(namespaceURI_.empty())
|
|
|
|
|
|
|
|
if((node->getNamespaceURI() == namespaceURI_) && (node->getLocalName() == localName_))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} // operator()
|
|
|
|
|
|
|
|
private:
|
|
|
|
stringT namespaceURI_;
|
|
|
|
stringT localName_;
|
|
|
|
}; // class namespaceAndNameIs
|
|
|
|
|
|
|
|
template<class stringT, class string_adaptorT>
|
2007-09-08 00:03:27 +02:00
|
|
|
class NamedNodeMapImpl : public DOM::NamedNodeMap_impl<stringT, string_adaptorT>
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
public:
|
2007-09-08 00:03:27 +02:00
|
|
|
typedef DOM::Node_impl<stringT, string_adaptorT> DOMNode_implT;
|
|
|
|
typedef NodeImpl<stringT, string_adaptorT> NodeImplT;
|
|
|
|
typedef NamedNodeMapImpl<stringT, string_adaptorT> NamedNodeMapImplT;
|
|
|
|
typedef DocumentImpl<stringT, string_adaptorT> DocumentImplT;
|
|
|
|
|
|
|
|
NamedNodeMapImpl(DocumentImplT* ownerDoc) :
|
|
|
|
DOM::NamedNodeMap_impl<stringT, string_adaptorT>(),
|
|
|
|
nodes_(),
|
2002-06-21 13:16:28 +02:00
|
|
|
readOnly_(false),
|
|
|
|
ownerDoc_(ownerDoc)
|
|
|
|
{
|
|
|
|
} // NamedNodeMapImpl
|
|
|
|
|
|
|
|
virtual ~NamedNodeMapImpl()
|
|
|
|
{
|
2002-11-23 21:03:54 +01:00
|
|
|
for(typename NodeListT::iterator i = nodes_.begin(); i != nodes_.end(); ++i)
|
2002-06-21 13:16:28 +02:00
|
|
|
delete (*i);
|
|
|
|
} // ~NodeImpl
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////
|
|
|
|
// Ref counting
|
|
|
|
virtual void addRef()
|
|
|
|
{
|
|
|
|
if(ownerDoc_)
|
|
|
|
ownerDoc_->addRef();
|
|
|
|
} // addRef
|
|
|
|
virtual void releaseRef()
|
|
|
|
{
|
|
|
|
if(ownerDoc_)
|
|
|
|
ownerDoc_->releaseRef();
|
|
|
|
} // releaseRef
|
2007-09-08 00:03:27 +02:00
|
|
|
virtual void setOwnerDoc(DocumentImplT* ownerDoc)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
ownerDoc_ = ownerDoc;
|
2002-11-23 21:03:54 +01:00
|
|
|
for(typename NodeListT::iterator i = nodes_.begin(); i != nodes_.end(); ++i)
|
2002-06-21 13:16:28 +02:00
|
|
|
(*i)->setOwnerDoc(ownerDoc);
|
|
|
|
} // setOwnerDoc
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////
|
|
|
|
// DOM::NamedNodeMap methods
|
2007-09-08 00:03:27 +02:00
|
|
|
virtual DOMNode_implT* getNamedItem(const stringT& name) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2007-09-08 00:03:27 +02:00
|
|
|
return getNode(const_cast<NamedNodeMapImplT*>(this)->findByName(name));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // getNamedItem
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
virtual DOMNode_implT* setNamedItem(DOMNode_implT* arg)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
throwIfReadOnly();
|
2007-09-08 00:03:27 +02:00
|
|
|
return setNode(findByName(arg->getNodeName()), dynamic_cast<NodeImplT*>(arg));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // setNamedItem
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
virtual DOMNode_implT* removeNamedItem(const stringT& name)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
throwIfReadOnly();
|
|
|
|
return removeNode(findByName(name));
|
|
|
|
} // removedNamedItem
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
virtual DOMNode_implT* item(unsigned int index) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
return do_item(index);
|
|
|
|
} // item
|
|
|
|
|
|
|
|
virtual unsigned int getLength() const
|
|
|
|
{
|
2003-04-28 16:47:19 +02:00
|
|
|
return static_cast<unsigned int>(nodes_.size());
|
2002-06-21 13:16:28 +02:00
|
|
|
} // getLength
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
virtual DOMNode_implT* getNamedItemNS(const stringT& namespaceURI, const stringT& localName) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2007-09-08 00:03:27 +02:00
|
|
|
return getNode(const_cast<NamedNodeMapImplT*>(this)->findByNamespaceAndName(namespaceURI, localName));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // getNamedItemNS
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
virtual DOMNode_implT* setNamedItemNS(DOMNode_implT* arg)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
throwIfReadOnly();
|
2007-09-08 00:03:27 +02:00
|
|
|
return setNode(findByNamespaceAndName(arg->getNamespaceURI(), arg->getLocalName()), dynamic_cast<NodeImplT*>(arg));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // setNamedItemNS
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
virtual DOMNode_implT* removeNamedItemNS(const stringT& namespaceURI, const stringT& localName)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
throwIfReadOnly();
|
|
|
|
return removeNode(findByNamespaceAndName(namespaceURI, localName));
|
|
|
|
} // removeNamedItemNS
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////
|
|
|
|
// extensions
|
|
|
|
void setReadOnly(bool readOnly)
|
|
|
|
{
|
|
|
|
readOnly_ = readOnly;
|
|
|
|
} // setReadOnly
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
NodeImplT* do_item(unsigned int index) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(index >= nodes_.size())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return nodes_[index];
|
|
|
|
} // item
|
|
|
|
|
|
|
|
private:
|
|
|
|
void throwIfReadOnly() const
|
|
|
|
{
|
|
|
|
if(readOnly_)
|
|
|
|
throw DOM::DOMException(DOM::DOMException::NO_MODIFICATION_ALLOWED_ERR);
|
|
|
|
} // throwIfReadOnly
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
typedef std::deque<NodeImplT*> NodeListT;
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
NodeImplT* getNode(typename NodeListT::const_iterator n) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(n == nodes_.end())
|
|
|
|
return 0;
|
|
|
|
return *n;
|
|
|
|
} // getNode
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
NodeImplT* setNode(typename NodeListT::iterator n, NodeImplT* arg)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(n == nodes_.end())
|
|
|
|
{
|
|
|
|
nodes_.push_back(arg);
|
|
|
|
return 0;
|
|
|
|
} // if(n == nodes_.end())
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
NodeImplT* removedNode = *n;
|
2002-06-21 13:16:28 +02:00
|
|
|
*n = arg;
|
|
|
|
ownerDoc_->orphaned(removedNode);
|
|
|
|
return removedNode;
|
|
|
|
} // setNode
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
NodeImplT* removeNode(typename NodeListT::iterator n)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
if(n == nodes_.end())
|
|
|
|
return 0;
|
2007-09-08 00:03:27 +02:00
|
|
|
NodeImplT* removedNode = *n;
|
2002-06-21 13:16:28 +02:00
|
|
|
nodes_.erase(n);
|
|
|
|
ownerDoc_->orphaned(removedNode);
|
|
|
|
return removedNode;
|
|
|
|
} // removeNode
|
|
|
|
|
2002-11-23 21:03:54 +01:00
|
|
|
typename NodeListT::iterator findByName(const stringT& name)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
return std::find_if(nodes_.begin(), nodes_.end(), nameIs<stringT, string_adaptorT>(name));
|
|
|
|
} // findByName
|
|
|
|
|
2002-11-23 21:03:54 +01:00
|
|
|
typename NodeListT::iterator findByNamespaceAndName(const stringT& namespaceURI, const stringT& localName)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
return std::find_if(nodes_.begin(), nodes_.end(), namespaceAndNameIs<stringT, string_adaptorT>(namespaceURI, localName));
|
|
|
|
} // findByName
|
|
|
|
|
|
|
|
NodeListT nodes_;
|
|
|
|
bool readOnly_;
|
|
|
|
|
|
|
|
protected:
|
2007-09-08 01:52:30 +02:00
|
|
|
DocumentImplT* ownerDoc_;
|
2002-06-21 13:16:28 +02:00
|
|
|
}; // class NamedNodeMapImpl
|
|
|
|
|
|
|
|
} // namespace SAX2DOM
|
2007-09-05 13:47:13 +02:00
|
|
|
} // namespace Arabica
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|