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  /// Parse a file, handing the callbacks below libxml2's UTF-8 unchanged.
39  /// parseFile() instead converts to Latin-1, which silently drops every
40  /// code point above U+00FF; that remains the default so existing
41  /// subclasses are unaffected. Use this one if your consumers are UTF-8
42  /// clean. Returns true on a successful parse.
43  bool parseFileUTF8(const char *filename);
44 
45 protected:
46 
47  /// An XML element attribute as defined by a name and a value.
48  typedef std::pair<UT_String, UT_String> UT_XMLAttribute;
49 
50 public:
51  /// A list of XML element attributes.
53 
54  /// Callback methods that are called at various times
55  /// of the parse.
56  virtual void onStartDocument();
57  virtual void onEndDocument();
58  virtual void onStartElement(const UT_String &name,
59  const UT_XMLAttributeList &attribs);
60  virtual void onEndElement(const UT_String &name);
61  virtual void onCharacters(const UT_String &characters);
62  virtual void onCDATABlock(const UT_String &characters);
63  virtual void onComment(const UT_String &text);
64  virtual void onWarning(const UT_String &text);
65  virtual void onError(const UT_String &text);
66  virtual void onFatalError(const UT_String &text);
67 
68  /// Parse XML using the current context.
69  /// Return true on a successful parse. Return false otherwise.
70  bool parseContextSubclass() override;
71 
72  /// Utility method for getting the value of an attribute.
73  static bool getAttribute(const UT_XMLAttributeList &attribs,
74  const char * name, UT_String & value);
75 
76 public:
78  const char * name)
79  {
80  UT_String tmp;
81  if (getAttribute(attribs, name, tmp))
82  return tmp;
83  return "";
84  }
85 
86  static bool hasAttribute(const UT_XMLAttributeList &attribs,
87  const char * name)
88 {
89  UT_String tmp;
90  return getAttribute(attribs, name, tmp);
91 }
92 
93 
94  static int getIntegerAttribute(const UT_XMLAttributeList &attribs,
95  const char * name)
96  {
97  UT_String tmp;
98  if (getAttribute(attribs, name, tmp))
99  return atoi(tmp.c_str());
100  return 0;
101  }
102 
103  static double getDoubleAttribute(const UT_XMLAttributeList &attribs,
104  const char * name, double defValue)
105  {
106  UT_String tmp;
107  if (getAttribute(attribs, name, tmp))
108  return atof(tmp.c_str());
109  return defValue;
110  }
111 private:
112 
113  /// SAX parsing callback handler.
114  UT_UniquePtr<_xmlSAXHandler> myCallbackHandler;
115 
116  /// We need to give the callback helper class
117  /// access to the parser's callback functions (i.e. onStartElement).
118  friend class utSaxParserCallback;
119 };
120 
121 #endif
122 
GT_API const UT_StringHolder filename
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:524
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