BaseElement.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "BaseElement.h"
00026 #include <sofa/helper/Factory.inl>
00027 #include <sofa/helper/system/SetDirectory.h>
00028 #include <string.h>
00029 namespace sofa
00030 {
00031
00032 namespace simulation
00033 {
00034
00035 namespace tree
00036 {
00037
00038 namespace xml
00039 {
00040
00041 BaseElement::BaseElement(const std::string& name, const std::string& type, BaseElement* newParent)
00042 : BaseObjectDescription(name.c_str(), type.c_str()), parent(NULL), groupType(false)
00043 {
00044 if (newParent!=NULL) newParent->addChild(this);
00045
00046
00047 }
00048
00049 BaseElement::~BaseElement()
00050 {
00051
00052
00053 for (ChildList::iterator it = children.begin();
00054 it != children.end(); ++it)
00055 {
00056 delete *it;
00057 }
00058 children.clear();
00059 }
00060
00062 std::string BaseElement::getBaseFile()
00063 {
00064 if (isFileRoot()) return basefile;
00065 else if (getParentElement()!=NULL) return getParentElement()->getBaseFile();
00066 else return "";
00067 }
00068
00069 void BaseElement::setBaseFile(const std::string& newBaseFile)
00070 {
00071 basefile = newBaseFile;
00072 }
00073
00075 bool BaseElement::isFileRoot()
00076 {
00077 return !basefile.empty();
00078 }
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00091 void BaseElement::setAttribute(const std::string& attr, const char* val)
00092 {
00093 attributes[attr] = val;
00094 }
00095
00096 bool BaseElement::presenceAttribute(const std::string& s)
00097 {
00098 return (attributes.find(s) != attributes.end());
00099 }
00101 bool BaseElement::removeAttribute(const std::string& attr)
00102 {
00103 AttributeMap::iterator it = attributes.find(attr);
00104 if (it == attributes.end())
00105 return false;
00106
00107
00108
00109
00110
00111 attributes.erase(it);
00112 return true;
00113 }
00114
00115 bool BaseElement::addChild(BaseElement* child)
00116 {
00117 if (child->getParent()==this) return false;
00118 BaseElement* oldParent = child->getParentElement();
00119 if (!child->setParent(this)) return false;
00120 if (oldParent != NULL)
00121 {
00122 oldParent->removeChild(child);
00123 }
00124 children.push_back(child);
00125 return true;
00126 }
00127
00128 bool BaseElement::removeChild(BaseElement* child)
00129 {
00130 if (child->getParent()!=this) return false;
00131 ChildList::iterator it = children.begin();
00132 while (it!=children.end())
00133 {
00134 if (*it == child)
00135 {
00136 child->setParent(NULL);
00137 children.erase(it);
00138 return true;
00139 }
00140 }
00141 return false;
00142 }
00143
00144 bool BaseElement::init()
00145 {
00146 sofa::helper::system::SetDirectory cwd(basefile);
00147 bool res = initNode();
00148 for (child_iterator<> it = begin(); it != end(); ++it)
00149 {
00150 res &= it->init();
00151 }
00152 return res;
00153 }
00154
00155 BaseElement* BaseElement::Create(const std::string& nodeClass, const std::string& name, const std::string& type)
00156 {
00157 return NodeFactory::CreateObject(nodeClass, std::pair<std::string,std::string>(name, type));
00158 }
00159
00161 BaseElement* BaseElement::findNode(const char* nodeName, bool absolute)
00162 {
00163 if (nodeName == NULL) return NULL;
00164 if (nodeName[0]=='\\' || nodeName[0]=='/')
00165 {
00166 if (!absolute && getParentElement()!=NULL)
00167 return getParentElement()->findNode(nodeName);
00168 else
00169 { ++nodeName; absolute = true; }
00170 }
00171 if (nodeName[0]=='\0')
00172 {
00173 if (absolute) return this;
00174 else return NULL;
00175 }
00176 const char* sep = nodeName;
00177 while (*sep!='\0' && *sep!='\\' && *sep!='/')
00178 ++sep;
00179 if (!strncmp(nodeName,".",sep-nodeName))
00180 return findNode(sep, true);
00181 if (!strncmp(nodeName,"..",sep-nodeName))
00182 {
00183 if (getParentElement()==NULL) return NULL;
00184 else return getParentElement()->findNode(sep,true);
00185 }
00186 for (child_iterator<> it = begin(); it != end(); ++it)
00187 {
00188 if (it->getName().length() == (unsigned)(sep-nodeName) && !strncmp(it->getName().c_str(), nodeName, sep-nodeName))
00189 {
00190 BaseElement* res = it->findNode(sep,true);
00191 if (res!=NULL) return res;
00192 }
00193 }
00194 if (!absolute && getParentElement()!=NULL)
00195 return getParentElement()->findNode(nodeName);
00196 else
00197 return NULL;
00198 }
00199
00200 }
00201
00202 }
00203
00204 }
00205
00206 }