arabica/DOM/Simple/AttrMap.h

182 lines
6.1 KiB
C
Raw Normal View History

2002-06-21 13:16:28 +02:00
#ifndef JEZUK_SimpleDOM_ATTRMAP_H
#define JEZUK_SimpleDOM_ATTRMAP_H
#include <DOM/Simple/NamedNodeMapImpl.h>
#include <DOM/Simple/AttrImpl.h>
#include <DOM/Simple/AttrNSImpl.h>
namespace SimpleDOM
{
template<class stringT, class string_adaptorT>
class AttrMap : public NamedNodeMapImpl<stringT, string_adaptorT>
{
2004-09-14 22:51:36 +02:00
typedef NamedNodeMapImpl<stringT, string_adaptorT> MapT;
2002-06-21 13:16:28 +02:00
public:
AttrMap(DocumentImpl<stringT, string_adaptorT>* ownerDoc) :
NamedNodeMapImpl<stringT, string_adaptorT>(ownerDoc),
ownerElement_(0)
{
} // AttrMap
~AttrMap()
{
} // ~AttrMap
/////////////////////////////////////////////////////
stringT getAttribute(const stringT& name) const
{
2004-10-12 22:49:30 +02:00
DOM::Node_impl<stringT>* attr = MapT::getNamedItem(name);
2002-06-21 13:16:28 +02:00
return attr ? attr->getNodeValue() : stringT();
} // getAttribute
void setAttribute(const stringT& name, const stringT& value)
{
AttrImpl<stringT, string_adaptorT>* a = new AttrImpl<stringT, string_adaptorT>(MapT::ownerDoc_, name, value);
2002-06-21 13:16:28 +02:00
a->setOwnerElement(ownerElement_);
2004-10-12 22:49:30 +02:00
MapT::setNamedItem(a);
2002-06-21 13:16:28 +02:00
} // setAttribute
void removeAttribute(const stringT& name)
{
2004-10-12 22:49:30 +02:00
MapT::removeNamedItem(name);
2002-06-21 13:16:28 +02:00
createDefault(name);
} // removeAttribute
DOM::Attr_impl<stringT>* getAttributeNode(const stringT& name) const
{
2004-10-12 22:49:30 +02:00
return dynamic_cast<DOM::Attr_impl<stringT>*>(MapT::getNamedItem(name));
2002-06-21 13:16:28 +02:00
} // getAttributeNode
DOM::Attr_impl<stringT>* setAttributeNode(DOM::Attr_impl<stringT>* newAttr)
{
dynamic_cast<AttrImpl<stringT, string_adaptorT>*>(newAttr)->setOwnerElement(ownerElement_);
2004-10-12 22:49:30 +02:00
return dynamic_cast<DOM::Attr_impl<stringT>*>(MapT::setNamedItem(newAttr));
2002-06-21 13:16:28 +02:00
} // setAttributeNode
DOM::Attr_impl<stringT>* removeAttributeNode(DOM::Attr_impl<stringT>* oldAttr)
{
2004-10-12 22:49:30 +02:00
if(MapT::removeNamedItem(oldAttr->getNodeName()) == 0)
2002-06-21 13:16:28 +02:00
throw DOM::DOMException(DOM::DOMException::NOT_FOUND_ERR);
createDefault(oldAttr->getNodeName());
return oldAttr;
} // removeAttributeNode
stringT getAttributeNS(const stringT& namespaceURI, const stringT& localName) const
{
2004-10-12 22:49:30 +02:00
DOM::Node_impl<stringT>* attr = MapT::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)
{
AttrNSImpl<stringT, string_adaptorT>* a =
new AttrNSImpl<stringT, string_adaptorT>(MapT::ownerDoc_,
namespaceURI,
!string_adaptorT::empty(namespaceURI),
qualifiedName);
2002-06-21 13:16:28 +02:00
a->setValue(value);
a->setOwnerElement(ownerElement_);
2004-10-12 22:49:30 +02:00
MapT::setNamedItemNS(a);
2002-06-21 13:16:28 +02:00
} // setAttributeNS
void removeAttributeNS(const stringT& namespaceURI, const stringT& localName)
{
2004-10-12 22:49:30 +02:00
MapT::removeNamedItemNS(namespaceURI, localName);
2002-06-21 13:16:28 +02:00
createDefault(namespaceURI, localName);
} // removeAttributeNS
DOM::Attr_impl<stringT>* getAttributeNodeNS(const stringT& namespaceURI, const stringT& localName) const
{
2004-10-12 22:49:30 +02:00
return dynamic_cast<DOM::Attr_impl<stringT>*>(MapT::getNamedItemNS(namespaceURI, localName));
2002-06-21 13:16:28 +02:00
} // getAttributeNodeNS
DOM::Attr_impl<stringT>* setAttributeNodeNS(DOM::Attr_impl<stringT>* newAttr)
{
dynamic_cast<AttrImpl<stringT, string_adaptorT>*>(newAttr)->setOwnerElement(ownerElement_);
2004-10-12 22:49:30 +02:00
return dynamic_cast<DOM::Attr_impl<stringT>*>(MapT::setNamedItemNS(newAttr));
2002-06-21 13:16:28 +02:00
} // setAttributeNodeNS
bool hasAttribute(const stringT& name) const
{
2004-10-12 22:49:30 +02:00
return (MapT::getNamedItem(name) != 0);
2002-06-21 13:16:28 +02:00
} // hasAttribute
bool hasAttributeNS(const stringT& namespaceURI, const stringT& localName) const
{
2004-10-12 22:49:30 +02:00
return (MapT::getNamedItemNS(namespaceURI, localName) != 0);
2002-06-21 13:16:28 +02:00
} // hasAttributeNS
void setOwnerElement(ElementImpl<stringT, string_adaptorT>* element)
{
ownerElement_ = element;
getDefaults();
} // setOwnerElement
virtual void setOwnerDoc(DocumentImpl<stringT, string_adaptorT>* ownerDoc)
{
NamedNodeMapImpl<stringT, string_adaptorT>::setOwnerDoc(ownerDoc);
getDefaults();
} // setOwnerDoc
private:
void createDefault(const stringT& name)
{
DOM::NamedNodeMap_impl<stringT>* attrs = getDefaultAttrs();
if(attrs)
{
DOM::Node_impl<stringT>* attr = attrs->getNamedItem(name);
if(attr)
setAttributeNode(dynamic_cast<DOM::Attr_impl<stringT>*>(attr->cloneNode(true)));
}
} // createDefault
void createDefault(const stringT& namespaceURI, const stringT& localName)
{
DOM::NamedNodeMap_impl<stringT>* attrs = getDefaultAttrs();
if(attrs)
{
DOM::Node_impl<stringT>* attr = attrs->getNamedItemNS(namespaceURI, localName);
if(attr)
setAttributeNodeNS(dynamic_cast<DOM::Attr_impl<stringT>*>(attr->cloneNode(true)));
}
} // createDefault
void getDefaults()
{
DOM::NamedNodeMap_impl<stringT>* attrs = getDefaultAttrs();
if(attrs)
{
for(unsigned int i = 0; i < attrs->getLength(); ++i)
{
DOM::Node_impl<stringT>* attr = attrs->item(i);
if(getAttributeNodeNS(attr->getNamespaceURI(), attr->getNodeName()) == 0)
setAttributeNodeNS(dynamic_cast<DOM::Attr_impl<stringT>*>(attr->cloneNode(true)));
}
}
} // getDefaults
DOM::NamedNodeMap_impl<stringT>* getDefaultAttrs()
{
if(!MapT::ownerDoc_)
2002-06-21 13:16:28 +02:00
return 0;
DocumentTypeImpl<stringT, string_adaptorT>* docType = dynamic_cast<DocumentTypeImpl<stringT, string_adaptorT>*>(MapT::ownerDoc_->getDoctype());
2002-06-21 13:16:28 +02:00
if(docType)
{
DOM::Node_impl<stringT>* exemplar = docType->getElements()->getNamedItem(ownerElement_->getNodeName());
if(exemplar)
return exemplar->getAttributes();
}
return 0;
} // getDefaultAttrs
ElementImpl<stringT, string_adaptorT>* ownerElement_;
}; // class AttrMap
} // namespace SAX2DOM
#endif