]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Host/XML.h
Update LLDB snapshot to upstream r241361
[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
15 #if defined( LIBXML2_DEFINED )
16 #include <libxml/xmlreader.h>
17 #endif
18
19 // C++ Includes
20
21 #include <functional>
22 #include <string>
23 #include <vector>
24
25 // Other libraries and framework includes
26 // Project includes
27 #include "lldb/lldb-private.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "lldb/Core/StreamString.h"
30 #include "lldb/Core/StructuredData.h"
31
32
33 namespace lldb_private {
34
35 #if defined( LIBXML2_DEFINED )
36     typedef xmlNodePtr XMLNodeImpl;
37     typedef xmlDocPtr  XMLDocumentImpl;
38 #else
39     typedef void * XMLNodeImpl;
40     typedef void * XMLDocumentImpl;
41 #endif
42
43     class XMLNode;
44     
45     typedef std::vector<std::string> NamePath;
46     typedef std::function <bool(const XMLNode &node)> NodeCallback;
47     typedef std::function <bool(const llvm::StringRef &name, const llvm::StringRef &value)> AttributeCallback;
48
49     class XMLNode
50     {
51     public:
52         XMLNode();
53         
54         XMLNode(XMLNodeImpl node);
55         
56         ~XMLNode();
57         
58         explicit operator bool() const
59         {
60             return IsValid();
61         }
62         
63         void
64         Clear();
65         
66         bool
67         IsValid() const;
68         
69         bool
70         IsElement () const;
71
72         llvm::StringRef
73         GetName() const;
74         
75         bool
76         GetElementText (std::string &text) const;
77
78         bool
79         GetElementTextAsUnsigned (uint64_t &value, uint64_t fail_value = 0, int base = 0) const;
80
81         bool
82         GetElementTextAsFloat (double &value, double fail_value = 0.0) const;
83
84         bool
85         NameIs (const char *name) const;
86         
87         XMLNode
88         GetParent() const;
89         
90         XMLNode
91         GetSibling() const;
92         
93         XMLNode
94         GetChild () const;
95         
96         llvm::StringRef
97         GetAttributeValue(const char *name, const char *fail_value = NULL) const;
98         
99         XMLNode
100         FindFirstChildElementWithName (const char *name) const;
101         
102         XMLNode
103         GetElementForPath (const NamePath &path);
104         
105         //----------------------------------------------------------------------
106         // Iterate through all sibling nodes of any type
107         //----------------------------------------------------------------------
108         void
109         ForEachSiblingNode (NodeCallback const &callback) const;
110
111         //----------------------------------------------------------------------
112         // Iterate through only the sibling nodes that are elements
113         //----------------------------------------------------------------------
114         void
115         ForEachSiblingElement (NodeCallback const &callback) const;
116
117         //----------------------------------------------------------------------
118         // Iterate through only the sibling nodes that are elements and whose
119         // name matches \a name.
120         //----------------------------------------------------------------------
121         void
122         ForEachSiblingElementWithName (const char *name, NodeCallback const &callback) const;
123         
124         void
125         ForEachChildNode (NodeCallback const &callback) const;
126         
127         void
128         ForEachChildElement (NodeCallback const &callback) const;
129         
130         void
131         ForEachChildElementWithName (const char *name, NodeCallback const &callback) const;
132         
133         void
134         ForEachAttribute (AttributeCallback const &callback) const;
135         
136     protected:
137         XMLNodeImpl m_node;
138     };
139
140     class XMLDocument
141     {
142     public:
143         
144         XMLDocument ();
145         
146         ~XMLDocument ();
147         
148         explicit operator bool() const
149         {
150             return IsValid();
151         }
152         
153         bool
154         IsValid() const;
155         
156         void
157         Clear();
158         
159         bool
160         ParseFile (const char *path);
161         
162         bool
163         ParseMemory (const char *xml, size_t xml_length, const char *url = "untitled.xml");
164
165         //----------------------------------------------------------------------
166         // If \a name is NULL, just get the root element node, else only return
167         // a value XMLNode if the name of the root element matches \a name.
168         //----------------------------------------------------------------------
169         XMLNode
170         GetRootElement(const char *required_name = nullptr);
171
172         const std::string &
173         GetErrors() const;
174         
175         static void
176         ErrorCallback (void *ctx, const char *format, ...);
177
178         static bool
179         XMLEnabled ();
180         
181     protected:
182         XMLDocumentImpl m_document;
183         StreamString m_errors;
184     };
185
186     class ApplePropertyList
187     {
188     public:
189         ApplePropertyList();
190
191         ApplePropertyList(const char *path);
192
193         ~ApplePropertyList();
194
195         bool
196         ParseFile (const char *path);
197
198         const std::string &
199         GetErrors() const;
200         
201         explicit operator bool() const
202         {
203             return IsValid();
204         }
205         
206         bool
207         IsValid() const;
208         
209         XMLNode
210         GetValueNode (const char *key) const;
211
212         bool
213         GetValueAsString (const char *key, std::string &value) const;
214
215         StructuredData::ObjectSP
216         GetStructuredData();
217
218     protected:
219
220         // Using a node returned from GetValueNode() extract its value as a
221         // string (if possible). Array and dictionary nodes will return false
222         // as they have no string value. Boolean nodes will return true and
223         // \a value will be "true" or "false" as the string value comes from
224         // the element name itself. All other nodes will return the text
225         // content of the XMLNode.
226         static bool
227         ExtractStringFromValueNode (const XMLNode &node, std::string &value);
228
229         XMLDocument m_xml_doc;
230         XMLNode m_dict_node;
231     };
232 } // namespace lldb_private
233
234 #endif  // liblldb_XML_h_