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