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 | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Actions on run if elements are matched, errors occur only when actions |
| 36 | * return false. |
| 37 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 38 | kNone, |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 39 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 40 | /** |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 45 | kWhitelist, |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 49 | * Contains the actions to perform at this XML node. This is a recursive data |
| 50 | * structure that |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 51 | * holds XmlNodeActions for child XML nodes. |
| 52 | */ |
| 53 | class XmlNodeAction { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | public: |
| 55 | using ActionFuncWithDiag = |
| 56 | std::function<bool(Element*, SourcePathDiagnostics*)>; |
| 57 | using ActionFunc = std::function<bool(Element*)>; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 58 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Find or create a child XmlNodeAction that will be performed for the child |
| 61 | * element |
| 62 | * with the name `name`. |
| 63 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 64 | XmlNodeAction& operator[](const std::string& name) { return map_[name]; } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 65 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | /** |
| 67 | * Add an action to be performed at this XmlNodeAction. |
| 68 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 69 | void Action(ActionFunc f); |
| 70 | void Action(ActionFuncWithDiag); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 71 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 72 | private: |
| 73 | friend class XmlActionExecutor; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 74 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 75 | bool Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | Element* el) const; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 77 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 78 | std::map<std::string, XmlNodeAction> map_; |
| 79 | std::vector<ActionFuncWithDiag> actions_; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 83 | * Allows the definition of actions to execute at specific XML elements defined |
| 84 | * by their |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 85 | * hierarchy. |
| 86 | */ |
| 87 | class XmlActionExecutor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | public: |
| 89 | XmlActionExecutor() = default; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 90 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Find or create a root XmlNodeAction that will be performed for the root XML |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 93 | * element with the name `name`. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 94 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 95 | XmlNodeAction& operator[](const std::string& name) { return map_[name]; } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 96 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | /** |
| 98 | * Execute the defined actions for this XmlResource. |
| 99 | * Returns true if all actions return true, otherwise returns false. |
| 100 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 101 | bool Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | XmlResource* doc) const; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 103 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame^] | 105 | std::map<std::string, XmlNodeAction> map_; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 106 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | DISALLOW_COPY_AND_ASSIGN(XmlActionExecutor); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | } // namespace xml |
| 111 | } // namespace aapt |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 112 | |
| 113 | #endif /* AAPT_XML_XMLPATTERN_H */ |