arabica/include/DOM/Simple/ElementImpl.hpp

255 lines
9.3 KiB
C++
Raw Normal View History

2002-06-21 13:16:28 +02:00
#ifndef JEZUK_SimpleDOM_ELEMENTIMPL_H
#define JEZUK_SimpleDOM_ELEMENTIMPL_H
2007-09-05 00:55:47 +02:00
#include <DOM/Element.hpp>
#include <DOM/Simple/NodeImpl.hpp>
#include <DOM/Simple/ElementByTagImpl.hpp>
#include <DOM/Simple/AttrMap.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 ElementImpl : public DOM::Element_impl<stringT, string_adaptorT>,
2002-06-21 13:16:28 +02:00
public NodeImplWithChildren<stringT, string_adaptorT>
{
protected:
typedef NodeImplWithChildren<stringT, string_adaptorT> NodeT;
typedef DocumentImpl<stringT, string_adaptorT> DocumentImplT;
typedef AttrMap<stringT, string_adaptorT> AttrMapT;
typedef ElementByTagList<stringT, string_adaptorT> ElementByTagListT;
typedef DOM::Node_impl<stringT, string_adaptorT> DOMNode_implT;
typedef DOM::Attr_impl<stringT, string_adaptorT> DOMAttr_implT;
typedef DOM::NodeList_impl<stringT, string_adaptorT> DOMNodeList_implT;
typedef DOM::NamedNodeMap_impl<stringT, string_adaptorT> DOMNamedNodeMap_implT;
typedef DOM::Element_impl<stringT, string_adaptorT> DOMElement_implT;
typedef DOM::Events::EventTarget<stringT, string_adaptorT> EventTargetT;
typedef DOM::Events::MutationEvent<stringT, string_adaptorT> MutationEventT;
2004-09-14 22:51:36 +02:00
2002-06-21 13:16:28 +02:00
public:
ElementImpl(DocumentImplT* ownerDoc, const stringT& tagName) :
DOMElement_implT(),
NodeT(ownerDoc),
2002-06-21 13:16:28 +02:00
attributes_(ownerDoc),
tagName_(ownerDoc->stringPool(tagName))
2002-06-21 13:16:28 +02:00
{
attributes_.setOwnerElement(this);
} // ElementImpl
virtual ~ElementImpl()
{
} // ~ElementImpl
/////////////////////////////////////////////////////
// DOM::Element functions
2005-12-09 15:09:08 +01:00
virtual const stringT& getTagName() const { return getNodeName(); }
2002-06-21 13:16:28 +02:00
virtual const stringT& getAttribute(const stringT& name) const
2002-06-21 13:16:28 +02:00
{
return attributes_.getAttribute(name);
} // getAttribute
virtual void setAttribute(const stringT& name, const stringT& value)
{
this->checkName(name);
stringT oldValue = getAttribute(name);
2002-06-21 13:16:28 +02:00
attributes_.setAttribute(name, value);
MutationEventT mutationEvent(NodeT::ownerDoc_->createEvent("MutationEvent"));
if (!string_adaptorT::empty(oldValue)) {
mutationEvent.initMutationEvent("DOMAttrModified", true, false, Arabica::DOM::Node<stringT, string_adaptorT>(this), string_adaptorT::empty_string(), value, name, MutationEventT::MODIFICATION);
} else {
mutationEvent.initMutationEvent("DOMAttrModified", true, false, Arabica::DOM::Node<stringT, string_adaptorT>(this), oldValue, value, name, MutationEventT::ADDITION);
}
EventTargetT eventTarget(this);
eventTarget.dispatchEvent(mutationEvent);
2002-06-21 13:16:28 +02:00
} // setAttribute
virtual void removeAttribute(const stringT& name)
{
stringT oldValue = getAttribute(name);
2002-06-21 13:16:28 +02:00
attributes_.removeAttribute(name);
// dispatch DOMAttrModified event
MutationEventT mutationEvent(NodeT::ownerDoc_->createEvent("MutationEvent"));
mutationEvent.initMutationEvent("DOMAttrModified", true, false, Arabica::DOM::Node<stringT, string_adaptorT>(this), oldValue, string_adaptorT::empty_string(), name, MutationEventT::REMOVAL);
EventTargetT eventTarget(this);
eventTarget.dispatchEvent(mutationEvent);
2002-06-21 13:16:28 +02:00
} // removeAttribute
virtual DOMAttr_implT* getAttributeNode(const stringT& name) const
2002-06-21 13:16:28 +02:00
{
return attributes_.getAttributeNode(name);
} // getAttributeNode
virtual DOMAttr_implT* setAttributeNode(DOMAttr_implT* newAttr)
2002-06-21 13:16:28 +02:00
{
return attributes_.setAttributeNode(newAttr);
} // setAttributeNode
virtual DOMAttr_implT* removeAttributeNode(DOMAttr_implT* oldAttr)
2002-06-21 13:16:28 +02:00
{
return attributes_.removeAttributeNode(oldAttr);
} // removeAttributeNode
virtual DOMNodeList_implT* getElementsByTagName(const stringT& tagName) const
2002-06-21 13:16:28 +02:00
{
return new ElementByTagListT(NodeT::ownerDoc_,
const_cast<ElementImpl*>(this),
tagName);
2002-06-21 13:16:28 +02:00
} // 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)
{
this->checkName(qualifiedName);
stringT oldValue = getAttribute(qualifiedName);
2002-06-21 13:16:28 +02:00
attributes_.setAttributeNS(namespaceURI, qualifiedName, value);
// dispatch DOMAttrModified event
MutationEventT mutationEvent(NodeT::ownerDoc_->createEvent("MutationEvent"));
if (!string_adaptorT::empty(oldValue)) {
mutationEvent.initMutationEvent("DOMAttrModified", true, false, Arabica::DOM::Node<stringT, string_adaptorT>(this), string_adaptorT::empty_string(), value, qualifiedName, MutationEventT::MODIFICATION);
} else {
mutationEvent.initMutationEvent("DOMAttrModified", true, false, Arabica::DOM::Node<stringT, string_adaptorT>(this), oldValue, value, qualifiedName, MutationEventT::ADDITION);
}
EventTargetT eventTarget(this);
eventTarget.dispatchEvent(mutationEvent);
2002-06-21 13:16:28 +02:00
} // setAttributeNS
virtual void removeAttributeNS(const stringT& namespaceURI, const stringT& localName)
2002-06-21 13:16:28 +02:00
{
stringT oldValue = getAttributeNS(namespaceURI, localName);
2002-06-21 13:16:28 +02:00
attributes_.removeAttributeNS(namespaceURI, localName);
// dispatch DOMAttrModified event
MutationEventT mutationEvent(NodeT::ownerDoc_->createEvent("MutationEvent"));
mutationEvent.initMutationEvent("DOMAttrModified", true, false, Arabica::DOM::Node<stringT, string_adaptorT>(this), oldValue, string_adaptorT::empty_string(), localName, MutationEventT::REMOVAL);
EventTargetT eventTarget(this);
eventTarget.dispatchEvent(mutationEvent);
2002-06-21 13:16:28 +02:00
} // removeAttributeNS
virtual DOMAttr_implT* getAttributeNodeNS(const stringT& namespaceURI, const stringT& localName) const
2002-06-21 13:16:28 +02:00
{
return attributes_.getAttributeNodeNS(namespaceURI, localName);
} // getAttributeNodeNS
virtual DOMAttr_implT* setAttributeNodeNS(DOMAttr_implT* newAttr)
2002-06-21 13:16:28 +02:00
{
return attributes_.setAttributeNodeNS(newAttr);
} // setAttributeNodeNS
virtual DOMNodeList_implT* getElementsByTagNameNS(const stringT& namespaceURI, const stringT& localName) const
2002-06-21 13:16:28 +02:00
{
return new ElementByTagListT(NodeT::ownerDoc_,
const_cast<ElementImpl*>(this),
namespaceURI,
localName);
2002-06-21 13:16:28 +02:00
} // 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
2005-12-09 15:09:08 +01:00
virtual const stringT& getNodeName() const
2002-06-21 13:16:28 +02:00
{
return *tagName_;
2002-06-21 13:16:28 +02:00
} // getNodeName
virtual DOMNamedNodeMap_implT* getAttributes() const
2002-06-21 13:16:28 +02:00
{
return const_cast<AttrMapT*>(&attributes_);
2002-06-21 13:16:28 +02:00
} // getAttributes
virtual DOMNode_implT* cloneNode(bool deep) const
2002-06-21 13:16:28 +02:00
{
ElementImpl* clone = dynamic_cast<ElementImpl*>(NodeT::ownerDoc_->createElement(*tagName_));
2002-06-21 13:16:28 +02:00
cloneChildren(clone, deep);
return clone;
} // cloneNode
virtual bool hasAttributes() const
{
return (attributes_.getLength() > 0);
} // hasAttributes
virtual void setOwnerDoc(DocumentImplT* ownerDoc)
2002-06-21 13:16:28 +02:00
{
attributes_.setOwnerDoc(ownerDoc);
NodeT::setOwnerDoc(ownerDoc);
2002-06-21 13:16:28 +02:00
} // setOwnerDoc
virtual void setReadOnly(bool readOnly)
{
attributes_.setReadOnly(readOnly);
NodeT::setReadOnly(readOnly);
2002-06-21 13:16:28 +02:00
} // setReadOnly
protected:
void cloneChildren(ElementImpl* clone, bool deep) const
{
for(unsigned int i = 0; i < attributes_.getLength(); ++i)
{
DOMAttr_implT* a = dynamic_cast<DOMAttr_implT*>(attributes_.item(i));
2002-06-21 13:16:28 +02:00
if(a->getSpecified())
{
DOMAttr_implT* newA = dynamic_cast<DOMAttr_implT*>(a->cloneNode(true));
if(string_adaptorT::empty(a->getLocalName()))
2002-06-21 13:16:28 +02:00
clone->setAttributeNode(newA);
else
clone->setAttributeNodeNS(newA);
} // if ...
} // for
if(deep)
for(DOMNode_implT* c = NodeT::getFirstChild(); c != 0; c = c->getNextSibling())
2002-06-21 13:16:28 +02:00
clone->appendChild(c->cloneNode(true));
} // cloneChildren
private:
virtual void checkChildType(DOMNode_implT* child)
{
typename DOM::Node_base::Type type = child->getNodeType();
if((type != DOM::Node_base::ELEMENT_NODE) &&
(type != DOM::Node_base::TEXT_NODE) &&
(type != DOM::Node_base::COMMENT_NODE) &&
(type != DOM::Node_base::PROCESSING_INSTRUCTION_NODE) &&
(type != DOM::Node_base::CDATA_SECTION_NODE) &&
(type != DOM::Node_base::ENTITY_REFERENCE_NODE))
2002-06-21 13:16:28 +02:00
throw DOM::DOMException(DOM::DOMException::HIERARCHY_REQUEST_ERR);
} // checkChildType
AttrMapT attributes_;
2002-06-21 13:16:28 +02:00
protected:
stringT const* tagName_;
2002-06-21 13:16:28 +02:00
}; // class ElementImpl
} // namespace SAX2DOM
2007-09-05 13:47:13 +02:00
} // namespace Arabica
2002-06-21 13:16:28 +02:00
#endif