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