blob: 3a3c5bc94fde232c60aaf29e4b9cbbec1007daee [file] [log] [blame]
Yifan Hong676447a2016-11-15 12:57:23 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// Convert objects from and to xml.
18
19#include <tinyxml2.h>
20
21#include "parse_string.h"
22#include "parse_xml.h"
23
24namespace android {
25namespace vintf {
26
27// --------------- tinyxml2 details
28
29using NodeType = tinyxml2::XMLElement;
30using DocType = tinyxml2::XMLDocument;
31
32// caller is responsible for deleteDocument() call
33inline DocType *createDocument() {
34 return new tinyxml2::XMLDocument();
35}
36
37// caller is responsible for deleteDocument() call
38inline DocType *createDocument(const std::string &xml) {
39 DocType *doc = new tinyxml2::XMLDocument();
40 if (doc->Parse(xml.c_str()) == tinyxml2::XML_NO_ERROR) {
41 return doc;
42 }
43 delete doc;
44 return nullptr;
45}
46
47inline void deleteDocument(DocType *d) {
48 delete d;
49}
50
51inline std::string printDocument(DocType *d) {
52 tinyxml2::XMLPrinter p;
53 d->Print(&p);
54 return std::string{p.CStr()};
55}
56
57inline NodeType *createNode(const std::string &name, DocType *d) {
58 return d->NewElement(name.c_str());
59}
60
61inline void appendChild(NodeType *parent, NodeType *child) {
62 parent->InsertEndChild(child);
63}
64
65inline void appendChild(DocType *parent, NodeType *child) {
66 parent->InsertEndChild(child);
67}
68
69inline void appendStrAttr(NodeType *e, const std::string &attrName, const std::string &attr) {
70 e->SetAttribute(attrName.c_str(), attr.c_str());
71}
72
73// text -> text
74inline void appendText(NodeType *parent, const std::string &text, DocType *d) {
75 parent->InsertEndChild(d->NewText(text.c_str()));
76}
77
78// text -> <name>text</name>
79inline void appendTextElement(NodeType *parent, const std::string &name,
80 const std::string &text, DocType *d) {
81 NodeType *c = createNode(name, d);
82 appendText(c, text, d);
83 appendChild(parent, c);
84}
85
86inline std::string nameOf(NodeType *root) {
Yifan Hongcd41ffc2017-01-20 13:46:32 -080087 return root->Name() == NULL ? "" : root->Name();
Yifan Hong676447a2016-11-15 12:57:23 -080088}
89
90inline std::string getText(NodeType *root) {
Yifan Hongcd41ffc2017-01-20 13:46:32 -080091 return root->GetText() == NULL ? "" : root->GetText();
Yifan Hong676447a2016-11-15 12:57:23 -080092}
93
94inline NodeType *getChild(NodeType *parent, const std::string &name) {
95 return parent->FirstChildElement(name.c_str());
96}
97
98inline NodeType *getRootChild(DocType *parent) {
99 return parent->FirstChildElement();
100}
101
102inline std::vector<NodeType *> getChildren(NodeType *parent, const std::string &name) {
103 std::vector<NodeType *> v;
104 for (NodeType *child = parent->FirstChildElement(name.c_str());
105 child != nullptr;
106 child = child->NextSiblingElement(name.c_str())) {
107 v.push_back(child);
108 }
109 return v;
110}
111
112inline bool getAttr(NodeType *root, const std::string &attrName, std::string *s) {
113 const char *c = root->Attribute(attrName.c_str());
114 if (c == NULL)
115 return false;
116 *s = c;
117 return true;
118}
119
120// --------------- tinyxml2 details end.
121
122// ---------------------- XmlNodeConverter definitions
123
124template<typename Object>
125struct XmlNodeConverter : public XmlConverter<Object> {
126 XmlNodeConverter() {}
127 virtual ~XmlNodeConverter() {}
128
129 // sub-types should implement these.
130 virtual void mutateNode(const Object &o, NodeType *n, DocType *d) const = 0;
131 virtual bool buildObject(Object *o, NodeType *n) const = 0;
132 virtual std::string elementName() const = 0;
133
134 // convenience methods for user
135 inline const std::string &lastError() const { return mLastError; }
136 inline NodeType *serialize(const Object &o, DocType *d) const {
137 NodeType *root = createNode(this->elementName(), d);
138 this->mutateNode(o, root, d);
139 return root;
140 }
141 inline std::string serialize(const Object &o) const {
142 DocType *doc = createDocument();
143 appendChild(doc, serialize(o, doc));
144 std::string s = printDocument(doc);
145 deleteDocument(doc);
146 return s;
147 }
148 inline bool deserialize(Object *object, NodeType *root) const {
149 if (nameOf(root) != this->elementName()) {
150 return false;
151 }
152 return this->buildObject(object, root);
153 }
154 inline bool deserialize(Object *o, const std::string &xml) const {
155 DocType *doc = createDocument(xml);
156 bool ret = doc != nullptr
157 && deserialize(o, getRootChild(doc));
158 deleteDocument(doc);
159 return ret;
160 }
161 inline NodeType *operator()(const Object &o, DocType *d) const {
162 return serialize(o, d);
163 }
164 inline std::string operator()(const Object &o) const {
165 return serialize(o);
166 }
167 inline bool operator()(Object *o, NodeType *node) const {
168 return deserialize(o, node);
169 }
170 inline bool operator()(Object *o, const std::string &xml) const {
171 return deserialize(o, xml);
172 }
173
174 // convenience methods for implementor.
175 template <typename T>
176 inline void appendAttr(NodeType *e, const std::string &attrName, const T &attr) const {
177 return appendStrAttr(e, attrName, ::android::vintf::to_string(attr));
178 }
179
180 inline void appendAttr(NodeType *e, const std::string &attrName, bool attr) const {
181 return appendStrAttr(e, attrName, attr ? "true" : "false");
182 }
183
184 template <typename T>
185 inline bool parseAttr(NodeType *root, const std::string &attrName, T *attr) const {
186 std::string attrText;
187 bool ret = getAttr(root, attrName, &attrText) && ::android::vintf::parse(attrText, attr);
188 if (!ret) {
189 mLastError = "Could not parse attr with name " + attrName;
190 }
191 return ret;
192 }
193
194 inline bool parseAttr(NodeType *root, const std::string &attrName, std::string *attr) const {
195 bool ret = getAttr(root, attrName, attr);
196 if (!ret) {
197 mLastError = "Could not find attr with name " + attrName;
198 }
199 return ret;
200 }
201
202 inline bool parseAttr(NodeType *root, const std::string &attrName, bool *attr) const {
203 std::string attrText;
204 if (!getAttr(root, attrName, &attrText)) {
205 mLastError = "Could not find attr with name " + attrName;
206 return false;
207 }
208 if (attrText == "true" || attrText == "1") {
209 *attr = true;
210 return true;
211 }
212 if (attrText == "false" || attrText == "0") {
213 *attr = false;
214 return true;
215 }
216 mLastError = "Could not parse attr with name \"" + attrName
217 + "\" and value \"" + attrText + "\"";
218 return false;
219 }
220
221 inline bool parseTextElement(NodeType *root,
222 const std::string &elementName, std::string *s) const {
223 NodeType *child = getChild(root, elementName);
224 if (child == nullptr) {
225 mLastError = "Could not find element with name " + elementName;
226 return false;
227 }
228 *s = getText(child);
229 return true;
230 }
231
232 template <typename T>
233 inline bool parseChild(NodeType *root, const XmlNodeConverter<T> &conv, T *t) const {
234 NodeType *child = getChild(root, conv.elementName());
235 if (child == nullptr) {
236 mLastError = "Could not find element with name " + conv.elementName();
237 return false;
238 }
239 bool success = conv.deserialize(t, child);
240 if (!success) {
241 mLastError = conv.lastError();
242 }
243 return success;
244 }
245
246 template <typename T>
247 inline bool parseChildren(NodeType *root, const XmlNodeConverter<T> &conv, std::vector<T> *v) const {
248 auto nodes = getChildren(root, conv.elementName());
249 v->resize(nodes.size());
250 for (size_t i = 0; i < nodes.size(); ++i) {
251 if (!conv.deserialize(&v->at(i), nodes[i])) {
252 mLastError = "Could not parse element with name " + conv.elementName();
253 return false;
254 }
255 }
256 return true;
257 }
258
259 inline bool parseText(NodeType *node, std::string *s) const {
260 *s = getText(node);
261 return true;
262 }
263
264protected:
265 mutable std::string mLastError;
266};
267
268template<typename Object>
269struct XmlTextConverter : public XmlNodeConverter<Object> {
270 XmlTextConverter(const std::string &elementName)
271 : mElementName(elementName) {}
272
273 virtual void mutateNode(const Object &object, NodeType *root, DocType *d) const override {
274 appendText(root, ::android::vintf::to_string(object), d);
275 }
276 virtual bool buildObject(Object *object, NodeType *root) const override {
277 std::string text = getText(root);
278 bool ret = ::android::vintf::parse(text, object);
279 if (!ret) {
280 this->mLastError = "Could not parse " + text;
281 }
282 return ret;
283 }
284 virtual std::string elementName() const { return mElementName; };
285private:
286 std::string mElementName;
287};
288
289// ---------------------- XmlNodeConverter definitions end
290
291const XmlTextConverter<Version> versionConverter{"version"};
292const XmlConverter<Version> &gVersionConverter = versionConverter;
293
294const XmlTextConverter<VersionRange> versionRangeConverter{"version"};
295const XmlConverter<VersionRange> &gVersionRangeConverter = versionRangeConverter;
296
297const XmlTextConverter<KernelConfig> kernelConfigConverter{"config"};
298const XmlConverter<KernelConfig> &gKernelConfigConverter = kernelConfigConverter;
299
300const XmlTextConverter<Transport> transportConverter{"transport"};
301const XmlConverter<Transport> &gTransportConverter = transportConverter;
302
Yifan Honga9993572017-01-24 19:33:15 -0800303struct MatrixHalConverter : public XmlNodeConverter<MatrixHal> {
Yifan Hong676447a2016-11-15 12:57:23 -0800304 std::string elementName() const override { return "hal"; }
305 void mutateNode(const MatrixHal &hal, NodeType *root, DocType *d) const override {
306 appendAttr(root, "format", hal.format);
307 appendAttr(root, "optional", hal.optional);
308 appendTextElement(root, "name", hal.name, d);
309 for (const auto &version : hal.versionRanges) {
310 appendChild(root, versionRangeConverter(version, d));
311 }
312 }
313 bool buildObject(MatrixHal *object, NodeType *root) const override {
314 if ( !parseAttr(root, "format", &object->format)
315 || !parseAttr(root, "optional", &object->optional)
316 || !parseTextElement(root, "name", &object->name)
317 || !parseChildren(root, versionRangeConverter, &object->versionRanges)) {
318 return false;
319 }
320 return true;
321 }
322};
323
324const MatrixHalConverter matrixHalConverter{};
325const XmlConverter<MatrixHal> &gMatrixHalConverter = matrixHalConverter;
326
Yifan Honga9993572017-01-24 19:33:15 -0800327struct MatrixKernelConverter : public XmlNodeConverter<MatrixKernel> {
Yifan Hong676447a2016-11-15 12:57:23 -0800328 std::string elementName() const override { return "kernel"; }
329 void mutateNode(const MatrixKernel &kernel, NodeType *root, DocType *d) const override {
330 appendAttr(root, "version", kernel.version);
331 for (const auto &config : kernel.configs) {
332 appendChild(root, kernelConfigConverter(config, d));
333 }
334 }
335 bool buildObject(MatrixKernel *object, NodeType *root) const override {
336 if ( !parseAttr(root, "version", &object->version)
337 || !parseChildren(root, kernelConfigConverter, &object->configs)) {
338 return false;
339 }
340 return true;
341 }
342};
343
344const MatrixKernelConverter matrixKernelConverter{};
345const XmlConverter<MatrixKernel> &gMatrixKernelConverter = matrixKernelConverter;
346
Yifan Honga9993572017-01-24 19:33:15 -0800347struct HalImplementationConverter : public XmlNodeConverter<HalImplementation> {
Yifan Hong676447a2016-11-15 12:57:23 -0800348 std::string elementName() const override { return "impl"; }
349 void mutateNode(const HalImplementation &impl, NodeType *root, DocType *d) const override {
350 appendAttr(root, "level", impl.implLevel);
351 appendText(root, impl.impl, d);
352 }
353 bool buildObject(HalImplementation *object, NodeType *root) const override {
354 if ( !parseAttr(root, "level", &object->implLevel)
355 || !parseText(root, &object->impl)) {
356 return false;
357 }
358 return true;
359 }
360};
361
362const HalImplementationConverter halImplementationConverter{};
363const XmlConverter<HalImplementation> &gHalImplementationConverter = halImplementationConverter;
364
Yifan Honga9993572017-01-24 19:33:15 -0800365struct ManifestHalConverter : public XmlNodeConverter<ManifestHal> {
Yifan Hong676447a2016-11-15 12:57:23 -0800366 std::string elementName() const override { return "hal"; }
367 void mutateNode(const ManifestHal &hal, NodeType *root, DocType *d) const override {
368 appendAttr(root, "format", hal.format);
369 appendTextElement(root, "name", hal.name, d);
370 appendChild(root, transportConverter(hal.transport, d));
371 appendChild(root,
372 halImplementationConverter(hal.impl, d));
373 for (const auto &version : hal.versions) {
374 appendChild(root, versionConverter(version, d));
375 }
376 }
377 bool buildObject(ManifestHal *object, NodeType *root) const override {
378 if ( !parseAttr(root, "format", &object->format)
379 || !parseTextElement(root, "name", &object->name)
380 || !parseChild(root, transportConverter, &object->transport)
381 || !parseChild(root, halImplementationConverter, &object->impl)
382 || !parseChildren(root, versionConverter, &object->versions)) {
383 return false;
384 }
385 return true;
386 }
387};
388
389const ManifestHalConverter manifestHalConverter{};
390const XmlConverter<ManifestHal> &gManifestHalConverter = manifestHalConverter;
391
Yifan Honga9993572017-01-24 19:33:15 -0800392struct SepolicyConverter : public XmlNodeConverter<Sepolicy> {
Yifan Hong676447a2016-11-15 12:57:23 -0800393 std::string elementName() const override { return "sepolicy"; }
394 void mutateNode(const Sepolicy &, NodeType *, DocType *) const override {
395 // TODO after writing sepolicy
396 }
397 bool buildObject(Sepolicy *, NodeType *) const override {
398 // TODO after writing sepolicy
399 return true;
400 }
401};
402const SepolicyConverter sepolicyConverter{};
403const XmlConverter<Sepolicy> &gSepolicyConverter = sepolicyConverter;
404
Yifan Honga9993572017-01-24 19:33:15 -0800405struct VendorManifestConverter : public XmlNodeConverter<VendorManifest> {
Yifan Hong676447a2016-11-15 12:57:23 -0800406 std::string elementName() const override { return "manifest"; }
407 void mutateNode(const VendorManifest &m, NodeType *root, DocType *d) const override {
408 appendAttr(root, "version", VendorManifest::kVersion);
Yifan Hongef6d4d32017-01-23 14:12:28 -0800409 for (const auto &hal : m.getHals()) {
410 appendChild(root, manifestHalConverter(hal, d));
Yifan Hong676447a2016-11-15 12:57:23 -0800411 }
412 }
413 bool buildObject(VendorManifest *object, NodeType *root) const override {
414 std::vector<ManifestHal> hals;
415 if (!parseChildren(root, manifestHalConverter, &hals)) {
416 return false;
417 }
418 for (auto &&hal : hals) {
419 object->add(std::move(hal));
420 }
421 return true;
422 }
423};
424
425const VendorManifestConverter vendorManifestConverter{};
426const XmlConverter<VendorManifest> &gVendorManifestConverter = vendorManifestConverter;
427
Yifan Honga9993572017-01-24 19:33:15 -0800428struct CompatibilityMatrixConverter : public XmlNodeConverter<CompatibilityMatrix> {
Yifan Hong676447a2016-11-15 12:57:23 -0800429 std::string elementName() const override { return "compatibility-matrix"; }
430 void mutateNode(const CompatibilityMatrix &m, NodeType *root, DocType *d) const override {
431 appendAttr(root, "version", CompatibilityMatrix::kVersion);
432 for (const auto &pair : m.hals) {
433 appendChild(root, matrixHalConverter(pair.second, d));
434 }
435 for (const auto &kernel : m.kernels) {
436 appendChild(root, matrixKernelConverter(kernel, d));
437 }
438 appendChild(root, sepolicyConverter(m.sepolicy, d));
439 }
440 bool buildObject(CompatibilityMatrix *object, NodeType *root) const override {
441 std::vector<MatrixHal> hals;
442 if ( !parseChildren(root, matrixHalConverter, &hals)
443 || !parseChildren(root, matrixKernelConverter, &object->kernels)
444 || !parseChild(root, sepolicyConverter, &object->sepolicy)) {
445 return false;
446 }
447 for (auto &&hal : hals) {
448 object->add(std::move(hal));
449 }
450 return true;
451 }
452};
453
454const CompatibilityMatrixConverter compatibilityMatrixConverter{};
455const XmlConverter<CompatibilityMatrix> &gCompatibilityMatrixConverter
456 = compatibilityMatrixConverter;
457
458} // namespace vintf
459} // namespace android