00001 #include "SDXNodeContainer.h"
00002 #include "SDXNode.h"
00003
00004 using namespace std;
00005 using namespace SDX;
00006
00007 NodeContainer::NodeContainer() :
00008 m_deallocateContentOnDestruct(false)
00009 {
00010 }
00011
00012 NodeContainer::~NodeContainer(){
00013 if(m_deallocateContentOnDestruct){
00014 for(vector<Node*>::iterator i = m_children.begin(); i != m_children.end(); ++i)
00015 delete *i;
00016 }
00017 }
00018
00019 void NodeContainer::setDeallocateContentOnDestruct(bool v){
00020 m_deallocateContentOnDestruct = v;
00021 }
00022
00023 NodeContainer& NodeContainer::addChild(Node* node){
00024 m_children.push_back(node);
00025
00026 return *this;
00027 }
00028
00029 void NodeContainer::removeChild(unsigned int index, bool deallocate){
00030 if(index >= 0 && index < m_children.size()){
00031 if(deallocate)
00032 delete m_children.at(index);
00033
00034 m_children.erase(m_children.begin() + index);
00035 }
00036 }
00037
00038 void NodeContainer::removeChild(Node* node, bool deallocate){
00039 for(unsigned int i = 0; i < m_children.size(); ++i){
00040 if(m_children.at(i) == node){
00041 removeChild(i, deallocate);
00042 return;
00043 }
00044 }
00045 }
00046
00047 const vector<Node*> NodeContainer::getChildren(){
00048 return m_children;
00049 }
00050
00051 const vector<Node*> NodeContainer::getChildren(std::string nodeName){
00052 vector<Node*> retVal;
00053
00054 for(vector<Node*>::iterator i = m_children.begin(); i != m_children.end(); ++i){
00055 if((*i)->getName() == nodeName)
00056 retVal.push_back(*i);
00057 }
00058
00059 return retVal;
00060 }
00061
00062 Node* NodeContainer::getChild(std::string nodeName){
00063 Node* retVal = 0;
00064
00065 const vector<Node*> childNodes = getChildren(nodeName);
00066 if(childNodes.size() > 0)
00067 retVal = childNodes.back();
00068
00069 return retVal;
00070 }
00071
00072 Node* NodeContainer::getChild(unsigned int index){
00073 Node* retVal = 0;
00074 if(index >= 0 && index < m_children.size())
00075 retVal = m_children[index];
00076
00077 return retVal;
00078 }
00079
00080 Node* NodeContainer::getChild(string nodeName, string attributeValue, string attributeName){
00081 Node* retVal = 0;
00082
00083 for(unsigned int i = 0; i < m_children.size(); ++i){
00084 Node* curNode = m_children.at(i);
00085
00086 vector<Attribute*> curAttributes = attributeName.empty() ? curNode->getUnnamedAttributes() : curNode->getNamedAttributes();
00087 for(unsigned int j = 0; j < curAttributes.size(); ++j){
00088 Attribute* curAttribute = curAttributes.at(j);
00089
00090 string curValue;
00091 if(!curAttribute->getValue(&curValue))
00092 continue;
00093
00094 if(curValue == attributeValue && (attributeName.empty() || curAttribute->getName() == attributeName))
00095 return curNode;
00096 }
00097
00098 }
00099
00100 return retVal;
00101 }