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 | |
| 20 | #include "Diagnostics.h" |
| 21 | #include "xml/XmlDom.h" |
| 22 | |
| 23 | #include <android-base/macros.h> |
| 24 | #include <functional> |
| 25 | #include <map> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
| 29 | namespace aapt { |
| 30 | namespace xml { |
| 31 | |
| 32 | enum class XmlActionExecutorPolicy { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 33 | /** |
| 34 | * Actions on run if elements are matched, errors occur only when actions |
| 35 | * return false. |
| 36 | */ |
| 37 | None, |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 38 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 39 | /** |
| 40 | * The actions defined must match and run. If an element is found that does |
| 41 | * not match |
| 42 | * an action, an error occurs. |
| 43 | */ |
| 44 | Whitelist, |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 48 | * Contains the actions to perform at this XML node. This is a recursive data |
| 49 | * structure that |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 50 | * holds XmlNodeActions for child XML nodes. |
| 51 | */ |
| 52 | class XmlNodeAction { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 53 | public: |
| 54 | using ActionFuncWithDiag = |
| 55 | std::function<bool(Element*, SourcePathDiagnostics*)>; |
| 56 | using ActionFunc = std::function<bool(Element*)>; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 57 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 58 | /** |
| 59 | * Find or create a child XmlNodeAction that will be performed for the child |
| 60 | * element |
| 61 | * with the name `name`. |
| 62 | */ |
| 63 | XmlNodeAction& operator[](const std::string& name) { return mMap[name]; } |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 64 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 65 | /** |
| 66 | * Add an action to be performed at this XmlNodeAction. |
| 67 | */ |
| 68 | void action(ActionFunc f); |
| 69 | void action(ActionFuncWithDiag); |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 70 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 71 | private: |
| 72 | friend class XmlActionExecutor; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 73 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 74 | bool execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag, |
| 75 | Element* el) const; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 76 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 77 | std::map<std::string, XmlNodeAction> mMap; |
| 78 | std::vector<ActionFuncWithDiag> mActions; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 82 | * Allows the definition of actions to execute at specific XML elements defined |
| 83 | * by their |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 84 | * hierarchy. |
| 85 | */ |
| 86 | class XmlActionExecutor { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 87 | public: |
| 88 | XmlActionExecutor() = default; |
Adam Lesinski | cc5609d | 2016-04-05 12:41:07 -0700 | [diff] [blame] | 89 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame^] | 90 | /** |
| 91 | * Find or create a root XmlNodeAction that will be performed for the root XML |
| 92 | * element |
| 93 | * with the name `name`. |
| 94 | */ |
| 95 | XmlNodeAction& operator[](const std::string& name) { return mMap[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 | */ |
| 101 | bool execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, |
| 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: |
| 105 | std::map<std::string, XmlNodeAction> mMap; |
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 */ |