XMLPrintVisitor.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 <sofa/simulation/common/XMLPrintVisitor.h>
00026 #include <sofa/helper/Factory.h>
00027 #include <sofa/simulation/common/Node.h>
00028 #include <sofa/core/componentmodel/behavior/InteractionForceField.h>
00029 #include <sofa/core/componentmodel/behavior/InteractionConstraint.h>
00030 
00031 namespace sofa
00032 {
00033 
00034 namespace simulation
00035 {
00036 
00037 
00038 static std::string xmlencode(const std::string& str)
00039 {
00040     std::string res;
00041     for (unsigned int i=0; i<str.length(); ++i)
00042     {
00043         switch(str[i])
00044         {
00045         case '<': res += "&lt;"; break;
00046         case '>': res += "&gt;"; break;
00047         case '&': res += "&amp;"; break;
00048         case '"': res += "&quot;"; break;
00049         case '\'': res += "&apos;"; break;
00050         default:  res += str[i];
00051         }
00052     }
00053     return res;
00054 }
00055 
00056 template<class T>
00057 void XMLPrintVisitor::processObject(T obj)
00058 {
00059     for (int i=0;i<level;i++)
00060         m_out << "\t";
00061 
00062     std::string classname = obj->getClassName();
00063     std::string templatename = obj->getTemplateName();
00064     
00065     m_out << "<Object type=\"" << xmlencode(classname) << "\"";
00066     if (!templatename.empty())
00067         m_out << " template=\"" << xmlencode(templatename) << "\"";
00068        
00069     m_out << ">\n";
00070     
00071     obj->xmlWriteDatas( m_out, level+1 );
00072     
00073     
00074     for (int i=0;i<level;i++)
00075       m_out << "\t";
00076     m_out << "</Object>" << std::endl;
00077 }
00078 
00079 template<class Seq>
00080 void XMLPrintVisitor::processObjects(Seq& list)
00081 {
00082     if (list.empty()) return;
00083     // the following line breaks the compilator on Visual2003
00084     //for_each<XMLPrintVisitor, Seq, typename Seq::value_type>(this, list, &XMLPrintVisitor::processObject<typename Seq::value_type>);
00085     for (typename Seq::iterator it = list.begin(); it != list.end(); ++it)
00086     {
00087         typename Seq::value_type obj = *it;
00088         this->processObject<typename Seq::value_type>(obj);
00089     }
00090 }
00091 
00092 Visitor::Result XMLPrintVisitor::processNodeTopDown(simulation::Node* node)
00093 {
00094     for (int i=0;i<level;i++)
00095         m_out << "\t";
00096     m_out << "<Node ";
00097     
00098     ++level;
00099         node->xmlWriteNodeDatas(m_out,level);     
00100     
00101     for (int i=0;i<level;i++)
00102       m_out << "\t";   
00103         m_out << " >\n";
00104 
00105     if (node->mechanicalMapping != NULL)
00106         (*(node->mechanicalMapping.begin()))->disable();
00107 
00108     //processObjects(node->object);
00109     // BUGFIX(Jeremie A.): filter objects to output interactions classes after the children nodes to resolve dependencies at creation time
00110     for (simulation::Node::ObjectIterator it = node->object.begin(); it != node->object.end(); ++it)
00111     {
00112         sofa::core::objectmodel::BaseObject* obj = *it;
00113         if (   dynamic_cast<sofa::core::componentmodel::behavior::InteractionForceField*> (obj) == NULL
00114             && dynamic_cast<sofa::core::componentmodel::behavior::InteractionConstraint*> (obj) == NULL )
00115             this->processObject(obj);
00116     }
00117     
00118     return RESULT_CONTINUE;
00119 }
00120 
00121 void XMLPrintVisitor::processNodeBottomUp(simulation::Node* node)
00122 {
00123     for (simulation::Node::ObjectIterator it = node->object.begin(); it != node->object.end(); ++it)
00124     {
00125         sofa::core::objectmodel::BaseObject* obj = *it;
00126         if (   dynamic_cast<sofa::core::componentmodel::behavior::InteractionForceField*> (obj) != NULL
00127             || dynamic_cast<sofa::core::componentmodel::behavior::InteractionConstraint*> (obj) != NULL )
00128             this->processObject(obj);
00129     }
00130     --level;
00131     
00132     for (int i=0;i<level;i++)
00133         m_out << "\t";
00134     m_out << "</Node>"<<std::endl;
00135     
00136 }
00137 
00138 } // namespace simulation
00139 
00140 } // namespace sofa
00141 

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