Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <map> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | #include "android-base/macros.h" |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 26 | #include "androidfw/IDiagnostics.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 27 | #include "xml/XmlDom.h" |
| 28 | |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 29 | namespace aapt { |
| 30 | namespace xml { |
| 31 | |
| 32 | enum class XmlActionExecutorPolicy { |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 33 | // Actions are run if elements are matched, errors occur only when actions return false. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | kNone, |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 35 | |
Izabela Orlowska | ad9e132 | 2017-12-19 16:22:42 +0000 | [diff] [blame] | 36 | // The actions defined must match and run. If an element is found that does not match an action, |
| 37 | // an error occurs. |
Adam Lesinski | 63699b1 | 2017-05-08 18:36:33 -0700 | [diff] [blame] | 38 | // Note: namespaced elements are always ignored. |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 39 | kAllowList, |
Izabela Orlowska | ad9e132 | 2017-12-19 16:22:42 +0000 | [diff] [blame] | 40 | |
| 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 Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 44 | kAllowListWarning, |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 47 | // Contains the actions to perform at this XML node. This is a recursive data structure that |
| 48 | // holds XmlNodeActions for child XML nodes. |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 49 | class XmlNodeAction { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 50 | public: |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 51 | using ActionFuncWithPolicyAndDiag = |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 52 | std::function<bool(Element*, XmlActionExecutorPolicy, android::SourcePathDiagnostics*)>; |
| 53 | using ActionFuncWithDiag = std::function<bool(Element*, android::SourcePathDiagnostics*)>; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | using ActionFunc = std::function<bool(Element*)>; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 55 | |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 56 | // 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 Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 61 | |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 62 | // Add an action to be performed at this XmlNodeAction. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 63 | void Action(ActionFunc f); |
| 64 | void Action(ActionFuncWithDiag); |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 65 | void Action(ActionFuncWithPolicyAndDiag); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 66 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | private: |
| 68 | friend class XmlActionExecutor; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 69 | |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 70 | bool Execute(XmlActionExecutorPolicy policy, std::vector<::android::StringPiece>* bread_crumb, |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 71 | android::SourcePathDiagnostics* diag, Element* el) const; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 72 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | std::map<std::string, XmlNodeAction> map_; |
Rhed Jao | 2c43442 | 2020-11-12 10:48:03 +0800 | [diff] [blame] | 74 | std::vector<ActionFuncWithPolicyAndDiag> actions_; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 75 | }; |
| 76 | |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 77 | // Allows the definition of actions to execute at specific XML elements defined by their hierarchy. |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 78 | class XmlActionExecutor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 79 | public: |
| 80 | XmlActionExecutor() = default; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 81 | |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 82 | // 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 Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 87 | |
Adam Lesinski | ed37f48 | 2017-11-02 12:07:08 -0700 | [diff] [blame] | 88 | // Execute the defined actions for this XmlResource. |
| 89 | // Returns true if all actions return true, otherwise returns false. |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame^] | 90 | bool Execute(XmlActionExecutorPolicy policy, android::IDiagnostics* diag, XmlResource* doc) const; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 91 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 92 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | std::map<std::string, XmlNodeAction> map_; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 94 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | DISALLOW_COPY_AND_ASSIGN(XmlActionExecutor); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 98 | } // namespace xml |
| 99 | } // namespace aapt |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 100 | |
| 101 | #endif /* AAPT_XML_XMLPATTERN_H */ |