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