00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #define LIBSMBIOS_SOURCE
00020 #include "XmlUtils.h"
00021
00022 using namespace std;
00023
00024 #if defined(DEBUG_XMLUTILS)
00025 #include <iostream>
00026 # define DCOUT(line) do { cout << line; } while(0)
00027 # define DCERR(line) do { cerr << line; } while(0)
00028 #else
00029 # define DCOUT(line) do {} while(0)
00030 # define DCERR(line) do {} while(0)
00031 #endif
00032
00033 namespace xmlutils
00034 {
00035
00036
00037
00038 DOMBuilder *getParser() { return 0; }
00039
00040
00041 void suppressLibxmlWarnings (void *ctx, const char *msg, ...)
00042 {
00043 UNREFERENCED_PARAMETER(ctx);
00044 UNREFERENCED_PARAMETER(msg);
00045 }
00046
00047
00048 string safeGetAttribute( const xmlNode *node, const string &attr )
00049 {
00050 string retval("");
00051 xmlChar *text = xmlGetProp(const_cast<xmlNode *>(node), reinterpret_cast<const xmlChar *>(attr.c_str()));
00052 if (text)
00053 retval = reinterpret_cast<const char *>(text);
00054 xmlFree(text);
00055 return retval;
00056 }
00057
00058
00059
00060
00061
00062 xmlNodePtr findElement( xmlNodePtr root, const string elementName, const string &attribute, const string &value )
00063 {
00064 xmlNodePtr elem = 0;
00065 DCERR("findElement( root, " << "\"" << elementName << "\", \"" << attribute << "\", \"" << value << "\");" << endl);
00066
00067
00068 if( ! root )
00069 throw NotFoundImpl("no root element ref to xml file, cannot findElement");
00070
00071 xmlNodePtr cur_node = NULL;
00072 for (cur_node = root; cur_node; cur_node = cur_node->next) {
00073 DCERR("\tnode type: Element, name: " << cur_node->name << endl);
00074 if (cur_node->type == XML_ELEMENT_NODE) {
00075 if (!xmlStrcmp(cur_node->name, reinterpret_cast<const xmlChar *>(elementName.c_str())))
00076 {
00077 string strAttrValue = safeGetAttribute( cur_node, attribute );
00078 DCERR("\tELEMENT attribute ("<< attribute <<") value: " << "\"" << strAttrValue << "\"" << endl);
00079 if( (strAttrValue == value) || (attribute == "") )
00080 {
00081 DCERR("MATCH!" << endl);
00082 elem = cur_node;
00083 goto out;
00084 }
00085 }
00086 }
00087 try
00088 {
00089 DCERR("\tsearching child: " << cur_node->name << endl);
00090 elem = findElement(cur_node->children, elementName, attribute, value);
00091 goto out;
00092 }
00093 catch (NotFound)
00094 {
00095
00096 DCERR("\tDid not find match in child: " << cur_node->name << endl);
00097 }
00098 }
00099
00100 out:
00101 if (!elem)
00102 {
00103 DCERR("Throwing not found error!"<< endl);
00104 throw NotFoundImpl("could not find element.");
00105 }
00106
00107 return elem;
00108 }
00109
00110
00111
00112
00113
00114
00115 xmlNodePtr findElement( xmlNodePtr root, const string elementName, const string &attribute, long value)
00116 {
00117 xmlNodePtr elem = 0;
00118
00119 DCERR("findElement( root, " << "\"" << elementName << "\", \"" << attribute << "\", \"" << value << "\");" << endl);
00120
00121
00122 if( ! root )
00123 throw NotFoundImpl("no root element ref to xml file, cannot findElement");
00124
00125 xmlNodePtr cur_node = NULL;
00126 for (cur_node = root; cur_node; cur_node = cur_node->next) {
00127 DCERR("\tnode type: Element, name: " << cur_node->name << endl);
00128 if (cur_node->type == XML_ELEMENT_NODE) {
00129 if (!xmlStrcmp(cur_node->name, reinterpret_cast<const xmlChar *>(elementName.c_str())))
00130 {
00131
00132 string strAttrValue = safeGetAttribute( cur_node, attribute );
00133 char *endptr = 0;
00134 long attrValue = strtol(strAttrValue.c_str(), &endptr, 0);
00135 DCERR("\tELEMENT attribute ("<< attribute <<") value: " << "\"" << strAttrValue << "\"" << endl);
00136 if(endptr != strAttrValue.c_str())
00137 if( (attrValue == value) || (attribute == "") )
00138 {
00139 DCERR("MATCH!" << endl);
00140 elem = cur_node;
00141 goto out;
00142 }
00143 }
00144 }
00145 try
00146 {
00147 DCERR("\tsearching child: " << cur_node->name << endl);
00148 elem = findElement(cur_node->children, elementName, attribute, value);
00149 goto out;
00150 }
00151 catch (NotFound) {}
00152 }
00153
00154 out:
00155 if (!elem)
00156 {
00157 DCERR("Throwing not found error!"<< endl);
00158 throw NotFoundImpl("could not find element.");
00159 }
00160
00161 return elem;
00162 }
00163
00164 xmlNodePtr findElementWithNumericAttr( xmlNodePtr root, const string elementName, const string &attribute, long value)
00165 {
00166 return findElement(root, elementName, attribute, value);
00167 }
00168
00169 string getNodeText( xmlNodePtr elem )
00170 {
00171 string retval = "";
00172 xmlChar *text = 0;
00173 text = xmlNodeGetContent(elem);
00174 retval = reinterpret_cast<const char *>(text);
00175 xmlFree(text);
00176 return retval;
00177 }
00178
00179
00180 int getNumberFromXmlAttr( xmlNodePtr element, const string field, int base )
00181 {
00182 int tempNum = 0;
00183 string tempStr = safeGetAttribute( element, field );
00184 if(tempStr.length() != 0)
00185 tempNum = strtol( tempStr.c_str(), 0, base);
00186
00187 return tempNum;
00188 }
00189 }