BaseElement.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
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     //attributes["name"]=&this->name;
00046     //attributes["type"]=&this->type;
00047 }
00048 
00049 BaseElement::~BaseElement()
00050 {
00051     //attributes.erase("name");
00052     //attributes.erase("type");
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 // const std::map<std::string,std::string*>& BaseElement::getAttributeMap() const
00081 // {
00082 //  return attributes;
00083 // }
00084 // 
00085 // std::map<std::string,std::string*>& BaseElement::getAttributeMap()
00086 // {
00087 //  return attributes;
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     //if (it->second == &name)
00107     //    return false;
00108     //if (it->second == &type)
00109     //    return false;
00110     //delete it->second;
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 } // namespace xml
00201 
00202 } // namespace tree
00203 
00204 } // namespace simulation
00205 
00206 } // namespace sofa

Generated on Sun Nov 23 04:05:48 2008 for Sofa by  doxygen 1.5.7.1