blob: 78c43345deb7aca91562f3d1198bcf4361ea647d [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 Lesinski63699b12017-05-08 18:36:33 -070034 // Actions are run if elements are matched, errors occur only when actions return false.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035 kNone,
Adam Lesinskicc5609d2016-04-05 12:41:07 -070036
Izabela Orlowskaad9e1322017-12-19 16:22:42 +000037 // The actions defined must match and run. If an element is found that does not match an action,
38 // an error occurs.
Adam Lesinski63699b12017-05-08 18:36:33 -070039 // Note: namespaced elements are always ignored.
Ryan Mitchell4ea90752020-07-31 08:21:43 -070040 kAllowList,
Izabela Orlowskaad9e1322017-12-19 16:22:42 +000041
42 // The actions defined should match and run. if an element is found that does not match an
43 // action, a warning is printed.
44 // Note: namespaced elements are always ignored.
Ryan Mitchell4ea90752020-07-31 08:21:43 -070045 kAllowListWarning,
Adam Lesinskicc5609d2016-04-05 12:41:07 -070046};
47
Adam Lesinskied37f482017-11-02 12:07:08 -070048// Contains the actions to perform at this XML node. This is a recursive data structure that
49// holds XmlNodeActions for child XML nodes.
Adam Lesinskicc5609d2016-04-05 12:41:07 -070050class XmlNodeAction {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 public:
Rhed Jao2c434422020-11-12 10:48:03 +080052 using ActionFuncWithPolicyAndDiag =
53 std::function<bool(Element*, XmlActionExecutorPolicy, SourcePathDiagnostics*)>;
Adam Lesinski63699b12017-05-08 18:36:33 -070054 using ActionFuncWithDiag = std::function<bool(Element*, SourcePathDiagnostics*)>;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 using ActionFunc = std::function<bool(Element*)>;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070056
Adam Lesinskied37f482017-11-02 12:07:08 -070057 // Find or create a child XmlNodeAction that will be performed for the child element with the
58 // name `name`.
59 XmlNodeAction& operator[](const std::string& name) {
60 return map_[name];
61 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070062
Adam Lesinskied37f482017-11-02 12:07:08 -070063 // Add an action to be performed at this XmlNodeAction.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 void Action(ActionFunc f);
65 void Action(ActionFuncWithDiag);
Rhed Jao2c434422020-11-12 10:48:03 +080066 void Action(ActionFuncWithPolicyAndDiag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070067
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 private:
69 friend class XmlActionExecutor;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070070
Adam Lesinskied37f482017-11-02 12:07:08 -070071 bool Execute(XmlActionExecutorPolicy policy, std::vector<::android::StringPiece>* bread_crumb,
72 SourcePathDiagnostics* diag, Element* el) const;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070073
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 std::map<std::string, XmlNodeAction> map_;
Rhed Jao2c434422020-11-12 10:48:03 +080075 std::vector<ActionFuncWithPolicyAndDiag> actions_;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070076};
77
Adam Lesinskied37f482017-11-02 12:07:08 -070078// Allows the definition of actions to execute at specific XML elements defined by their hierarchy.
Adam Lesinskicc5609d2016-04-05 12:41:07 -070079class XmlActionExecutor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 public:
81 XmlActionExecutor() = default;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070082
Adam Lesinskied37f482017-11-02 12:07:08 -070083 // Find or create a root XmlNodeAction that will be performed for the root XML element with the
84 // name `name`.
85 XmlNodeAction& operator[](const std::string& name) {
86 return map_[name];
87 }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070088
Adam Lesinskied37f482017-11-02 12:07:08 -070089 // Execute the defined actions for this XmlResource.
90 // Returns true if all actions return true, otherwise returns false.
Adam Lesinski63699b12017-05-08 18:36:33 -070091 bool Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, XmlResource* doc) const;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070092
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 std::map<std::string, XmlNodeAction> map_;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 DISALLOW_COPY_AND_ASSIGN(XmlActionExecutor);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070097};
98
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099} // namespace xml
100} // namespace aapt
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700101
102#endif /* AAPT_XML_XMLPATTERN_H */