1 //===-- XML.cpp -------------------------------------------------*- 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 #include <stdlib.h> /* atof */
12 #include "lldb/Host/StringConvert.h"
13 #include "lldb/Host/XML.h"
16 using namespace lldb_private;
18 #pragma mark-- XMLDocument
20 XMLDocument::XMLDocument() : m_document(nullptr) {}
22 XMLDocument::~XMLDocument() { Clear(); }
24 void XMLDocument::Clear() {
25 #if defined(LIBXML2_DEFINED)
27 xmlDocPtr doc = m_document;
34 bool XMLDocument::IsValid() const { return m_document != nullptr; }
36 void XMLDocument::ErrorCallback(void *ctx, const char *format, ...) {
37 XMLDocument *document = (XMLDocument *)ctx;
39 va_start(args, format);
40 document->m_errors.PrintfVarArg(format, args);
41 document->m_errors.EOL();
45 bool XMLDocument::ParseFile(const char *path) {
46 #if defined(LIBXML2_DEFINED)
48 xmlSetGenericErrorFunc((void *)this, XMLDocument::ErrorCallback);
49 m_document = xmlParseFile(path);
50 xmlSetGenericErrorFunc(nullptr, nullptr);
55 bool XMLDocument::ParseMemory(const char *xml, size_t xml_length,
57 #if defined(LIBXML2_DEFINED)
59 xmlSetGenericErrorFunc((void *)this, XMLDocument::ErrorCallback);
60 m_document = xmlReadMemory(xml, (int)xml_length, url, nullptr, 0);
61 xmlSetGenericErrorFunc(nullptr, nullptr);
66 XMLNode XMLDocument::GetRootElement(const char *required_name) {
67 #if defined(LIBXML2_DEFINED)
69 XMLNode root_node(xmlDocGetRootElement(m_document));
71 llvm::StringRef actual_name = root_node.GetName();
72 if (actual_name == required_name)
82 llvm::StringRef XMLDocument::GetErrors() const { return m_errors.GetString(); }
84 bool XMLDocument::XMLEnabled() {
85 #if defined(LIBXML2_DEFINED)
92 #pragma mark-- XMLNode
94 XMLNode::XMLNode() : m_node(nullptr) {}
96 XMLNode::XMLNode(XMLNodeImpl node) : m_node(node) {}
98 XMLNode::~XMLNode() {}
100 void XMLNode::Clear() { m_node = nullptr; }
102 XMLNode XMLNode::GetParent() const {
103 #if defined(LIBXML2_DEFINED)
105 return XMLNode(m_node->parent);
113 XMLNode XMLNode::GetSibling() const {
114 #if defined(LIBXML2_DEFINED)
116 return XMLNode(m_node->next);
124 XMLNode XMLNode::GetChild() const {
125 #if defined(LIBXML2_DEFINED)
128 return XMLNode(m_node->children);
136 llvm::StringRef XMLNode::GetAttributeValue(const char *name,
137 const char *fail_value) const {
138 const char *attr_value = NULL;
139 #if defined(LIBXML2_DEFINED)
142 attr_value = (const char *)xmlGetProp(m_node, (const xmlChar *)name);
144 attr_value = fail_value;
146 attr_value = fail_value;
149 return llvm::StringRef(attr_value);
151 return llvm::StringRef();
154 bool XMLNode::GetAttributeValueAsUnsigned(const char *name, uint64_t &value,
155 uint64_t fail_value, int base) const {
156 #if defined(LIBXML2_DEFINED)
157 llvm::StringRef str_value = GetAttributeValue(name, "");
159 llvm::StringRef str_value;
161 bool success = false;
162 value = StringConvert::ToUInt64(str_value.data(), fail_value, base, &success);
166 void XMLNode::ForEachChildNode(NodeCallback const &callback) const {
167 #if defined(LIBXML2_DEFINED)
169 GetChild().ForEachSiblingNode(callback);
173 void XMLNode::ForEachChildElement(NodeCallback const &callback) const {
174 #if defined(LIBXML2_DEFINED)
175 XMLNode child = GetChild();
177 child.ForEachSiblingElement(callback);
181 void XMLNode::ForEachChildElementWithName(const char *name,
182 NodeCallback const &callback) const {
183 #if defined(LIBXML2_DEFINED)
184 XMLNode child = GetChild();
186 child.ForEachSiblingElementWithName(name, callback);
190 void XMLNode::ForEachAttribute(AttributeCallback const &callback) const {
191 #if defined(LIBXML2_DEFINED)
194 for (xmlAttrPtr attr = m_node->properties; attr != nullptr;
196 // check if name matches
198 // check child is a text node
199 xmlNodePtr child = attr->children;
200 if (child->type == XML_TEXT_NODE) {
201 llvm::StringRef attr_value;
203 attr_value = llvm::StringRef((const char *)child->content);
204 if (!callback(llvm::StringRef((const char *)attr->name), attr_value))
213 void XMLNode::ForEachSiblingNode(NodeCallback const &callback) const {
214 #if defined(LIBXML2_DEFINED)
217 // iterate through all siblings
218 for (xmlNodePtr node = m_node; node; node = node->next) {
219 if (!callback(XMLNode(node)))
226 void XMLNode::ForEachSiblingElement(NodeCallback const &callback) const {
227 #if defined(LIBXML2_DEFINED)
230 // iterate through all siblings
231 for (xmlNodePtr node = m_node; node; node = node->next) {
232 // we are looking for element nodes only
233 if (node->type != XML_ELEMENT_NODE)
236 if (!callback(XMLNode(node)))
243 void XMLNode::ForEachSiblingElementWithName(
244 const char *name, NodeCallback const &callback) const {
245 #if defined(LIBXML2_DEFINED)
248 // iterate through all siblings
249 for (xmlNodePtr node = m_node; node; node = node->next) {
250 // we are looking for element nodes only
251 if (node->type != XML_ELEMENT_NODE)
254 // If name is nullptr, we take all nodes of type "t", else just the ones
255 // whose name matches
257 if (strcmp((const char *)node->name, name) != 0)
258 continue; // Name mismatch, ignore this one
261 continue; // nullptr name specified and this element has a name,
265 if (!callback(XMLNode(node)))
272 llvm::StringRef XMLNode::GetName() const {
273 #if defined(LIBXML2_DEFINED)
276 return llvm::StringRef((const char *)m_node->name);
279 return llvm::StringRef();
282 bool XMLNode::GetElementText(std::string &text) const {
284 #if defined(LIBXML2_DEFINED)
286 bool success = false;
287 if (m_node->type == XML_ELEMENT_NODE) {
288 // check child is a text node
289 for (xmlNodePtr node = m_node->children; node != nullptr;
291 if (node->type == XML_TEXT_NODE) {
292 text.append((const char *)node->content);
303 bool XMLNode::GetElementTextAsUnsigned(uint64_t &value, uint64_t fail_value,
305 bool success = false;
306 #if defined(LIBXML2_DEFINED)
309 if (GetElementText(text))
310 value = StringConvert::ToUInt64(text.c_str(), fail_value, base, &success);
318 bool XMLNode::GetElementTextAsFloat(double &value, double fail_value) const {
319 bool success = false;
320 #if defined(LIBXML2_DEFINED)
323 if (GetElementText(text)) {
324 value = atof(text.c_str());
334 bool XMLNode::NameIs(const char *name) const {
335 #if defined(LIBXML2_DEFINED)
338 // In case we are looking for a nullptr name or an exact pointer match
339 if (m_node->name == (const xmlChar *)name)
342 return strcmp((const char *)m_node->name, name) == 0;
348 XMLNode XMLNode::FindFirstChildElementWithName(const char *name) const {
351 #if defined(LIBXML2_DEFINED)
352 ForEachChildElementWithName(
353 name, [&result_node](const XMLNode &node) -> bool {
355 // Stop iterating, we found the node we wanted
363 bool XMLNode::IsValid() const { return m_node != nullptr; }
365 bool XMLNode::IsElement() const {
366 #if defined(LIBXML2_DEFINED)
368 return m_node->type == XML_ELEMENT_NODE;
373 XMLNode XMLNode::GetElementForPath(const NamePath &path) {
374 #if defined(LIBXML2_DEFINED)
380 XMLNode node = FindFirstChildElementWithName(path[0].c_str());
381 const size_t n = path.size();
382 for (size_t i = 1; node && i < n; ++i)
383 node = node.FindFirstChildElementWithName(path[i].c_str());
392 #pragma mark-- ApplePropertyList
394 ApplePropertyList::ApplePropertyList() : m_xml_doc(), m_dict_node() {}
396 ApplePropertyList::ApplePropertyList(const char *path)
397 : m_xml_doc(), m_dict_node() {
401 ApplePropertyList::~ApplePropertyList() {}
403 llvm::StringRef ApplePropertyList::GetErrors() const {
404 return m_xml_doc.GetErrors();
407 bool ApplePropertyList::ParseFile(const char *path) {
408 if (m_xml_doc.ParseFile(path)) {
409 XMLNode plist = m_xml_doc.GetRootElement("plist");
411 plist.ForEachChildElementWithName("dict",
412 [this](const XMLNode &dict) -> bool {
413 this->m_dict_node = dict;
414 return false; // Stop iterating
416 return (bool)m_dict_node;
422 bool ApplePropertyList::IsValid() const { return (bool)m_dict_node; }
424 bool ApplePropertyList::GetValueAsString(const char *key,
425 std::string &value) const {
426 XMLNode value_node = GetValueNode(key);
428 return ApplePropertyList::ExtractStringFromValueNode(value_node, value);
432 XMLNode ApplePropertyList::GetValueNode(const char *key) const {
434 #if defined(LIBXML2_DEFINED)
437 m_dict_node.ForEachChildElementWithName(
438 "key", [key, &value_node](const XMLNode &key_node) -> bool {
439 std::string key_name;
440 if (key_node.GetElementText(key_name)) {
441 if (key_name == key) {
442 value_node = key_node.GetSibling();
443 while (value_node && !value_node.IsElement())
444 value_node = value_node.GetSibling();
445 return false; // Stop iterating
448 return true; // Keep iterating
455 bool ApplePropertyList::ExtractStringFromValueNode(const XMLNode &node,
456 std::string &value) {
458 #if defined(LIBXML2_DEFINED)
459 if (node.IsValid()) {
460 llvm::StringRef element_name = node.GetName();
461 if (element_name == "true" || element_name == "false") {
462 // The text value _is_ the element name itself...
463 value = element_name.str();
465 } else if (element_name == "dict" || element_name == "array")
466 return false; // dictionaries and arrays have no text value, so we fail
468 return node.GetElementText(value);
474 #if defined(LIBXML2_DEFINED)
478 StructuredData::ObjectSP CreatePlistValue(XMLNode node) {
479 llvm::StringRef element_name = node.GetName();
480 if (element_name == "array") {
481 std::shared_ptr<StructuredData::Array> array_sp(
482 new StructuredData::Array());
483 node.ForEachChildElement([&array_sp](const XMLNode &node) -> bool {
484 array_sp->AddItem(CreatePlistValue(node));
485 return true; // Keep iterating through all child elements of the array
488 } else if (element_name == "dict") {
490 std::shared_ptr<StructuredData::Dictionary> dict_sp(
491 new StructuredData::Dictionary());
492 node.ForEachChildElement(
493 [&key_node, &dict_sp](const XMLNode &node) -> bool {
494 if (node.NameIs("key")) {
495 // This is a "key" element node
498 // This is a value node
500 std::string key_name;
501 key_node.GetElementText(key_name);
502 dict_sp->AddItem(key_name, CreatePlistValue(node));
506 return true; // Keep iterating through all child elements of the
510 } else if (element_name == "real") {
512 node.GetElementTextAsFloat(value);
513 return StructuredData::ObjectSP(new StructuredData::Float(value));
514 } else if (element_name == "integer") {
516 node.GetElementTextAsUnsigned(value, 0, 0);
517 return StructuredData::ObjectSP(new StructuredData::Integer(value));
518 } else if ((element_name == "string") || (element_name == "data") ||
519 (element_name == "date")) {
521 node.GetElementText(text);
522 return StructuredData::ObjectSP(
523 new StructuredData::String(std::move(text)));
524 } else if (element_name == "true") {
525 return StructuredData::ObjectSP(new StructuredData::Boolean(true));
526 } else if (element_name == "false") {
527 return StructuredData::ObjectSP(new StructuredData::Boolean(false));
529 return StructuredData::ObjectSP(new StructuredData::Null());
534 StructuredData::ObjectSP ApplePropertyList::GetStructuredData() {
535 StructuredData::ObjectSP root_sp;
536 #if defined(LIBXML2_DEFINED)
538 return CreatePlistValue(m_dict_node);