blob: 1d70045b30239b256d117086ebae7bc06388e2e3 [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
Adam Lesinski63699b12017-05-08 18:36:33 -070037 // The actions defined must match and run. If an element is found that does
38 // not match an action, an error occurs.
39 // Note: namespaced elements are always ignored.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070040 kWhitelist,
Adam Lesinskicc5609d2016-04-05 12:41:07 -070041};
42
43/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 * Contains the actions to perform at this XML node. This is a recursive data
45 * structure that
Adam Lesinskicc5609d2016-04-05 12:41:07 -070046 * holds XmlNodeActions for child XML nodes.
47 */
48class XmlNodeAction {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 public:
Adam Lesinski63699b12017-05-08 18:36:33 -070050 using ActionFuncWithDiag = std::function<bool(Element*, SourcePathDiagnostics*)>;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 using ActionFunc = std::function<bool(Element*)>;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070052
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 /**
54 * Find or create a child XmlNodeAction that will be performed for the child
Adam Lesinski63699b12017-05-08 18:36:33 -070055 * element with the name `name`.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 XmlNodeAction& operator[](const std::string& name) { return map_[name]; }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070058
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 /**
60 * Add an action to be performed at this XmlNodeAction.
61 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 void Action(ActionFunc f);
63 void Action(ActionFuncWithDiag);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 private:
66 friend class XmlActionExecutor;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070067
Adam Lesinski63699b12017-05-08 18:36:33 -070068 bool Execute(XmlActionExecutorPolicy policy, SourcePathDiagnostics* diag, Element* el) const;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070069
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 std::map<std::string, XmlNodeAction> map_;
71 std::vector<ActionFuncWithDiag> actions_;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070072};
73
74/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 * Allows the definition of actions to execute at specific XML elements defined
76 * by their
Adam Lesinskicc5609d2016-04-05 12:41:07 -070077 * hierarchy.
78 */
79class XmlActionExecutor {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 public:
81 XmlActionExecutor() = default;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 /**
84 * Find or create a root XmlNodeAction that will be performed for the root XML
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085 * element with the name `name`.
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 XmlNodeAction& operator[](const std::string& name) { return map_[name]; }
Adam Lesinskicc5609d2016-04-05 12:41:07 -070088
Adam Lesinskicacb28f2016-10-19 12:18:14 -070089 /**
90 * Execute the defined actions for this XmlResource.
91 * Returns true if all actions return true, otherwise returns false.
92 */
Adam Lesinski63699b12017-05-08 18:36:33 -070093 bool Execute(XmlActionExecutorPolicy policy, IDiagnostics* diag, XmlResource* doc) const;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070094
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 std::map<std::string, XmlNodeAction> map_;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070097
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 DISALLOW_COPY_AND_ASSIGN(XmlActionExecutor);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070099};
100
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700101} // namespace xml
102} // namespace aapt
Adam Lesinskicc5609d2016-04-05 12:41:07 -0700103
104#endif /* AAPT_XML_XMLPATTERN_H */