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