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