]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Host/XML.h
MFV r326785: 8880 improve DTrace error checking
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Host / XML.h
1 //===-- XML.h ---------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_XML_h_
11 #define liblldb_XML_h_
12
13 // C Includes
14 #if defined(LIBXML2_DEFINED)
15 #include <libxml/xmlreader.h>
16 #endif
17
18 // C++ Includes
19 #include <functional>
20 #include <string>
21 #include <vector>
22
23 // Other libraries and framework includes
24 #include "llvm/ADT/StringRef.h"
25
26 // Project includes
27 #include "lldb/Utility/StreamString.h"
28 #include "lldb/Utility/StructuredData.h"
29 #include "lldb/lldb-private.h"
30
31 namespace lldb_private {
32
33 #if defined(LIBXML2_DEFINED)
34 typedef xmlNodePtr XMLNodeImpl;
35 typedef xmlDocPtr XMLDocumentImpl;
36 #else
37 typedef void *XMLNodeImpl;
38 typedef void *XMLDocumentImpl;
39 #endif
40
41 class XMLNode;
42
43 typedef std::vector<std::string> NamePath;
44 typedef std::function<bool(const XMLNode &node)> NodeCallback;
45 typedef std::function<bool(const llvm::StringRef &name,
46                            const llvm::StringRef &value)>
47     AttributeCallback;
48
49 class XMLNode {
50 public:
51   XMLNode();
52
53   XMLNode(XMLNodeImpl node);
54
55   ~XMLNode();
56
57   explicit operator bool() const { return IsValid(); }
58
59   void Clear();
60
61   bool IsValid() const;
62
63   bool IsElement() const;
64
65   llvm::StringRef GetName() const;
66
67   bool GetElementText(std::string &text) const;
68
69   bool GetElementTextAsUnsigned(uint64_t &value, uint64_t fail_value = 0,
70                                 int base = 0) const;
71
72   bool GetElementTextAsFloat(double &value, double fail_value = 0.0) const;
73
74   bool NameIs(const char *name) const;
75
76   XMLNode GetParent() const;
77
78   XMLNode GetSibling() const;
79
80   XMLNode GetChild() const;
81
82   llvm::StringRef GetAttributeValue(const char *name,
83                                     const char *fail_value = nullptr) const;
84
85   XMLNode FindFirstChildElementWithName(const char *name) const;
86
87   XMLNode GetElementForPath(const NamePath &path);
88
89   //----------------------------------------------------------------------
90   // Iterate through all sibling nodes of any type
91   //----------------------------------------------------------------------
92   void ForEachSiblingNode(NodeCallback const &callback) const;
93
94   //----------------------------------------------------------------------
95   // Iterate through only the sibling nodes that are elements
96   //----------------------------------------------------------------------
97   void ForEachSiblingElement(NodeCallback const &callback) const;
98
99   //----------------------------------------------------------------------
100   // Iterate through only the sibling nodes that are elements and whose
101   // name matches \a name.
102   //----------------------------------------------------------------------
103   void ForEachSiblingElementWithName(const char *name,
104                                      NodeCallback const &callback) const;
105
106   void ForEachChildNode(NodeCallback const &callback) const;
107
108   void ForEachChildElement(NodeCallback const &callback) const;
109
110   void ForEachChildElementWithName(const char *name,
111                                    NodeCallback const &callback) const;
112
113   void ForEachAttribute(AttributeCallback const &callback) const;
114
115 protected:
116   XMLNodeImpl m_node;
117 };
118
119 class XMLDocument {
120 public:
121   XMLDocument();
122
123   ~XMLDocument();
124
125   explicit operator bool() const { return IsValid(); }
126
127   bool IsValid() const;
128
129   void Clear();
130
131   bool ParseFile(const char *path);
132
133   bool ParseMemory(const char *xml, size_t xml_length,
134                    const char *url = "untitled.xml");
135
136   //----------------------------------------------------------------------
137   // If \a name is nullptr, just get the root element node, else only return
138   // a value XMLNode if the name of the root element matches \a name.
139   //----------------------------------------------------------------------
140   XMLNode GetRootElement(const char *required_name = nullptr);
141
142   llvm::StringRef GetErrors() const;
143
144   static void ErrorCallback(void *ctx, const char *format, ...);
145
146   static bool XMLEnabled();
147
148 protected:
149   XMLDocumentImpl m_document;
150   StreamString m_errors;
151 };
152
153 class ApplePropertyList {
154 public:
155   ApplePropertyList();
156
157   ApplePropertyList(const char *path);
158
159   ~ApplePropertyList();
160
161   bool ParseFile(const char *path);
162
163   llvm::StringRef GetErrors() const;
164
165   explicit operator bool() const { return IsValid(); }
166
167   bool IsValid() const;
168
169   XMLNode GetValueNode(const char *key) const;
170
171   bool GetValueAsString(const char *key, std::string &value) const;
172
173   StructuredData::ObjectSP GetStructuredData();
174
175 protected:
176   // Using a node returned from GetValueNode() extract its value as a
177   // string (if possible). Array and dictionary nodes will return false
178   // as they have no string value. Boolean nodes will return true and
179   // \a value will be "true" or "false" as the string value comes from
180   // the element name itself. All other nodes will return the text
181   // content of the XMLNode.
182   static bool ExtractStringFromValueNode(const XMLNode &node,
183                                          std::string &value);
184
185   XMLDocument m_xml_doc;
186   XMLNode m_dict_node;
187 };
188
189 } // namespace lldb_private
190
191 #endif // liblldb_XML_h_