HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_XMLSaxParser.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_XMLSaxParser.h ( Library, C++)
7  *
8  * COMMENTS:
9  * The SAX parser uses the libxml2 API documented at:
10  * http://xmlsoft.org/html/libxml-parser.html
11  * http://xmlsoft.org/html/libxml-tree.html
12  *
13  */
14 
15 #ifndef __UT_XMLSaxParser__
16 #define __UT_XMLSaxParser__
17 
18 #include "UT_API.h"
19 #include "UT_Array.h"
20 #include "UT_String.h"
21 #include "UT_UniquePtr.h"
22 #include "UT_XMLParser.h"
23 
24 #include <utility>
25 
26 struct _xmlSAXHandler;
27 typedef struct _xmlSAXHandler *xmlSAXHandlerPtr;
28 
30 {
31 public:
32  /// Standard constructor.
34 
35  /// Standard destructor.
36  ~UT_XMLSaxParser() override;
37 
38 protected:
39 
40  /// An XML element attribute as defined by a name and a value.
41  typedef std::pair<UT_String, UT_String> UT_XMLAttribute;
42 
43 public:
44  /// A list of XML element attributes.
46 
47  /// Callback methods that are called at various times
48  /// of the parse.
49  virtual void onStartDocument();
50  virtual void onEndDocument();
51  virtual void onStartElement(const UT_String &name,
52  const UT_XMLAttributeList &attribs);
53  virtual void onEndElement(const UT_String &name);
54  virtual void onCharacters(const UT_String &characters);
55  virtual void onCDATABlock(const UT_String &characters);
56  virtual void onComment(const UT_String &text);
57  virtual void onWarning(const UT_String &text);
58  virtual void onError(const UT_String &text);
59  virtual void onFatalError(const UT_String &text);
60 
61  /// Parse XML using the current context.
62  /// Return true on a successful parse. Return false otherwise.
63  bool parseContextSubclass() override;
64 
65  /// Utility method for getting the value of an attribute.
66  static bool getAttribute(const UT_XMLAttributeList &attribs,
67  const char * name, UT_String & value);
68 
69 public:
71  const char * name)
72  {
73  UT_String tmp;
74  if (getAttribute(attribs, name, tmp))
75  return tmp;
76  return "";
77  }
78 
79  static bool hasAttribute(const UT_XMLAttributeList &attribs,
80  const char * name)
81 {
82  UT_String tmp;
83  return getAttribute(attribs, name, tmp);
84 }
85 
86 
87  static int getIntegerAttribute(const UT_XMLAttributeList &attribs,
88  const char * name)
89  {
90  UT_String tmp;
91  if (getAttribute(attribs, name, tmp))
92  return atoi(tmp.c_str());
93  return 0;
94  }
95 
96  static double getDoubleAttribute(const UT_XMLAttributeList &attribs,
97  const char * name, double defValue)
98  {
99  UT_String tmp;
100  if (getAttribute(attribs, name, tmp))
101  return atof(tmp.c_str());
102  return defValue;
103  }
104 private:
105 
106  /// SAX parsing callback handler.
107  UT_UniquePtr<_xmlSAXHandler> myCallbackHandler;
108 
109  /// We need to give the callback helper class
110  /// access to the parser's callback functions (i.e. onStartElement).
111  friend class utSaxParserCallback;
112 };
113 
114 #endif
115 
GLsizei const GLfloat * value
Definition: glcorearb.h:824
#define UT_API
Definition: UT_API.h:14
const char * c_str() const
Definition: UT_String.h:515
static bool hasAttribute(const UT_XMLAttributeList &attribs, const char *name)
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
static double getDoubleAttribute(const UT_XMLAttributeList &attribs, const char *name, double defValue)
static int getIntegerAttribute(const UT_XMLAttributeList &attribs, const char *name)
UT_Array< UT_XMLAttribute * > UT_XMLAttributeList
A list of XML element attributes.
std::pair< UT_String, UT_String > UT_XMLAttribute
An XML element attribute as defined by a name and a value.
GLuint const GLchar * name
Definition: glcorearb.h:786
static UT_String getStringAttribute(const UT_XMLAttributeList &attribs, const char *name)
struct _xmlSAXHandler * xmlSAXHandlerPtr
virtual bool parseContextSubclass()=0