1 //===-- XML.h ---------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_XML_h_
11 #define liblldb_XML_h_
14 #if defined(LIBXML2_DEFINED)
15 #include <libxml/xmlreader.h>
23 // Other libraries and framework includes
24 #include "llvm/ADT/StringRef.h"
27 #include "lldb/Utility/StreamString.h"
28 #include "lldb/Utility/StructuredData.h"
29 #include "lldb/lldb-private.h"
31 namespace lldb_private {
33 #if defined(LIBXML2_DEFINED)
34 typedef xmlNodePtr XMLNodeImpl;
35 typedef xmlDocPtr XMLDocumentImpl;
37 typedef void *XMLNodeImpl;
38 typedef void *XMLDocumentImpl;
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)>
53 XMLNode(XMLNodeImpl node);
57 explicit operator bool() const { return IsValid(); }
63 bool IsElement() const;
65 llvm::StringRef GetName() const;
67 bool GetElementText(std::string &text) const;
69 bool GetElementTextAsUnsigned(uint64_t &value, uint64_t fail_value = 0,
72 bool GetElementTextAsFloat(double &value, double fail_value = 0.0) const;
74 bool NameIs(const char *name) const;
76 XMLNode GetParent() const;
78 XMLNode GetSibling() const;
80 XMLNode GetChild() const;
82 llvm::StringRef GetAttributeValue(const char *name,
83 const char *fail_value = nullptr) const;
85 XMLNode FindFirstChildElementWithName(const char *name) const;
87 XMLNode GetElementForPath(const NamePath &path);
89 //----------------------------------------------------------------------
90 // Iterate through all sibling nodes of any type
91 //----------------------------------------------------------------------
92 void ForEachSiblingNode(NodeCallback const &callback) const;
94 //----------------------------------------------------------------------
95 // Iterate through only the sibling nodes that are elements
96 //----------------------------------------------------------------------
97 void ForEachSiblingElement(NodeCallback const &callback) const;
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;
106 void ForEachChildNode(NodeCallback const &callback) const;
108 void ForEachChildElement(NodeCallback const &callback) const;
110 void ForEachChildElementWithName(const char *name,
111 NodeCallback const &callback) const;
113 void ForEachAttribute(AttributeCallback const &callback) const;
125 explicit operator bool() const { return IsValid(); }
127 bool IsValid() const;
131 bool ParseFile(const char *path);
133 bool ParseMemory(const char *xml, size_t xml_length,
134 const char *url = "untitled.xml");
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);
142 llvm::StringRef GetErrors() const;
144 static void ErrorCallback(void *ctx, const char *format, ...);
146 static bool XMLEnabled();
149 XMLDocumentImpl m_document;
150 StreamString m_errors;
153 class ApplePropertyList {
157 ApplePropertyList(const char *path);
159 ~ApplePropertyList();
161 bool ParseFile(const char *path);
163 llvm::StringRef GetErrors() const;
165 explicit operator bool() const { return IsValid(); }
167 bool IsValid() const;
169 XMLNode GetValueNode(const char *key) const;
171 bool GetValueAsString(const char *key, std::string &value) const;
173 StructuredData::ObjectSP GetStructuredData();
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,
185 XMLDocument m_xml_doc;
189 } // namespace lldb_private
191 #endif // liblldb_XML_h_