2002-06-21 13:16:28 +02:00
|
|
|
#ifndef JEZUK_SimpleDOM_ATTRMAP_H
|
|
|
|
#define JEZUK_SimpleDOM_ATTRMAP_H
|
|
|
|
|
2007-09-05 00:55:47 +02:00
|
|
|
#include <DOM/Simple/NamedNodeMapImpl.hpp>
|
|
|
|
#include <DOM/Simple/AttrImpl.hpp>
|
|
|
|
#include <DOM/Simple/AttrNSImpl.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 AttrMap : public NamedNodeMapImpl<stringT, string_adaptorT>
|
|
|
|
{
|
|
|
|
public:
|
2007-09-09 00:31:24 +02:00
|
|
|
typedef NamedNodeMapImpl<stringT, string_adaptorT> NamedNodeMapImplT;
|
2007-09-08 00:03:27 +02:00
|
|
|
typedef AttrImpl<stringT, string_adaptorT> AttrImplT;
|
|
|
|
typedef AttrNSImpl<stringT, string_adaptorT> AttrNSImplT;
|
|
|
|
typedef ElementImpl<stringT, string_adaptorT> ElementImplT;
|
|
|
|
typedef DocumentImpl<stringT, string_adaptorT> DocumentImplT;
|
2007-09-08 01:52:30 +02:00
|
|
|
typedef DocumentTypeImpl<stringT, string_adaptorT> DocumentTypeImplT;
|
2007-09-08 00:03:27 +02:00
|
|
|
typedef DOM::Node_impl<stringT, string_adaptorT> DOMNode_implT;
|
|
|
|
typedef DOM::Attr_impl<stringT, string_adaptorT> DOMAttr_implT;
|
|
|
|
typedef DOM::NamedNodeMap_impl<stringT, string_adaptorT> DOMNamedNodeMap_implT;
|
2007-09-08 01:52:30 +02:00
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
AttrMap(DocumentImplT* ownerDoc) :
|
|
|
|
NamedNodeMapImplT(ownerDoc),
|
2002-06-21 13:16:28 +02:00
|
|
|
ownerElement_(0)
|
|
|
|
{
|
|
|
|
} // AttrMap
|
|
|
|
|
|
|
|
~AttrMap()
|
|
|
|
{
|
|
|
|
} // ~AttrMap
|
|
|
|
|
2010-12-27 14:06:18 +01:00
|
|
|
/////////////////////////////////////////////////////
|
|
|
|
virtual DOMNode_implT* setNamedItem(DOMNode_implT* newAttr)
|
|
|
|
{
|
|
|
|
AttrImplT* attr = dynamic_cast<AttrImplT*>(newAttr);
|
|
|
|
if(alreadyHere(attr))
|
|
|
|
return attr;
|
|
|
|
NamedNodeMapImplT::throwIfReadOnly();
|
|
|
|
checkNotInUse(attr);
|
|
|
|
attr->setOwnerElement(ownerElement_);
|
|
|
|
return dynamic_cast<DOMAttr_implT*>(NamedNodeMapImplT::setNamedItem(attr));
|
|
|
|
} // setNamedItem
|
|
|
|
|
|
|
|
virtual DOMNode_implT* removeNamedItem(const stringT& name)
|
|
|
|
{
|
|
|
|
DOMNode_implT* n = NamedNodeMapImplT::removeNamedItem(name);
|
|
|
|
createDefault(name);
|
|
|
|
return n;
|
|
|
|
} // removeNamedItem
|
|
|
|
|
|
|
|
virtual DOMNode_implT* removeNamedItemNS(const stringT& namespaceURI, const stringT& localName)
|
|
|
|
{
|
|
|
|
DOMNode_implT* n = NamedNodeMapImplT::removeNamedItemNS(namespaceURI, localName);
|
|
|
|
createDefault(namespaceURI, localName);
|
|
|
|
return n;
|
|
|
|
} // removedNamedItemNS
|
|
|
|
|
|
|
|
|
2002-06-21 13:16:28 +02:00
|
|
|
/////////////////////////////////////////////////////
|
2005-12-09 17:01:31 +01:00
|
|
|
const stringT& getAttribute(const stringT& name) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2007-09-09 00:31:24 +02:00
|
|
|
DOMNode_implT* attr = NamedNodeMapImplT::getNamedItem(name);
|
|
|
|
return attr ? attr->getNodeValue() : NamedNodeMapImplT::ownerDoc_->empty_string();
|
2002-06-21 13:16:28 +02:00
|
|
|
} // getAttribute
|
|
|
|
|
|
|
|
void setAttribute(const stringT& name, const stringT& value)
|
|
|
|
{
|
2007-09-09 00:31:24 +02:00
|
|
|
AttrImplT* a = new AttrImplT(NamedNodeMapImplT::ownerDoc_, name, value);
|
2002-06-21 13:16:28 +02:00
|
|
|
a->setOwnerElement(ownerElement_);
|
2007-09-09 00:31:24 +02:00
|
|
|
NamedNodeMapImplT::setNamedItem(a);
|
2002-06-21 13:16:28 +02:00
|
|
|
} // setAttribute
|
|
|
|
|
|
|
|
void removeAttribute(const stringT& name)
|
|
|
|
{
|
2010-12-27 14:06:18 +01:00
|
|
|
removeNamedItem(name);
|
2002-06-21 13:16:28 +02:00
|
|
|
} // removeAttribute
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
DOMAttr_implT* getAttributeNode(const stringT& name) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2007-09-09 00:31:24 +02:00
|
|
|
return dynamic_cast<DOMAttr_implT*>(NamedNodeMapImplT::getNamedItem(name));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // getAttributeNode
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
DOMAttr_implT* setAttributeNode(DOMAttr_implT* newAttr)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2010-12-09 12:35:54 +01:00
|
|
|
return dynamic_cast<DOMAttr_implT*>(setNamedItem(newAttr));
|
|
|
|
} // setAttributeNode
|
2002-06-21 13:16:28 +02:00
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
DOMAttr_implT* removeAttributeNode(DOMAttr_implT* oldAttr)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2010-12-29 16:41:56 +01:00
|
|
|
if(oldAttr->getOwnerElement() != ownerElement_)
|
2002-06-21 13:16:28 +02:00
|
|
|
throw DOM::DOMException(DOM::DOMException::NOT_FOUND_ERR);
|
2010-12-29 16:41:56 +01:00
|
|
|
removeNamedItem(oldAttr->getNodeName());
|
2002-06-21 13:16:28 +02:00
|
|
|
return oldAttr;
|
|
|
|
} // removeAttributeNode
|
|
|
|
|
|
|
|
stringT getAttributeNS(const stringT& namespaceURI, const stringT& localName) const
|
|
|
|
{
|
2007-09-09 00:31:24 +02:00
|
|
|
DOMNode_implT* attr = NamedNodeMapImplT::getNamedItemNS(namespaceURI, localName);
|
2002-06-21 13:16:28 +02:00
|
|
|
return attr ? attr->getNodeValue() : stringT();
|
|
|
|
} // getAttributeNS
|
|
|
|
|
|
|
|
void setAttributeNS(const stringT& namespaceURI, const stringT& qualifiedName, const stringT& value)
|
|
|
|
{
|
2007-09-09 00:31:24 +02:00
|
|
|
AttrNSImplT* a = new AttrNSImplT(NamedNodeMapImplT::ownerDoc_,
|
2007-09-08 00:03:27 +02:00
|
|
|
namespaceURI,
|
|
|
|
!string_adaptorT::empty(namespaceURI),
|
|
|
|
qualifiedName);
|
2002-06-21 13:16:28 +02:00
|
|
|
a->setValue(value);
|
|
|
|
a->setOwnerElement(ownerElement_);
|
2007-09-09 00:31:24 +02:00
|
|
|
NamedNodeMapImplT::setNamedItemNS(a);
|
2002-06-21 13:16:28 +02:00
|
|
|
} // setAttributeNS
|
|
|
|
|
|
|
|
void removeAttributeNS(const stringT& namespaceURI, const stringT& localName)
|
|
|
|
{
|
2010-12-27 14:06:18 +01:00
|
|
|
removeNamedItemNS(namespaceURI, localName);
|
2002-06-21 13:16:28 +02:00
|
|
|
} // removeAttributeNS
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
DOMAttr_implT* getAttributeNodeNS(const stringT& namespaceURI, const stringT& localName) const
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2007-09-09 00:31:24 +02:00
|
|
|
return dynamic_cast<DOMAttr_implT*>(NamedNodeMapImplT::getNamedItemNS(namespaceURI, localName));
|
2002-06-21 13:16:28 +02:00
|
|
|
} // getAttributeNodeNS
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
DOMAttr_implT* setAttributeNodeNS(DOMAttr_implT* newAttr)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2010-12-09 12:35:54 +01:00
|
|
|
return dynamic_cast<DOMAttr_implT*>(setNamedItemNS(newAttr));
|
|
|
|
} // setAttributeNodeNS
|
2010-12-27 14:06:18 +01:00
|
|
|
|
2010-12-09 12:35:54 +01:00
|
|
|
virtual DOMNode_implT* setNamedItemNS(DOMNode_implT* newAttr)
|
|
|
|
{
|
2010-12-11 02:11:22 +01:00
|
|
|
AttrImplT* attr = dynamic_cast<AttrImplT*>(newAttr);
|
|
|
|
if(alreadyHere(attr))
|
|
|
|
return attr;
|
2010-12-09 15:04:13 +01:00
|
|
|
NamedNodeMapImplT::throwIfReadOnly();
|
2010-12-11 02:11:22 +01:00
|
|
|
checkNotInUse(attr);
|
|
|
|
attr->setOwnerElement(ownerElement_);
|
2007-09-09 00:31:24 +02:00
|
|
|
return dynamic_cast<DOMAttr_implT*>(NamedNodeMapImplT::setNamedItemNS(newAttr));
|
2010-12-09 12:35:54 +01:00
|
|
|
} // setNamedItem
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
bool hasAttribute(const stringT& name) const
|
|
|
|
{
|
2007-09-09 00:31:24 +02:00
|
|
|
return (NamedNodeMapImplT::getNamedItem(name) != 0);
|
2002-06-21 13:16:28 +02:00
|
|
|
} // hasAttribute
|
|
|
|
|
|
|
|
bool hasAttributeNS(const stringT& namespaceURI, const stringT& localName) const
|
|
|
|
{
|
2007-09-09 00:31:24 +02:00
|
|
|
return (NamedNodeMapImplT::getNamedItemNS(namespaceURI, localName) != 0);
|
2002-06-21 13:16:28 +02:00
|
|
|
} // hasAttributeNS
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
void setOwnerElement(ElementImplT* element)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
|
|
|
ownerElement_ = element;
|
|
|
|
getDefaults();
|
|
|
|
} // setOwnerElement
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
virtual void setOwnerDoc(DocumentImplT* ownerDoc)
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2007-09-08 00:03:27 +02:00
|
|
|
NamedNodeMapImplT::setOwnerDoc(ownerDoc);
|
2002-06-21 13:16:28 +02:00
|
|
|
getDefaults();
|
|
|
|
} // setOwnerDoc
|
|
|
|
|
|
|
|
private:
|
|
|
|
void createDefault(const stringT& name)
|
|
|
|
{
|
2007-09-08 00:03:27 +02:00
|
|
|
DOMNamedNodeMap_implT* attrs = getDefaultAttrs();
|
2002-06-21 13:16:28 +02:00
|
|
|
if(attrs)
|
|
|
|
{
|
2007-09-08 00:03:27 +02:00
|
|
|
DOMNode_implT* attr = attrs->getNamedItem(name);
|
2002-06-21 13:16:28 +02:00
|
|
|
if(attr)
|
2007-09-08 00:03:27 +02:00
|
|
|
setAttributeNode(dynamic_cast<DOMAttr_implT*>(attr->cloneNode(true)));
|
2002-06-21 13:16:28 +02:00
|
|
|
}
|
|
|
|
} // createDefault
|
|
|
|
|
|
|
|
void createDefault(const stringT& namespaceURI, const stringT& localName)
|
|
|
|
{
|
2007-09-08 00:03:27 +02:00
|
|
|
DOMNamedNodeMap_implT* attrs = getDefaultAttrs();
|
2002-06-21 13:16:28 +02:00
|
|
|
if(attrs)
|
|
|
|
{
|
2007-09-08 00:03:27 +02:00
|
|
|
DOMNode_implT* attr = attrs->getNamedItemNS(namespaceURI, localName);
|
2002-06-21 13:16:28 +02:00
|
|
|
if(attr)
|
2007-09-08 00:03:27 +02:00
|
|
|
setAttributeNodeNS(dynamic_cast<DOMAttr_implT*>(attr->cloneNode(true)));
|
2002-06-21 13:16:28 +02:00
|
|
|
}
|
|
|
|
} // createDefault
|
|
|
|
|
|
|
|
void getDefaults()
|
|
|
|
{
|
2007-09-08 00:03:27 +02:00
|
|
|
DOMNamedNodeMap_implT* attrs = getDefaultAttrs();
|
2002-06-21 13:16:28 +02:00
|
|
|
if(attrs)
|
|
|
|
{
|
|
|
|
for(unsigned int i = 0; i < attrs->getLength(); ++i)
|
|
|
|
{
|
2007-09-08 00:03:27 +02:00
|
|
|
DOMNode_implT* attr = attrs->item(i);
|
2002-06-21 13:16:28 +02:00
|
|
|
if(getAttributeNodeNS(attr->getNamespaceURI(), attr->getNodeName()) == 0)
|
2007-09-08 00:03:27 +02:00
|
|
|
setAttributeNodeNS(dynamic_cast<DOMAttr_implT*>(attr->cloneNode(true)));
|
2002-06-21 13:16:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // getDefaults
|
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
DOMNamedNodeMap_implT* getDefaultAttrs()
|
2002-06-21 13:16:28 +02:00
|
|
|
{
|
2007-09-09 00:31:24 +02:00
|
|
|
if(!NamedNodeMapImplT::ownerDoc_)
|
2002-06-21 13:16:28 +02:00
|
|
|
return 0;
|
|
|
|
|
2007-09-09 00:31:24 +02:00
|
|
|
DocumentTypeImplT* docType = dynamic_cast<DocumentTypeImplT*>(NamedNodeMapImplT::ownerDoc_->getDoctype());
|
2002-06-21 13:16:28 +02:00
|
|
|
if(docType)
|
|
|
|
{
|
2007-09-08 00:03:27 +02:00
|
|
|
DOMNode_implT* exemplar = docType->getElements()->getNamedItem(ownerElement_->getNodeName());
|
2002-06-21 13:16:28 +02:00
|
|
|
if(exemplar)
|
|
|
|
return exemplar->getAttributes();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} // getDefaultAttrs
|
|
|
|
|
2010-12-11 02:11:22 +01:00
|
|
|
bool alreadyHere(AttrImplT* attr)
|
|
|
|
{
|
|
|
|
return ((!attr->isOrphaned()) && (attr->getOwnerElement() == ownerElement_));
|
|
|
|
} // alreadyHere
|
|
|
|
|
|
|
|
void checkNotInUse(AttrImplT* attr)
|
2010-12-09 15:04:13 +01:00
|
|
|
{
|
2010-12-11 02:11:22 +01:00
|
|
|
if((!attr->isOrphaned()) && (attr->getOwnerElement() != ownerElement_))
|
2010-12-09 12:35:54 +01:00
|
|
|
throw DOM::DOMException(DOM::DOMException::INUSE_ATTRIBUTE_ERR);
|
2010-12-09 15:04:13 +01:00
|
|
|
} // checkNotInUse
|
2010-12-09 12:35:54 +01:00
|
|
|
|
2007-09-08 00:03:27 +02:00
|
|
|
ElementImplT* ownerElement_;
|
2002-06-21 13:16:28 +02:00
|
|
|
}; // class AttrMap
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace SAX2DOM
|
2007-09-05 13:47:13 +02:00
|
|
|
} // namespace Arabica
|
2002-06-21 13:16:28 +02:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|