Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "stdr_parser/stdr_xml_parser.h"
00023
00024 namespace stdr_parser
00025 {
00026
00031 XmlParser::XmlParser(void)
00032 {
00033
00034 }
00035
00041 void XmlParser::parse(std::string file_name, Node* base_node)
00042 {
00043
00044 std::string path = file_name;
00045 TiXmlDocument doc;
00046 doc.SetTabSize(2);
00047 bool loadOkay = doc.LoadFile(path.c_str());
00048 if (!loadOkay)
00049 {
00050 std::string error =
00051 std::string("Failed to load file '") +
00052 path + std::string("'") +
00053 std::string("\nError was '") + std::string(doc.ErrorDesc()) +
00054 std::string("'\nIf error was 'Error reading end tag' you have a \
00055 malformed xml file");
00056 throw ParserException(error);
00057 }
00058 base_node->file_origin = path;
00059 base_node->file_row = doc.Row();
00060 parseLow(&doc,base_node);
00061 }
00062
00069 void XmlParser::parseLow(TiXmlNode* node, Node* n)
00070 {
00071 Node* new_node = new Node();
00072 TiXmlNode* pChild;
00073 int type = node->Type();
00074 std::string node_text(node->Value());
00075 switch (type)
00076 {
00077 case 0 :
00078 {
00079 new_node = n;
00080 break;
00081 }
00082 case 1 :
00083 {
00084 new_node->tag = node_text;
00085 new_node->file_origin = n->file_origin;
00086 n->file_row = node->Row();
00087 n->elements.push_back(new_node);
00088 break;
00089 }
00090 case 4 :
00091 {
00092
00093 if(std::string(node->Parent()->Value()) == "filename")
00094 {
00095 try
00096 {
00097 parse(ros::package::getPath("stdr_resources") +
00098 std::string("/resources/") + std::string(node->Value()), n);
00099 }
00100 catch(ParserException ex)
00101 {
00102 try
00103 {
00104
00105
00106 parse(extractDirname(new_node->file_origin) +
00107 std::string("/") + std::string(node->Value()), n);
00108 }
00109 catch(ParserException ex)
00110 {
00111 throw ex;
00112 }
00113 }
00114 }
00115 else
00116 {
00117 new_node->value = node_text;
00118 new_node->file_origin = n->file_origin;
00119 n->file_row = node->Row();
00120 n->elements.push_back(new_node);
00121 }
00122 break;
00123 }
00124 }
00125
00126 for (
00127 pChild = node->FirstChild();
00128 pChild != 0;
00129 pChild = pChild->NextSibling())
00130 {
00131 parseLow( pChild , new_node );
00132 }
00133 }
00134
00135 }
00136