RegisterPropertiesFactory.cpp
Go to the documentation of this file.
00001 #include "robot_instance/RegisterPropertiesFactory.h"
00002 
00003 using namespace std;
00004 using namespace log4cpp;
00005 
00006 RegisterPropertiesFactory::RegisterPropertiesFactory()
00007 {
00008 }
00009 
00010 RegisterPropertiesFactory::~RegisterPropertiesFactory()
00011 {
00012 }
00013 
00014 RegisterPropertiesPtr RegisterPropertiesFactory::fromXml(const std::string& xml)
00015 {
00016     TiXmlDocument file;
00017     file.Parse(xml.c_str());
00018     TiXmlHandle doc(&file);
00019     TiXmlHandle registerHandle(doc.FirstChild("Register"));
00020 
00021     RegisterPropertiesPtr registerProperties = boost::make_shared<RegisterProperties>();
00022 
00023     if (registerHandle.ToElement())
00024     {
00025         if (registerHandle.ToElement()->Attribute("type"))
00026         {
00027             if (string(registerHandle.ToElement()->Attribute("type")) == string("Control"))
00028             {
00029                 registerProperties->isWritable = true;
00030             }
00031         }
00032 
00033         // Check for Certificate
00034         TiXmlHandle certificateElement(registerHandle.FirstChildElement("Certificate"));
00035 
00036         if (certificateElement.ToElement())
00037         {
00038             if (certificateElement.ToElement()->Attribute("value"))
00039             {
00040                 uint16_t value;
00041                 stringstream ss;
00042                 ss << std::hex << certificateElement.ToElement()->Attribute("value");
00043                 ss >> value;
00044                 registerProperties->REGISTER_CERTIFICATE_VALUE = value;
00045             }
00046             else
00047             {
00048                 registerProperties->REGISTER_CERTIFICATE_VALUE = 0;
00049                 NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::WARN, "No certificate value found, using default of [0x0000].");
00050             }
00051 
00052             if (certificateElement.ToElement()->Attribute("mask"))
00053             {
00054                 uint16_t mask;
00055                 stringstream ss;
00056                 ss << std::hex << certificateElement.ToElement()->Attribute("mask");
00057                 ss >> mask;
00058                 registerProperties->REGISTER_CERTIFICATE_MASK = mask;
00059             }
00060             else
00061             {
00062                 registerProperties->REGISTER_CERTIFICATE_MASK = 0;
00063                 NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::WARN, "No certificate mask found, using default of [0x0000].");
00064             }
00065         }
00066         else
00067         {
00068             registerProperties->REGISTER_CERTIFICATE_VALUE = 0;
00069             registerProperties->REGISTER_CERTIFICATE_MASK = 0;
00070             NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::WARN, "The element [Certificate] was not found, using defaults for value and mask [0x0000].");
00071         }
00072 
00073         NasaCommonLogging::Logger::getCategory("gov.nasa.robonet.RegisterPropertiesFactory") << Priority::INFO << "Register property [REGISTER_CERTIFICATE_VALUE]: 0x" << std::hex << registerProperties->REGISTER_CERTIFICATE_VALUE;
00074         NasaCommonLogging::Logger::getCategory("gov.nasa.robonet.RegisterPropertiesFactory") << Priority::INFO << "Register property [REGISTER_CERTIFICATE_MASK]: 0x" << std::hex << registerProperties->REGISTER_CERTIFICATE_MASK;
00075 
00076         // Check for Entries
00077         TiXmlHandle entriesElement(registerHandle.FirstChildElement("Entries"));
00078 
00079         if (certificateElement.ToElement())
00080         {
00081             TiXmlHandle currentEntryElement = 0;
00082 
00083             currentEntryElement = entriesElement.FirstChild("Entry");
00084 
00085             while (currentEntryElement.ToElement())
00086             {
00087                 string id("");
00088                 string description("");
00089                 BitMinion::BitIndexEnumType index = BitMinion::BIT_ZERO_INDEX;
00090                 BitMinion::BitCountMaskEnumType size = BitMinion::ONE_BIT_MASK;
00091 
00092                 if (currentEntryElement.ToElement()->Attribute("id"))
00093                 {
00094                     id = currentEntryElement.ToElement()->Attribute("id");
00095 
00096                     if (id.length() == 0)
00097                     {
00098                         NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::ERROR, "The attribute [id] is malformed.");
00099                         MissingXMLElementException missingXMLElementException;
00100                         throw missingXMLElementException;
00101                     }
00102                 }
00103                 else
00104                 {
00105                     NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::ERROR, "The attribute [id] was not found for element [Entry].");
00106                     MissingXMLElementException missingXMLElementException;
00107                     throw missingXMLElementException;
00108                 }
00109 
00110                 NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::DEBUG, "Attribute [id] parsed.");
00111 
00112                 if (currentEntryElement.ToElement()->Attribute("index"))
00113                 {
00114                     try
00115                     {
00116                         index = (BitMinion::BitIndexEnumType)boost::lexical_cast<unsigned int>(currentEntryElement.ToElement()->Attribute("index"));
00117                     }
00118                     catch (boost::bad_lexical_cast)
00119                     {
00120                         NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::ERROR, "The attribute [index] is malformed.");
00121                         MissingXMLElementException missingXMLElementException;
00122                         throw missingXMLElementException;
00123                     }
00124                 }
00125                 else
00126                 {
00127                     NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::ERROR, "The attribute [index] was not found for element [Entry].");
00128                     MissingXMLElementException missingXMLElementException;
00129                     throw missingXMLElementException;
00130                 }
00131 
00132                 NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::DEBUG, "Attribute [index] parsed.");
00133 
00134                 if (currentEntryElement.ToElement()->Attribute("size"))
00135                 {
00136                     try
00137                     {
00138                         size = (BitMinion::BitCountMaskEnumType)((1 << boost::lexical_cast<unsigned int>(currentEntryElement.ToElement()->Attribute("size"))) - 1);
00139                     }
00140                     catch (boost::bad_lexical_cast)
00141                     {
00142                         NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::ERROR, "The attribute [size] is malformed.");
00143                         MissingXMLElementException missingXMLElementException;
00144                         throw missingXMLElementException;
00145                     }
00146                 }
00147                 else
00148                 {
00149                     NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::ERROR, "The attribute [size] was not found for element [Entry].");
00150                     MissingXMLElementException missingXMLElementException;
00151                     throw missingXMLElementException;
00152                 }
00153 
00154                 NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::DEBUG, "Attribute [size] parsed.");
00155 
00156                 TiXmlHandle descriptionElement(currentEntryElement.FirstChildElement("Description"));
00157 
00158                 if (descriptionElement.ToElement())
00159                 {
00160                     if (descriptionElement.ToElement()->GetText())
00161                     {
00162                         description = descriptionElement.ToElement()->GetText();
00163                     }
00164                 }
00165                 else
00166                 {
00167                     NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::WARN, "The element [Description] was not found, using default of [\"\"].");
00168                 }
00169 
00170                 NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::DEBUG, "Attribute [description] parsed.");
00171 
00172                 registerProperties->registerProperty[id] = PropertyDef(id, description, index, size);
00173 
00174                 NasaCommonLogging::Logger::getCategory("gov.nasa.robonet.RegisterPropertiesFactory") << Priority::INFO << "Register property [" << id << "]: index=" << index << " size=" << size << " desc=\"" << description << "\"";
00175 
00176                 currentEntryElement = currentEntryElement.ToElement()->NextSiblingElement("Entry");
00177             }
00178 
00179             NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::INFO, "Property map is populated.");
00180 
00181             for (PropertyDefMap::iterator it = registerProperties->registerProperty.begin(); it != registerProperties->registerProperty.end(); it++)
00182             {
00183                 registerProperties->registerPropertyVector.push_back(&(it->second));
00184             }
00185 
00186             sort(registerProperties->registerPropertyVector.begin(), registerProperties->registerPropertyVector.end(), RegisterProperties::comparePropertyDefPointers());
00187 
00188             NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::INFO, "Property map vector is populated and sorted.");
00189         }
00190 
00191         return registerProperties;
00192     }
00193     else
00194     {
00195         NasaCommonLogging::Logger::log("gov.nasa.robonet.RegisterPropertiesFactory", Priority::ERROR, "The element [Register] was not found.");
00196         MissingXMLElementException missingXMLElementException;
00197         throw missingXMLElementException;
00198     }
00199 }
00200 


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