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; |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 630 | } else if (resource_type == "macro") { |
| 631 | if (!maybe_name) { |
| 632 | diag_->Error(DiagMessage(out_resource->source) |
| 633 | << "<" << parser->element_name() << "> missing 'name' attribute"); |
| 634 | return false; |
| 635 | } |
| 636 | |
| 637 | out_resource->name.type = ResourceType::kMacro; |
| 638 | out_resource->name.entry = maybe_name.value().to_string(); |
| 639 | return ParseMacro(parser, out_resource); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 642 | if (can_be_item) { |
| 643 | const auto item_iter = elToItemMap.find(resource_type); |
| 644 | if (item_iter != elToItemMap.end()) { |
| 645 | // This is an item, record its type and format and start parsing. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 646 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 647 | if (!maybe_name) { |
| 648 | diag_->Error(DiagMessage(out_resource->source) |
| 649 | << "<" << parser->element_name() << "> missing 'name' attribute"); |
| 650 | return false; |
| 651 | } |
| 652 | |
| 653 | out_resource->name.type = item_iter->second.type; |
| 654 | out_resource->name.entry = maybe_name.value().to_string(); |
| 655 | |
Adam Lesinski | e597d68 | 2017-06-01 17:16:44 -0700 | [diff] [blame] | 656 | // Only use the implied format of the type when there is no explicit format. |
| 657 | if (resource_format == 0u) { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 658 | resource_format = item_iter->second.format; |
| 659 | } |
| 660 | |
| 661 | if (!ParseItem(parser, out_resource, resource_format)) { |
| 662 | return false; |
| 663 | } |
| 664 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 665 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | // This might be a bag or something. |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 669 | if (can_be_bag) { |
| 670 | const auto bag_iter = elToBagMap.find(resource_type); |
| 671 | if (bag_iter != elToBagMap.end()) { |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 672 | // 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] | 673 | if (resource_type != kPublicGroupTag && resource_type != kStagingPublicGroupTag && |
| 674 | resource_type != "overlayable") { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 675 | if (!maybe_name) { |
| 676 | diag_->Error(DiagMessage(out_resource->source) |
| 677 | << "<" << parser->element_name() << "> missing 'name' attribute"); |
| 678 | return false; |
| 679 | } |
| 680 | |
| 681 | out_resource->name.entry = maybe_name.value().to_string(); |
| 682 | } |
| 683 | |
| 684 | // Call the associated parse method. The type will be filled in by the |
| 685 | // parse func. |
| 686 | if (!bag_iter->second(this, parser, out_resource)) { |
| 687 | return false; |
| 688 | } |
| 689 | return true; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if (can_be_item) { |
| 694 | // Try parsing the elementName (or type) as a resource. These shall only be |
| 695 | // resources like 'layout' or 'xml' and they can only be references. |
| 696 | const ResourceType* parsed_type = ParseResourceType(resource_type); |
| 697 | if (parsed_type) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 698 | if (!maybe_name) { |
| 699 | diag_->Error(DiagMessage(out_resource->source) |
| 700 | << "<" << parser->element_name() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 701 | << "> missing 'name' attribute"); |
| 702 | return false; |
| 703 | } |
| 704 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 705 | out_resource->name.type = *parsed_type; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 706 | out_resource->name.entry = maybe_name.value().to_string(); |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 707 | out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString); |
| 708 | if (!out_resource->value) { |
| 709 | diag_->Error(DiagMessage(out_resource->source) |
| 710 | << "invalid value for type '" << *parsed_type << "'. Expected a reference"); |
| 711 | return false; |
| 712 | } |
| 713 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 714 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Izabela Orlowska | a3ab21f | 2019-01-17 12:09:59 +0000 | [diff] [blame] | 717 | // If the resource type was not recognized, write the error and return false. |
| 718 | diag_->Error(DiagMessage(out_resource->source) |
Ryan Mitchell | 2e2c3b6 | 2019-04-26 01:16:52 -0700 | [diff] [blame] | 719 | << "unknown resource type '" << resource_type << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 720 | return false; |
| 721 | } |
| 722 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 723 | bool ResourceParser::ParseItem(xml::XmlPullParser* parser, |
| 724 | ParsedResource* out_resource, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 725 | const uint32_t format) { |
| 726 | if (format == android::ResTable_map::TYPE_STRING) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 727 | return ParseString(parser, out_resource); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 730 | out_resource->value = ParseXml(parser, format, kNoRawString); |
| 731 | if (!out_resource->value) { |
| 732 | diag_->Error(DiagMessage(out_resource->source) << "invalid " |
| 733 | << out_resource->name.type); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 734 | return false; |
| 735 | } |
| 736 | return true; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 737 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 738 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 739 | std::optional<FlattenedXmlSubTree> ResourceParser::CreateFlattenSubTree( |
| 740 | xml::XmlPullParser* parser) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 741 | const size_t begin_xml_line = parser->line_number(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 742 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 743 | std::string raw_value; |
| 744 | StyleString style_string; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 745 | std::vector<UntranslatableSection> untranslatable_sections; |
| 746 | if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 747 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 750 | return FlattenedXmlSubTree{.raw_value = raw_value, |
| 751 | .style_string = style_string, |
| 752 | .untranslatable_sections = untranslatable_sections, |
| 753 | .namespace_resolver = parser, |
| 754 | .source = source_.WithLine(begin_xml_line)}; |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * Reads the entire XML subtree and attempts to parse it as some Item, |
| 759 | * with typeMask denoting which items it can be. If allowRawValue is |
| 760 | * true, a RawString is returned if the XML couldn't be parsed as |
| 761 | * an Item. If allowRawValue is false, nullptr is returned in this |
| 762 | * case. |
| 763 | */ |
| 764 | std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser, const uint32_t type_mask, |
| 765 | const bool allow_raw_value) { |
| 766 | auto sub_tree = CreateFlattenSubTree(parser); |
| 767 | if (!sub_tree.has_value()) { |
| 768 | return {}; |
| 769 | } |
| 770 | return ParseXml(sub_tree.value(), type_mask, allow_raw_value, *table_, config_, *diag_); |
| 771 | } |
| 772 | |
| 773 | std::unique_ptr<Item> ResourceParser::ParseXml(const FlattenedXmlSubTree& xmlsub_tree, |
| 774 | const uint32_t type_mask, const bool allow_raw_value, |
| 775 | ResourceTable& table, |
| 776 | const android::ConfigDescription& config, |
| 777 | IDiagnostics& diag) { |
| 778 | if (!xmlsub_tree.style_string.spans.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 779 | // This can only be a StyledString. |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 780 | std::unique_ptr<StyledString> styled_string = |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 781 | util::make_unique<StyledString>(table.string_pool.MakeRef( |
| 782 | xmlsub_tree.style_string, |
| 783 | StringPool::Context(StringPool::Context::kNormalPriority, config))); |
| 784 | styled_string->untranslatable_sections = xmlsub_tree.untranslatable_sections; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 785 | return std::move(styled_string); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 788 | auto on_create_reference = [&](const ResourceName& name) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 789 | // name.package can be empty here, as it will assume the package name of the |
| 790 | // table. |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 791 | auto id = util::make_unique<Id>(); |
| 792 | id->SetSource(xmlsub_tree.source); |
| 793 | return table.AddResource(NewResourceBuilder(name).SetValue(std::move(id)).Build(), &diag); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 794 | }; |
| 795 | |
| 796 | // Process the raw value. |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 797 | std::unique_ptr<Item> processed_item = ResourceUtils::TryParseItemForAttribute( |
| 798 | xmlsub_tree.raw_value, type_mask, on_create_reference); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 799 | if (processed_item) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 800 | // Fix up the reference. |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 801 | if (auto ref = ValueCast<Reference>(processed_item.get())) { |
| 802 | ref->allow_raw = allow_raw_value; |
| 803 | ResolvePackage(xmlsub_tree.namespace_resolver, ref); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 804 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 805 | return processed_item; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | // Try making a regular string. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 809 | if (type_mask & android::ResTable_map::TYPE_STRING) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 810 | // Use the trimmed, escaped string. |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 811 | std::unique_ptr<String> string = util::make_unique<String>( |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 812 | table.string_pool.MakeRef(xmlsub_tree.style_string.str, StringPool::Context(config))); |
| 813 | string->untranslatable_sections = xmlsub_tree.untranslatable_sections; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 814 | return std::move(string); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 817 | if (allow_raw_value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 818 | // We can't parse this so return a RawString if we are allowed. |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 819 | return util::make_unique<RawString>(table.string_pool.MakeRef( |
| 820 | util::TrimWhitespace(xmlsub_tree.raw_value), StringPool::Context(config))); |
| 821 | } else if (util::TrimWhitespace(xmlsub_tree.raw_value).empty()) { |
Ryan Mitchell | fc3874a | 2019-05-28 16:30:17 -0700 | [diff] [blame] | 822 | // If the text is empty, and the value is not allowed to be a string, encode it as a @null. |
| 823 | return ResourceUtils::MakeNull(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 824 | } |
| 825 | return {}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 826 | } |
| 827 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 828 | bool ResourceParser::ParseString(xml::XmlPullParser* parser, |
| 829 | ParsedResource* out_resource) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 830 | bool formatted = true; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 831 | if (Maybe<StringPiece> formatted_attr = |
| 832 | xml::FindAttribute(parser, "formatted")) { |
| 833 | Maybe<bool> maybe_formatted = |
| 834 | ResourceUtils::ParseBool(formatted_attr.value()); |
| 835 | if (!maybe_formatted) { |
| 836 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 837 | << "invalid value for 'formatted'. Must be a boolean"); |
| 838 | return false; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 839 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 840 | formatted = maybe_formatted.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 841 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 842 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 843 | bool translatable = options_.translatable; |
| 844 | if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { |
| 845 | Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); |
| 846 | if (!maybe_translatable) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 847 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 848 | << "invalid value for 'translatable'. Must be a boolean"); |
| 849 | return false; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 850 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 851 | translatable = maybe_translatable.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 852 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 853 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 854 | out_resource->value = |
| 855 | ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString); |
| 856 | if (!out_resource->value) { |
| 857 | diag_->Error(DiagMessage(out_resource->source) << "not a valid string"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 858 | return false; |
| 859 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 860 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 861 | if (String* string_value = ValueCast<String>(out_resource->value.get())) { |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 862 | string_value->SetTranslatable(translatable); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 863 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 864 | if (formatted && translatable) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 865 | if (!util::VerifyJavaStringFormat(*string_value->value)) { |
| 866 | DiagMessage msg(out_resource->source); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 867 | msg << "multiple substitutions specified in non-positional format; " |
| 868 | "did you mean to add the formatted=\"false\" attribute?"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 869 | if (options_.error_on_positional_arguments) { |
| 870 | diag_->Error(msg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 871 | return false; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 872 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 873 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 874 | diag_->Warn(msg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 875 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 876 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 877 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 878 | } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) { |
| 879 | string_value->SetTranslatable(translatable); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 880 | } |
| 881 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 882 | } |
| 883 | |
Ryan Mitchell | 326e35ff | 2021-04-12 07:50:42 -0700 | [diff] [blame] | 884 | bool ResourceParser::ParseMacro(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 885 | auto sub_tree = CreateFlattenSubTree(parser); |
| 886 | if (!sub_tree) { |
| 887 | return false; |
| 888 | } |
| 889 | |
| 890 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 891 | diag_->Error(DiagMessage(out_resource->source) |
| 892 | << "<macro> tags cannot be declared in configurations other than the default " |
| 893 | "configuration'"); |
| 894 | return false; |
| 895 | } |
| 896 | |
| 897 | auto macro = std::make_unique<Macro>(); |
| 898 | macro->raw_value = std::move(sub_tree->raw_value); |
| 899 | macro->style_string = std::move(sub_tree->style_string); |
| 900 | macro->untranslatable_sections = std::move(sub_tree->untranslatable_sections); |
| 901 | |
| 902 | for (const auto& decl : parser->package_decls()) { |
| 903 | macro->alias_namespaces.emplace_back( |
| 904 | Macro::Namespace{.alias = decl.prefix, |
| 905 | .package_name = decl.package.package, |
| 906 | .is_private = decl.package.private_namespace}); |
| 907 | } |
| 908 | |
| 909 | out_resource->value = std::move(macro); |
| 910 | return true; |
| 911 | } |
| 912 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 913 | bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 914 | if (options_.visibility) { |
| 915 | diag_->Error(DiagMessage(out_resource->source) |
| 916 | << "<public> tag not allowed with --visibility flag"); |
| 917 | return false; |
| 918 | } |
| 919 | |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 920 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 921 | diag_->Warn(DiagMessage(out_resource->source) |
| 922 | << "ignoring configuration '" << out_resource->config << "' for <public> tag"); |
| 923 | } |
| 924 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 925 | Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); |
| 926 | if (!maybe_type) { |
| 927 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 928 | << "<public> must have a 'type' attribute"); |
| 929 | return false; |
| 930 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 931 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 932 | const ResourceType* parsed_type = ParseResourceType(maybe_type.value()); |
| 933 | if (!parsed_type) { |
| 934 | diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '" |
| 935 | << maybe_type.value() |
| 936 | << "' in <public>"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 937 | return false; |
| 938 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 939 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 940 | out_resource->name.type = *parsed_type; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 941 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 942 | if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) { |
| 943 | Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 944 | if (!maybe_id) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 945 | diag_->Error(DiagMessage(out_resource->source) |
| 946 | << "invalid resource ID '" << maybe_id_str.value() << "' in <public>"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 947 | return false; |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 948 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 949 | out_resource->id = maybe_id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 950 | } |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 951 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 952 | if (*parsed_type == ResourceType::kId) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 953 | // An ID marked as public is also the definition of an ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 954 | out_resource->value = util::make_unique<Id>(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 955 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 956 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 957 | out_resource->visibility_level = Visibility::Level::kPublic; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 958 | return true; |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 959 | } |
| 960 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 961 | template <typename Func> |
| 962 | bool static ParseGroupImpl(xml::XmlPullParser* parser, ParsedResource* out_resource, |
| 963 | const char* tag_name, IDiagnostics* diag, Func&& func) { |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 964 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 965 | diag->Warn(DiagMessage(out_resource->source) |
| 966 | << "ignoring configuration '" << out_resource->config << "' for <" << tag_name |
| 967 | << "> tag"); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 968 | } |
| 969 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 970 | Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); |
| 971 | if (!maybe_type) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 972 | diag->Error(DiagMessage(out_resource->source) |
| 973 | << "<" << tag_name << "> must have a 'type' attribute"); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 974 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 975 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 976 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 977 | const ResourceType* parsed_type = ParseResourceType(maybe_type.value()); |
| 978 | if (!parsed_type) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 979 | diag->Error(DiagMessage(out_resource->source) |
| 980 | << "invalid resource type '" << maybe_type.value() << "' in <" << tag_name << ">"); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 981 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 982 | } |
| 983 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 984 | Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "first-id"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 985 | if (!maybe_id_str) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 986 | diag->Error(DiagMessage(out_resource->source) |
| 987 | << "<" << tag_name << "> must have a 'first-id' attribute"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 988 | return false; |
| 989 | } |
| 990 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 991 | Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 992 | if (!maybe_id) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 993 | diag->Error(DiagMessage(out_resource->source) |
| 994 | << "invalid resource ID '" << maybe_id_str.value() << "' in <" << tag_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 995 | return false; |
| 996 | } |
| 997 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 998 | std::string comment; |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 999 | ResourceId next_id = maybe_id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1000 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1001 | const size_t depth = parser->depth(); |
| 1002 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1003 | if (parser->event() == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 1004 | comment = util::TrimWhitespace(parser->comment()).to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1005 | continue; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1006 | } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1007 | // Skip text. |
| 1008 | continue; |
| 1009 | } |
| 1010 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1011 | const Source item_source = out_resource->source.WithLine(parser->line_number()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1012 | const std::string& element_namespace = parser->element_namespace(); |
| 1013 | const std::string& element_name = parser->element_name(); |
| 1014 | if (element_namespace.empty() && element_name == "public") { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1015 | auto maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1016 | if (!maybe_name) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1017 | diag->Error(DiagMessage(item_source) << "<public> must have a 'name' attribute"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1018 | error = true; |
| 1019 | continue; |
| 1020 | } |
| 1021 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1022 | if (xml::FindNonEmptyAttribute(parser, "id")) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1023 | diag->Error(DiagMessage(item_source) << "'id' is ignored within <" << tag_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1024 | error = true; |
| 1025 | continue; |
| 1026 | } |
| 1027 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1028 | if (xml::FindNonEmptyAttribute(parser, "type")) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1029 | diag->Error(DiagMessage(item_source) << "'type' is ignored within <" << tag_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1030 | error = true; |
| 1031 | continue; |
| 1032 | } |
| 1033 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1034 | ParsedResource& entry_res = out_resource->child_resources.emplace_back(ParsedResource{ |
| 1035 | .name = ResourceName{{}, *parsed_type, maybe_name.value().to_string()}, |
| 1036 | .source = item_source, |
| 1037 | .id = next_id, |
| 1038 | .comment = std::move(comment), |
| 1039 | }); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1040 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1041 | // Execute group specific code. |
| 1042 | func(entry_res, next_id); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1043 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1044 | next_id.id++; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1045 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1046 | diag->Error(DiagMessage(item_source) << ":" << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1047 | error = true; |
| 1048 | } |
| 1049 | } |
| 1050 | return !error; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 1051 | } |
| 1052 | |
Ryan Mitchell | 2e9bec1 | 2021-03-22 09:31:00 -0700 | [diff] [blame] | 1053 | bool ResourceParser::ParseStagingPublicGroup(xml::XmlPullParser* parser, |
| 1054 | ParsedResource* out_resource) { |
| 1055 | return ParseGroupImpl(parser, out_resource, kStagingPublicGroupTag, diag_, |
| 1056 | [](ParsedResource& parsed_entry, ResourceId id) { |
| 1057 | parsed_entry.id = id; |
| 1058 | parsed_entry.staged_api = true; |
| 1059 | parsed_entry.visibility_level = Visibility::Level::kPublic; |
| 1060 | }); |
| 1061 | } |
| 1062 | |
| 1063 | bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 1064 | if (options_.visibility) { |
| 1065 | diag_->Error(DiagMessage(out_resource->source) |
| 1066 | << "<" << kPublicGroupTag << "> tag not allowed with --visibility flag"); |
| 1067 | return false; |
| 1068 | } |
| 1069 | |
| 1070 | return ParseGroupImpl(parser, out_resource, kPublicGroupTag, diag_, |
| 1071 | [](ParsedResource& parsed_entry, ResourceId id) { |
| 1072 | parsed_entry.id = id; |
| 1073 | parsed_entry.visibility_level = Visibility::Level::kPublic; |
| 1074 | }); |
| 1075 | } |
| 1076 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1077 | bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser, |
| 1078 | ParsedResource* out_resource) { |
| 1079 | Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); |
| 1080 | if (!maybe_type) { |
| 1081 | diag_->Error(DiagMessage(out_resource->source) |
| 1082 | << "<" << parser->element_name() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1083 | << "> must have a 'type' attribute"); |
| 1084 | return false; |
| 1085 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1086 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1087 | const ResourceType* parsed_type = ParseResourceType(maybe_type.value()); |
| 1088 | if (!parsed_type) { |
| 1089 | diag_->Error(DiagMessage(out_resource->source) |
| 1090 | << "invalid resource type '" << maybe_type.value() << "' in <" |
| 1091 | << parser->element_name() << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1092 | return false; |
| 1093 | } |
| 1094 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1095 | out_resource->name.type = *parsed_type; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1096 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1097 | } |
| 1098 | |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1099 | bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 1100 | if (options_.visibility) { |
| 1101 | diag_->Error(DiagMessage(out_resource->source) |
| 1102 | << "<java-symbol> and <symbol> tags not allowed with --visibility flag"); |
| 1103 | return false; |
| 1104 | } |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1105 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 1106 | diag_->Warn(DiagMessage(out_resource->source) |
| 1107 | << "ignoring configuration '" << out_resource->config << "' for <" |
| 1108 | << parser->element_name() << "> tag"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1109 | } |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1110 | |
| 1111 | if (!ParseSymbolImpl(parser, out_resource)) { |
| 1112 | return false; |
| 1113 | } |
| 1114 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 1115 | out_resource->visibility_level = Visibility::Level::kPrivate; |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1116 | return true; |
| 1117 | } |
| 1118 | |
| 1119 | bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 1120 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 1121 | diag_->Warn(DiagMessage(out_resource->source) |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1122 | << "ignoring configuration '" << out_resource->config |
| 1123 | << "' for <overlayable> tag"); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1126 | Maybe<StringPiece> overlayable_name = xml::FindNonEmptyAttribute(parser, "name"); |
| 1127 | if (!overlayable_name) { |
| 1128 | diag_->Error(DiagMessage(out_resource->source) |
| 1129 | << "<overlayable> tag must have a 'name' attribute"); |
| 1130 | return false; |
| 1131 | } |
| 1132 | |
| 1133 | const std::string kActorUriScheme = |
| 1134 | android::base::StringPrintf("%s://", Overlayable::kActorScheme); |
| 1135 | Maybe<StringPiece> overlayable_actor = xml::FindNonEmptyAttribute(parser, "actor"); |
| 1136 | if (overlayable_actor && !util::StartsWith(overlayable_actor.value(), kActorUriScheme)) { |
| 1137 | diag_->Error(DiagMessage(out_resource->source) |
| 1138 | << "specified <overlayable> tag 'actor' attribute must use the scheme '" |
| 1139 | << Overlayable::kActorScheme << "'"); |
| 1140 | return false; |
| 1141 | } |
| 1142 | |
| 1143 | // Create a overlayable entry grouping that represents this <overlayable> |
| 1144 | auto overlayable = std::make_shared<Overlayable>( |
| 1145 | overlayable_name.value(), (overlayable_actor) ? overlayable_actor.value() : "", |
Ryan Mitchell | b5b162b | 2019-02-07 20:07:21 -0800 | [diff] [blame] | 1146 | source_); |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1147 | |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1148 | bool error = false; |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1149 | std::string comment; |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 1150 | PolicyFlags current_policies = PolicyFlags::NONE; |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1151 | const size_t start_depth = parser->depth(); |
| 1152 | while (xml::XmlPullParser::IsGoodEvent(parser->Next())) { |
| 1153 | xml::XmlPullParser::Event event = parser->event(); |
| 1154 | if (event == xml::XmlPullParser::Event::kEndElement && parser->depth() == start_depth) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1155 | // Break the loop when exiting the <overlayable> |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1156 | break; |
| 1157 | } else if (event == xml::XmlPullParser::Event::kEndElement |
| 1158 | && parser->depth() == start_depth + 1) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1159 | // Clear the current policies when exiting the <policy> tags |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 1160 | current_policies = PolicyFlags::NONE; |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1161 | continue; |
| 1162 | } else if (event == xml::XmlPullParser::Event::kComment) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1163 | // Retrieve the comment of individual <item> tags |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1164 | comment = parser->comment(); |
| 1165 | continue; |
| 1166 | } else if (event != xml::XmlPullParser::Event::kStartElement) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1167 | // Skip to the start of the next element |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1168 | continue; |
| 1169 | } |
| 1170 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1171 | const Source element_source = source_.WithLine(parser->line_number()); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1172 | const std::string& element_name = parser->element_name(); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1173 | const std::string& element_namespace = parser->element_namespace(); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1174 | if (element_namespace.empty() && element_name == "item") { |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 1175 | if (current_policies == PolicyFlags::NONE) { |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 1176 | diag_->Error(DiagMessage(element_source) |
| 1177 | << "<item> within an <overlayable> must be inside a <policy> block"); |
| 1178 | error = true; |
| 1179 | continue; |
| 1180 | } |
| 1181 | |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1182 | // Items specify the name and type of resource that should be overlayable |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1183 | Maybe<StringPiece> item_name = xml::FindNonEmptyAttribute(parser, "name"); |
| 1184 | if (!item_name) { |
| 1185 | diag_->Error(DiagMessage(element_source) |
| 1186 | << "<item> within an <overlayable> must have a 'name' attribute"); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1187 | error = true; |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1188 | continue; |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1189 | } |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1190 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1191 | Maybe<StringPiece> item_type = xml::FindNonEmptyAttribute(parser, "type"); |
| 1192 | if (!item_type) { |
| 1193 | diag_->Error(DiagMessage(element_source) |
| 1194 | << "<item> within an <overlayable> must have a 'type' attribute"); |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1195 | error = true; |
| 1196 | continue; |
| 1197 | } |
| 1198 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1199 | const ResourceType* type = ParseResourceType(item_type.value()); |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1200 | if (type == nullptr) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1201 | diag_->Error(DiagMessage(element_source) |
| 1202 | << "invalid resource type '" << item_type.value() |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1203 | << "' in <item> within an <overlayable>"); |
| 1204 | error = true; |
| 1205 | continue; |
| 1206 | } |
| 1207 | |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1208 | OverlayableItem overlayable_item(overlayable); |
| 1209 | overlayable_item.policies = current_policies; |
| 1210 | overlayable_item.comment = comment; |
| 1211 | overlayable_item.source = element_source; |
| 1212 | |
| 1213 | ParsedResource child_resource{}; |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1214 | child_resource.name.type = *type; |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1215 | child_resource.name.entry = item_name.value().to_string(); |
| 1216 | child_resource.overlayable_item = overlayable_item; |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1217 | out_resource->child_resources.push_back(std::move(child_resource)); |
| 1218 | |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1219 | } else if (element_namespace.empty() && element_name == "policy") { |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 1220 | if (current_policies != PolicyFlags::NONE) { |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1221 | // 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] | 1222 | diag_->Error(DiagMessage(element_source) << "<policy> blocks cannot be recursively nested"); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1223 | error = true; |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1224 | break; |
| 1225 | } else if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { |
| 1226 | // 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] | 1227 | // policies. Items within the policy tag will have the specified policy. |
Ryan Mitchell | 939df09 | 2019-04-09 17:13:50 -0700 | [diff] [blame] | 1228 | for (const StringPiece& part : util::Tokenize(maybe_type.value(), '|')) { |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1229 | StringPiece trimmed_part = util::TrimWhitespace(part); |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 1230 | const auto policy = std::find_if(kPolicyStringToFlag.begin(), |
| 1231 | kPolicyStringToFlag.end(), |
| 1232 | [trimmed_part](const auto& it) { |
| 1233 | return trimmed_part == it.first; |
| 1234 | }); |
| 1235 | if (policy == kPolicyStringToFlag.end()) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1236 | diag_->Error(DiagMessage(element_source) |
Ryan Mitchell | 1bb1fe0 | 2018-11-16 11:21:41 -0800 | [diff] [blame] | 1237 | << "<policy> has unsupported type '" << trimmed_part << "'"); |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1238 | error = true; |
| 1239 | continue; |
| 1240 | } |
Ryan Mitchell | 939df09 | 2019-04-09 17:13:50 -0700 | [diff] [blame] | 1241 | |
| 1242 | current_policies |= policy->second; |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1243 | } |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 1244 | } else { |
| 1245 | diag_->Error(DiagMessage(element_source) |
Ryan Mitchell | 939df09 | 2019-04-09 17:13:50 -0700 | [diff] [blame] | 1246 | << "<policy> must have a 'type' attribute"); |
Winson | b2d7f53 | 2019-02-04 16:32:43 -0800 | [diff] [blame] | 1247 | error = true; |
| 1248 | continue; |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1249 | } |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1250 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1251 | diag_->Error(DiagMessage(element_source) << "invalid element <" << element_name << "> " |
Ryan Mitchell | 939df09 | 2019-04-09 17:13:50 -0700 | [diff] [blame] | 1252 | << " in <overlayable>"); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1253 | error = true; |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1254 | break; |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1255 | } |
Ryan Mitchell | 54237ff | 2018-12-13 15:44:29 -0800 | [diff] [blame] | 1256 | |
| 1257 | comment.clear(); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1258 | } |
Ryan Mitchell | e4e989c | 2018-10-29 02:21:50 -0700 | [diff] [blame] | 1259 | |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1260 | return !error; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1261 | } |
| 1262 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 1263 | bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1264 | if (ParseSymbolImpl(parser, out_resource)) { |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 1265 | out_resource->visibility_level = Visibility::Level::kUndefined; |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 1266 | out_resource->allow_new = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1267 | return true; |
| 1268 | } |
| 1269 | return false; |
| 1270 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1271 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1272 | bool ResourceParser::ParseAttr(xml::XmlPullParser* parser, |
| 1273 | ParsedResource* out_resource) { |
| 1274 | return ParseAttrImpl(parser, out_resource, false); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1277 | bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, |
| 1278 | ParsedResource* out_resource, bool weak) { |
| 1279 | out_resource->name.type = ResourceType::kAttr; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1280 | |
| 1281 | // Attributes only end up in default configuration. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1282 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 1283 | diag_->Warn(DiagMessage(out_resource->source) |
| 1284 | << "ignoring configuration '" << out_resource->config |
| 1285 | << "' for attribute " << out_resource->name); |
| 1286 | out_resource->config = ConfigDescription::DefaultConfig(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1289 | uint32_t type_mask = 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1290 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1291 | Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format"); |
| 1292 | if (maybe_format) { |
| 1293 | type_mask = ParseFormatAttribute(maybe_format.value()); |
| 1294 | if (type_mask == 0) { |
| 1295 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1296 | << "invalid attribute format '" << maybe_format.value() << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1297 | return false; |
| 1298 | } |
| 1299 | } |
| 1300 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1301 | Maybe<int32_t> maybe_min, maybe_max; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1302 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1303 | if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) { |
| 1304 | StringPiece min_str = util::TrimWhitespace(maybe_min_str.value()); |
| 1305 | if (!min_str.empty()) { |
| 1306 | std::u16string min_str16 = util::Utf8ToUtf16(min_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1307 | android::Res_value value; |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1308 | if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1309 | maybe_min = static_cast<int32_t>(value.data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1310 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1313 | if (!maybe_min) { |
| 1314 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1315 | << "invalid 'min' value '" << min_str << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1316 | return false; |
| 1317 | } |
| 1318 | } |
| 1319 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1320 | if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) { |
| 1321 | StringPiece max_str = util::TrimWhitespace(maybe_max_str.value()); |
| 1322 | if (!max_str.empty()) { |
| 1323 | std::u16string max_str16 = util::Utf8ToUtf16(max_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1324 | android::Res_value value; |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1325 | if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1326 | maybe_max = static_cast<int32_t>(value.data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1327 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1328 | } |
| 1329 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1330 | if (!maybe_max) { |
| 1331 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1332 | << "invalid 'max' value '" << max_str << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1333 | return false; |
| 1334 | } |
| 1335 | } |
| 1336 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1337 | if ((maybe_min || maybe_max) && |
| 1338 | (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) { |
| 1339 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1340 | << "'min' and 'max' can only be used when format='integer'"); |
| 1341 | return false; |
| 1342 | } |
| 1343 | |
| 1344 | struct SymbolComparator { |
Yi Kong | 087e2d4 | 2017-05-02 12:49:25 -0700 | [diff] [blame] | 1345 | bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1346 | return a.symbol.name.value() < b.symbol.name.value(); |
| 1347 | } |
| 1348 | }; |
| 1349 | |
| 1350 | std::set<Attribute::Symbol, SymbolComparator> items; |
| 1351 | |
| 1352 | std::string comment; |
| 1353 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1354 | const size_t depth = parser->depth(); |
| 1355 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1356 | if (parser->event() == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 1357 | comment = util::TrimWhitespace(parser->comment()).to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1358 | continue; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1359 | } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1360 | // Skip text. |
| 1361 | continue; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1362 | } |
| 1363 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1364 | const Source item_source = source_.WithLine(parser->line_number()); |
| 1365 | const std::string& element_namespace = parser->element_namespace(); |
| 1366 | const std::string& element_name = parser->element_name(); |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1367 | if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1368 | if (element_name == "enum") { |
| 1369 | if (type_mask & android::ResTable_map::TYPE_FLAGS) { |
| 1370 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1371 | << "can not define an <enum>; already defined a <flag>"); |
| 1372 | error = true; |
| 1373 | continue; |
| 1374 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1375 | type_mask |= android::ResTable_map::TYPE_ENUM; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1376 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1377 | } else if (element_name == "flag") { |
| 1378 | if (type_mask & android::ResTable_map::TYPE_ENUM) { |
| 1379 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1380 | << "can not define a <flag>; already defined an <enum>"); |
| 1381 | error = true; |
| 1382 | continue; |
| 1383 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1384 | type_mask |= android::ResTable_map::TYPE_FLAGS; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | if (Maybe<Attribute::Symbol> s = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1388 | ParseEnumOrFlagItem(parser, element_name)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1389 | Attribute::Symbol& symbol = s.value(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1390 | ParsedResource child_resource; |
| 1391 | child_resource.name = symbol.symbol.name.value(); |
| 1392 | child_resource.source = item_source; |
| 1393 | child_resource.value = util::make_unique<Id>(); |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 1394 | if (options_.visibility) { |
| 1395 | child_resource.visibility_level = options_.visibility.value(); |
| 1396 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1397 | out_resource->child_resources.push_back(std::move(child_resource)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1398 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1399 | symbol.symbol.SetComment(std::move(comment)); |
| 1400 | symbol.symbol.SetSource(item_source); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1401 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1402 | auto insert_result = items.insert(std::move(symbol)); |
| 1403 | if (!insert_result.second) { |
| 1404 | const Attribute::Symbol& existing_symbol = *insert_result.first; |
| 1405 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1406 | << "duplicate symbol '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1407 | << existing_symbol.symbol.name.value().entry << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1408 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1409 | diag_->Note(DiagMessage(existing_symbol.symbol.GetSource()) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1410 | << "first defined here"); |
| 1411 | error = true; |
| 1412 | } |
| 1413 | } else { |
| 1414 | error = true; |
| 1415 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1416 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1417 | diag_->Error(DiagMessage(item_source) << ":" << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1418 | error = true; |
| 1419 | } |
| 1420 | |
| 1421 | comment = {}; |
| 1422 | } |
| 1423 | |
| 1424 | if (error) { |
| 1425 | return false; |
| 1426 | } |
| 1427 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1428 | std::unique_ptr<Attribute> attr = util::make_unique<Attribute>( |
| 1429 | type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY}); |
| 1430 | attr->SetWeak(weak); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1431 | attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end()); |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1432 | attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min()); |
| 1433 | 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] | 1434 | out_resource->value = std::move(attr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1435 | return true; |
| 1436 | } |
| 1437 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1438 | Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1439 | xml::XmlPullParser* parser, const StringPiece& tag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1440 | const Source source = source_.WithLine(parser->line_number()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1441 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1442 | Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
| 1443 | if (!maybe_name) { |
| 1444 | diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1445 | << tag << ">"); |
| 1446 | return {}; |
| 1447 | } |
| 1448 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1449 | Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value"); |
| 1450 | if (!maybe_value) { |
| 1451 | diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1452 | << tag << ">"); |
| 1453 | return {}; |
| 1454 | } |
| 1455 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1456 | std::u16string value16 = util::Utf8ToUtf16(maybe_value.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1457 | android::Res_value val; |
| 1458 | if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1459 | diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1460 | << "' for <" << tag |
| 1461 | << ">; must be an integer"); |
| 1462 | return {}; |
| 1463 | } |
| 1464 | |
| 1465 | return Attribute::Symbol{ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1466 | Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())), |
Ryan Mitchell | c167680 | 2019-05-20 16:47:08 -0700 | [diff] [blame] | 1467 | val.data, val.dataType}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1468 | } |
| 1469 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1470 | bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) { |
| 1471 | const Source source = source_.WithLine(parser->line_number()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1472 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1473 | Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
| 1474 | if (!maybe_name) { |
| 1475 | diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1476 | return false; |
| 1477 | } |
| 1478 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1479 | Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1480 | if (!maybe_key) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1481 | diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1482 | return false; |
| 1483 | } |
| 1484 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1485 | ResolvePackage(parser, &maybe_key.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1486 | maybe_key.value().SetSource(source); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1487 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1488 | std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1489 | if (!value) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1490 | diag_->Error(DiagMessage(source) << "could not parse style item"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1491 | return false; |
| 1492 | } |
| 1493 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1494 | 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] | 1495 | return true; |
| 1496 | } |
| 1497 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 1498 | bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1499 | ParsedResource* out_resource) { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 1500 | out_resource->name.type = type; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1501 | |
| 1502 | std::unique_ptr<Style> style = util::make_unique<Style>(); |
| 1503 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1504 | Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent"); |
| 1505 | if (maybe_parent) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1506 | // 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] | 1507 | if (!maybe_parent.value().empty()) { |
| 1508 | std::string err_str; |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1509 | style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1510 | if (!style->parent) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1511 | diag_->Error(DiagMessage(out_resource->source) << err_str); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1512 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1515 | // 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] | 1516 | // private if appropriate. |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1517 | ResolvePackage(parser, &style->parent.value()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1518 | } |
| 1519 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1520 | } else { |
| 1521 | // No parent was specified, so try inferring it from the style name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1522 | std::string style_name = out_resource->name.entry; |
| 1523 | size_t pos = style_name.find_last_of(u'.'); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1524 | if (pos != std::string::npos) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1525 | style->parent_inferred = true; |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1526 | style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1531 | const size_t depth = parser->depth(); |
| 1532 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1533 | if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1534 | // Skip text and comments. |
| 1535 | continue; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1536 | } |
| 1537 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1538 | const std::string& element_namespace = parser->element_namespace(); |
| 1539 | const std::string& element_name = parser->element_name(); |
| 1540 | if (element_namespace == "" && element_name == "item") { |
| 1541 | error |= !ParseStyleItem(parser, style.get()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1542 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1543 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1544 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1545 | << ":" << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1546 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1547 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1548 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1549 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1550 | if (error) { |
| 1551 | return false; |
| 1552 | } |
| 1553 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1554 | out_resource->value = std::move(style); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1555 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1556 | } |
| 1557 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 1558 | bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 1559 | uint32_t resource_format = android::ResTable_map::TYPE_ANY; |
| 1560 | if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) { |
| 1561 | resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value()); |
| 1562 | if (resource_format == 0u) { |
| 1563 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1564 | << "'" << format_attr.value() << "' is an invalid format"); |
| 1565 | return false; |
| 1566 | } |
| 1567 | } |
| 1568 | return ParseArrayImpl(parser, out_resource, resource_format); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1569 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1570 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 1571 | bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 1572 | return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1573 | } |
| 1574 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 1575 | bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 1576 | return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1577 | } |
| 1578 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1579 | bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser, |
| 1580 | ParsedResource* out_resource, |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1581 | const uint32_t typeMask) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1582 | out_resource->name.type = ResourceType::kArray; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1583 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1584 | std::unique_ptr<Array> array = util::make_unique<Array>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1585 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 1586 | bool translatable = options_.translatable; |
| 1587 | if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { |
| 1588 | Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); |
| 1589 | if (!maybe_translatable) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1590 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1591 | << "invalid value for 'translatable'. Must be a boolean"); |
| 1592 | return false; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 1593 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 1594 | translatable = maybe_translatable.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1595 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 1596 | array->SetTranslatable(translatable); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 1597 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1598 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1599 | const size_t depth = parser->depth(); |
| 1600 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1601 | if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1602 | // Skip text and comments. |
| 1603 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1604 | } |
| 1605 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1606 | const Source item_source = source_.WithLine(parser->line_number()); |
| 1607 | const std::string& element_namespace = parser->element_namespace(); |
| 1608 | const std::string& element_name = parser->element_name(); |
| 1609 | if (element_namespace.empty() && element_name == "item") { |
| 1610 | std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1611 | if (!item) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1612 | diag_->Error(DiagMessage(item_source) << "could not parse array item"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1613 | error = true; |
| 1614 | continue; |
| 1615 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1616 | item->SetSource(item_source); |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 1617 | array->elements.emplace_back(std::move(item)); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1618 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1619 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1620 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1621 | << "unknown tag <" << element_namespace << ":" |
| 1622 | << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1623 | error = true; |
| 1624 | } |
| 1625 | } |
| 1626 | |
| 1627 | if (error) { |
| 1628 | return false; |
| 1629 | } |
| 1630 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1631 | out_resource->value = std::move(array); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1632 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1633 | } |
| 1634 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1635 | bool ResourceParser::ParsePlural(xml::XmlPullParser* parser, |
| 1636 | ParsedResource* out_resource) { |
| 1637 | out_resource->name.type = ResourceType::kPlurals; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1638 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1639 | std::unique_ptr<Plural> plural = util::make_unique<Plural>(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1640 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1641 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1642 | const size_t depth = parser->depth(); |
| 1643 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1644 | if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1645 | // Skip text and comments. |
| 1646 | continue; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1647 | } |
| 1648 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1649 | const Source item_source = source_.WithLine(parser->line_number()); |
| 1650 | const std::string& element_namespace = parser->element_namespace(); |
| 1651 | const std::string& element_name = parser->element_name(); |
| 1652 | if (element_namespace.empty() && element_name == "item") { |
| 1653 | Maybe<StringPiece> maybe_quantity = |
| 1654 | xml::FindNonEmptyAttribute(parser, "quantity"); |
| 1655 | if (!maybe_quantity) { |
| 1656 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1657 | << "<item> in <plurals> requires attribute " |
| 1658 | << "'quantity'"); |
| 1659 | error = true; |
| 1660 | continue; |
| 1661 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1662 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1663 | StringPiece trimmed_quantity = |
| 1664 | util::TrimWhitespace(maybe_quantity.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1665 | size_t index = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1666 | if (trimmed_quantity == "zero") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1667 | index = Plural::Zero; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1668 | } else if (trimmed_quantity == "one") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1669 | index = Plural::One; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1670 | } else if (trimmed_quantity == "two") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1671 | index = Plural::Two; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1672 | } else if (trimmed_quantity == "few") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1673 | index = Plural::Few; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1674 | } else if (trimmed_quantity == "many") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1675 | index = Plural::Many; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1676 | } else if (trimmed_quantity == "other") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1677 | index = Plural::Other; |
| 1678 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1679 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1680 | << "<item> in <plural> has invalid value '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1681 | << trimmed_quantity << "' for attribute 'quantity'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1682 | error = true; |
| 1683 | continue; |
| 1684 | } |
| 1685 | |
| 1686 | if (plural->values[index]) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1687 | diag_->Error(DiagMessage(item_source) << "duplicate quantity '" |
| 1688 | << trimmed_quantity << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1689 | error = true; |
| 1690 | continue; |
| 1691 | } |
| 1692 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1693 | if (!(plural->values[index] = ParseXml( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1694 | parser, android::ResTable_map::TYPE_STRING, kNoRawString))) { |
| 1695 | error = true; |
Ryan Mitchell | 2f9077d | 2019-02-15 16:02:09 -0800 | [diff] [blame] | 1696 | continue; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1697 | } |
Ryan Mitchell | 2f9077d | 2019-02-15 16:02:09 -0800 | [diff] [blame] | 1698 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1699 | plural->values[index]->SetSource(item_source); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1700 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1701 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1702 | diag_->Error(DiagMessage(item_source) << "unknown tag <" |
| 1703 | << element_namespace << ":" |
| 1704 | << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1705 | error = true; |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | if (error) { |
| 1710 | return false; |
| 1711 | } |
| 1712 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1713 | out_resource->value = std::move(plural); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1714 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1715 | } |
| 1716 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1717 | bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser, |
| 1718 | ParsedResource* out_resource) { |
| 1719 | out_resource->name.type = ResourceType::kStyleable; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1720 | |
Donald Chai | 94e4a01 | 2020-01-06 13:52:41 -0800 | [diff] [blame] | 1721 | if (!options_.preserve_visibility_of_styleables) { |
| 1722 | // This was added in change Idd21b5de4d20be06c6f8c8eb5a22ccd68afc4927 to mimic aapt1, but no one |
| 1723 | // knows exactly what for. |
| 1724 | // |
| 1725 | // FWIW, styleables only appear in generated R classes. For custom views these should always be |
| 1726 | // package-private (to be used only by the view class); themes are a different story. |
| 1727 | out_resource->visibility_level = Visibility::Level::kPublic; |
| 1728 | } |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 1729 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1730 | // Declare-styleable only ends up in default config; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1731 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 1732 | diag_->Warn(DiagMessage(out_resource->source) |
| 1733 | << "ignoring configuration '" << out_resource->config |
| 1734 | << "' for styleable " << out_resource->name.entry); |
| 1735 | out_resource->config = ConfigDescription::DefaultConfig(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1736 | } |
| 1737 | |
| 1738 | std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>(); |
| 1739 | |
| 1740 | std::string comment; |
| 1741 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1742 | const size_t depth = parser->depth(); |
| 1743 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1744 | if (parser->event() == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 1745 | comment = util::TrimWhitespace(parser->comment()).to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1746 | continue; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1747 | } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1748 | // Ignore text. |
| 1749 | continue; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1750 | } |
| 1751 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1752 | const Source item_source = source_.WithLine(parser->line_number()); |
| 1753 | const std::string& element_namespace = parser->element_namespace(); |
| 1754 | const std::string& element_name = parser->element_name(); |
| 1755 | if (element_namespace.empty() && element_name == "attr") { |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1756 | Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1757 | if (!maybe_name) { |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1758 | diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1759 | error = true; |
| 1760 | continue; |
| 1761 | } |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1762 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1763 | // If this is a declaration, the package name may be in the name. Separate |
| 1764 | // these out. |
| 1765 | // Eg. <attr name="android:text" /> |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1766 | Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1767 | if (!maybe_ref) { |
| 1768 | diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '" |
| 1769 | << maybe_name.value() << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1770 | error = true; |
| 1771 | continue; |
| 1772 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1773 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1774 | Reference& child_ref = maybe_ref.value(); |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1775 | xml::ResolvePackage(parser, &child_ref); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1776 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1777 | // Create the ParsedResource that will add the attribute to the table. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1778 | ParsedResource child_resource; |
| 1779 | child_resource.name = child_ref.name.value(); |
| 1780 | child_resource.source = item_source; |
Chih-Hung Hsieh | 7a616f6 | 2020-03-05 15:59:25 -0800 | [diff] [blame] | 1781 | // NOLINTNEXTLINE(bugprone-use-after-move) move+reset comment |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1782 | child_resource.comment = std::move(comment); |
Izabela Orlowska | c7ac3a1 | 2018-03-27 14:46:52 +0100 | [diff] [blame] | 1783 | if (options_.visibility) { |
| 1784 | child_resource.visibility_level = options_.visibility.value(); |
| 1785 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1786 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1787 | if (!ParseAttrImpl(parser, &child_resource, true)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1788 | error = true; |
| 1789 | continue; |
| 1790 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1791 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1792 | // Create the reference to this attribute. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1793 | child_ref.SetComment(child_resource.comment); |
| 1794 | child_ref.SetSource(item_source); |
| 1795 | styleable->entries.push_back(std::move(child_ref)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1796 | |
Ryan Mitchell | acde95c3 | 2019-04-23 05:44:21 -0700 | [diff] [blame] | 1797 | // Do not add referenced attributes that do not define a format to the table. |
| 1798 | CHECK(child_resource.value != nullptr); |
| 1799 | Attribute* attr = ValueCast<Attribute>(child_resource.value.get()); |
| 1800 | |
| 1801 | CHECK(attr != nullptr); |
| 1802 | if (attr->type_mask != android::ResTable_map::TYPE_ANY) { |
| 1803 | out_resource->child_resources.push_back(std::move(child_resource)); |
| 1804 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1805 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1806 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1807 | diag_->Error(DiagMessage(item_source) << "unknown tag <" |
| 1808 | << element_namespace << ":" |
| 1809 | << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1810 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1811 | } |
| 1812 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1813 | comment = {}; |
| 1814 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1815 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1816 | if (error) { |
| 1817 | return false; |
| 1818 | } |
| 1819 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1820 | out_resource->value = std::move(styleable); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1821 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1822 | } |
| 1823 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1824 | } // namespace aapt |