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