arabica/include/DOM/Simple/AttrMap.hpp

238 lines
7.7 KiB
C++
Raw Normal View History

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:
typedef NamedNodeMapImpl<stringT, string_adaptorT> NamedNodeMapImplT;
typedef AttrImpl<stringT, string_adaptorT> AttrImplT;
typedef AttrNSImpl<stringT, string_adaptorT> AttrNSImplT;
typedef ElementImpl<stringT, string_adaptorT> ElementImplT;
typedef DocumentImpl<stringT, string_adaptorT> DocumentImplT;
typedef DocumentTypeImpl<stringT, string_adaptorT> DocumentTypeImplT;
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;
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
/////////////////////////////////////////////////////
const stringT& getAttribute(const stringT& name) const
2002-06-21 13:16:28 +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)
{
AttrImplT* a = new AttrImplT(NamedNodeMapImplT::ownerDoc_, name, value);
2002-06-21 13:16:28 +02:00
a->setOwnerElement(ownerElement_);
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
DOMAttr_implT* getAttributeNode(const stringT& name) const
2002-06-21 13:16:28 +02:00
{
return dynamic_cast<DOMAttr_implT*>(NamedNodeMapImplT::getNamedItem(name));
2002-06-21 13:16:28 +02:00
} // getAttributeNode
DOMAttr_implT* setAttributeNode(DOMAttr_implT* newAttr)
2002-06-21 13:16:28 +02:00
{
return dynamic_cast<DOMAttr_implT*>(setNamedItem(newAttr));
} // setAttributeNode
2002-06-21 13:16:28 +02:00
DOMAttr_implT* removeAttributeNode(DOMAttr_implT* oldAttr)
2002-06-21 13:16:28 +02:00
{
if(oldAttr->getOwnerElement() != ownerElement_)
2002-06-21 13:16:28 +02:00
throw DOM::DOMException(DOM::DOMException::NOT_FOUND_ERR);
removeNamedItem(oldAttr->getNodeName());
2002-06-21 13:16:28 +02:00
return oldAttr;
} // removeAttributeNode
stringT getAttributeNS(const stringT& namespaceURI, const stringT& localName) const
{
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)
{
AttrNSImplT* a = new AttrNSImplT(NamedNodeMapImplT::ownerDoc_,
namespaceURI,
!string_adaptorT::empty(namespaceURI),
qualifiedName);
2002-06-21 13:16:28 +02:00
a->setValue(value);
a->setOwnerElement(ownerElement_);
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
DOMAttr_implT* getAttributeNodeNS(const stringT& namespaceURI, const stringT& localName) const
2002-06-21 13:16:28 +02:00
{
return dynamic_cast<DOMAttr_implT*>(NamedNodeMapImplT::getNamedItemNS(namespaceURI, localName));
2002-06-21 13:16:28 +02:00
} // getAttributeNodeNS
DOMAttr_implT* setAttributeNodeNS(DOMAttr_implT* newAttr)
2002-06-21 13:16:28 +02:00
{
return dynamic_cast<DOMAttr_implT*>(setNamedItemNS(newAttr));
} // setAttributeNodeNS
2010-12-27 14:06:18 +01:00
virtual DOMNode_implT* setNamedItemNS(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::setNamedItemNS(newAttr));
} // setNamedItem
2002-06-21 13:16:28 +02:00
bool hasAttribute(const stringT& name) const
{
return (NamedNodeMapImplT::getNamedItem(name) != 0);
2002-06-21 13:16:28 +02:00
} // hasAttribute
bool hasAttributeNS(const stringT& namespaceURI, const stringT& localName) const
{
return (NamedNodeMapImplT::getNamedItemNS(namespaceURI, localName) != 0);
2002-06-21 13:16:28 +02:00
} // hasAttributeNS
void setOwnerElement(ElementImplT* element)
2002-06-21 13:16:28 +02:00
{
ownerElement_ = element;
getDefaults();
} // setOwnerElement
virtual void setOwnerDoc(DocumentImplT* ownerDoc)
2002-06-21 13:16:28 +02:00
{
NamedNodeMapImplT::setOwnerDoc(ownerDoc);
2002-06-21 13:16:28 +02:00
getDefaults();
} // setOwnerDoc
private:
void createDefault(const stringT& name)
{
DOMNamedNodeMap_implT* attrs = getDefaultAttrs();
2002-06-21 13:16:28 +02:00
if(attrs)
{
DOMNode_implT* attr = attrs->getNamedItem(name);
2002-06-21 13:16:28 +02:00
if(attr)
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)
{
DOMNamedNodeMap_implT* attrs = getDefaultAttrs();
2002-06-21 13:16:28 +02:00
if(attrs)
{
DOMNode_implT* attr = attrs->getNamedItemNS(namespaceURI, localName);
2002-06-21 13:16:28 +02:00
if(attr)
setAttributeNodeNS(dynamic_cast<DOMAttr_implT*>(attr->cloneNode(true)));
2002-06-21 13:16:28 +02:00
}
} // createDefault
void getDefaults()
{
DOMNamedNodeMap_implT* attrs = getDefaultAttrs();
2002-06-21 13:16:28 +02:00
if(attrs)
{
for(unsigned int i = 0; i < attrs->getLength(); ++i)
{
DOMNode_implT* attr = attrs->item(i);
2002-06-21 13:16:28 +02:00
if(getAttributeNodeNS(attr->getNamespaceURI(), attr->getNodeName()) == 0)
setAttributeNodeNS(dynamic_cast<DOMAttr_implT*>(attr->cloneNode(true)));
2002-06-21 13:16:28 +02:00
}
}
} // getDefaults
DOMNamedNodeMap_implT* getDefaultAttrs()
2002-06-21 13:16:28 +02:00
{
if(!NamedNodeMapImplT::ownerDoc_)
2002-06-21 13:16:28 +02:00
return 0;
DocumentTypeImplT* docType = dynamic_cast<DocumentTypeImplT*>(NamedNodeMapImplT::ownerDoc_->getDoctype());
2002-06-21 13:16:28 +02:00
if(docType)
{
DOMNode_implT* exemplar = docType->getElements()->getNamedItem(ownerElement_->getNodeName());
2002-06-21 13:16:28 +02:00
if(exemplar)
return exemplar->getAttributes();
}
return 0;
} // getDefaultAttrs
bool alreadyHere(AttrImplT* attr)
{
return ((!attr->isOrphaned()) && (attr->getOwnerElement() == ownerElement_));
} // alreadyHere
void checkNotInUse(AttrImplT* attr)
{
if((!attr->isOrphaned()) && (attr->getOwnerElement() != ownerElement_))
throw DOM::DOMException(DOM::DOMException::INUSE_ATTRIBUTE_ERR);
} // checkNotInUse
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