GripperSetpointFactory.cpp
Go to the documentation of this file.
00001 #include "robot_instance/GripperSetpointFactory.h"
00002 
00003 using namespace std;
00004 using namespace log4cpp;
00005 
00006 GripperSetpointMap::value_type GripperSetpointFactory::fromXml(const std::string& xml)
00007 {
00008     TiXmlDocument file;
00009     file.Parse(xml.c_str());
00010     TiXmlHandle doc(&file);
00011     TiXmlHandle elementHandle(doc.FirstChild("Setpoint"));
00012 
00013     if (elementHandle.ToElement())
00014     {
00015         string name = "";
00016 
00017         if (elementHandle.ToElement()->Attribute("id"))
00018         {
00019             name = elementHandle.ToElement()->Attribute("id");
00020         }
00021         else
00022         {
00023             stringstream err;
00024             err << "The attribute [id] was not found for element [" << elementHandle.ToElement()->ValueStr() << "]";
00025             NasaCommonLogging::Logger::log("gov.nasa.configuration.GripperSetpointFactory", Priority::ERROR, err.str());
00026             MissingXMLElementException missingXMLElementException;
00027             throw missingXMLElementException;
00028         }
00029 
00030         GripperSetpoint setpoint;
00031 
00032         TiXmlHandle jawPositionElement(elementHandle.FirstChildElement("JawPosition"));
00033 
00034         if (jawPositionElement.ToElement())
00035         {
00036             setpoint.jawPosition      = boost::lexical_cast<float>(Private::getAttribute(jawPositionElement, "value"));
00037             setpoint.jawPositionDelta = boost::lexical_cast<float>(Private::getAttribute(jawPositionElement, "delta"));
00038         }
00039         else
00040         {
00041             stringstream err;
00042             err << "Setpoint [" << name << "] has no element named [JawPosition]";
00043             NasaCommonLogging::Logger::log("gov.nasa.configuration.GripperSetpointFactory", Priority::INFO, err.str());
00044             throw runtime_error(err.str());
00045         }
00046 
00047         TiXmlHandle expectedLoadElement(elementHandle.FirstChildElement("ExpectedLoad"));
00048 
00049         if (expectedLoadElement.ToElement())
00050         {
00051             setpoint.expectedLoad      = boost::lexical_cast<float>(Private::getAttribute(expectedLoadElement, "value"));
00052             setpoint.expectedLoadDelta = boost::lexical_cast<float>(Private::getAttribute(expectedLoadElement, "delta"));
00053         }
00054         else
00055         {
00056             stringstream err;
00057             err << "Setpoint [" << name << "] has no element named [ExpectedLoad]";
00058             NasaCommonLogging::Logger::log("gov.nasa.configuration.GripperSetpointFactory", Priority::INFO, err.str());
00059             throw runtime_error(err.str());
00060         }
00061 
00062         // Create and return the new setpoint
00063         return GripperSetpointMap::value_type(name, setpoint);
00064     }
00065     else
00066     {
00067         stringstream err;
00068         err << "The element [Setpoint] was not found.";
00069         NasaCommonLogging::Logger::log("gov.nasa.configuration.GripperSetpointFactory", Priority::ERROR, err.str());
00070         MissingXMLElementException missingXMLElementException;
00071         throw missingXMLElementException;
00072     }
00073 }
00074 
00075 void GripperSetpointFactory::fromFile(const std::string& filename, GripperSetpointMapPtr setpointMap)
00076 {
00077     TiXmlDocument file(filename.c_str());
00078     bool          loadOkay = file.LoadFile();
00079 
00080     if (!loadOkay)
00081     {
00082         stringstream err;
00083         err << "Failed to load file [" << filename << "]";
00084         NasaCommonLogging::Logger::log("gov.nasa.configuration.GripperSetpointFactory", Priority::FATAL, err.str());
00085         throw runtime_error(err.str());
00086     }
00087 
00088     TiXmlHandle doc(&file);
00089     NasaCommonLogging::Logger::log("gov.nasa.configuration.GripperSetpointFactory", Priority::INFO, "GripperSetpointFile [" + filename + "] successfully loaded.");
00090 
00091     setpointMap->clear();
00092 
00093     string elementString;
00094 
00095     // Print the XML file's contents
00096     // cout << "Successfully loaded file: " << filename << endl;
00097     // TiXmlPrinter printer;
00098     // printer.SetIndent("\t");
00099     // file.Accept(&printer);
00100     // cout << printer.Str() << endl;
00101 
00102     // Check for GripperSetpoints
00103     if (!(doc.FirstChildElement("GripperSetpoints").ToElement()))
00104     {
00105         stringstream err;
00106         err << "The file " << filename << " has no element named [GripperSetpoints]";
00107         NasaCommonLogging::Logger::log("gov.nasa.configuration.GripperSetpointFactory", Priority::ERROR, err.str());
00108         throw runtime_error(err.str());
00109     }
00110 
00111     // Check for SetPoints
00112     TiXmlHandle setpointsElement(doc.FirstChildElement("GripperSetpoints").FirstChildElement("Setpoints"));
00113 
00114     if (setpointsElement.ToElement())
00115     {
00116         TiXmlHandle currentSetpointElement = 0;
00117 
00118         // Iterate over SetPoints
00119         currentSetpointElement = setpointsElement.FirstChild("Setpoint");
00120 
00121         while (currentSetpointElement.ToElement())
00122         {
00123             elementString.clear();
00124             elementString << *(currentSetpointElement.ToElement());
00125             elementString.append("\n");
00126 
00127             GripperSetpointMap::value_type newGripperSetpointMapMember(fromXml(elementString));
00128 
00129             if (setpointMap->find(newGripperSetpointMapMember.first) == setpointMap->end())
00130             {
00131                 setpointMap->insert(newGripperSetpointMapMember);
00132             }
00133             else
00134             {
00135                 NasaCommonLogging::Logger::log("gov.nasa.configuration.GripperSetpointFactory", Priority::WARN, "setpointMap already contains element with key = " + newGripperSetpointMapMember.first);
00136             }
00137 
00138             currentSetpointElement = currentSetpointElement.ToElement()->NextSiblingElement("Setpoint");
00139         }
00140     }
00141     else
00142     {
00143         stringstream err;
00144         err << "The file " << filename << " has no element named [Setpoints]";
00145         NasaCommonLogging::Logger::log("gov.nasa.configuration.GripperSetpointFactory", Priority::ERROR, err.str());
00146         throw runtime_error(err.str());
00147     }
00148 }
00149 
00157 const std::string GripperSetpointFactory::Private::getAttribute(TiXmlHandle handle, const std::string& attribute, bool required)
00158 {
00159     if (handle.ToElement()->Attribute(attribute))
00160     {
00161         return *(handle.ToElement()->Attribute(attribute));
00162     }
00163     else
00164     {
00165         if (required)
00166         {
00167             stringstream err;
00168             err << "The attribute [" << attribute << "] was not found for element [" << handle.ToElement()->ValueStr() << "]";
00169             NasaCommonLogging::Logger::log("gov.nasa.configuration.GripperSetpointFactory", Priority::ERROR, err.str());
00170             throw runtime_error(err.str());
00171         }
00172 
00173         return "";
00174     }
00175 }


robot_instance
Author(s):
autogenerated on Sat Jun 8 2019 20:43:12