blob: 68e35631988e2459d31f82b1e2b75ce491b72434 [file] [log] [blame]
Adam Lesinskicc5609d2016-04-05 12:41:07 -07001/*
2 * Copyright (C) 2016 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#ifndef AAPT_XML_XMLPATTERN_H
18#define AAPT_XML_XMLPATTERN_H
19
Adam Lesinskicc5609d2016-04-05 12:41:07 -070020#include <functional>
21#include <map>
22#include <string>
23#include <vector>
24
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025#include "android-base/macros.h"
26
27#include "Diagnostics.h"
28#include "xml/XmlDom.h"
29
Adam Lesinskicc5609d2016-04-05 12:41:07 -070030namespace aapt {
31namespace xml {
32
33enum class XmlActionExecutorPolicy {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 /**
35 * Actions on run if elements are matched, errors occur only when actions
36 * return false.
37 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038 kNone,
Adam Lesinskicc5609d2016-04-05 12:41:07 -070039
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 /**
41 * The actions defined must match and run. If an element is found that does
42 * not match
43 * an action, an error occurs.
44 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 kWhitelist,
Adam Lesinskicc5609d2016-04-05 12:41:07 -070046};
47
48/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 * Contains the actions to perform at this XML node. This is a recursive data
50 * structure that
Adam Lesinskicc5609d2016-04-05 12:41:07 -070051 * holds XmlNodeActions for child XML nodes.
52 */
53class XmlNodeAction {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 public:
55 using ActionFuncWithDiag =
56 std::function<bool(Element*, SourcePathDiagnostics*)>;
57 using ActionFunc = std::function<bool(Element*)>;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070058
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 /**
60 * Find or create a child XmlNodeAction that will be performed for the child
61 * element
62 * with the name `name`.
63 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 XmlNodeAction& operator[](const std::string& name) { return map_[name]; }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070065
Adam Lesinskicacb28f2016-10-19 12:18:14 -070066 /**
67 * Add an action to be performed at this XmlNodeAction.
68 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 void Action(ActionFunc f);
70 void Action(ActionFuncWithDiag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 private:
73 friend class XmlActionExecutor;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070074
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 bool Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 Element* el) const;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 std::map<std::string, XmlNodeAction> map_;
79 std::vector<ActionFuncWithDiag> actions_;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070080};
81
82/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 * Allows the definition of actions to execute at specific XML elements defined
84 * by their
Adam Lesinskicc5609d2016-04-05 12:41:07 -070085 * hierarchy.
86 */
87class XmlActionExecutor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 public:
89 XmlActionExecutor() = default;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070090
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 /**
92 * Find or create a root XmlNodeAction that will be performed for the root XML
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 * element with the name `name`.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 XmlNodeAction& operator[](const std::string& name) { return map_[name]; }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 /**
98 * Execute the defined actions for this XmlResource.
99 * Returns true if all actions return true, otherwise returns false.
100 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 bool Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 XmlResource* doc) const;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 std::map<std::string, XmlNodeAction> map_;
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 DISALLOW_COPY_AND_ASSIGN(XmlActionExecutor);
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700108};
109
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110} // namespace xml
111} // namespace aapt
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700112
113#endif /* AAPT_XML_XMLPATTERN_H */