1 #ifndef NoXmlTreeReader
4 #include "StlXmlTree.h"
5 #include "ChapiStringUtilities.h"
11 using namespace chapi_string_utilities;
12 using stl_xml_tree::sep;
13 using stl_xml_tree::NO_ERROR;
14 using stl_xml_tree::NO_XML_BASE;
15 using stl_xml_tree::BAD_XML;
18 #ifndef __STDB_STANDALONE__
19 #include "StMessMgr.h"
21 #define LOG_DEBUG cout
24 #define LOG_ERROR cerr
25 #define LOG_FATAL cerr
32 typedef vector<string>::const_iterator VCI;
33 typedef map<string,string>::const_iterator MCI;
34 typedef multimap<string,string>::const_iterator MMCI;
39 StlXmlTree::StlXmlTree():
41 XmlTree(multimap<string,string>()),
44 MaxDepthWanted(INT_MAX),
50 StlXmlTree::StlXmlTree(
const string xmlfilename,
StlXmlTree* filter) :
52 XmlTree(multimap<string,string>()),
55 MaxDepthWanted(INT_MAX),
60 reader = xmlNewTextReaderFilename(xmlfilename.c_str());
63 ret = xmlTextReaderRead(reader);
68 ret = xmlTextReaderRead(reader);
70 xmlFreeTextReader(reader);
73 LOG_ERROR<<
"StlXmlTree::StlXmlTree: failed to parse "<< xmlfilename<<endm;
79 LOG_ERROR<<
"StlXmlTree::StlXmlTree: unable to open "<< xmlfilename<< endm;
80 MyStatus = NO_XML_BASE;
84 void StlXmlTree::ShowTree()
86 LOG_INFO <<
"-----------------------------------------"<<endm;
87 LOG_INFO <<
"XmlTree contains"<<endm;
88 for (MMCI I=XmlTree.begin(); I!=XmlTree.end(); ++I)
90 LOG_INFO << (*I).first <<
" -- "<<(*I).second <<endm;
92 LOG_INFO <<
"-----------------------------------------"<<endm;
95 string StlXmlTree::MakeKey(
string FullyQualifiedParent,
string Child)
97 return FullyQualifiedParent + sep + Child;
100 string StlXmlTree::QualifyParent(
string Parent,
string Attributes)
102 string rtrn = Parent +
"(" + Attributes +
")";
106 vector<string> StlXmlTree::LookUpValueByKey(
string key)
109 MMCI f = XmlTree.find(key);
110 if (f!=XmlTree.end())
112 MMCI b = XmlTree.lower_bound(key);
113 MMCI e = XmlTree.upper_bound(key);
115 for (MMCI i = b; i!=e; ++i)
117 rtrn.push_back((*i).second);
123 vector<string> StlXmlTree::LookUpValueByKey
124 (
string& Parent,
string ParentAttributes,
string Child)
126 string qp = QualifyParent(Parent,ParentAttributes);
127 string key = MakeKey(qp,Child);
129 LOG_INFO <<
"StlXmlTree::LookUpValueByKey key is " << key <<endm;
131 vector<string> rtrn = LookUpValueByKey(key);
136 void StlXmlTree::InsertKeyValuePair(
string Key,
string Value)
138 OnTheTree = XmlTree.insert(make_pair(Key,Value));
141 bool StlXmlTree::AttributesContain(
string A,
string N,
string V)
144 if ((N==
"" && V==
"") || N==
"")
148 map<string,string> nv = ParseAttributeString(A);
149 map<string,string>::iterator I = nv.find(N);
168 void StlXmlTree::ProcessNodeDebug()
184 const xmlChar *name, *value;
187 name = xmlTextReaderName(reader);
189 name = BAD_CAST
"--";
192 value = xmlTextReaderValue(reader);
194 LOG_INFO << xmlTextReaderDepth(reader) <<
" "<<xmlTextReaderNodeType(reader)<<
" "<<name<<
" "<<xmlTextReaderIsEmptyElement(reader)<<
" "<< xmlTextReaderHasValue(reader)<<endm;
201 if (xmlStrlen(value) > 40)
203 LOG_INFO<<value<<
".40s..."<endm;
207 LOG_INFO<<
" "<<value<<endm;
211 short HasAttributes = xmlTextReaderHasAttributes(reader);
214 while (xmlTextReaderMoveToNextAttribute(reader))
221 void StlXmlTree::UpdateDepth()
224 PreviousDepth = Depth;
225 Depth = xmlTextReaderDepth(reader);
228 void StlXmlTree::ProcessAttribute()
236 int NodeType = xmlTextReaderNodeType(reader);
240 if (NodeType!=XML_READER_TYPE_ATTRIBUTE)
245 xmlChar* atname = xmlTextReaderName(reader);
246 string AttributeName;
247 if ( atname != NULL ) {
248 AttributeName = (
char*)atname;
253 xmlChar* tmp = xmlTextReaderValue(reader);
256 NodeValue = filter_string((
char*)tmp);
261 IdString += AttributeName +
"=" + NodeValue +
";";
264 void StlXmlTree::ProcessNode()
266 int NodeType = xmlTextReaderNodeType(reader);
267 short HasValue = xmlTextReaderHasValue(reader);
268 short HasAttributes = xmlTextReaderHasAttributes(reader);
269 xmlChar* name = xmlTextReaderName(reader);
270 if ( name != NULL ) {
271 NodeName = (
char*)name;
276 string NodeValue =
"";
281 xmlChar* tmp = xmlTextReaderValue(reader);
283 NodeValue = filter_string((
char*)tmp);
288 IdString = NodeName +
"=" + NodeValue +
";";
293 if (Depth>MaxDepthWanted)
297 if (Depth<MaxDepthWanted)
299 MaxDepthWanted = INT_MAX;
302 if (Depth<PreviousDepth || NodeType==XML_READER_TYPE_END_ELEMENT)
304 cut_string_after_sub(XmlTreeNodeName,sep);
307 if ((NodeType==XML_READER_TYPE_ELEMENT || NodeValue !=
"") && NodeType != XML_READER_TYPE_COMMENT )
309 if (NodeType==XML_READER_TYPE_ELEMENT)
311 XmlTreeNodeName = XmlTreeNodeName + sep + NodeName;
316 while (xmlTextReaderMoveToNextAttribute(reader))
323 if (!SkipBasedOnValue(XmlTreeNodeName,IdString))
325 string key = XmlTreeNodeName;
326 string value = IdString;
329 XmlTreeNodeName = QualifyParent(XmlTreeNodeName,IdString);
333 OnTheTree = XmlTree.insert(make_pair(key,value));
334 MaxDepthWanted = INT_MAX;
339 MaxDepthWanted = Depth-1;
346 map<string,string> StlXmlTree::ParseAttributeString(
string in)
348 vector<string> v = slice(in,
";");
349 map<string,string> m = associate_pieces(v,
"=");
353 bool StlXmlTree::SkipBasedOnValue(
const string key,
const string value)
360 map<string,string> m = ParseAttributeString(value);
363 LOG_INFO <<
"StlXmlTree::SkipBasedOnValue key = "<<key<<
" value = "<<value<<endm;
365 MMCI b = Filter->XmlTree.lower_bound(key);
366 MMCI e = Filter->XmlTree.upper_bound(key);
377 for (MMCI i=b; i!=e; ++i)
379 map<string,string> f = ParseAttributeString((*i).second);
383 for (MCI ii = f.begin(); ii != f.end(); ++ii)
385 string search_key = (*ii).first;
387 MCI mykey = m.find(search_key);
396 vector<string> mykey_v = slice((*mykey).second,
",");
397 vector<string> f_v = slice((*ii).second,
",");
400 VCI m_v_i = mykey_v.begin();
401 while (m_v_i != mykey_v.end() && rtrn)
403 VCI f_v_i = f_v.begin();
405 while (f_v_i != f_v.end() && rtrn)
408 LOG_INFO <<
" compare " <<*m_v_i <<
" and "<<*f_v_i <<endm;
410 if (*m_v_i == *f_v_i)
423 LOG_INFO <<
"StlXmlTree::SkipBasedOnValue "<< rtrn <<endm;
438 LOG_INFO <<
"StlXmlTree::SkipBasedOnValue "<< rtrn <<endm;