blob: 8cc4573d2c45fee970bdf654a76db72e25f3d7a1 [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"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000026#include "androidfw/IDiagnostics.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027#include "xml/XmlDom.h"
28
Adam Lesinskicc5609d2016-04-05 12:41:07 -070029namespace aapt {
30namespace xml {
31
32enum class XmlActionExecutorPolicy {
Adam Lesinski63699b12017-05-08 18:36:33 -070033 // Actions are run if elements are matched, errors occur only when actions return false.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 kNone,
Adam Lesinskicc5609d2016-04-05 12:41:07 -070035
Izabela Orlowskaad9e1322017-12-19 16:22:42 +000036 // The actions defined must match and run. If an element is found that does not match an action,
37 // an error occurs.
Adam Lesinski63699b12017-05-08 18:36:33 -070038 // Note: namespaced elements are always ignored.
Ryan Mitchell4ea90752020-07-31 08:21:43 -070039 kAllowList,
Izabela Orlowskaad9e1322017-12-19 16:22:42 +000040
41 // The actions defined should match and run. if an element is found that does not match an
42 // action, a warning is printed.
43 // Note: namespaced elements are always ignored.
Ryan Mitchell4ea90752020-07-31 08:21:43 -070044 kAllowListWarning,
Adam Lesinskicc5609d2016-04-05 12:41:07 -070045};
46
Adam Lesinskied37f482017-11-02 12:07:08 -070047// Contains the actions to perform at this XML node. This is a recursive data structure that
48// holds XmlNodeActions for child XML nodes.
Adam Lesinskicc5609d2016-04-05 12:41:07 -070049class XmlNodeAction {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 public:
Rhed Jao2c434422020-11-12 10:48:03 +080051 using ActionFuncWithPolicyAndDiag =
Jeremy Meyer56f36e82022-05-20 20:35:42 +000052 std::function<bool(Element*, XmlActionExecutorPolicy, android::SourcePathDiagnostics*)>;
53 using ActionFuncWithDiag = std::function<bool(Element*, android::SourcePathDiagnostics*)>;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 using ActionFunc = std::function<bool(Element*)>;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070055
Adam Lesinskied37f482017-11-02 12:07:08 -070056 // Find or create a child XmlNodeAction that will be performed for the child element with the
57 // name `name`.
58 XmlNodeAction& operator[](const std::string& name) {
59 return map_[name];
60 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070061
Adam Lesinskied37f482017-11-02 12:07:08 -070062 // Add an action to be performed at this XmlNodeAction.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 void Action(ActionFunc f);
64 void Action(ActionFuncWithDiag);
Rhed Jao2c434422020-11-12 10:48:03 +080065 void Action(ActionFuncWithPolicyAndDiag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070066
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 private:
68 friend class XmlActionExecutor;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070069
Adam Lesinskied37f482017-11-02 12:07:08 -070070 bool Execute(XmlActionExecutorPolicy policy, std::vector<::android::StringPiece>* bread_crumb,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000071 android::SourcePathDiagnostics* diag, Element* el) const;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070072
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 std::map<std::string, XmlNodeAction> map_;
Rhed Jao2c434422020-11-12 10:48:03 +080074 std::vector<ActionFuncWithPolicyAndDiag> actions_;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070075};
76
Adam Lesinskied37f482017-11-02 12:07:08 -070077// Allows the definition of actions to execute at specific XML elements defined by their hierarchy.
Adam Lesinskicc5609d2016-04-05 12:41:07 -070078class XmlActionExecutor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 public:
80 XmlActionExecutor() = default;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070081
Adam Lesinskied37f482017-11-02 12:07:08 -070082 // Find or create a root XmlNodeAction that will be performed for the root XML element with the
83 // name `name`.
84 XmlNodeAction& operator[](const std::string& name) {
85 return map_[name];
86 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070087
Adam Lesinskied37f482017-11-02 12:07:08 -070088 // Execute the defined actions for this XmlResource.
89 // Returns true if all actions return true, otherwise returns false.
Jeremy Meyer56f36e82022-05-20 20:35:42 +000090 bool Execute(XmlActionExecutorPolicy policy, android::IDiagnostics* diag, XmlResource* doc) const;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070091
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 std::map<std::string, XmlNodeAction> map_;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 DISALLOW_COPY_AND_ASSIGN(XmlActionExecutor);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070096};
97
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098} // namespace xml
99} // namespace aapt
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700100
101#endif /* AAPT_XML_XMLPATTERN_H */