00001 #include <iostream>
00002
00003 #include "SDXNode.h"
00004 #include "SDXAttribute.h"
00005 #include "SDXContentHandler.h"
00006
00007 using namespace std;
00008 using namespace SDX;
00009
00010 Node::Node(string name) :
00011 m_name(name)
00012 {
00013 }
00014
00015 Node::Node(Node& other){
00016 m_name = other.m_name;
00017 for(vector<Attribute*>::iterator i = other.m_attributes.begin(); i != other.m_attributes.end(); ++i)
00018 m_attributes.push_back(new Attribute(*i));
00019
00020 vector<Node*> children = other.getChildren();
00021 for(vector<Node*>::iterator i = children.begin(); i != children.end(); ++i)
00022 addChild(new Node(*i));
00023 }
00024
00025 Node::Node(Node* other){
00026 m_name = other->m_name;
00027 for(vector<Attribute*>::iterator i = other->m_attributes.begin(); i != other->m_attributes.end(); ++i)
00028 m_attributes.push_back(new Attribute(*i));
00029
00030 vector<Node*> children = other->getChildren();
00031 for(vector<Node*>::iterator i = children.begin(); i != children.end(); ++i)
00032 addChild(new Node(*i));
00033 }
00034
00035 Node::~Node(){
00036 if(m_deallocateContentOnDestruct){
00037 for(vector<Attribute*>::iterator i = m_attributes.begin(); i != m_attributes.end(); ++i)
00038 delete *i;
00039 }
00040 }
00041
00042 Node* Node::addAttribute(Attribute* attribute){
00043 m_attributes.push_back(attribute);
00044
00045 return this;
00046 }
00047
00048 Node* Node::addAttribute(string name, string value){
00049 m_attributes.push_back(new Attribute(name, value));
00050
00051 return this;
00052 }
00053
00054 const string Node::getName(){
00055 return m_name;
00056 }
00057
00058 const vector<Attribute*> Node::getAttributes(){
00059 return m_attributes;
00060 }
00061
00062 const vector<Attribute*> Node::getAttributes(string attributeName){
00063 vector<Attribute*> retVal;
00064
00065 for(vector<Attribute*>::iterator i = m_attributes.begin(); i != m_attributes.end(); ++i){
00066 if((*i)->getName() == attributeName)
00067 retVal.push_back(*i);
00068 }
00069
00070 return retVal;
00071 }
00072
00073 Attribute* Node::getAttribute(unsigned int index){
00074 Attribute* retVal = 0;
00075 if(index >= 0 && index < m_attributes.size())
00076 retVal = m_attributes[index];
00077
00078 return retVal;
00079 }
00080
00081 Attribute* Node::getAttribute(std::string attributeName){
00082 const vector<Attribute*> attributes(getAttributes(attributeName));
00083 if(attributes.size() == 0)
00084 return 0;
00085
00086 return attributes.back();
00087 }
00088
00089 const vector<Attribute*> Node::getNamedAttributes(){
00090 vector<Attribute*> retVal;
00091
00092 for(unsigned int i = 0; i < m_attributes.size(); ++i){
00093 Attribute* curAttribute = m_attributes.at(i);
00094 if(curAttribute->isNamedAttribute())
00095 retVal.push_back(curAttribute);
00096 }
00097
00098 return retVal;
00099 }
00100
00101 const vector<Attribute*> Node::getUnnamedAttributes(){
00102 vector<Attribute*> retVal;
00103
00104 for(unsigned int i = 0; i < m_attributes.size(); ++i){
00105 Attribute* curAttribute = m_attributes.at(i);
00106 if(!curAttribute->isNamedAttribute())
00107 retVal.push_back(curAttribute);
00108 }
00109
00110 return retVal;
00111 }
00112
00113 void Node::sendToContentHandler(ContentHandler* contentHandler, bool onlySendContents){
00114 if(!onlySendContents)
00115 contentHandler->startNode(m_name);
00116
00117 for(vector<Attribute*>::iterator i = m_attributes.begin(); i != m_attributes.end(); ++i)
00118 (*i)->sendToContentHandler(contentHandler);
00119
00120 vector<Node*> children = getChildren();
00121 for(vector<Node*>::iterator i = children.begin(); i != children.end(); ++i)
00122 (*i)->sendToContentHandler(contentHandler);
00123
00124 if(!onlySendContents)
00125 contentHandler->endNode();
00126 }