SMBIOS Library
XmlUtils.h
Go to the documentation of this file.
1 // vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=c:cindent:
2 /*
3  * Copyright (C) 2005 Dell Inc.
4  * by Michael Brown <Michael_E_Brown@dell.com>
5  * Licensed under the Open Software License version 2.1
6  *
7  * Alternatively, you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published
9  * by the Free Software Foundation; either version 2 of the License,
10  * or (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU General Public License for more details.
16  */
17 
18 #ifndef XMLUTILS_H
19 #define XMLUTILS_H
20 
21 // compat header should always be first header
22 #include "smbios/compat.h"
23 
24 #include <libxml/xmlmemory.h>
25 #include <libxml/parser.h>
26 
27 #include "ExceptionImpl.h"
28 
29 // xerces compat stuff until transition is complete
30 #define XERCES_CPP_NAMESPACE_QUALIFIER
31 #define DOMElement xmlNode
32 #define DOMNode xmlNode
33 #define DOMDocument xmlDoc
34 #define DOMBuilder int
35 #define SETUP_XML_NAMESPACE
36 #define XML_NAMESPACE
37 #define CHECK_VERSION_COMPAT LIBXML_TEST_VERSION
38 #define xmlDocGetRootElement(doc) xmlDocGetRootElement(const_cast<xmlDocPtr>(doc))
39 #define InitXML() \
40  do { \
41  } while(0)
42 #define FiniXML() do{}while(0)
43 #define xmlFreeParser(parser) do{parser=0;}while(0)
44 #define xmlFreeDoc(doc) do{xmlFreeDoc(doc); doc=0;}while(0)
45 
46  /*
47 XML_PARSE_RECOVER = 1 : recover on errors
48 XML_PARSE_NOENT = 2 : substitute entities
49 XML_PARSE_DTDLOAD = 4 : load the external subset
50 XML_PARSE_DTDATTR = 8 : default DTD attributes
51 XML_PARSE_DTDVALID = 16 : validate with the DTD
52 XML_PARSE_NOERROR = 32 : suppress error reports
53 XML_PARSE_NOWARNING = 64 : suppress warning reports
54 XML_PARSE_PEDANTIC = 128 : pedantic error reporting
55 XML_PARSE_NOBLANKS = 256 : remove blank nodes
56 XML_PARSE_SAX1 = 512 : use the SAX1 interface internally
57 XML_PARSE_XINCLUDE = 1024 : Implement XInclude substitition
58 XML_PARSE_NONET = 2048 : Forbid network access
59 XML_PARSE_NODICT = 4096 : Do not reuse the context dictionnary
60 XML_PARSE_NSCLEAN = 8192 : remove redundant namespaces declarations
61 XML_PARSE_NOCDATA = 16384 : merge CDATA as text nodes
62 XML_PARSE_NOXINCNODE = 32768 : do not generate XINCLUDE START/END nodes
63 XML_PARSE_COMPACT = 65536 : compact small text nodes
64  */
65 
66 // We program to the 2.6 API. Here are some backwards compat stuff
67 #if LIBXML_VERSION < 20600
68 
69 #undef InitXML
70 #define InitXML() xmlSetGenericErrorFunc(NULL, xmlutils::suppressLibxmlWarnings);
71 namespace xmlutils
72 {
73  void suppressLibxmlWarnings (void *ctx, const char *msg, ...);
74 }
75 
76 # define compatXmlReadFile(parser, doc, name) do{UNREFERENCED_PARAMETER(parser); doc = xmlParseFile(name);}while(0)
77 # define compatXmlReadMemory(parser, doc, ptr, len) do{UNREFERENCED_PARAMETER(parser); doc = xmlParseMemory(ptr, len);}while(0)
78 
79 #else
80 
81 # define compatXmlReadFile(parser, doc, name) do{UNREFERENCED_PARAMETER(parser); doc = xmlReadFile(name, \
82  NULL, \
83  XML_PARSE_RECOVER | XML_PARSE_NOENT | XML_PARSE_DTDATTR | XML_PARSE_NOWARNING | XML_PARSE_NONET | XML_PARSE_NOCDATA \
84  );}while(0)
85 # define compatXmlReadMemory(parser, doc, buf, len) do{UNREFERENCED_PARAMETER(parser);doc = xmlReadMemory(\
86  buf, \
87  len, \
88  NULL, \
89  NULL, \
90  XML_PARSE_RECOVER | \
91  XML_PARSE_NOENT | \
92  XML_PARSE_DTDATTR | \
93  XML_PARSE_NOWARNING | \
94  XML_PARSE_NONET | \
95  XML_PARSE_NOCDATA \
96  );}while(0)
97 #endif
98 
99 
100 namespace xmlutils
101 {
102 
103  // declare exceptions
104  // Internal users should catch() these...
105  DECLARE_EXCEPTION( XmlUtilsException );
106  DECLARE_EXCEPTION_EX( NotFound, xmlutils, XmlUtilsException );
107  DECLARE_EXCEPTION_EX( Invalid, xmlutils, XmlUtilsException );
108 
109  // Since this is also a private header, define them
110  // internal use only inside XmlUtils.cpp
111  DEFINE_EXCEPTION_EX( NotFoundImpl, xmlutils, NotFound );
112  DEFINE_EXCEPTION_EX( InvalidImpl, xmlutils, Invalid );
113 
114  std::string safeGetAttribute( const xmlNode *node, const std::string &attr );
115  DOMBuilder *getParser( );
116 
117  xmlNodePtr findElement( xmlNodePtr root, const std::string elementName, const std::string &attribute, const std::string &value );
118  xmlNodePtr findElementWithNumericAttr( xmlNodePtr root, const std::string elementName, const std::string &attribute, long value);
119 
120  std::string getNodeText( xmlNodePtr elem );
121  int getNumberFromXmlAttr( xmlNodePtr element, const std::string field, int base );
122 }
123 
124 #endif