blob: 9d3ecc866c5df545327555dea1b89b129ab61543 [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>
21
22#include "android-base/macros.h"
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020023#include "androidfw/ConfigDescription.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include "Diagnostics.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027#include "ResourceTable.h"
28#include "ResourceValues.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080029#include "StringPool.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include "util/Maybe.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.
57 Maybe<Visibility::Level> visibility;
Adam Lesinski9ba47d82015-10-13 11:37:10 -070058};
59
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080060/*
61 * Parses an XML file for resources and adds them to a ResourceTable.
62 */
63class ResourceParser {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 public:
65 ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source,
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020066 const android::ConfigDescription& config,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 const ResourceParserOptions& options = {});
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 bool Parse(xml::XmlPullParser* parser);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 DISALLOW_COPY_AND_ASSIGN(ResourceParser);
72
Adam Lesinski75421622017-01-06 15:20:04 -080073 // Parses the XML subtree as a StyleString (flattened XML representation for strings with
74 // formatting). If parsing fails, false is returned and the out parameters are left in an
75 // unspecified state. Otherwise,
76 // `out_style_string` contains the escaped and whitespace trimmed text.
77 // `out_raw_string` contains the un-escaped text.
78 // `out_untranslatable_sections` contains the sections of the string that should not be
79 // translated.
80 bool FlattenXmlSubtree(xml::XmlPullParser* parser, std::string* out_raw_string,
81 StyleString* out_style_string,
82 std::vector<UntranslatableSection>* out_untranslatable_sections);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 /*
85 * Parses the XML subtree and returns an Item.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 * The type of Item that can be parsed is denoted by the `type_mask`.
87 * If `allow_raw_value` is true and the subtree can not be parsed as a regular
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 * Item, then a
89 * RawString is returned. Otherwise this returns false;
90 */
Adam Lesinskibab4ef52017-06-01 15:22:57 -070091 std::unique_ptr<Item> ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 const bool allow_raw_value);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 bool ParseResources(xml::XmlPullParser* parser);
95 bool ParseResource(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080096
Adam Lesinskibab4ef52017-06-01 15:22:57 -070097 bool ParseItem(xml::XmlPullParser* parser, ParsedResource* out_resource, uint32_t format);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 bool ParseString(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinski7ff3ee12015-12-14 16:08:50 -080099
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 bool ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700101 bool ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource);
102 bool ParseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 bool ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinski46c4d722017-08-23 13:03:56 -0700104 bool ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700105 bool ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 bool ParseAttr(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700107 bool ParseAttrImpl(xml::XmlPullParser* parser, ParsedResource* out_resource, bool weak);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 Maybe<Attribute::Symbol> ParseEnumOrFlagItem(xml::XmlPullParser* parser,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800109 const android::StringPiece& tag);
Adam Lesinski86d67df2017-01-31 13:47:27 -0800110 bool ParseStyle(const ResourceType type, xml::XmlPullParser* parser,
111 ParsedResource* out_resource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700112 bool ParseStyleItem(xml::XmlPullParser* parser, Style* style);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700113 bool ParseDeclareStyleable(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 bool ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinskibab4ef52017-06-01 15:22:57 -0700115 bool ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource);
116 bool ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource);
117 bool ParseArrayImpl(xml::XmlPullParser* parser, ParsedResource* out_resource, uint32_t typeMask);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 bool ParsePlural(xml::XmlPullParser* parser, ParsedResource* out_resource);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120 IDiagnostics* diag_;
121 ResourceTable* table_;
122 Source source_;
Mårten Kongstad24c9aa62018-06-20 08:46:41 +0200123 android::ConfigDescription config_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 ResourceParserOptions options_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125};
126
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700127} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800128
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129#endif // AAPT_RESOURCE_PARSER_H