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