00001 // This is brl/bbas/bxml/bxml_find.cxx 00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE 00003 #pragma implementation 00004 #endif 00005 //: 00006 // \file 00007 // \author Matt Leotta 00008 // \date November 30, 2006 00009 00010 #include "bxml_find.h" 00011 00012 00013 //: Return true if elm has the same name and contains the same attributes as query 00014 bool bxml_matches(const bxml_element& elm, const bxml_element& query) 00015 { 00016 if (elm.name() != query.name()) 00017 return false; 00018 00019 if (elm.num_attributes() < query.num_attributes()) 00020 return false; 00021 00022 for (bxml_element::const_attr_iterator a = query.attr_begin(); 00023 a != query.attr_end(); ++a) 00024 { 00025 if (elm.attribute(a->first) != a->second) 00026 return false; 00027 } 00028 return true; 00029 } 00030 00031 00032 //: Find the first element that matches 00033 bxml_data_sptr bxml_find(const bxml_data_sptr& head, 00034 const bxml_element& query) 00035 { 00036 if (head->type() != bxml_data::ELEMENT) 00037 return NULL; 00038 bxml_element* h_elm = static_cast<bxml_element*>(head.ptr()); 00039 if ( bxml_matches(*h_elm, query) ) 00040 return head; 00041 else { 00042 // recursively check nested elements 00043 for (bxml_element::const_data_iterator i = h_elm->data_begin(); 00044 i != h_elm->data_end(); ++i) 00045 { 00046 bxml_data_sptr result = bxml_find(*i, query); 00047 if (result) 00048 return result; 00049 } 00050 } 00051 return NULL; 00052 } 00053 00054 //: Return true if elm has the same name and contains the same attributes as query 00055 bool bxml_matches_by_name(const bxml_element& elm, const bxml_element& query) 00056 { 00057 if (elm.name() != query.name()) 00058 return false; 00059 00060 return true; 00061 } 00062 00063 00064 //: Find the first element that matches 00065 bxml_data_sptr bxml_find_by_name(const bxml_data_sptr& head, 00066 const bxml_element& query) 00067 { 00068 if (head->type() != bxml_data::ELEMENT) 00069 return NULL; 00070 bxml_element* h_elm = static_cast<bxml_element*>(head.ptr()); 00071 if ( bxml_matches_by_name(*h_elm, query) ) 00072 return head; 00073 else{ 00074 // recursively check nested elements 00075 for (bxml_element::const_data_iterator i = h_elm->data_begin(); 00076 i != h_elm->data_end(); ++i) 00077 { 00078 bxml_data_sptr result = bxml_find_by_name(*i, query); 00079 if (result) 00080 return result; 00081 } 00082 } 00083 return NULL; 00084 } 00085
1.5.1