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