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