#ifndef JEZUK_SimpleDOM_ELEMENTIMPL_H #define JEZUK_SimpleDOM_ELEMENTIMPL_H #include #include #include #include namespace SimpleDOM { template class ElementImpl : public DOM::Element_impl, public NodeImplWithChildren { typedef DOM::Element_impl ElementT; using ElementT::getElementsByTagName; using ElementT::ownerDoc_; using ElementT::cloneNode; using ElementT::getFirstChild; public: ElementImpl(DocumentImpl* ownerDoc, const stringT& tagName) : DOM::Element_impl(), NodeImplWithChildren(ownerDoc), attributes_(ownerDoc), tagName_(tagName) { attributes_.setOwnerElement(this); } // ElementImpl virtual ~ElementImpl() { } // ~ElementImpl ///////////////////////////////////////////////////// // DOM::Element functions virtual stringT getTagName() const { return getNodeName(); } virtual stringT getAttribute(const stringT& name) const { return attributes_.getAttribute(name); } // getAttribute virtual void setAttribute(const stringT& name, const stringT& value) { attributes_.setAttribute(name, value); } // setAttribute virtual void removeAttribute(const stringT& name) { attributes_.removeAttribute(name); } // removeAttribute virtual DOM::Attr_impl* getAttributeNode(const stringT& name) const { return attributes_.getAttributeNode(name); } // getAttributeNode virtual DOM::Attr_impl* setAttributeNode(DOM::Attr_impl* newAttr) { return attributes_.setAttributeNode(newAttr); } // setAttributeNode virtual DOM::Attr_impl* removeAttributeNode(DOM::Attr_impl* oldAttr) { return attributes_.removeAttributeNode(oldAttr); } // removeAttributeNode virtual DOM::NodeList_impl* getElementsByTagName(const stringT& tagName) const { return new ElementByTagList(ownerDoc_, const_cast(this), tagName); } // getElementsByTagName virtual stringT getAttributeNS(const stringT& namespaceURI, const stringT& localName) const { return attributes_.getAttributeNS(namespaceURI, localName); } // getAttributeNS virtual void setAttributeNS(const stringT& namespaceURI, const stringT& qualifiedName, const stringT& value) { attributes_.setAttributeNS(namespaceURI, qualifiedName, value); } // setAttributeNS virtual void removeAttributeNS(const stringT& namespaceURI, const stringT& localName) { attributes_.removeAttributeNS(namespaceURI, localName); } // removeAttributeNS virtual DOM::Attr_impl* getAttributeNodeNS(const stringT& namespaceURI, const stringT& localName) const { return attributes_.getAttributeNodeNS(namespaceURI, localName); } // getAttributeNodeNS virtual DOM::Attr_impl* setAttributeNodeNS(DOM::Attr_impl* newAttr) { return attributes_.setAttributeNodeNS(newAttr); } // setAttributeNodeNS virtual DOM::NodeList_impl* getElementsByTagNameNS(const stringT& namespaceURI, const stringT& localName) const { return new ElementByTagList(ownerDoc_, const_cast(this), namespaceURI, localName); } // getElementsByTagNameNS virtual bool hasAttribute(const stringT& name) const { return attributes_.hasAttribute(name); } // hasAttribute virtual bool hasAttributeNS(const stringT& namespaceURI, const stringT& localName) const { return attributes_.hasAttributeNS(namespaceURI, localName); } // hasAttributeNS /////////////////////////////////////////////////////// // DOM::Node methods virtual DOM::Node_base::Type getNodeType() const { return DOM::Node_base::ELEMENT_NODE; } // getNodeType virtual stringT getNodeName() const { return tagName_; } // getNodeName virtual DOM::NamedNodeMap_impl* getAttributes() const { return const_cast*>(&attributes_); } // getAttributes virtual DOM::Node_impl* cloneNode(bool deep) const { ElementImpl* clone = dynamic_cast(ownerDoc_->createElement(tagName_)); cloneChildren(clone, deep); return clone; } // cloneNode virtual bool hasAttributes() const { return (attributes_.getLength() > 0); } // hasAttributes virtual void setOwnerDoc(DocumentImpl* ownerDoc) { attributes_.setOwnerDoc(ownerDoc); NodeImplWithChildren::setOwnerDoc(ownerDoc); } // setOwnerDoc virtual void setReadOnly(bool readOnly) { attributes_.setReadOnly(readOnly); NodeImplWithChildren::setReadOnly(readOnly); } // setReadOnly protected: void cloneChildren(ElementImpl* clone, bool deep) const { for(unsigned int i = 0; i < attributes_.getLength(); ++i) { DOM::Attr_impl* a = dynamic_cast*>(attributes_.item(i)); if(a->getSpecified()) { DOM::Attr_impl* newA = dynamic_cast*>(a->cloneNode(true)); if(a->getLocalName().empty()) clone->setAttributeNode(newA); else clone->setAttributeNodeNS(newA); } // if ... } // for if(deep) for(DOM::Node_impl* c = getFirstChild(); c != 0; c = c->getNextSibling()) clone->appendChild(c->cloneNode(true)); } // cloneChildren private: virtual void checkChildType(DOM::Node_impl* child) { typename DOM::Node::Type type = child->getNodeType(); if((type != DOM::Node::ELEMENT_NODE) && (type != DOM::Node::TEXT_NODE) && (type != DOM::Node::COMMENT_NODE) && (type != DOM::Node::PROCESSING_INSTRUCTION_NODE) && (type != DOM::Node::CDATA_SECTION_NODE) && (type != DOM::Node::ENTITY_REFERENCE_NODE)) throw DOM::DOMException(DOM::DOMException::HIERARCHY_REQUEST_ERR); } // checkChildType AttrMap attributes_; protected: stringT tagName_; }; // class ElementImpl } // namespace SAX2DOM #endif