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 LLDB_HOST_XML_H
10 #define LLDB_HOST_XML_H
11 
12 #include "lldb/Host/Config.h"
13 
14 #if LLDB_ENABLE_LIBXML2
15 #include <libxml/xmlreader.h>
16 #endif
17 
18 #include <functional>
19 #include <string>
20 #include <vector>
21 
22 #include "llvm/ADT/StringRef.h"
23 
26 #include "lldb/lldb-private.h"
27 
28 namespace lldb_private {
29 
30 #if LLDB_ENABLE_LIBXML2
31 typedef xmlNodePtr XMLNodeImpl;
32 typedef xmlDocPtr XMLDocumentImpl;
33 #else
34 typedef void *XMLNodeImpl;
35 typedef void *XMLDocumentImpl;
36 #endif
37 
38 class XMLNode;
39 
40 typedef std::vector<std::string> NamePath;
41 typedef std::function<bool(const XMLNode &node)> NodeCallback;
42 typedef std::function<bool(const llvm::StringRef &name,
43  const llvm::StringRef &value)>
45 
46 class XMLNode {
47 public:
48  XMLNode();
49 
50  XMLNode(XMLNodeImpl node);
51 
52  ~XMLNode();
53 
54  explicit operator bool() const { return IsValid(); }
55 
56  void Clear();
57 
58  bool IsValid() const;
59 
60  bool IsElement() const;
61 
62  llvm::StringRef GetName() const;
63 
64  bool GetElementText(std::string &text) const;
65 
66  bool GetElementTextAsUnsigned(uint64_t &value, uint64_t fail_value = 0,
67  int base = 0) const;
68 
69  bool GetElementTextAsFloat(double &value, double fail_value = 0.0) const;
70 
71  bool NameIs(const char *name) const;
72 
73  XMLNode GetParent() const;
74 
75  XMLNode GetSibling() const;
76 
77  XMLNode GetChild() const;
78 
79  llvm::StringRef GetAttributeValue(const char *name,
80  const char *fail_value = nullptr) const;
81 
82  bool GetAttributeValueAsUnsigned(const char *name, uint64_t &value,
83  uint64_t fail_value = 0, int base = 0) const;
84 
85  XMLNode FindFirstChildElementWithName(const char *name) const;
86 
87  XMLNode GetElementForPath(const NamePath &path);
88 
89  // Iterate through all sibling nodes of any type
90  void ForEachSiblingNode(NodeCallback const &callback) const;
91 
92  // Iterate through only the sibling nodes that are elements
93  void ForEachSiblingElement(NodeCallback const &callback) const;
94 
95  // Iterate through only the sibling nodes that are elements and whose name
96  // matches \a name.
97  void ForEachSiblingElementWithName(const char *name,
98  NodeCallback const &callback) const;
99 
100  void ForEachChildNode(NodeCallback const &callback) const;
101 
102  void ForEachChildElement(NodeCallback const &callback) const;
103 
104  void ForEachChildElementWithName(const char *name,
105  NodeCallback const &callback) const;
106 
107  void ForEachAttribute(AttributeCallback const &callback) const;
108 
109 protected:
110  XMLNodeImpl m_node = nullptr;
111 };
112 
113 class XMLDocument {
114 public:
115  XMLDocument();
116 
117  ~XMLDocument();
118 
119  explicit operator bool() const { return IsValid(); }
120 
121  bool IsValid() const;
122 
123  void Clear();
124 
125  bool ParseFile(const char *path);
126 
127  bool ParseMemory(const char *xml, size_t xml_length,
128  const char *url = "untitled.xml");
129 
130  // If \a name is nullptr, just get the root element node, else only return a
131  // value XMLNode if the name of the root element matches \a name.
132  XMLNode GetRootElement(const char *required_name = nullptr);
133 
134  llvm::StringRef GetErrors() const;
135 
136  static void ErrorCallback(void *ctx, const char *format, ...);
137 
138  static bool XMLEnabled();
139 
140 protected:
143 };
144 
146 public:
148 
149  ApplePropertyList(const char *path);
150 
152 
153  bool ParseFile(const char *path);
154 
155  llvm::StringRef GetErrors() const;
156 
157  explicit operator bool() const { return IsValid(); }
158 
159  bool IsValid() const;
160 
161  XMLNode GetValueNode(const char *key) const;
162 
163  bool GetValueAsString(const char *key, std::string &value) const;
164 
166 
167 protected:
168  // Using a node returned from GetValueNode() extract its value as a string
169  // (if possible). Array and dictionary nodes will return false as they have
170  // no string value. Boolean nodes will return true and \a value will be
171  // "true" or "false" as the string value comes from the element name itself.
172  // All other nodes will return the text content of the XMLNode.
173  static bool ExtractStringFromValueNode(const XMLNode &node,
174  std::string &value);
175 
178 };
179 
180 } // namespace lldb_private
181 
182 #endif // LLDB_HOST_XML_H
lldb_private::XMLNode::XMLNode
XMLNode()
lldb_private::XMLNode::IsElement
bool IsElement() const
Definition: XML.cpp:365
lldb_private::XMLNode::GetElementTextAsFloat
bool GetElementTextAsFloat(double &value, double fail_value=0.0) const
Definition: XML.cpp:318
lldb_private::ApplePropertyList::ParseFile
bool ParseFile(const char *path)
Definition: XML.cpp:407
StructuredData.h
lldb_private::ApplePropertyList::m_dict_node
XMLNode m_dict_node
Definition: XML.h:177
lldb_private::NamePath
std::vector< std::string > NamePath
Definition: XML.h:38
lldb_private::XMLDocument
Definition: XML.h:113
lldb_private::XMLNode::ForEachChildElementWithName
void ForEachChildElementWithName(const char *name, NodeCallback const &callback) const
Definition: XML.cpp:181
lldb_private::XMLNode::ForEachChildNode
void ForEachChildNode(NodeCallback const &callback) const
Definition: XML.cpp:166
lldb_private::XMLDocument::ParseMemory
bool ParseMemory(const char *xml, size_t xml_length, const char *url="untitled.xml")
Definition: XML.cpp:55
lldb_private::XMLNode
Definition: XML.h:46
lldb_private::XMLDocument::ParseFile
bool ParseFile(const char *path)
Definition: XML.cpp:45
lldb_private::XMLNode::GetElementText
bool GetElementText(std::string &text) const
Definition: XML.cpp:282
lldb_private::XMLNode::GetName
llvm::StringRef GetName() const
Definition: XML.cpp:272
lldb_private::XMLNode::GetChild
XMLNode GetChild() const
Definition: XML.cpp:124
lldb_private::XMLNode::NameIs
bool NameIs(const char *name) const
Definition: XML.cpp:334
lldb_private::XMLNode::ForEachSiblingElement
void ForEachSiblingElement(NodeCallback const &callback) const
Definition: XML.cpp:226
lldb_private::XMLDocument::m_errors
StreamString m_errors
Definition: XML.h:142
lldb_private::ApplePropertyList::IsValid
bool IsValid() const
Definition: XML.cpp:422
lldb_private::ApplePropertyList::GetValueAsString
bool GetValueAsString(const char *key, std::string &value) const
Definition: XML.cpp:424
lldb_private::XMLNode::ForEachAttribute
void ForEachAttribute(AttributeCallback const &callback) const
Definition: XML.cpp:190
lldb_private::XMLNode::GetElementForPath
XMLNode GetElementForPath(const NamePath &path)
Definition: XML.cpp:373
lldb_private::XMLDocument::GetRootElement
XMLNode GetRootElement(const char *required_name=nullptr)
Definition: XML.cpp:66
lldb_private::AttributeCallback
std::function< bool(const llvm::StringRef &name, const llvm::StringRef &value)> AttributeCallback
Definition: XML.h:44
lldb_private::XMLNode::GetAttributeValueAsUnsigned
bool GetAttributeValueAsUnsigned(const char *name, uint64_t &value, uint64_t fail_value=0, int base=0) const
Definition: XML.cpp:154
lldb_private::XMLNode::Clear
void Clear()
Definition: XML.cpp:100
lldb_private::StructuredData::ObjectSP
std::shared_ptr< Object > ObjectSP
Definition: StructuredData.h:59
StreamString.h
lldb_private::XMLDocument::m_document
XMLDocumentImpl m_document
Definition: XML.h:141
lldb_private::XMLDocument::XMLEnabled
static bool XMLEnabled()
Definition: XML.cpp:84
lldb_private::XMLNode::GetAttributeValue
llvm::StringRef GetAttributeValue(const char *name, const char *fail_value=nullptr) const
Definition: XML.cpp:136
lldb_private::StreamString
Definition: StreamString.h:23
lldb_private::XMLNode::ForEachChildElement
void ForEachChildElement(NodeCallback const &callback) const
Definition: XML.cpp:173
lldb_private::XMLDocumentImpl
void * XMLDocumentImpl
Definition: XML.h:35
lldb_private::XMLNode::GetElementTextAsUnsigned
bool GetElementTextAsUnsigned(uint64_t &value, uint64_t fail_value=0, int base=0) const
Definition: XML.cpp:303
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:39
lldb_private::ApplePropertyList::GetStructuredData
StructuredData::ObjectSP GetStructuredData()
Definition: XML.cpp:534
lldb_private::XMLDocument::~XMLDocument
~XMLDocument()
Definition: XML.cpp:22
lldb_private::ApplePropertyList::GetErrors
llvm::StringRef GetErrors() const
Definition: XML.cpp:403
lldb-private.h
lldb_private::NodeCallback
std::function< bool(const XMLNode &node)> NodeCallback
Definition: XML.h:41
lldb_private::XMLNode::ForEachSiblingElementWithName
void ForEachSiblingElementWithName(const char *name, NodeCallback const &callback) const
Definition: XML.cpp:243
lldb_private::ApplePropertyList
Definition: XML.h:145
lldb_private::ApplePropertyList::~ApplePropertyList
~ApplePropertyList()
lldb_private::ApplePropertyList::m_xml_doc
XMLDocument m_xml_doc
Definition: XML.h:176
lldb_private::ApplePropertyList::ExtractStringFromValueNode
static bool ExtractStringFromValueNode(const XMLNode &node, std::string &value)
Definition: XML.cpp:455
lldb_private::XMLDocument::XMLDocument
XMLDocument()
lldb_private::XMLNode::GetParent
XMLNode GetParent() const
Definition: XML.cpp:102
lldb_private::XMLNodeImpl
void * XMLNodeImpl
Definition: XML.h:34
lldb_private::XMLNode::FindFirstChildElementWithName
XMLNode FindFirstChildElementWithName(const char *name) const
Definition: XML.cpp:348
lldb_private::ApplePropertyList::GetValueNode
XMLNode GetValueNode(const char *key) const
Definition: XML.cpp:432
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::XMLDocument::Clear
void Clear()
Definition: XML.cpp:24
lldb_private::XMLDocument::IsValid
bool IsValid() const
Definition: XML.cpp:34
lldb_private::ApplePropertyList::ApplePropertyList
ApplePropertyList()
Definition: XML.cpp:394
lldb_private::XMLNode::m_node
XMLNodeImpl m_node
Definition: XML.h:110
lldb_private::XMLDocument::ErrorCallback
static void ErrorCallback(void *ctx, const char *format,...)
Definition: XML.cpp:36
lldb_private::XMLNode::IsValid
bool IsValid() const
Definition: XML.cpp:363
lldb_private::XMLNode::ForEachSiblingNode
void ForEachSiblingNode(NodeCallback const &callback) const
Definition: XML.cpp:213
lldb_private::XMLDocument::GetErrors
llvm::StringRef GetErrors() const
Definition: XML.cpp:82
lldb_private::XMLNode::~XMLNode
~XMLNode()
lldb_private::XMLNode::GetSibling
XMLNode GetSibling() const
Definition: XML.cpp:113