Go to the documentation of this file.00001 #include "robot_instance/TokenFactory.h"
00002
00003 using namespace std;
00004 using namespace log4cpp;
00005
00006 TokenMap::value_type TokenFactory::fromXml(const std::string& xml, const std::string& namePrefix)
00007 {
00008 TiXmlDocument file;
00009 file.Parse(xml.c_str());
00010 TiXmlHandle doc(&file);
00011 TiXmlHandle elementHandle(doc.FirstChild("Token"));
00012
00013 string name = "";
00014
00015 if (elementHandle.ToElement()->Attribute("id"))
00016 {
00017 if (namePrefix.size() > 0)
00018 {
00019 name = StringUtilities::makeFullyQualifiedRobonetElement(namePrefix, elementHandle.ToElement()->Attribute("id"));
00020 }
00021 else
00022 {
00023 name = elementHandle.ToElement()->Attribute("id");
00024 }
00025 }
00026 else
00027 {
00028 NasaCommonLogging::Logger::getCategory("gov.nasa.robonet.TokenFactory") << Priority::ERROR << "The attribute [id] was not found for element [" << elementHandle.ToElement()->ValueStr() << "]";
00029 MissingXMLElementException missingXMLElementException;
00030 throw missingXMLElementException;
00031 }
00032
00033 stringstream ss;
00034 uint8_t value8 = 0;
00035 uint16_t value16 = 0;
00036 string valueStr = "";
00037
00038 if (elementHandle.ToElement()->Attribute("value"))
00039 {
00040 valueStr = elementHandle.ToElement()->Attribute("value");
00041 ss << valueStr;
00042 ss >> hex >> value16;
00043
00044 try
00045 {
00046 value8 = boost::numeric_cast<uint8_t>(value16);
00047 }
00048 catch (boost::numeric::positive_overflow& e)
00049 {
00050 NasaCommonLogging::Logger::getCategory("gov.nasa.robonet.TokenFactory") << Priority::ERROR << "Exception caught: bad numeric conversion: positive overflow on element named [" << name << "]";
00051 }
00052 }
00053 else
00054 {
00055 NasaCommonLogging::Logger::getCategory("gov.nasa.robonet.TokenFactory") << Priority::ERROR << "The attribute [value] was not found for element [" << elementHandle.ToElement()->ValueStr() << "]";
00056 MissingXMLElementException missingXMLElementException;
00057 throw missingXMLElementException;
00058 }
00059
00060
00061 NasaCommonLogging::Logger::getCategory("gov.nasa.robonet.TokenFactory") << Priority::INFO << "Token created: [" << name << "] - [" << boost::lexical_cast<std::string>((int16_t)(value8)) << "]";
00062 return TokenMap::value_type(name, value8);
00063 }
00064
00065