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 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 17 | #include "ResourceParser.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <functional> |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 20 | #include <limits> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 21 | #include <sstream> |
| 22 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 23 | #include <android-base/logging.h> |
| 24 | #include <idmap2/Policies.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 "ResourceTable.h" |
| 27 | #include "ResourceUtils.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 28 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | #include "ValueVisitor.h" |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 30 | #include "text/Utf8Iterator.h" |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 31 | #include "util/ImmutableMap.h" |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 32 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 33 | #include "util/Util.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 34 | #include "xml/XmlPullParser.h" |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 35 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 36 | using ::aapt::ResourceUtils::StringBuilder; |
| 37 | using ::aapt::text::Utf8Iterator; |
MÃ¥rten Kongstad | 24c9aa6 | 2018-06-20 08:46:41 +0200 | [diff] [blame] | 38 | using ::android::ConfigDescription; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 39 | using ::android::StringPiece; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 40 | |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 41 | using android::idmap2::policy::kPolicyStringToFlag; |
| 42 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | namespace aapt { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 44 | namespace { |
| 45 | constexpr const char* kPublicGroupTag = "public-group"; |
| 46 | constexpr const char* kStagingPublicGroupTag = "staging-public-group"; |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 47 | constexpr const char* kStagingPublicGroupFinalTag = "staging-public-group-final"; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 48 | } // namespace |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 50 | constexpr const char* sXliffNamespaceUri = "urn:oasis:names:tc:xliff:document:1.2"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 51 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 52 | // Returns true if the element is <skip> or <eat-comment> and can be safely ignored. |
| 53 | static bool ShouldIgnoreElement(const StringPiece& ns, const StringPiece& name) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 54 | return ns.empty() && (name == "skip" || name == "eat-comment"); |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 57 | static uint32_t ParseFormatTypeNoEnumsOrFlags(const StringPiece& piece) { |
| 58 | if (piece == "reference") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 59 | return android::ResTable_map::TYPE_REFERENCE; |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 60 | } else if (piece == "string") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 61 | return android::ResTable_map::TYPE_STRING; |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 62 | } else if (piece == "integer") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | return android::ResTable_map::TYPE_INTEGER; |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 64 | } else if (piece == "boolean") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | return android::ResTable_map::TYPE_BOOLEAN; |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 66 | } else if (piece == "color") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 67 | return android::ResTable_map::TYPE_COLOR; |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 68 | } else if (piece == "float") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 69 | return android::ResTable_map::TYPE_FLOAT; |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 70 | } else if (piece == "dimension") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 71 | return android::ResTable_map::TYPE_DIMENSION; |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 72 | } else if (piece == "fraction") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 73 | return android::ResTable_map::TYPE_FRACTION; |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 74 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 75 | return 0; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 78 | static uint32_t ParseFormatType(const StringPiece& piece) { |
| 79 | if (piece == "enum") { |
| 80 | return android::ResTable_map::TYPE_ENUM; |
| 81 | } else if (piece == "flags") { |
| 82 | return android::ResTable_map::TYPE_FLAGS; |
| 83 | } |
| 84 | return ParseFormatTypeNoEnumsOrFlags(piece); |
| 85 | } |
| 86 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 87 | static uint32_t ParseFormatAttribute(const StringPiece& str) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | uint32_t mask = 0; |
Chih-Hung Hsieh | a1b644e | 2018-12-11 11:09:20 -0800 | [diff] [blame] | 89 | for (const StringPiece& part : util::Tokenize(str, '|')) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 90 | StringPiece trimmed_part = util::TrimWhitespace(part); |
| 91 | uint32_t type = ParseFormatType(trimmed_part); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 92 | if (type == 0) { |
| 93 | return 0; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 94 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 95 | mask |= type; |
| 96 | } |
| 97 | return mask; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 100 | // A parsed resource ready to be added to the ResourceTable. |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 101 | struct ParsedResource { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 102 | ResourceName name; |
| 103 | ConfigDescription config; |
| 104 | std::string product; |
| 105 | Source source; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 106 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 107 | ResourceId id; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 108 | Visibility::Level visibility_level = Visibility::Level::kUndefined; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 109 | bool staged_api = false; |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 110 | bool allow_new = false; |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 111 | std::optional<OverlayableItem> overlayable_item; |
| 112 | std::optional<StagedId> staged_alias; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 113 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 114 | std::string comment; |
| 115 | std::unique_ptr<Value> value; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 116 | std::list<ParsedResource> child_resources; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | // Recursively adds resources to the ResourceTable. |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 120 | static bool AddResourcesToTable(ResourceTable* table, IDiagnostics* diag, ParsedResource* res) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | StringPiece trimmed_comment = util::TrimWhitespace(res->comment); |
| 122 | if (trimmed_comment.size() != res->comment.size()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 123 | // Only if there was a change do we re-assign. |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 124 | res->comment = trimmed_comment.to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 125 | } |
Adam Lesinski | 7656554 | 2016-03-10 21:55:04 -0800 | [diff] [blame] | 126 | |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 127 | NewResourceBuilder res_builder(res->name); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 128 | if (res->visibility_level != Visibility::Level::kUndefined) { |
| 129 | Visibility visibility; |
| 130 | visibility.level = res->visibility_level; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 131 | visibility.staged_api = res->staged_api; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 132 | visibility.source = res->source; |
| 133 | visibility.comment = res->comment; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 134 | res_builder.SetVisibility(visibility); |
| 135 | } |
| 136 | |
| 137 | if (res->id.is_valid()) { |
| 138 | res_builder.SetId(res->id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 139 | } |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 140 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 141 | if (res->allow_new) { |
| 142 | AllowNew allow_new; |
| 143 | allow_new.source = res->source; |
| 144 | allow_new.comment = res->comment; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 145 | res_builder.SetAllowNew(allow_new); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 148 | if (res->overlayable_item) { |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 149 | res_builder.SetOverlayable(res->overlayable_item.value()); |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (res->value != nullptr) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 153 | // Attach the comment, source and config to the value. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 154 | res->value->SetComment(std::move(res->comment)); |
| 155 | res->value->SetSource(std::move(res->source)); |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 156 | res_builder.SetValue(std::move(res->value), res->config, res->product); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 157 | } |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 158 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 159 | if (res->staged_alias) { |
| 160 | res_builder.SetStagedId(res->staged_alias.value()); |
| 161 | } |
| 162 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 163 | bool error = false; |
Ryan Mitchell | 9634efb | 2021-03-19 14:53:17 -0700 | [diff] [blame] | 164 | if (!res->name.entry.empty()) { |
| 165 | if (!table->AddResource(res_builder.Build(), diag)) { |
| 166 | return false; |
| 167 | } |
| 168 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 169 | for (ParsedResource& child : res->child_resources) { |
| 170 | error |= !AddResourcesToTable(table, diag, &child); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 171 | } |
| 172 | return !error; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Convenient aliases for more readable function calls. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 176 | enum { kAllowRawString = true, kNoRawString = false }; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 177 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 178 | ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table, |
| 179 | const Source& source, |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 180 | const ConfigDescription& config, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 181 | const ResourceParserOptions& options) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 182 | : diag_(diag), |
| 183 | table_(table), |
| 184 | source_(source), |
| 185 | config_(config), |
| 186 | options_(options) {} |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 187 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 188 | // Base class Node for representing the various Spans and UntranslatableSections of an XML string. |
| 189 | // This will be used to traverse and flatten the XML string into a single std::string, with all |
| 190 | // Span and Untranslatable data maintained in parallel, as indices into the string. |
| 191 | class Node { |
| 192 | public: |
| 193 | virtual ~Node() = default; |
| 194 | |
| 195 | // Adds the given child node to this parent node's set of child nodes, moving ownership to the |
| 196 | // parent node as well. |
| 197 | // Returns a pointer to the child node that was added as a convenience. |
| 198 | template <typename T> |
| 199 | T* AddChild(std::unique_ptr<T> node) { |
| 200 | T* raw_ptr = node.get(); |
| 201 | children.push_back(std::move(node)); |
| 202 | return raw_ptr; |
| 203 | } |
| 204 | |
| 205 | virtual void Build(StringBuilder* builder) const { |
| 206 | for (const auto& child : children) { |
| 207 | child->Build(builder); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | std::vector<std::unique_ptr<Node>> children; |
| 212 | }; |
| 213 | |
| 214 | // A chunk of text in the XML string. This lives between other tags, such as XLIFF tags and Spans. |
| 215 | class SegmentNode : public Node { |
| 216 | public: |
| 217 | std::string data; |
| 218 | |
| 219 | void Build(StringBuilder* builder) const override { |
| 220 | builder->AppendText(data); |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | // A tag that will be encoded into the final flattened string. Tags like <b> or <i>. |
| 225 | class SpanNode : public Node { |
| 226 | public: |
| 227 | std::string name; |
| 228 | |
| 229 | void Build(StringBuilder* builder) const override { |
| 230 | StringBuilder::SpanHandle span_handle = builder->StartSpan(name); |
| 231 | Node::Build(builder); |
| 232 | builder->EndSpan(span_handle); |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | // An XLIFF 'g' tag, which marks a section of the string as untranslatable. |
| 237 | class UntranslatableNode : public Node { |
| 238 | public: |
| 239 | void Build(StringBuilder* builder) const override { |
| 240 | StringBuilder::UntranslatableHandle handle = builder->StartUntranslatable(); |
| 241 | Node::Build(builder); |
| 242 | builder->EndUntranslatable(handle); |
| 243 | } |
| 244 | }; |
| 245 | |
| 246 | // Build a string from XML that converts nested elements into Span objects. |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 247 | bool ResourceParser::FlattenXmlSubtree( |
| 248 | xml::XmlPullParser* parser, std::string* out_raw_string, StyleString* out_style_string, |
| 249 | std::vector<UntranslatableSection>* out_untranslatable_sections) { |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 250 | std::string raw_string; |
| 251 | std::string current_text; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 252 | |
| 253 | // The first occurrence of a <xliff:g> tag. Nested <xliff:g> tags are illegal. |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 254 | std::optional<size_t> untranslatable_start_depth; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 255 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 256 | Node root; |
| 257 | std::vector<Node*> node_stack; |
| 258 | node_stack.push_back(&root); |
| 259 | |
| 260 | bool saw_span_node = false; |
| 261 | SegmentNode* first_segment = nullptr; |
| 262 | SegmentNode* last_segment = nullptr; |
| 263 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 264 | size_t depth = 1; |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 265 | while (depth > 0 && xml::XmlPullParser::IsGoodEvent(parser->Next())) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | const xml::XmlPullParser::Event event = parser->event(); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 267 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 268 | // First take care of any SegmentNodes that should be created. |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 269 | if (event == xml::XmlPullParser::Event::kStartElement |
Ryan Mitchell | 1d358ff | 2019-03-06 15:06:49 -0800 | [diff] [blame] | 270 | || event == xml::XmlPullParser::Event::kEndElement) { |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 271 | if (!current_text.empty()) { |
Ryan Mitchell | 1d358ff | 2019-03-06 15:06:49 -0800 | [diff] [blame] | 272 | auto segment_node = util::make_unique<SegmentNode>(); |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 273 | segment_node->data = std::move(current_text); |
Ryan Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 274 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 275 | last_segment = node_stack.back()->AddChild(std::move(segment_node)); |
| 276 | if (first_segment == nullptr) { |
| 277 | first_segment = last_segment; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 278 | } |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 279 | current_text = {}; |
| 280 | } |
| 281 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 282 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 283 | switch (event) { |
| 284 | case xml::XmlPullParser::Event::kText: { |
| 285 | current_text += parser->text(); |
| 286 | raw_string += parser->text(); |
| 287 | } break; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 288 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 289 | case xml::XmlPullParser::Event::kStartElement: { |
| 290 | if (parser->element_namespace().empty()) { |
| 291 | // This is an HTML tag which we encode as a span. Add it to the span stack. |
| 292 | std::unique_ptr<SpanNode> span_node = util::make_unique<SpanNode>(); |
| 293 | span_node->name = parser->element_name(); |
| 294 | const auto end_attr_iter = parser->end_attributes(); |
| 295 | for (auto attr_iter = parser->begin_attributes(); attr_iter != end_attr_iter; |
| 296 | ++attr_iter) { |
| 297 | span_node->name += ";"; |
| 298 | span_node->name += attr_iter->name; |
| 299 | span_node->name += "="; |
| 300 | span_node->name += attr_iter->value; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 301 | } |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 302 | |
| 303 | node_stack.push_back(node_stack.back()->AddChild(std::move(span_node))); |
| 304 | saw_span_node = true; |
| 305 | } else if (parser->element_namespace() == sXliffNamespaceUri) { |
| 306 | // This is an XLIFF tag, which is not encoded as a span. |
| 307 | if (parser->element_name() == "g") { |
| 308 | // Check that an 'untranslatable' tag is not already being processed. Nested |
| 309 | // <xliff:g> tags are illegal. |
| 310 | if (untranslatable_start_depth) { |
| 311 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 312 | << "illegal nested XLIFF 'g' tag"); |
| 313 | return false; |
| 314 | } else { |
| 315 | // Mark the beginning of an 'untranslatable' section. |
| 316 | untranslatable_start_depth = depth; |
| 317 | node_stack.push_back( |
| 318 | node_stack.back()->AddChild(util::make_unique<UntranslatableNode>())); |
| 319 | } |
| 320 | } else { |
| 321 | // Ignore unknown XLIFF tags, but don't warn. |
| 322 | node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>())); |
| 323 | } |
| 324 | } else { |
| 325 | // Besides XLIFF, any other namespaced tag is unsupported and ignored. |
| 326 | diag_->Warn(DiagMessage(source_.WithLine(parser->line_number())) |
| 327 | << "ignoring element '" << parser->element_name() |
| 328 | << "' with unknown namespace '" << parser->element_namespace() << "'"); |
| 329 | node_stack.push_back(node_stack.back()->AddChild(util::make_unique<Node>())); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 330 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 331 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 332 | // Enter one level inside the element. |
| 333 | depth++; |
| 334 | } break; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 335 | |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 336 | case xml::XmlPullParser::Event::kEndElement: { |
| 337 | // Return one level from within the element. |
| 338 | depth--; |
| 339 | if (depth == 0) { |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | node_stack.pop_back(); |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 344 | if (untranslatable_start_depth == depth) { |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 345 | // This is the end of an untranslatable section. |
| 346 | untranslatable_start_depth = {}; |
| 347 | } |
| 348 | } break; |
| 349 | |
| 350 | default: |
| 351 | // ignore. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 352 | break; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 353 | } |
| 354 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 355 | |
Ryan Mitchell | 4ea9075 | 2020-07-31 08:21:43 -0700 | [diff] [blame] | 356 | // Validity check to make sure we processed all the nodes. |
Adam Lesinski | 2eed52e | 2018-02-21 15:55:58 -0800 | [diff] [blame] | 357 | CHECK(node_stack.size() == 1u); |
| 358 | CHECK(node_stack.back() == &root); |
| 359 | |
| 360 | if (!saw_span_node) { |
| 361 | // If there were no spans, we must treat this string a little differently (according to AAPT). |
| 362 | // Find and strip the leading whitespace from the first segment, and the trailing whitespace |
| 363 | // from the last segment. |
| 364 | if (first_segment != nullptr) { |
| 365 | // Trim leading whitespace. |
| 366 | StringPiece trimmed = util::TrimLeadingWhitespace(first_segment->data); |
| 367 | if (trimmed.size() != first_segment->data.size()) { |
| 368 | first_segment->data = trimmed.to_string(); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if (last_segment != nullptr) { |
| 373 | // Trim trailing whitespace. |
| 374 | StringPiece trimmed = util::TrimTrailingWhitespace(last_segment->data); |
| 375 | if (trimmed.size() != last_segment->data.size()) { |
| 376 | last_segment->data = trimmed.to_string(); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // Have the XML structure flatten itself into the StringBuilder. The StringBuilder will take |
| 382 | // care of recording the correctly adjusted Spans and UntranslatableSections. |
| 383 | StringBuilder builder; |
| 384 | root.Build(&builder); |
| 385 | if (!builder) { |
| 386 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) << builder.GetError()); |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | ResourceUtils::FlattenedXmlString flattened_string = builder.GetFlattenedString(); |
| 391 | *out_raw_string = std::move(raw_string); |
| 392 | *out_untranslatable_sections = std::move(flattened_string.untranslatable_sections); |
| 393 | out_style_string->str = std::move(flattened_string.text); |
| 394 | out_style_string->spans = std::move(flattened_string.spans); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 395 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 398 | bool ResourceParser::Parse(xml::XmlPullParser* parser) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 399 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 400 | const size_t depth = parser->depth(); |
| 401 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 402 | if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 403 | // Skip comments and text. |
| 404 | continue; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 405 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 406 | |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 407 | if (!parser->element_namespace().empty() || parser->element_name() != "resources") { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 408 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 409 | << "root element must be <resources>"); |
| 410 | return false; |
| 411 | } |
| 412 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 413 | error |= !ParseResources(parser); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 414 | break; |
| 415 | }; |
| 416 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 417 | if (parser->event() == xml::XmlPullParser::Event::kBadDocument) { |
| 418 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 419 | << "xml parser error: " << parser->error()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 420 | return false; |
| 421 | } |
| 422 | return !error; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 425 | bool ResourceParser::ParseResources(xml::XmlPullParser* parser) { |
| 426 | std::set<ResourceName> stripped_resources; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 427 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 428 | bool error = false; |
| 429 | std::string comment; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 430 | const size_t depth = parser->depth(); |
| 431 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 432 | const xml::XmlPullParser::Event event = parser->event(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 433 | if (event == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 434 | comment = parser->comment(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 435 | continue; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 436 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 437 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 438 | if (event == xml::XmlPullParser::Event::kText) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 439 | if (!util::TrimWhitespace(parser->text()).empty()) { |
| 440 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 441 | << "plain text not allowed here"); |
| 442 | error = true; |
| 443 | } |
| 444 | continue; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 447 | CHECK(event == xml::XmlPullParser::Event::kStartElement); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 448 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 449 | if (!parser->element_namespace().empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 450 | // Skip unknown namespace. |
| 451 | continue; |
| 452 | } |
| 453 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 454 | std::string element_name = parser->element_name(); |
| 455 | if (element_name == "skip" || element_name == "eat-comment") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 456 | comment = ""; |
| 457 | continue; |
| 458 | } |
| 459 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 460 | ParsedResource parsed_resource; |
| 461 | parsed_resource.config = config_; |
| 462 | parsed_resource.source = source_.WithLine(parser->line_number()); |
Chih-Hung Hsieh | 7a616f6 | 2020-03-05 15:59:25 -0800 | [diff] [blame] | 463 | // NOLINTNEXTLINE(bugprone-use-after-move) move+reset comment |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 464 | parsed_resource.comment = std::move(comment); |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 465 | if (options_.visibility) { |
| 466 | parsed_resource.visibility_level = options_.visibility.value(); |
| 467 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 468 | |
| 469 | // Extract the product name if it exists. |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 470 | if (std::optional<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 471 | parsed_resource.product = maybe_product.value().to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | // Parse the resource regardless of product. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 475 | if (!ParseResource(parser, &parsed_resource)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 476 | error = true; |
| 477 | continue; |
| 478 | } |
| 479 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 480 | if (!AddResourcesToTable(table_, diag_, &parsed_resource)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 481 | error = true; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | // Check that we included at least one variant of each stripped resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 486 | for (const ResourceName& stripped_resource : stripped_resources) { |
| 487 | if (!table_->FindResource(stripped_resource)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 488 | // Failed to find the resource. |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 489 | diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource |
| 490 | << "' was filtered out but no product variant remains"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 491 | error = true; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | return !error; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 498 | bool ResourceParser::ParseResource(xml::XmlPullParser* parser, |
| 499 | ParsedResource* out_resource) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 500 | struct ItemTypeFormat { |
| 501 | ResourceType type; |
| 502 | uint32_t format; |
| 503 | }; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 504 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 505 | using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*, |
| 506 | ParsedResource*)>; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 507 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 508 | static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({ |
| 509 | {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}}, |
| 510 | {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}}, |
| 511 | {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}}, |
| 512 | {"dimen", |
| 513 | {ResourceType::kDimen, |
| 514 | android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION | |
| 515 | android::ResTable_map::TYPE_DIMENSION}}, |
| 516 | {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}}, |
| 517 | {"fraction", |
| 518 | {ResourceType::kFraction, |
| 519 | android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION | |
| 520 | android::ResTable_map::TYPE_DIMENSION}}, |
| 521 | {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}}, |
| 522 | {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}}, |
| 523 | }); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 524 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 525 | static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({ |
| 526 | {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)}, |
| 527 | {"array", std::mem_fn(&ResourceParser::ParseArray)}, |
| 528 | {"attr", std::mem_fn(&ResourceParser::ParseAttr)}, |
| 529 | {"configVarying", |
| 530 | std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying, |
| 531 | std::placeholders::_2, std::placeholders::_3)}, |
| 532 | {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)}, |
| 533 | {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)}, |
| 534 | {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)}, |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 535 | {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)}, |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 536 | {"plurals", std::mem_fn(&ResourceParser::ParsePlural)}, |
| 537 | {"public", std::mem_fn(&ResourceParser::ParsePublic)}, |
| 538 | {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)}, |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 539 | {"staging-public-group", std::mem_fn(&ResourceParser::ParseStagingPublicGroup)}, |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 540 | {"staging-public-group-final", std::mem_fn(&ResourceParser::ParseStagingPublicGroupFinal)}, |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 541 | {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)}, |
| 542 | {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle, |
| 543 | std::placeholders::_2, std::placeholders::_3)}, |
| 544 | {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)}, |
| 545 | }); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 546 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 547 | std::string resource_type = parser->element_name(); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 548 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 549 | // The value format accepted for this resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 550 | uint32_t resource_format = 0u; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 551 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 552 | bool can_be_item = true; |
| 553 | bool can_be_bag = true; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 554 | if (resource_type == "item") { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 555 | can_be_bag = false; |
| 556 | |
Adam Lesinski | e597d68 | 2017-06-01 17:16:44 -0700 | [diff] [blame] | 557 | // The default format for <item> is any. If a format attribute is present, that one will |
| 558 | // override the default. |
| 559 | resource_format = android::ResTable_map::TYPE_ANY; |
| 560 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 561 | // Items have their type encoded in the type attribute. |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 562 | if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 563 | resource_type = maybe_type.value().to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 564 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 565 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 566 | << "<item> must have a 'type' attribute"); |
| 567 | return false; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 568 | } |
| 569 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 570 | if (std::optional<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 571 | // An explicit format for this resource was specified. The resource will |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 572 | // retain its type in its name, but the accepted value for this type is |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 573 | // overridden. |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 574 | resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 575 | if (!resource_format) { |
| 576 | diag_->Error(DiagMessage(out_resource->source) |
| 577 | << "'" << maybe_format.value() |
| 578 | << "' is an invalid format"); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 579 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 580 | } |
| 581 | } |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 582 | } else if (resource_type == "bag") { |
| 583 | can_be_item = false; |
| 584 | |
| 585 | // Bags have their type encoded in the type attribute. |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 586 | if (std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 587 | resource_type = maybe_type.value().to_string(); |
| 588 | } else { |
| 589 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 590 | << "<bag> must have a 'type' attribute"); |
| 591 | return false; |
| 592 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | // Get the name of the resource. This will be checked later, because not all |
| 596 | // XML elements require a name. |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 597 | std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 598 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 599 | if (resource_type == "id") { |
| 600 | if (!maybe_name) { |
| 601 | diag_->Error(DiagMessage(out_resource->source) |
| 602 | << "<" << parser->element_name() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 603 | << "> missing 'name' attribute"); |
| 604 | return false; |
| 605 | } |
| 606 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 607 | out_resource->name.type = ResourceType::kId; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 608 | out_resource->name.entry = maybe_name.value().to_string(); |
y | 9efbbef | 2018-04-18 11:29:09 -0700 | [diff] [blame] | 609 | |
| 610 | // Ids either represent a unique resource id or reference another resource id |
| 611 | auto item = ParseItem(parser, out_resource, resource_format); |
| 612 | if (!item) { |
| 613 | return false; |
| 614 | } |
| 615 | |
| 616 | String* empty = ValueCast<String>(out_resource->value.get()); |
| 617 | if (empty && *empty->value == "") { |
| 618 | // If no inner element exists, represent a unique identifier |
| 619 | out_resource->value = util::make_unique<Id>(); |
| 620 | } else { |
y | 9efbbef | 2018-04-18 11:29:09 -0700 | [diff] [blame] | 621 | Reference* ref = ValueCast<Reference>(out_resource->value.get()); |
Ryan Mitchell | eaf77e1 | 2018-04-25 15:00:50 -0700 | [diff] [blame] | 622 | if (ref && !ref->name && !ref->id) { |
| 623 | // A null reference also means there is no inner element when ids are in the form: |
| 624 | // <id name="name"/> |
| 625 | out_resource->value = util::make_unique<Id>(); |
| 626 | } else if (!ref || ref->name.value().type != ResourceType::kId) { |
| 627 | // If an inner element exists, the inner element must be a reference to another resource id |
y | 9efbbef | 2018-04-18 11:29:09 -0700 | [diff] [blame] | 628 | diag_->Error(DiagMessage(out_resource->source) |
| 629 | << "<" << parser->element_name() |
| 630 | << "> inner element must either be a resource reference or empty"); |
| 631 | return false; |
| 632 | } |
| 633 | } |
| 634 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 635 | return true; |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 636 | } else if (resource_type == "macro") { |
| 637 | if (!maybe_name) { |
| 638 | diag_->Error(DiagMessage(out_resource->source) |
| 639 | << "<" << parser->element_name() << "> missing 'name' attribute"); |
| 640 | return false; |
| 641 | } |
| 642 | |
| 643 | out_resource->name.type = ResourceType::kMacro; |
| 644 | out_resource->name.entry = maybe_name.value().to_string(); |
| 645 | return ParseMacro(parser, out_resource); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 648 | if (can_be_item) { |
| 649 | const auto item_iter = elToItemMap.find(resource_type); |
| 650 | if (item_iter != elToItemMap.end()) { |
| 651 | // This is an item, record its type and format and start parsing. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 652 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 653 | if (!maybe_name) { |
| 654 | diag_->Error(DiagMessage(out_resource->source) |
| 655 | << "<" << parser->element_name() << "> missing 'name' attribute"); |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | out_resource->name.type = item_iter->second.type; |
| 660 | out_resource->name.entry = maybe_name.value().to_string(); |
| 661 | |
Adam Lesinski | e597d68 | 2017-06-01 17:16:44 -0700 | [diff] [blame] | 662 | // Only use the implied format of the type when there is no explicit format. |
| 663 | if (resource_format == 0u) { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 664 | resource_format = item_iter->second.format; |
| 665 | } |
| 666 | |
| 667 | if (!ParseItem(parser, out_resource, resource_format)) { |
| 668 | return false; |
| 669 | } |
| 670 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 671 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | // This might be a bag or something. |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 675 | if (can_be_bag) { |
| 676 | const auto bag_iter = elToBagMap.find(resource_type); |
| 677 | if (bag_iter != elToBagMap.end()) { |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 678 | // Ensure we have a name (unless this is a <public-group> or <overlayable>). |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 679 | if (resource_type != kPublicGroupTag && resource_type != kStagingPublicGroupTag && |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 680 | resource_type != kStagingPublicGroupFinalTag && resource_type != "overlayable") { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 681 | if (!maybe_name) { |
| 682 | diag_->Error(DiagMessage(out_resource->source) |
| 683 | << "<" << parser->element_name() << "> missing 'name' attribute"); |
| 684 | return false; |
| 685 | } |
| 686 | |
| 687 | out_resource->name.entry = maybe_name.value().to_string(); |
| 688 | } |
| 689 | |
| 690 | // Call the associated parse method. The type will be filled in by the |
| 691 | // parse func. |
| 692 | if (!bag_iter->second(this, parser, out_resource)) { |
| 693 | return false; |
| 694 | } |
| 695 | return true; |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | if (can_be_item) { |
| 700 | // Try parsing the elementName (or type) as a resource. These shall only be |
| 701 | // resources like 'layout' or 'xml' and they can only be references. |
| 702 | const ResourceType* parsed_type = ParseResourceType(resource_type); |
| 703 | if (parsed_type) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 704 | if (!maybe_name) { |
| 705 | diag_->Error(DiagMessage(out_resource->source) |
| 706 | << "<" << parser->element_name() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 707 | << "> missing 'name' attribute"); |
| 708 | return false; |
| 709 | } |
| 710 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 711 | out_resource->name.type = *parsed_type; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 712 | out_resource->name.entry = maybe_name.value().to_string(); |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 713 | out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString); |
| 714 | if (!out_resource->value) { |
| 715 | diag_->Error(DiagMessage(out_resource->source) |
| 716 | << "invalid value for type '" << *parsed_type << "'. Expected a reference"); |
| 717 | return false; |
| 718 | } |
| 719 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 720 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Izabela Orlowska | a3ab21f | 2019-01-17 12:09:59 +0000 | [diff] [blame] | 723 | // If the resource type was not recognized, write the error and return false. |
| 724 | diag_->Error(DiagMessage(out_resource->source) |
Ryan Mitchell | 2e2c3b6 | 2019-04-26 01:16:52 -0700 | [diff] [blame] | 725 | << "unknown resource type '" << resource_type << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 726 | return false; |
| 727 | } |
| 728 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 729 | bool ResourceParser::ParseItem(xml::XmlPullParser* parser, |
| 730 | ParsedResource* out_resource, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 731 | const uint32_t format) { |
| 732 | if (format == android::ResTable_map::TYPE_STRING) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 733 | return ParseString(parser, out_resource); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 736 | out_resource->value = ParseXml(parser, format, kNoRawString); |
| 737 | if (!out_resource->value) { |
| 738 | diag_->Error(DiagMessage(out_resource->source) << "invalid " |
| 739 | << out_resource->name.type); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 740 | return false; |
| 741 | } |
| 742 | return true; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 743 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 744 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 745 | std::optional<FlattenedXmlSubTree> ResourceParser::CreateFlattenSubTree( |
| 746 | xml::XmlPullParser* parser) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 747 | const size_t begin_xml_line = parser->line_number(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 748 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 749 | std::string raw_value; |
| 750 | StyleString style_string; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 751 | std::vector<UntranslatableSection> untranslatable_sections; |
| 752 | if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 753 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 756 | return FlattenedXmlSubTree{.raw_value = raw_value, |
| 757 | .style_string = style_string, |
| 758 | .untranslatable_sections = untranslatable_sections, |
| 759 | .namespace_resolver = parser, |
| 760 | .source = source_.WithLine(begin_xml_line)}; |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Reads the entire XML subtree and attempts to parse it as some Item, |
| 765 | * with typeMask denoting which items it can be. If allowRawValue is |
| 766 | * true, a RawString is returned if the XML couldn't be parsed as |
| 767 | * an Item. If allowRawValue is false, nullptr is returned in this |
| 768 | * case. |
| 769 | */ |
| 770 | std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask, |
| 771 | const bool allow_raw_value) { |
| 772 | auto sub_tree = CreateFlattenSubTree(parser); |
| 773 | if (!sub_tree.has_value()) { |
| 774 | return {}; |
| 775 | } |
| 776 | return ParseXml(sub_tree.value(), type_mask, allow_raw_value, *table_, config_, *diag_); |
| 777 | } |
| 778 | |
| 779 | std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub_tree, |
| 780 | const uint32_t type_mask, const bool allow_raw_value, |
| 781 | ResourceTable& table, |
| 782 | const android::ConfigDescription& config, |
| 783 | IDiagnostics& diag) { |
| 784 | if (!xmlsub_tree.style_string.spans.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 785 | // This can only be a StyledString. |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 786 | std::unique_ptr<StyledString> styled_string = |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 787 | util::make_unique<StyledString>(table.string_pool.MakeRef( |
| 788 | xmlsub_tree.style_string, |
| 789 | StringPool::Context(StringPool::Context::kNormalPriority, config))); |
| 790 | styled_string->untranslatable_sections = xmlsub_tree.untranslatable_sections; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 791 | return std::move(styled_string); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 794 | auto on_create_reference = [&](const ResourceName& name) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 795 | // name.package can be empty here, as it will assume the package name of the |
| 796 | // table. |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 797 | auto id = util::make_unique<Id>(); |
| 798 | id->SetSource(xmlsub_tree.source); |
| 799 | return table.AddResource(NewResourceBuilder(name).SetValue(std::move(id)).Build(), &diag); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 800 | }; |
| 801 | |
| 802 | // Process the raw value. |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 803 | std::unique_ptr<Item> processed_item = ResourceUtils::TryParseItemForAttribute( |
| 804 | xmlsub_tree.raw_value, type_mask, on_create_reference); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 805 | if (processed_item) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 806 | // Fix up the reference. |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 807 | if (auto ref = ValueCast<Reference>(processed_item.get())) { |
| 808 | ref->allow_raw = allow_raw_value; |
| 809 | ResolvePackage(xmlsub_tree.namespace_resolver, ref); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 810 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 811 | return processed_item; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | // Try making a regular string. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 815 | if (type_mask & android::ResTable_map::TYPE_STRING) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 816 | // Use the trimmed, escaped string. |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 817 | std::unique_ptr<String> string = util::make_unique<String>( |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 818 | table.string_pool.MakeRef(xmlsub_tree.style_string.str, StringPool::Context(config))); |
| 819 | string->untranslatable_sections = xmlsub_tree.untranslatable_sections; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 820 | return std::move(string); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 823 | if (allow_raw_value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 824 | // We can't parse this so return a RawString if we are allowed. |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 825 | return util::make_unique<RawString>(table.string_pool.MakeRef( |
| 826 | util::TrimWhitespace(xmlsub_tree.raw_value), StringPool::Context(config))); |
| 827 | } else if (util::TrimWhitespace(xmlsub_tree.raw_value).empty()) { |
Ryan Mitchell | fc3874a | 2019-05-28 16:30:17 -0700 | [diff] [blame] | 828 | // If the text is empty, and the value is not allowed to be a string, encode it as a @null. |
| 829 | return ResourceUtils::MakeNull(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 830 | } |
| 831 | return {}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 832 | } |
| 833 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 834 | bool ResourceParser::ParseString(xml::XmlPullParser* parser, |
| 835 | ParsedResource* out_resource) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 836 | bool formatted = true; |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 837 | if (std::optional<StringPiece> formatted_attr = xml::FindAttribute(parser, "formatted")) { |
| 838 | std::optional<bool> maybe_formatted = ResourceUtils::ParseBool(formatted_attr.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 839 | if (!maybe_formatted) { |
| 840 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 841 | << "invalid value for 'formatted'. Must be a boolean"); |
| 842 | return false; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 843 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 844 | formatted = maybe_formatted.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 845 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 846 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 847 | bool translatable = options_.translatable; |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 848 | if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { |
| 849 | std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 850 | if (!maybe_translatable) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 851 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 852 | << "invalid value for 'translatable'. Must be a boolean"); |
| 853 | return false; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 854 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 855 | translatable = maybe_translatable.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 856 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 857 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 858 | out_resource->value = |
| 859 | ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString); |
| 860 | if (!out_resource->value) { |
| 861 | diag_->Error(DiagMessage(out_resource->source) << "not a valid string"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 862 | return false; |
| 863 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 864 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 865 | if (String* string_value = ValueCast<String>(out_resource->value.get())) { |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 866 | string_value->SetTranslatable(translatable); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 867 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 868 | if (formatted && translatable) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 869 | if (!util::VerifyJavaStringFormat(*string_value->value)) { |
| 870 | DiagMessage msg(out_resource->source); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 871 | msg << "multiple substitutions specified in non-positional format; " |
| 872 | "did you mean to add the formatted=\"false\" attribute?"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 873 | if (options_.error_on_positional_arguments) { |
| 874 | diag_->Error(msg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 875 | return false; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 876 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 877 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 878 | diag_->Warn(msg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 879 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 880 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 881 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 882 | } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) { |
| 883 | string_value->SetTranslatable(translatable); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 884 | } |
| 885 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 886 | } |
| 887 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 888 | bool ResourceParser::ParseMacro(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 889 | auto sub_tree = CreateFlattenSubTree(parser); |
| 890 | if (!sub_tree) { |
| 891 | return false; |
| 892 | } |
| 893 | |
| 894 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 895 | diag_->Error(DiagMessage(out_resource->source) |
| 896 | << "<macro> tags cannot be declared in configurations other than the default " |
| 897 | "configuration'"); |
| 898 | return false; |
| 899 | } |
| 900 | |
| 901 | auto macro = std::make_unique<Macro>(); |
| 902 | macro->raw_value = std::move(sub_tree->raw_value); |
| 903 | macro->style_string = std::move(sub_tree->style_string); |
| 904 | macro->untranslatable_sections = std::move(sub_tree->untranslatable_sections); |
| 905 | |
| 906 | for (const auto& decl : parser->package_decls()) { |
| 907 | macro->alias_namespaces.emplace_back( |
| 908 | Macro::Namespace{.alias = decl.prefix, |
| 909 | .package_name = decl.package.package, |
| 910 | .is_private = decl.package.private_namespace}); |
| 911 | } |
| 912 | |
| 913 | out_resource->value = std::move(macro); |
| 914 | return true; |
| 915 | } |
| 916 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 917 | bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 918 | if (options_.visibility) { |
| 919 | diag_->Error(DiagMessage(out_resource->source) |
| 920 | << "<public> tag not allowed with --visibility flag"); |
| 921 | return false; |
| 922 | } |
| 923 | |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 924 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 925 | diag_->Warn(DiagMessage(out_resource->source) |
| 926 | << "ignoring configuration '" << out_resource->config << "' for <public> tag"); |
| 927 | } |
| 928 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 929 | std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 930 | if (!maybe_type) { |
| 931 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 932 | << "<public> must have a 'type' attribute"); |
| 933 | return false; |
| 934 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 935 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 936 | const ResourceType* parsed_type = ParseResourceType(maybe_type.value()); |
| 937 | if (!parsed_type) { |
| 938 | diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '" |
| 939 | << maybe_type.value() |
| 940 | << "' in <public>"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 941 | return false; |
| 942 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 943 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 944 | out_resource->name.type = *parsed_type; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 945 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 946 | if (std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) { |
| 947 | std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 948 | if (!maybe_id) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 949 | diag_->Error(DiagMessage(out_resource->source) |
| 950 | << "invalid resource ID '" << maybe_id_str.value() << "' in <public>"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 951 | return false; |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 952 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 953 | out_resource->id = maybe_id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 954 | } |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 955 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 956 | if (*parsed_type == ResourceType::kId) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 957 | // An ID marked as public is also the definition of an ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 958 | out_resource->value = util::make_unique<Id>(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 959 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 960 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 961 | out_resource->visibility_level = Visibility::Level::kPublic; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 962 | return true; |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 963 | } |
| 964 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 965 | template <typename Func> |
| 966 | bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resource, |
| 967 | const char* tag_name, IDiagnostics* diag, Func&& func) { |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 968 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 969 | diag->Warn(DiagMessage(out_resource->source) |
| 970 | << "ignoring configuration '" << out_resource->config << "' for <" << tag_name |
| 971 | << "> tag"); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 974 | std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 975 | if (!maybe_type) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 976 | diag->Error(DiagMessage(out_resource->source) |
| 977 | << "<" << tag_name << "> must have a 'type' attribute"); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 978 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 979 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 980 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 981 | const ResourceType* parsed_type = ParseResourceType(maybe_type.value()); |
| 982 | if (!parsed_type) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 983 | diag->Error(DiagMessage(out_resource->source) |
| 984 | << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">"); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 985 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 988 | std::optional<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 989 | if (!maybe_id_str) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 990 | diag->Error(DiagMessage(out_resource->source) |
| 991 | << "<" << tag_name << "> must have a 'first-id' attribute"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 992 | return false; |
| 993 | } |
| 994 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 995 | std::optional<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 996 | if (!maybe_id) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 997 | diag->Error(DiagMessage(out_resource->source) |
| 998 | << "invalid resource ID '" << maybe_id_str.value() << "' in <" << tag_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 999 | return false; |
| 1000 | } |
| 1001 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1002 | std::string comment; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1003 | ResourceId next_id = maybe_id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1004 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1005 | const size_t depth = parser->depth(); |
| 1006 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1007 | if (parser->event() == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 1008 | comment = util::TrimWhitespace(parser->comment()).to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1009 | continue; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1010 | } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1011 | // Skip text. |
| 1012 | continue; |
| 1013 | } |
| 1014 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1015 | const Source item_source = out_resource->source.WithLine(parser->line_number()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1016 | const std::string& element_namespace = parser->element_namespace(); |
| 1017 | const std::string& element_name = parser->element_name(); |
| 1018 | if (element_namespace.empty() && element_name == "public") { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1019 | auto maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1020 | if (!maybe_name) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1021 | diag->Error(DiagMessage(item_source) << "<public> must have a 'name' attribute"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1022 | error = true; |
| 1023 | continue; |
| 1024 | } |
| 1025 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1026 | if (xml::FindNonEmptyAttribute(parser, "id")) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1027 | diag->Error(DiagMessage(item_source) << "'id' is ignored within <" << tag_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1028 | error = true; |
| 1029 | continue; |
| 1030 | } |
| 1031 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1032 | if (xml::FindNonEmptyAttribute(parser, "type")) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1033 | diag->Error(DiagMessage(item_source) << "'type' is ignored within <" << tag_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1034 | error = true; |
| 1035 | continue; |
| 1036 | } |
| 1037 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1038 | ParsedResource& entry_res = out_resource->child_resources.emplace_back(ParsedResource{ |
| 1039 | .name = ResourceName{{}, *parsed_type, maybe_name.value().to_string()}, |
| 1040 | .source = item_source, |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1041 | .comment = std::move(comment), |
| 1042 | }); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1043 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1044 | // Execute group specific code. |
| 1045 | func(entry_res, next_id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1046 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1047 | next_id.id++; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1048 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1049 | diag->Error(DiagMessage(item_source) << ":" << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1050 | error = true; |
| 1051 | } |
| 1052 | } |
| 1053 | return !error; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1054 | } |
| 1055 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1056 | bool ResourceParser::ParseStagingPublicGroup(xml::XmlPullParser* parser, |
| 1057 | ParsedResource* out_resource) { |
| 1058 | return ParseGroupImpl(parser, out_resource, kStagingPublicGroupTag, diag_, |
| 1059 | [](ParsedResource& parsed_entry, ResourceId id) { |
| 1060 | parsed_entry.id = id; |
| 1061 | parsed_entry.staged_api = true; |
| 1062 | parsed_entry.visibility_level = Visibility::Level::kPublic; |
| 1063 | }); |
| 1064 | } |
| 1065 | |
Ryan Mitchell | 2fedba9 | 2021-04-23 07:47:38 -0700 | [diff] [blame] | 1066 | bool ResourceParser::ParseStagingPublicGroupFinal(xml::XmlPullParser* parser, |
| 1067 | ParsedResource* out_resource) { |
| 1068 | return ParseGroupImpl(parser, out_resource, kStagingPublicGroupFinalTag, diag_, |
| 1069 | [](ParsedResource& parsed_entry, ResourceId id) { |
| 1070 | parsed_entry.staged_alias = StagedId{id, parsed_entry.source}; |
| 1071 | }); |
| 1072 | } |
| 1073 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1074 | bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 1075 | if (options_.visibility) { |
| 1076 | diag_->Error(DiagMessage(out_resource->source) |
| 1077 | << "<" << kPublicGroupTag << "> tag not allowed with --visibility flag"); |
| 1078 | return false; |
| 1079 | } |
| 1080 | |
| 1081 | return ParseGroupImpl(parser, out_resource, kPublicGroupTag, diag_, |
| 1082 | [](ParsedResource& parsed_entry, ResourceId id) { |
| 1083 | parsed_entry.id = id; |
| 1084 | parsed_entry.visibility_level = Visibility::Level::kPublic; |
| 1085 | }); |
| 1086 | } |
| 1087 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1088 | bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser, |
| 1089 | ParsedResource* out_resource) { |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1090 | std::optional<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1091 | if (!maybe_type) { |
| 1092 | diag_->Error(DiagMessage(out_resource->source) |
| 1093 | << "<" << parser->element_name() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1094 | << "> must have a 'type' attribute"); |
| 1095 | return false; |
| 1096 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1097 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1098 | const ResourceType* parsed_type = ParseResourceType(maybe_type.value()); |
| 1099 | if (!parsed_type) { |
| 1100 | diag_->Error(DiagMessage(out_resource->source) |
| 1101 | << "invalid resource type '" << maybe_type.value() << "' in <" |
| 1102 | << parser->element_name() << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1103 | return false; |
| 1104 | } |
| 1105 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1106 | out_resource->name.type = *parsed_type; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1107 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1110 | bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 1111 | if (options_.visibility) { |
| 1112 | diag_->Error(DiagMessage(out_resource->source) |
| 1113 | << "<java-symbol> and <symbol> tags not allowed with --visibility flag"); |
| 1114 | return false; |
| 1115 | } |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1116 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 1117 | diag_->Warn(DiagMessage(out_resource->source) |
| 1118 | << "ignoring configuration '" << out_resource->config << "' for <" |
| 1119 | << parser->element_name() << "> tag"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1120 | } |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1121 | |
| 1122 | if (!ParseSymbolImpl(parser, out_resource)) { |
| 1123 | return false; |
| 1124 | } |
| 1125 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 1126 | out_resource->visibility_level = Visibility::Level::kPrivate; |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1127 | return true; |
| 1128 | } |
| 1129 | |
| 1130 | bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 1131 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 1132 | diag_->Warn(DiagMessage(out_resource->source) |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1133 | << "ignoring configuration '" << out_resource->config |
| 1134 | << "' for <overlayable> tag"); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1135 | } |
| 1136 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1137 | std::optional<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name"); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1138 | if (!overlayable_name) { |
| 1139 | diag_->Error(DiagMessage(out_resource->source) |
| 1140 | << "<overlayable> tag must have a 'name' attribute"); |
| 1141 | return false; |
| 1142 | } |
| 1143 | |
| 1144 | const std::string kActorUriScheme = |
| 1145 | android::base::StringPrintf("%s://", Overlayable::kActorScheme); |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1146 | std::optional<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor"); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1147 | if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) { |
| 1148 | diag_->Error(DiagMessage(out_resource->source) |
| 1149 | << "specified <overlayable> tag 'actor' attribute must use the scheme '" |
| 1150 | << Overlayable::kActorScheme << "'"); |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
| 1154 | // Create a overlayable entry grouping that represents this <overlayable> |
| 1155 | auto overlayable = std::make_shared<Overlayable>( |
| 1156 | overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "", |
Ryan Mitchell | b5b162b | 2019-02-07 20:07:21 -0800 | [diff] [blame] | 1157 | source_); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1158 | |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1159 | bool error = false; |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1160 | std::string comment; |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 1161 | PolicyFlags current_policies = PolicyFlags::NONE; |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1162 | const size_t start_depth = parser->depth(); |
| 1163 | while (xml::XmlPullParser::IsGoodEvent(parser->Next())) { |
| 1164 | xml::XmlPullParser::Event event = parser->event(); |
| 1165 | if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1166 | // Break the loop when exiting the <overlayable> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1167 | break; |
| 1168 | } else if (event == xml::XmlPullParser::Event::kEndElement |
| 1169 | && parser->depth() == start_depth + 1) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1170 | // Clear the current policies when exiting the <policy> tags |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 1171 | current_policies = PolicyFlags::NONE; |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1172 | continue; |
| 1173 | } else if (event == xml::XmlPullParser::Event::kComment) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1174 | // Retrieve the comment of individual <item> tags |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1175 | comment = parser->comment(); |
| 1176 | continue; |
| 1177 | } else if (event != xml::XmlPullParser::Event::kStartElement) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1178 | // Skip to the start of the next element |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1179 | continue; |
| 1180 | } |
| 1181 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1182 | const Source element_source = source_.WithLine(parser->line_number()); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1183 | const std::string& element_name = parser->element_name(); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1184 | const std::string& element_namespace = parser->element_namespace(); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1185 | if (element_namespace.empty() && element_name == "item") { |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 1186 | if (current_policies == PolicyFlags::NONE) { |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 1187 | diag_->Error(DiagMessage(element_source) |
| 1188 | << "<item> within an <overlayable> must be inside a <policy> block"); |
| 1189 | error = true; |
| 1190 | continue; |
| 1191 | } |
| 1192 | |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1193 | // Items specify the name and type of resource that should be overlayable |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1194 | std::optional<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name"); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1195 | if (!item_name) { |
| 1196 | diag_->Error(DiagMessage(element_source) |
| 1197 | << "<item> within an <overlayable> must have a 'name' attribute"); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1198 | error = true; |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1199 | continue; |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1200 | } |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1201 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1202 | std::optional<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type"); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1203 | if (!item_type) { |
| 1204 | diag_->Error(DiagMessage(element_source) |
| 1205 | << "<item> within an <overlayable> must have a 'type' attribute"); |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1206 | error = true; |
| 1207 | continue; |
| 1208 | } |
| 1209 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1210 | const ResourceType* type = ParseResourceType(item_type.value()); |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1211 | if (type == nullptr) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1212 | diag_->Error(DiagMessage(element_source) |
| 1213 | << "invalid resource type '" << item_type.value() |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1214 | << "' in <item> within an <overlayable>"); |
| 1215 | error = true; |
| 1216 | continue; |
| 1217 | } |
| 1218 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1219 | OverlayableItem overlayable_item(overlayable); |
| 1220 | overlayable_item.policies = current_policies; |
| 1221 | overlayable_item.comment = comment; |
| 1222 | overlayable_item.source = element_source; |
| 1223 | |
| 1224 | ParsedResource child_resource{}; |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1225 | child_resource.name.type = *type; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1226 | child_resource.name.entry = item_name.value().to_string(); |
| 1227 | child_resource.overlayable_item = overlayable_item; |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1228 | out_resource->child_resources.push_back(std::move(child_resource)); |
| 1229 | |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1230 | } else if (element_namespace.empty() && element_name == "policy") { |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 1231 | if (current_policies != PolicyFlags::NONE) { |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1232 | // If the policy list is not empty, then we are currently inside a policy element |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1233 | diag_->Error(DiagMessage(element_source) << "<policy> blocks cannot be recursively nested"); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1234 | error = true; |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1235 | break; |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1236 | } else if (std::optional<StringPiece> maybe_type = |
| 1237 | xml::FindNonEmptyAttribute(parser, "type")) { |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1238 | // Parse the polices separated by vertical bar characters to allow for specifying multiple |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1239 | // policies. Items within the policy tag will have the specified policy. |
Ryan Mitchell | 939df09 | 2019-04-09 17:13:50 -0700 | [diff] [blame] | 1240 | for (const StringPiece& part : util::Tokenize(maybe_type.value(), '|')) { |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1241 | StringPiece trimmed_part = util::TrimWhitespace(part); |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 1242 | const auto policy = std::find_if(kPolicyStringToFlag.begin(), |
| 1243 | kPolicyStringToFlag.end(), |
| 1244 | [trimmed_part](const auto& it) { |
| 1245 | return trimmed_part == it.first; |
| 1246 | }); |
| 1247 | if (policy == kPolicyStringToFlag.end()) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1248 | diag_->Error(DiagMessage(element_source) |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1249 | << "<policy> has unsupported type '" << trimmed_part << "'"); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1250 | error = true; |
| 1251 | continue; |
| 1252 | } |
Ryan Mitchell | 939df09 | 2019-04-09 17:13:50 -0700 | [diff] [blame] | 1253 | |
| 1254 | current_policies |= policy->second; |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1255 | } |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 1256 | } else { |
| 1257 | diag_->Error(DiagMessage(element_source) |
Ryan Mitchell | 939df09 | 2019-04-09 17:13:50 -0700 | [diff] [blame] | 1258 | << "<policy> must have a 'type' attribute"); |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 1259 | error = true; |
| 1260 | continue; |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1261 | } |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1262 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1263 | diag_->Error(DiagMessage(element_source) << "invalid element <" << element_name << "> " |
Ryan Mitchell | 939df09 | 2019-04-09 17:13:50 -0700 | [diff] [blame] | 1264 | << " in <overlayable>"); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1265 | error = true; |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1266 | break; |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1267 | } |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1268 | |
| 1269 | comment.clear(); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1270 | } |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1271 | |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1272 | return !error; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1273 | } |
| 1274 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 1275 | bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1276 | if (ParseSymbolImpl(parser, out_resource)) { |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 1277 | out_resource->visibility_level = Visibility::Level::kUndefined; |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 1278 | out_resource->allow_new = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1279 | return true; |
| 1280 | } |
| 1281 | return false; |
| 1282 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1283 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1284 | bool ResourceParser::ParseAttr(xml::XmlPullParser* parser, |
| 1285 | ParsedResource* out_resource) { |
| 1286 | return ParseAttrImpl(parser, out_resource, false); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1289 | bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, |
| 1290 | ParsedResource* out_resource, bool weak) { |
| 1291 | out_resource->name.type = ResourceType::kAttr; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1292 | |
| 1293 | // Attributes only end up in default configuration. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1294 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 1295 | diag_->Warn(DiagMessage(out_resource->source) |
| 1296 | << "ignoring configuration '" << out_resource->config |
| 1297 | << "' for attribute " << out_resource->name); |
| 1298 | out_resource->config = ConfigDescription::DefaultConfig(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1299 | } |
| 1300 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1301 | uint32_t type_mask = 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1302 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1303 | std::optional<StringPiece> maybe_format = xml::FindAttribute(parser, "format"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1304 | if (maybe_format) { |
| 1305 | type_mask = ParseFormatAttribute(maybe_format.value()); |
| 1306 | if (type_mask == 0) { |
| 1307 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1308 | << "invalid attribute format '" << maybe_format.value() << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1309 | return false; |
| 1310 | } |
| 1311 | } |
| 1312 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1313 | std::optional<int32_t> maybe_min, maybe_max; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1314 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1315 | if (std::optional<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1316 | StringPiece min_str = util::TrimWhitespace(maybe_min_str.value()); |
| 1317 | if (!min_str.empty()) { |
| 1318 | std::u16string min_str16 = util::Utf8ToUtf16(min_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1319 | android::Res_value value; |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1320 | if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1321 | maybe_min = static_cast<int32_t>(value.data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1322 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1323 | } |
| 1324 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1325 | if (!maybe_min) { |
| 1326 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1327 | << "invalid 'min' value '" << min_str << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1328 | return false; |
| 1329 | } |
| 1330 | } |
| 1331 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1332 | if (std::optional<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1333 | StringPiece max_str = util::TrimWhitespace(maybe_max_str.value()); |
| 1334 | if (!max_str.empty()) { |
| 1335 | std::u16string max_str16 = util::Utf8ToUtf16(max_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1336 | android::Res_value value; |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1337 | if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1338 | maybe_max = static_cast<int32_t>(value.data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1339 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1340 | } |
| 1341 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1342 | if (!maybe_max) { |
| 1343 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1344 | << "invalid 'max' value '" << max_str << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1345 | return false; |
| 1346 | } |
| 1347 | } |
| 1348 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1349 | if ((maybe_min || maybe_max) && |
| 1350 | (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) { |
| 1351 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1352 | << "'min' and 'max' can only be used when format='integer'"); |
| 1353 | return false; |
| 1354 | } |
| 1355 | |
| 1356 | struct SymbolComparator { |
Yi Kong | 087e2d4 | 2017-05-02 12:49:25 -0700 | [diff] [blame] | 1357 | bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1358 | return a.symbol.name.value() < b.symbol.name.value(); |
| 1359 | } |
| 1360 | }; |
| 1361 | |
| 1362 | std::set<Attribute::Symbol, SymbolComparator> items; |
| 1363 | |
| 1364 | std::string comment; |
| 1365 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1366 | const size_t depth = parser->depth(); |
| 1367 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1368 | if (parser->event() == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 1369 | comment = util::TrimWhitespace(parser->comment()).to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1370 | continue; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1371 | } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1372 | // Skip text. |
| 1373 | continue; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1374 | } |
| 1375 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1376 | const Source item_source = source_.WithLine(parser->line_number()); |
| 1377 | const std::string& element_namespace = parser->element_namespace(); |
| 1378 | const std::string& element_name = parser->element_name(); |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1379 | if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1380 | if (element_name == "enum") { |
| 1381 | if (type_mask & android::ResTable_map::TYPE_FLAGS) { |
| 1382 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1383 | << "can not define an <enum>; already defined a <flag>"); |
| 1384 | error = true; |
| 1385 | continue; |
| 1386 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1387 | type_mask |= android::ResTable_map::TYPE_ENUM; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1388 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1389 | } else if (element_name == "flag") { |
| 1390 | if (type_mask & android::ResTable_map::TYPE_ENUM) { |
| 1391 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1392 | << "can not define a <flag>; already defined an <enum>"); |
| 1393 | error = true; |
| 1394 | continue; |
| 1395 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1396 | type_mask |= android::ResTable_map::TYPE_FLAGS; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1399 | if (std::optional<Attribute::Symbol> s = ParseEnumOrFlagItem(parser, element_name)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1400 | Attribute::Symbol& symbol = s.value(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1401 | ParsedResource child_resource; |
| 1402 | child_resource.name = symbol.symbol.name.value(); |
| 1403 | child_resource.source = item_source; |
| 1404 | child_resource.value = util::make_unique<Id>(); |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 1405 | if (options_.visibility) { |
| 1406 | child_resource.visibility_level = options_.visibility.value(); |
| 1407 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1408 | out_resource->child_resources.push_back(std::move(child_resource)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1409 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1410 | symbol.symbol.SetComment(std::move(comment)); |
| 1411 | symbol.symbol.SetSource(item_source); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1412 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1413 | auto insert_result = items.insert(std::move(symbol)); |
| 1414 | if (!insert_result.second) { |
| 1415 | const Attribute::Symbol& existing_symbol = *insert_result.first; |
| 1416 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1417 | << "duplicate symbol '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1418 | << existing_symbol.symbol.name.value().entry << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1419 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1420 | diag_->Note(DiagMessage(existing_symbol.symbol.GetSource()) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1421 | << "first defined here"); |
| 1422 | error = true; |
| 1423 | } |
| 1424 | } else { |
| 1425 | error = true; |
| 1426 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1427 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1428 | diag_->Error(DiagMessage(item_source) << ":" << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1429 | error = true; |
| 1430 | } |
| 1431 | |
| 1432 | comment = {}; |
| 1433 | } |
| 1434 | |
| 1435 | if (error) { |
| 1436 | return false; |
| 1437 | } |
| 1438 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1439 | std::unique_ptr<Attribute> attr = util::make_unique<Attribute>( |
| 1440 | type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY}); |
| 1441 | attr->SetWeak(weak); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1442 | attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end()); |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1443 | attr->min_int = maybe_min.value_or(std::numeric_limits<int32_t>::min()); |
| 1444 | attr->max_int = maybe_max.value_or(std::numeric_limits<int32_t>::max()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1445 | out_resource->value = std::move(attr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1446 | return true; |
| 1447 | } |
| 1448 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1449 | std::optional<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem(xml::XmlPullParser* parser, |
| 1450 | const StringPiece& tag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1451 | const Source source = source_.WithLine(parser->line_number()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1452 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1453 | std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1454 | if (!maybe_name) { |
| 1455 | diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1456 | << tag << ">"); |
| 1457 | return {}; |
| 1458 | } |
| 1459 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1460 | std::optional<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1461 | if (!maybe_value) { |
| 1462 | diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1463 | << tag << ">"); |
| 1464 | return {}; |
| 1465 | } |
| 1466 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1467 | std::u16string value16 = util::Utf8ToUtf16(maybe_value.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1468 | android::Res_value val; |
| 1469 | if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1470 | diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1471 | << "' for <" << tag |
| 1472 | << ">; must be an integer"); |
| 1473 | return {}; |
| 1474 | } |
| 1475 | |
| 1476 | return Attribute::Symbol{ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1477 | Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())), |
Ryan Mitchell | c167680 | 2019-05-20 16:47:08 -0700 | [diff] [blame] | 1478 | val.data, val.dataType}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1479 | } |
| 1480 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1481 | bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) { |
| 1482 | const Source source = source_.WithLine(parser->line_number()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1483 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1484 | std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1485 | if (!maybe_name) { |
| 1486 | diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1487 | return false; |
| 1488 | } |
| 1489 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1490 | std::optional<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1491 | if (!maybe_key) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1492 | diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1493 | return false; |
| 1494 | } |
| 1495 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1496 | ResolvePackage(parser, &maybe_key.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1497 | maybe_key.value().SetSource(source); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1498 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1499 | std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1500 | if (!value) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1501 | diag_->Error(DiagMessage(source) << "could not parse style item"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1502 | return false; |
| 1503 | } |
| 1504 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1505 | style->entries.push_back(Style::Entry{std::move(maybe_key.value()), std::move(value)}); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1506 | return true; |
| 1507 | } |
| 1508 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 1509 | bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1510 | ParsedResource* out_resource) { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 1511 | out_resource->name.type = type; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1512 | |
| 1513 | std::unique_ptr<Style> style = util::make_unique<Style>(); |
| 1514 | |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1515 | std::optional<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1516 | if (maybe_parent) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1517 | // If the parent is empty, we don't have a parent, but we also don't infer either. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1518 | if (!maybe_parent.value().empty()) { |
| 1519 | std::string err_str; |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1520 | style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1521 | if (!style->parent) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1522 | diag_->Error(DiagMessage(out_resource->source) << err_str); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1523 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1524 | } |
| 1525 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1526 | // Transform the namespace prefix to the actual package name, and mark the reference as |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1527 | // private if appropriate. |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1528 | ResolvePackage(parser, &style->parent.value()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1529 | } |
| 1530 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1531 | } else { |
| 1532 | // No parent was specified, so try inferring it from the style name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1533 | std::string style_name = out_resource->name.entry; |
| 1534 | size_t pos = style_name.find_last_of(u'.'); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1535 | if (pos != std::string::npos) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1536 | style->parent_inferred = true; |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1537 | style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1542 | const size_t depth = parser->depth(); |
| 1543 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1544 | if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1545 | // Skip text and comments. |
| 1546 | continue; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1547 | } |
| 1548 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1549 | const std::string& element_namespace = parser->element_namespace(); |
| 1550 | const std::string& element_name = parser->element_name(); |
| 1551 | if (element_namespace == "" && element_name == "item") { |
| 1552 | error |= !ParseStyleItem(parser, style.get()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1553 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1554 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1555 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1556 | << ":" << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1557 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1558 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1559 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1560 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1561 | if (error) { |
| 1562 | return false; |
| 1563 | } |
| 1564 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1565 | out_resource->value = std::move(style); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1566 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 1569 | bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 1570 | uint32_t resource_format = android::ResTable_map::TYPE_ANY; |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1571 | if (std::optional<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) { |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 1572 | resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value()); |
| 1573 | if (resource_format == 0u) { |
| 1574 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1575 | << "'" << format_attr.value() << "' is an invalid format"); |
| 1576 | return false; |
| 1577 | } |
| 1578 | } |
| 1579 | return ParseArrayImpl(parser, out_resource, resource_format); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1580 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1581 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 1582 | bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 1583 | return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1584 | } |
| 1585 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 1586 | bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 1587 | return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1588 | } |
| 1589 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1590 | bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser, |
| 1591 | ParsedResource* out_resource, |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1592 | const uint32_t typeMask) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1593 | out_resource->name.type = ResourceType::kArray; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1594 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1595 | std::unique_ptr<Array> array = util::make_unique<Array>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1596 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 1597 | bool translatable = options_.translatable; |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1598 | if (std::optional<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { |
| 1599 | std::optional<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 1600 | if (!maybe_translatable) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1601 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1602 | << "invalid value for 'translatable'. Must be a boolean"); |
| 1603 | return false; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 1604 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 1605 | translatable = maybe_translatable.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1606 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 1607 | array->SetTranslatable(translatable); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 1608 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1609 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1610 | const size_t depth = parser->depth(); |
| 1611 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1612 | if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1613 | // Skip text and comments. |
| 1614 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1615 | } |
| 1616 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1617 | const Source item_source = source_.WithLine(parser->line_number()); |
| 1618 | const std::string& element_namespace = parser->element_namespace(); |
| 1619 | const std::string& element_name = parser->element_name(); |
| 1620 | if (element_namespace.empty() && element_name == "item") { |
| 1621 | std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1622 | if (!item) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1623 | diag_->Error(DiagMessage(item_source) << "could not parse array item"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1624 | error = true; |
| 1625 | continue; |
| 1626 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1627 | item->SetSource(item_source); |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 1628 | array->elements.emplace_back(std::move(item)); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1629 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1630 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1631 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1632 | << "unknown tag <" << element_namespace << ":" |
| 1633 | << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1634 | error = true; |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | if (error) { |
| 1639 | return false; |
| 1640 | } |
| 1641 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1642 | out_resource->value = std::move(array); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1643 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1644 | } |
| 1645 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1646 | bool ResourceParser::ParsePlural(xml::XmlPullParser* parser, |
| 1647 | ParsedResource* out_resource) { |
| 1648 | out_resource->name.type = ResourceType::kPlurals; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1649 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1650 | std::unique_ptr<Plural> plural = util::make_unique<Plural>(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1651 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1652 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1653 | const size_t depth = parser->depth(); |
| 1654 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1655 | if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1656 | // Skip text and comments. |
| 1657 | continue; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1658 | } |
| 1659 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1660 | const Source item_source = source_.WithLine(parser->line_number()); |
| 1661 | const std::string& element_namespace = parser->element_namespace(); |
| 1662 | const std::string& element_name = parser->element_name(); |
| 1663 | if (element_namespace.empty() && element_name == "item") { |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1664 | std::optional<StringPiece> maybe_quantity = xml::FindNonEmptyAttribute(parser, "quantity"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1665 | if (!maybe_quantity) { |
| 1666 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1667 | << "<item> in <plurals> requires attribute " |
| 1668 | << "'quantity'"); |
| 1669 | error = true; |
| 1670 | continue; |
| 1671 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1672 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1673 | StringPiece trimmed_quantity = |
| 1674 | util::TrimWhitespace(maybe_quantity.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1675 | size_t index = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1676 | if (trimmed_quantity == "zero") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1677 | index = Plural::Zero; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1678 | } else if (trimmed_quantity == "one") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1679 | index = Plural::One; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1680 | } else if (trimmed_quantity == "two") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1681 | index = Plural::Two; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1682 | } else if (trimmed_quantity == "few") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1683 | index = Plural::Few; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1684 | } else if (trimmed_quantity == "many") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1685 | index = Plural::Many; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1686 | } else if (trimmed_quantity == "other") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1687 | index = Plural::Other; |
| 1688 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1689 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1690 | << "<item> in <plural> has invalid value '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1691 | << trimmed_quantity << "' for attribute 'quantity'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1692 | error = true; |
| 1693 | continue; |
| 1694 | } |
| 1695 | |
| 1696 | if (plural->values[index]) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1697 | diag_->Error(DiagMessage(item_source) << "duplicate quantity '" |
| 1698 | << trimmed_quantity << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1699 | error = true; |
| 1700 | continue; |
| 1701 | } |
| 1702 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1703 | if (!(plural->values[index] = ParseXml( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1704 | parser, android::ResTable_map::TYPE_STRING, kNoRawString))) { |
| 1705 | error = true; |
Ryan Mitchell | 2f9077d | 2019-02-15 16:02:09 -0800 | [diff] [blame] | 1706 | continue; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1707 | } |
Ryan Mitchell | 2f9077d | 2019-02-15 16:02:09 -0800 | [diff] [blame] | 1708 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1709 | plural->values[index]->SetSource(item_source); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1710 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1711 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1712 | diag_->Error(DiagMessage(item_source) << "unknown tag <" |
| 1713 | << element_namespace << ":" |
| 1714 | << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1715 | error = true; |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | if (error) { |
| 1720 | return false; |
| 1721 | } |
| 1722 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1723 | out_resource->value = std::move(plural); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1724 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1725 | } |
| 1726 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1727 | bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser, |
| 1728 | ParsedResource* out_resource) { |
| 1729 | out_resource->name.type = ResourceType::kStyleable; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1730 | |
Donald Chai | 94e4a01 | 2020-01-06 13:52:41 -0800 | [diff] [blame] | 1731 | if (!options_.preserve_visibility_of_styleables) { |
| 1732 | // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one |
| 1733 | // knows exactly what for. |
| 1734 | // |
| 1735 | // FWIW, styleables only appear in generated R classes. For custom views these should always be |
| 1736 | // package-private (to be used only by the view class); themes are a different story. |
| 1737 | out_resource->visibility_level = Visibility::Level::kPublic; |
| 1738 | } |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 1739 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1740 | // Declare-styleable only ends up in default config; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1741 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 1742 | diag_->Warn(DiagMessage(out_resource->source) |
| 1743 | << "ignoring configuration '" << out_resource->config |
| 1744 | << "' for styleable " << out_resource->name.entry); |
| 1745 | out_resource->config = ConfigDescription::DefaultConfig(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>(); |
| 1749 | |
| 1750 | std::string comment; |
| 1751 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1752 | const size_t depth = parser->depth(); |
| 1753 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1754 | if (parser->event() == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 1755 | comment = util::TrimWhitespace(parser->comment()).to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1756 | continue; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1757 | } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1758 | // Ignore text. |
| 1759 | continue; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1760 | } |
| 1761 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1762 | const Source item_source = source_.WithLine(parser->line_number()); |
| 1763 | const std::string& element_namespace = parser->element_namespace(); |
| 1764 | const std::string& element_name = parser->element_name(); |
| 1765 | if (element_namespace.empty() && element_name == "attr") { |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1766 | std::optional<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1767 | if (!maybe_name) { |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1768 | diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1769 | error = true; |
| 1770 | continue; |
| 1771 | } |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1772 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1773 | // If this is a declaration, the package name may be in the name. Separate |
| 1774 | // these out. |
| 1775 | // Eg. <attr name="android:text" /> |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame^] | 1776 | std::optional<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1777 | if (!maybe_ref) { |
| 1778 | diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '" |
| 1779 | << maybe_name.value() << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1780 | error = true; |
| 1781 | continue; |
| 1782 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1783 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1784 | Reference& child_ref = maybe_ref.value(); |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1785 | xml::ResolvePackage(parser, &child_ref); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1786 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1787 | // Create the ParsedResource that will add the attribute to the table. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1788 | ParsedResource child_resource; |
| 1789 | child_resource.name = child_ref.name.value(); |
| 1790 | child_resource.source = item_source; |
Chih-Hung Hsieh | 7a616f6 | 2020-03-05 15:59:25 -0800 | [diff] [blame] | 1791 | // NOLINTNEXTLINE(bugprone-use-after-move) move+reset comment |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1792 | child_resource.comment = std::move(comment); |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 1793 | if (options_.visibility) { |
| 1794 | child_resource.visibility_level = options_.visibility.value(); |
| 1795 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1796 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1797 | if (!ParseAttrImpl(parser, &child_resource, true)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1798 | error = true; |
| 1799 | continue; |
| 1800 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1801 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1802 | // Create the reference to this attribute. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1803 | child_ref.SetComment(child_resource.comment); |
| 1804 | child_ref.SetSource(item_source); |
| 1805 | styleable->entries.push_back(std::move(child_ref)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1806 | |
Ryan Mitchell | acde95c3 | 2019-04-23 05:44:21 -0700 | [diff] [blame] | 1807 | // Do not add referenced attributes that do not define a format to the table. |
| 1808 | CHECK(child_resource.value != nullptr); |
| 1809 | Attribute* attr = ValueCast<Attribute>(child_resource.value.get()); |
| 1810 | |
| 1811 | CHECK(attr != nullptr); |
| 1812 | if (attr->type_mask != android::ResTable_map::TYPE_ANY) { |
| 1813 | out_resource->child_resources.push_back(std::move(child_resource)); |
| 1814 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1815 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1816 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1817 | diag_->Error(DiagMessage(item_source) << "unknown tag <" |
| 1818 | << element_namespace << ":" |
| 1819 | << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1820 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1821 | } |
| 1822 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1823 | comment = {}; |
| 1824 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1825 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1826 | if (error) { |
| 1827 | return false; |
| 1828 | } |
| 1829 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1830 | out_resource->value = std::move(styleable); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1831 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1832 | } |
| 1833 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1834 | } // namespace aapt |