Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

XmlUtils.cpp

Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:cindent:textwidth=0:
00003  *
00004  * Copyright (C) 2005 Dell Inc.
00005  *  by Michael Brown <Michael_E_Brown@dell.com>
00006  * Licensed under the Open Software License version 2.1
00007  *
00008  * Alternatively, you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published
00010  * by the Free Software Foundation; either version 2 of the License,
00011  * or (at your option) any later version.
00012 
00013  * This program is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
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     // NON-MEMBER FUNCTIONS
00037     //
00038     DOMBuilder *getParser() { return 0; }
00039 
00040     // backwards compat for libxml < 2.6 without xmlReadFile()
00041     void  suppressLibxmlWarnings (void *ctx, const char *msg, ...)
00042     { 
00043         UNREFERENCED_PARAMETER(ctx); 
00044         UNREFERENCED_PARAMETER(msg); 
00045     }
00046 
00047     // the best. use this when possible.
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     // Finds a "STRUCTURE" element with the specified attribute and returns
00060     // a pointer to it.
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         // If we don't have a ref to XML file, we cannot find this info
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             // not an error yet
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     // Finds a "STRUCTURE" element with the specified attribute and returns
00113     // a pointer to it.
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         // If we don't have a ref to XML file, we cannot find this info
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                     // printf("node type: Element, name: %s\n", cur_node->name);
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) {} // not an error yet
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 }

Generated on Wed Apr 11 16:25:10 2007 for SMBIOS Library by doxygen 1.3.5