Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | |
| 22 | #include "android-base/macros.h" |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 23 | #include "androidfw/ConfigDescription.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 24 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 26 | #include "Diagnostics.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 27 | #include "ResourceTable.h" |
| 28 | #include "ResourceValues.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 29 | #include "StringPool.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 30 | #include "util/Maybe.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 31 | #include "xml/XmlPullParser.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 32 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 33 | namespace aapt { |
| 34 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 35 | struct ParsedResource; |
| 36 | |
| 37 | struct ResourceParserOptions { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Whether the default setting for this parser is to allow translation. |
| 40 | */ |
| 41 | bool translatable = true; |
Adam Lesinski | 979ccb2 | 2016-01-11 10:42:19 -0800 | [diff] [blame] | 42 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Whether positional arguments in formatted strings are treated as errors or |
| 45 | * warnings. |
| 46 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | bool error_on_positional_arguments = true; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 50 | /* |
| 51 | * Parses an XML file for resources and adds them to a ResourceTable. |
| 52 | */ |
| 53 | class ResourceParser { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | public: |
| 55 | ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source, |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 56 | const android::ConfigDescription& config, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 57 | const ResourceParserOptions& options = {}); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 58 | bool Parse(xml::XmlPullParser* parser); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 59 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 60 | private: |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | DISALLOW_COPY_AND_ASSIGN(ResourceParser); |
| 62 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 63 | // Parses the XML subtree as a StyleString (flattened XML representation for strings with |
| 64 | // formatting). If parsing fails, false is returned and the out parameters are left in an |
| 65 | // unspecified state. Otherwise, |
| 66 | // `out_style_string` contains the escaped and whitespace trimmed text. |
| 67 | // `out_raw_string` contains the un-escaped text. |
| 68 | // `out_untranslatable_sections` contains the sections of the string that should not be |
| 69 | // translated. |
| 70 | bool FlattenXmlSubtree(xml::XmlPullParser* parser, std::string* out_raw_string, |
| 71 | StyleString* out_style_string, |
| 72 | std::vector<UntranslatableSection>* out_untranslatable_sections); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 73 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 74 | /* |
| 75 | * Parses the XML subtree and returns an Item. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | * The type of Item that can be parsed is denoted by the `type_mask`. |
| 77 | * If `allow_raw_value` is true and the subtree can not be parsed as a regular |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 78 | * Item, then a |
| 79 | * RawString is returned. Otherwise this returns false; |
| 80 | */ |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 81 | std::unique_ptr<Item> ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | const bool allow_raw_value); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 83 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | bool ParseResources(xml::XmlPullParser* parser); |
| 85 | bool ParseResource(xml::XmlPullParser* parser, ParsedResource* out_resource); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 86 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 87 | bool ParseItem(xml::XmlPullParser* parser, ParsedResource* out_resource, uint32_t format); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 88 | bool ParseString(xml::XmlPullParser* parser, ParsedResource* out_resource); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 89 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | bool ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 91 | bool ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource); |
| 92 | bool ParseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* out_resource); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | bool ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 94 | bool ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 95 | bool ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 96 | bool ParseAttr(xml::XmlPullParser* parser, ParsedResource* out_resource); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 97 | bool ParseAttrImpl(xml::XmlPullParser* parser, ParsedResource* out_resource, bool weak); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 98 | Maybe<Attribute::Symbol> ParseEnumOrFlagItem(xml::XmlPullParser* parser, |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 99 | const android::StringPiece& tag); |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 100 | bool ParseStyle(const ResourceType type, xml::XmlPullParser* parser, |
| 101 | ParsedResource* out_resource); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 102 | bool ParseStyleItem(xml::XmlPullParser* parser, Style* style); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 103 | bool ParseDeclareStyleable(xml::XmlPullParser* parser, ParsedResource* out_resource); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | bool ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource); |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 105 | bool ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource); |
| 106 | bool ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource); |
| 107 | bool ParseArrayImpl(xml::XmlPullParser* parser, ParsedResource* out_resource, uint32_t typeMask); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 108 | bool ParsePlural(xml::XmlPullParser* parser, ParsedResource* out_resource); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 109 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 110 | IDiagnostics* diag_; |
| 111 | ResourceTable* table_; |
| 112 | Source source_; |
Mårten Kongstad | 5c541f6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 113 | android::ConfigDescription config_; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 114 | ResourceParserOptions options_; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 118 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 119 | #endif // AAPT_RESOURCE_PARSER_H |