blob: 90690d522ef2b4c3f9bc4d414007e7e87ec900ce [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
2 * Copyright (C) 2015 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_RESOURCE_PARSER_H
18#define AAPT_RESOURCE_PARSER_H
19
Adam Lesinskice5e56e2016-10-21 17:56:45 -070020#include <memory>
Ryan Mitchell4382e442021-07-14 12:53:01 -070021#include <optional>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include "ResourceTable.h"
24#include "ResourceValues.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000025#include "android-base/macros.h"
26#include "androidfw/ConfigDescription.h"
27#include "androidfw/IDiagnostics.h"
28#include "androidfw/StringPiece.h"
29#include "androidfw/StringPool.h"
Jeremy Meyer211bec22024-06-04 14:22:03 -070030#include "cmd/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080031#include "xml/XmlPullParser.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070032
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033namespace aapt {
34
Adam Lesinski9ba47d82015-10-13 11:37:10 -070035struct ParsedResource;
36
37struct ResourceParserOptions {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 /**
39 * Whether the default setting for this parser is to allow translation.
40 */
41 bool translatable = true;
Adam Lesinski979ccb22016-01-11 10:42:19 -080042
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 /**
44 * Whether positional arguments in formatted strings are treated as errors or
45 * warnings.
46 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 bool error_on_positional_arguments = true;
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +010048
Donald Chai94e4a012020-01-06 13:52:41 -080049 /**
50 * If true, apply the same visibility rules for styleables as are used for
51 * all other resources. Otherwise, all styleables will be made public.
52 */
53 bool preserve_visibility_of_styleables = false;
54
Izabela Orlowskac7ac3a12018-03-27 14:46:52 +010055 // If visibility was forced, we need to use it when creating a new resource and also error if we
56 // try to parse the <public>, <public-group>, <java-symbol> or <symbol> tags.
Ryan Mitchell4382e442021-07-14 12:53:01 -070057 std::optional<Visibility::Level> visibility;
Jeremy Meyer211bec22024-06-04 14:22:03 -070058
59 FeatureFlagValues feature_flag_values;
Jeremy Meyer988be5d2024-09-03 16:31:54 -070060
61 // The flag that should be applied to all resources parsed
62 std::optional<FeatureFlagAttribute> flag;
63
64 FlagStatus flag_status = FlagStatus::NoFlag;
Adam Lesinski9ba47d82015-10-13 11:37:10 -070065};
66
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070067struct FlattenedXmlSubTree {
68 std::string raw_value;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000069 android::StyleString style_string;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070070 std::vector<UntranslatableSection> untranslatable_sections;
71 xml::IPackageDeclStack* namespace_resolver;
Jeremy Meyer56f36e82022-05-20 20:35:42 +000072 android::Source source;
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070073};
74
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075/*
76 * Parses an XML file for resources and adds them to a ResourceTable.
77 */
78class ResourceParser {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 public:
Jeremy Meyer56f36e82022-05-20 20:35:42 +000080 ResourceParser(android::IDiagnostics* diag, ResourceTable* table, const android::Source& source,
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020081 const android::ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070082 const ResourceParserOptions& options = {});
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 bool Parse(xml::XmlPullParser* parser);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070085 static std::unique_ptr<Item> ParseXml(const FlattenedXmlSubTree& xmlsub_tree, uint32_t type_mask,
86 bool allow_raw_value, ResourceTable& table,
87 const android::ConfigDescription& config,
Jeremy Meyer56f36e82022-05-20 20:35:42 +000088 android::IDiagnostics& diag);
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070089
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 DISALLOW_COPY_AND_ASSIGN(ResourceParser);
92
Ryan Mitchell326e35ff2021-04-12 07:50:42 -070093 std::optional<FlattenedXmlSubTree> CreateFlattenSubTree(xml::XmlPullParser* parser);
94
Adam Lesinski75421622017-01-06 15:20:04 -080095 // Parses the XML subtree as a StyleString (flattened XML representation for strings with
96 // formatting). If parsing fails, false is returned and the out parameters are left in an
97 // unspecified state. Otherwise,
98 // `out_style_string` contains the escaped and whitespace trimmed text.
99 // `out_raw_string` contains the un-escaped text.
100 // `out_untranslatable_sections` contains the sections of the string that should not be
101 // translated.
102 bool FlattenXmlSubtree(xml::XmlPullParser* parser, std::string* out_raw_string,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000103 android::StyleString* out_style_string,
Adam Lesinski75421622017-01-06 15:20:04 -0800104 std::vector<UntranslatableSection>* out_untranslatable_sections);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 /*
107 * Parses the XML subtree and returns an Item.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 * The type of Item that can be parsed is denoted by the `type_mask`.
109 * If `allow_raw_value` is true and the subtree can not be parsed as a regular
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 * Item, then a
111 * RawString is returned. Otherwise this returns false;
112 */
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700113 std::unique_ptr<Item> ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 const bool allow_raw_value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800115
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700116 bool ParseResources(xml::XmlPullParser* parser);
117 bool ParseResource(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -0800118
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700119 bool ParseItem(xml::XmlPullParser* parser, ParsedResource* out_resource, uint32_t format);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 bool ParseString(xml::XmlPullParser* parser, ParsedResource* out_resource);
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700121 bool ParseMacro(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 bool ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700123 bool ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource);
Ryan Mitchell2e9bec12021-03-22 09:31:00 -0700124 bool ParseStagingPublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource);
Ryan Mitchell2fedba92021-04-23 07:47:38 -0700125 bool ParseStagingPublicGroupFinal(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700126 bool ParseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 bool ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinski46c4d722017-08-23 13:03:56 -0700128 bool ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700129 bool ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700130 bool ParseAttr(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700131 bool ParseAttrImpl(xml::XmlPullParser* parser, ParsedResource* out_resource, bool weak);
Ryan Mitchell4382e442021-07-14 12:53:01 -0700132 std::optional<Attribute::Symbol> ParseEnumOrFlagItem(xml::XmlPullParser* parser,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700133 android::StringPiece tag);
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700134 bool ParseStyle(ResourceType type, xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 bool ParseStyleItem(xml::XmlPullParser* parser, Style* style);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700136 bool ParseDeclareStyleable(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 bool ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700138 bool ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource);
139 bool ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource);
140 bool ParseArrayImpl(xml::XmlPullParser* parser, ParsedResource* out_resource, uint32_t typeMask);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 bool ParsePlural(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000143 android::IDiagnostics* diag_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700144 ResourceTable* table_;
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000145 android::Source source_;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200146 android::ConfigDescription config_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 ResourceParserOptions options_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800148};
149
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800151
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152#endif // AAPT_RESOURCE_PARSER_H