LibOFX
|
00001 /*************************************************************************** 00002 nodeparser.cpp 00003 ------------------- 00004 copyright : (C) 2005 by Ace Jones 00005 email : acejones@users.sourceforge.net 00006 ***************************************************************************/ 00011 /*************************************************************************** 00012 * * 00013 * This program is free software; you can redistribute it and/or modify * 00014 * it under the terms of the GNU General Public License as published by * 00015 * the Free Software Foundation; either version 2 of the License, or * 00016 * (at your option) any later version. * 00017 * * 00018 ***************************************************************************/ 00019 00020 #include "nodeparser.h" 00021 00022 using std::string; 00023 using std::vector; 00024 00025 NodeParser::NodeParser(const xmlpp::Node::NodeList& list): xmlpp::Node::NodeList(list) 00026 { 00027 } 00028 00029 NodeParser::NodeParser(const xmlpp::Node* node) 00030 { 00031 push_back(const_cast<xmlpp::Node*>(node)); 00032 } 00033 00034 NodeParser::NodeParser(const xmlpp::DomParser& parser) 00035 { 00036 xmlpp::Node* node = parser.get_document()->get_root_node(); 00037 push_back(const_cast<xmlpp::Node*>(node)); 00038 } 00039 00040 NodeParser NodeParser::Path(const xmlpp::Node* node,const std::string& path) 00041 { 00042 //std::cout << __PRETTY_FUNCTION__ << std::endl; 00043 00044 NodeParser result; 00045 00046 // split path string into the 1st level, and the rest 00047 std::string key = path; 00048 std::string remainder; 00049 std::string::size_type token_pos = path.find('/'); 00050 if ( token_pos != std::string::npos ) 00051 { 00052 key = path.substr(0, token_pos ); 00053 remainder = path.substr( token_pos + 1 ); 00054 } 00055 00056 // find the first level nodes that match 00057 xmlpp::Node::NodeList list = node->get_children(); 00058 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter) 00059 { 00060 if ( (*iter)->get_name() == key ) 00061 { 00062 // if there is still some path left, ask for the rest of the path from those nodes. 00063 if ( remainder.length() ) 00064 { 00065 NodeParser remain_list = NodeParser(*iter).Path(remainder); 00066 result.splice(result.end(),remain_list); 00067 } 00068 00069 // otherwise add the node to the result list. 00070 else 00071 result.push_back(*iter); 00072 } 00073 } 00074 00075 return result; 00076 } 00077 00078 NodeParser NodeParser::Path(const std::string& path) const 00079 { 00080 //std::cout << __PRETTY_FUNCTION__ << std::endl; 00081 NodeParser result; 00082 00083 for(const_iterator iter = begin(); iter != end(); ++iter) 00084 { 00085 NodeParser iter_list = Path(*iter,path); 00086 result.splice(result.end(),iter_list); 00087 } 00088 00089 return result; 00090 } 00091 00092 NodeParser NodeParser::Select(const std::string& key, const std::string& value) const 00093 { 00094 //std::cout << __PRETTY_FUNCTION__ << std::endl; 00095 NodeParser result; 00096 for(const_iterator iter = begin(); iter != end(); ++iter) 00097 { 00098 xmlpp::Node::NodeList list = (*iter)->get_children(); 00099 for(xmlpp::Node::NodeList::const_iterator iter3 = list.begin(); iter3 != list.end(); ++iter3) 00100 { 00101 if ( (*iter3)->get_name() == key ) 00102 { 00103 xmlpp::Node::NodeList list = (*iter3)->get_children(); 00104 for(xmlpp::Node::NodeList::const_iterator iter4 = list.begin(); iter4 != list.end(); ++iter4) 00105 { 00106 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(*iter4); 00107 if ( nodeText && nodeText->get_content() == value ) 00108 result.push_back(*iter); 00109 break; 00110 } 00111 } 00112 } 00113 } 00114 return result; 00115 } 00116 00117 vector<string> NodeParser::Text(void) const 00118 { 00119 vector<string> result; 00120 00121 // Go through the list of nodes 00122 for(xmlpp::Node::NodeList::const_iterator iter = begin(); iter != end(); ++iter) 00123 { 00124 // Find the text child node, and print that 00125 xmlpp::Node::NodeList list = (*iter)->get_children(); 00126 for(xmlpp::Node::NodeList::const_iterator iter2 = list.begin(); iter2 != list.end(); ++iter2) 00127 { 00128 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(*iter2); 00129 if ( nodeText ) 00130 { 00131 result.push_back(nodeText->get_content()); 00132 } 00133 } 00134 } 00135 if ( result.empty() ) 00136 result.push_back(string()); 00137 return result; 00138 } 00139 00140 // vim:cin:si:ai:et:ts=2:sw=2: