1 // Copyright 2014 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROMEOS_DBUS_BINDINGS_XML_INTERFACE_PARSER_H_ 6 #define CHROMEOS_DBUS_BINDINGS_XML_INTERFACE_PARSER_H_ 7 8 #include <expat.h> 9 10 #include <map> 11 #include <string> 12 #include <vector> 13 14 #include <base/macros.h> 15 16 #include "chromeos-dbus-bindings/interface.h" 17 18 namespace base { 19 20 class FilePath; 21 22 } // namespace base 23 24 namespace chromeos_dbus_bindings { 25 26 class XmlInterfaceParser { 27 public: 28 using XmlAttributeMap = std::map<std::string, std::string>; 29 30 XmlInterfaceParser() = default; 31 virtual ~XmlInterfaceParser() = default; 32 33 bool ParseXmlInterfaceFile(const std::string& contents, 34 const std::vector<std::string>& ignore_interfaces); 35 const std::vector<Interface>& interfaces() const { return interfaces_; } 36 37 private: 38 friend class XmlInterfaceParserTest; 39 40 // XML tag names. 41 static const char kArgumentTag[]; 42 static const char kInterfaceTag[]; 43 static const char kMethodTag[]; 44 static const char kNodeTag[]; 45 static const char kSignalTag[]; 46 static const char kPropertyTag[]; 47 static const char kAnnotationTag[]; 48 static const char kDocStringTag[]; 49 50 // XML attribute names. 51 static const char kNameAttribute[]; 52 static const char kTypeAttribute[]; 53 static const char kDirectionAttribute[]; 54 static const char kAccessAttribute[]; 55 static const char kValueAttribute[]; 56 57 // XML argument directions. 58 static const char kArgumentDirectionIn[]; 59 static const char kArgumentDirectionOut[]; 60 61 // XML annotations. 62 static const char kTrue[]; 63 static const char kFalse[]; 64 65 static const char kMethodConst[]; 66 static const char kMethodAsync[]; 67 static const char kMethodIncludeDBusMessage[]; 68 69 static const char kMethodKind[]; 70 static const char kMethodKindSimple[]; 71 static const char kMethodKindNormal[]; 72 static const char kMethodKindAsync[]; 73 static const char kMethodKindRaw[]; 74 75 // Element callbacks on |this| called by HandleElementStart() and 76 // HandleElementEnd(), respectively. 77 void OnOpenElement(const std::string& element_name, 78 const XmlAttributeMap& attributes); 79 void OnCloseElement(const std::string& element_name); 80 void OnCharData(const std::string& content); 81 82 // Methods for appending individual argument elements to the parser. 83 void AddMethodArgument(const XmlAttributeMap& attributes); 84 void AddSignalArgument(const XmlAttributeMap& attributes); 85 86 // Finds the |element_key| element in |attributes|. Returns true and sets 87 // |element_value| on success. Returns false otherwise. 88 static bool GetElementAttribute(const XmlAttributeMap& attributes, 89 const std::vector<std::string>& element_path, 90 const std::string& element_key, 91 std::string* element_value); 92 93 // Asserts that a non-empty |element_key| attribute appears in |attributes|. 94 // Returns the name on success, triggers a CHECK() otherwise. 95 static std::string GetValidatedElementAttribute( 96 const XmlAttributeMap& attributes, 97 const std::vector<std::string>& element_path, 98 const std::string& element_key); 99 100 // Calls GetValidatedElementAttribute() for the "name" property. 101 static std::string GetValidatedElementName( 102 const XmlAttributeMap& attributes, 103 const std::vector<std::string>& element_path); 104 105 // Method for extracting signal/method tag attributes to a struct. 106 static Interface::Argument ParseArgument( 107 const XmlAttributeMap& attributes, 108 const std::vector<std::string>& element_path); 109 110 // Method for extracting property tag attributes to a struct. 111 static Interface::Property ParseProperty( 112 const XmlAttributeMap& attributes, 113 const std::vector<std::string>& element_path); 114 115 // Expat element callback functions. 116 static void HandleElementStart(void* user_data, 117 const XML_Char* element, 118 const XML_Char** attr); 119 static void HandleElementEnd(void* user_data, const XML_Char* element); 120 static void HandleCharData(void* user_data, const char *content, int length); 121 122 // The output of the parse. 123 std::vector<Interface> interfaces_; 124 125 // A stack of <node> names used to track the object paths for interfaces. 126 std::vector<std::string> node_names_; 127 128 // Tracks where in the element traversal our parse has taken us. 129 std::vector<std::string> element_path_; 130 131 DISALLOW_COPY_AND_ASSIGN(XmlInterfaceParser); 132 }; 133 134 } // namespace chromeos_dbus_bindings 135 136 #endif // CHROMEOS_DBUS_BINDINGS_XML_INTERFACE_PARSER_H_ 137