LLDB  mainline
XML.h
Go to the documentation of this file.
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 
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)>
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;
141 };
142 
144 public:
146 
147  ApplePropertyList(const char *path);
148 
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 
176 };
177 
178 } // namespace lldb_private
179 
180 #endif // liblldb_XML_h_
XMLDocumentImpl m_document
Definition: XML.h:139
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
bool GetElementTextAsFloat(double &value, double fail_value=0.0) const
Definition: XML.cpp:317
void ForEachSiblingElement(NodeCallback const &callback) const
Definition: XML.cpp:225
XMLNode GetSibling() const
Definition: XML.cpp:112
void ForEachSiblingElementWithName(const char *name, NodeCallback const &callback) const
Definition: XML.cpp:242
void ForEachAttribute(AttributeCallback const &callback) const
Definition: XML.cpp:189
XMLNodeImpl m_node
Definition: XML.h:108
std::function< bool(const llvm::StringRef &name, const llvm::StringRef &value)> AttributeCallback
Definition: XML.h:42
XMLNode GetParent() const
Definition: XML.cpp:101
bool IsValid() const
Definition: XML.cpp:362
bool GetElementText(std::string &text) const
Definition: XML.cpp:281
StreamString m_errors
Definition: XML.h:140
XMLNode GetElementForPath(const NamePath &path)
Definition: XML.cpp:372
void ForEachChildNode(NodeCallback const &callback) const
Definition: XML.cpp:165
void ForEachSiblingNode(NodeCallback const &callback) const
Definition: XML.cpp:212
llvm::StringRef GetName() const
Definition: XML.cpp:271
bool GetAttributeValueAsUnsigned(const char *name, uint64_t &value, uint64_t fail_value=0, int base=0) const
Definition: XML.cpp:153
XMLNode GetChild() const
Definition: XML.cpp:123
bool GetElementTextAsUnsigned(uint64_t &value, uint64_t fail_value=0, int base=0) const
Definition: XML.cpp:302
void ForEachChildElement(NodeCallback const &callback) const
Definition: XML.cpp:172
bool IsElement() const
Definition: XML.cpp:364
void * XMLDocumentImpl
Definition: XML.h:33
std::function< bool(const XMLNode &node)> NodeCallback
Definition: XML.h:39
void ForEachChildElementWithName(const char *name, NodeCallback const &callback) const
Definition: XML.cpp:180
llvm::StringRef GetAttributeValue(const char *name, const char *fail_value=nullptr) const
Definition: XML.cpp:135
XMLNode FindFirstChildElementWithName(const char *name) const
Definition: XML.cpp:347
std::shared_ptr< Object > ObjectSP
void * XMLNodeImpl
Definition: XML.h:32
bool NameIs(const char *name) const
Definition: XML.cpp:333
std::vector< std::string > NamePath
Definition: XML.h:36