summaryrefslogtreecommitdiff
path: root/chromeos-dbus-bindings/xml_interface_parser.h
blob: 219ea0d8ff2ebe77658e59ed136d05e37436d521 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_DBUS_BINDINGS_XML_INTERFACE_PARSER_H_
#define CHROMEOS_DBUS_BINDINGS_XML_INTERFACE_PARSER_H_

#include <expat.h>

#include <map>
#include <string>
#include <vector>

#include <base/macros.h>

#include "chromeos-dbus-bindings/interface.h"

namespace base {

class FilePath;

}  // namespace base

namespace chromeos_dbus_bindings {

class XmlInterfaceParser {
 public:
  using XmlAttributeMap = std::map<std::string, std::string>;

  XmlInterfaceParser() = default;
  virtual ~XmlInterfaceParser() = default;

  bool ParseXmlInterfaceFile(const std::string& contents,
                             const std::vector<std::string>& ignore_interfaces);
  const std::vector<Interface>& interfaces() const { return interfaces_; }

 private:
  friend class XmlInterfaceParserTest;

  // XML tag names.
  static const char kArgumentTag[];
  static const char kInterfaceTag[];
  static const char kMethodTag[];
  static const char kNodeTag[];
  static const char kSignalTag[];
  static const char kPropertyTag[];
  static const char kAnnotationTag[];
  static const char kDocStringTag[];

  // XML attribute names.
  static const char kNameAttribute[];
  static const char kTypeAttribute[];
  static const char kDirectionAttribute[];
  static const char kAccessAttribute[];
  static const char kValueAttribute[];

  // XML argument directions.
  static const char kArgumentDirectionIn[];
  static const char kArgumentDirectionOut[];

  // XML annotations.
  static const char kTrue[];
  static const char kFalse[];

  static const char kMethodConst[];
  static const char kMethodAsync[];

  static const char kMethodKind[];
  static const char kMethodKindSimple[];
  static const char kMethodKindNormal[];
  static const char kMethodKindAsync[];
  static const char kMethodKindRaw[];

  // Element callbacks on |this| called by HandleElementStart() and
  // HandleElementEnd(), respectively.
  void OnOpenElement(const std::string& element_name,
                     const XmlAttributeMap& attributes);
  void OnCloseElement(const std::string& element_name);
  void OnCharData(const std::string& content);

  // Methods for appending individual argument elements to the parser.
  void AddMethodArgument(const XmlAttributeMap& attributes);
  void AddSignalArgument(const XmlAttributeMap& attributes);

  // Finds the |element_key| element in |attributes|.  Returns true and sets
  // |element_value| on success.  Returns false otherwise.
  static bool GetElementAttribute(const XmlAttributeMap& attributes,
                                  const std::vector<std::string>& element_path,
                                  const std::string& element_key,
                                  std::string* element_value);

  // Asserts that a non-empty |element_key| attribute appears in |attributes|.
  // Returns the name on success, triggers a CHECK() otherwise.
  static std::string GetValidatedElementAttribute(
      const XmlAttributeMap& attributes,
      const std::vector<std::string>& element_path,
      const std::string& element_key);

  // Calls GetValidatedElementAttribute() for the "name" property.
  static std::string GetValidatedElementName(
      const XmlAttributeMap& attributes,
      const std::vector<std::string>& element_path);

  // Method for extracting signal/method tag attributes to a struct.
  static Interface::Argument ParseArgument(
      const XmlAttributeMap& attributes,
      const std::vector<std::string>& element_path);

  // Method for extracting property tag attributes to a struct.
  static Interface::Property ParseProperty(
      const XmlAttributeMap& attributes,
      const std::vector<std::string>& element_path);

  // Expat element callback functions.
  static void HandleElementStart(void* user_data,
                                 const XML_Char* element,
                                 const XML_Char** attr);
  static void HandleElementEnd(void* user_data, const XML_Char* element);
  static void HandleCharData(void* user_data, const char *content, int length);

  // The output of the parse.
  std::vector<Interface> interfaces_;

  // A stack of <node> names used to track the object paths for interfaces.
  std::vector<std::string> node_names_;

  // Tracks where in the element traversal our parse has taken us.
  std::vector<std::string> element_path_;

  DISALLOW_COPY_AND_ASSIGN(XmlInterfaceParser);
};

}  // namespace chromeos_dbus_bindings

#endif  // CHROMEOS_DBUS_BINDINGS_XML_INTERFACE_PARSER_H_