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); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 450 | |
| 451 | // Extract the product name if it exists. |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 452 | if (Maybe<StringPiece> maybe_product = xml::FindNonEmptyAttribute(parser, "product")) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 453 | parsed_resource.product = maybe_product.value().to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | // Parse the resource regardless of product. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 457 | if (!ParseResource(parser, &parsed_resource)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 458 | error = true; |
| 459 | continue; |
| 460 | } |
| 461 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 462 | if (!AddResourcesToTable(table_, diag_, &parsed_resource)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 463 | error = true; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // Check that we included at least one variant of each stripped resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 468 | for (const ResourceName& stripped_resource : stripped_resources) { |
| 469 | if (!table_->FindResource(stripped_resource)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 470 | // Failed to find the resource. |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 471 | diag_->Error(DiagMessage(source_) << "resource '" << stripped_resource |
| 472 | << "' was filtered out but no product variant remains"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 473 | error = true; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | return !error; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 478 | } |
| 479 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 480 | bool ResourceParser::ParseResource(xml::XmlPullParser* parser, |
| 481 | ParsedResource* out_resource) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 482 | struct ItemTypeFormat { |
| 483 | ResourceType type; |
| 484 | uint32_t format; |
| 485 | }; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 486 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 487 | using BagParseFunc = std::function<bool(ResourceParser*, xml::XmlPullParser*, |
| 488 | ParsedResource*)>; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 489 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 490 | static const auto elToItemMap = ImmutableMap<std::string, ItemTypeFormat>::CreatePreSorted({ |
| 491 | {"bool", {ResourceType::kBool, android::ResTable_map::TYPE_BOOLEAN}}, |
| 492 | {"color", {ResourceType::kColor, android::ResTable_map::TYPE_COLOR}}, |
| 493 | {"configVarying", {ResourceType::kConfigVarying, android::ResTable_map::TYPE_ANY}}, |
| 494 | {"dimen", |
| 495 | {ResourceType::kDimen, |
| 496 | android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION | |
| 497 | android::ResTable_map::TYPE_DIMENSION}}, |
| 498 | {"drawable", {ResourceType::kDrawable, android::ResTable_map::TYPE_COLOR}}, |
| 499 | {"fraction", |
| 500 | {ResourceType::kFraction, |
| 501 | android::ResTable_map::TYPE_FLOAT | android::ResTable_map::TYPE_FRACTION | |
| 502 | android::ResTable_map::TYPE_DIMENSION}}, |
| 503 | {"integer", {ResourceType::kInteger, android::ResTable_map::TYPE_INTEGER}}, |
| 504 | {"string", {ResourceType::kString, android::ResTable_map::TYPE_STRING}}, |
| 505 | }); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 506 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 507 | static const auto elToBagMap = ImmutableMap<std::string, BagParseFunc>::CreatePreSorted({ |
| 508 | {"add-resource", std::mem_fn(&ResourceParser::ParseAddResource)}, |
| 509 | {"array", std::mem_fn(&ResourceParser::ParseArray)}, |
| 510 | {"attr", std::mem_fn(&ResourceParser::ParseAttr)}, |
| 511 | {"configVarying", |
| 512 | std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kConfigVarying, |
| 513 | std::placeholders::_2, std::placeholders::_3)}, |
| 514 | {"declare-styleable", std::mem_fn(&ResourceParser::ParseDeclareStyleable)}, |
| 515 | {"integer-array", std::mem_fn(&ResourceParser::ParseIntegerArray)}, |
| 516 | {"java-symbol", std::mem_fn(&ResourceParser::ParseSymbol)}, |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 517 | {"overlayable", std::mem_fn(&ResourceParser::ParseOverlayable)}, |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 518 | {"plurals", std::mem_fn(&ResourceParser::ParsePlural)}, |
| 519 | {"public", std::mem_fn(&ResourceParser::ParsePublic)}, |
| 520 | {"public-group", std::mem_fn(&ResourceParser::ParsePublicGroup)}, |
| 521 | {"string-array", std::mem_fn(&ResourceParser::ParseStringArray)}, |
| 522 | {"style", std::bind(&ResourceParser::ParseStyle, std::placeholders::_1, ResourceType::kStyle, |
| 523 | std::placeholders::_2, std::placeholders::_3)}, |
| 524 | {"symbol", std::mem_fn(&ResourceParser::ParseSymbol)}, |
| 525 | }); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 526 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 527 | std::string resource_type = parser->element_name(); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 528 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 529 | // The value format accepted for this resource. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 530 | uint32_t resource_format = 0u; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 531 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 532 | bool can_be_item = true; |
| 533 | bool can_be_bag = true; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 534 | if (resource_type == "item") { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 535 | can_be_bag = false; |
| 536 | |
Adam Lesinski | e597d68 | 2017-06-01 17:16:44 -0700 | [diff] [blame] | 537 | // The default format for <item> is any. If a format attribute is present, that one will |
| 538 | // override the default. |
| 539 | resource_format = android::ResTable_map::TYPE_ANY; |
| 540 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 541 | // Items have their type encoded in the type attribute. |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 542 | if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 543 | resource_type = maybe_type.value().to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 544 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 545 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 546 | << "<item> must have a 'type' attribute"); |
| 547 | return false; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 550 | if (Maybe<StringPiece> maybe_format = xml::FindNonEmptyAttribute(parser, "format")) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 551 | // An explicit format for this resource was specified. The resource will |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 552 | // 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] | 553 | // overridden. |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 554 | resource_format = ParseFormatTypeNoEnumsOrFlags(maybe_format.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 555 | if (!resource_format) { |
| 556 | diag_->Error(DiagMessage(out_resource->source) |
| 557 | << "'" << maybe_format.value() |
| 558 | << "' is an invalid format"); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 559 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 560 | } |
| 561 | } |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 562 | } else if (resource_type == "bag") { |
| 563 | can_be_item = false; |
| 564 | |
| 565 | // Bags have their type encoded in the type attribute. |
| 566 | if (Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type")) { |
| 567 | resource_type = maybe_type.value().to_string(); |
| 568 | } else { |
| 569 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 570 | << "<bag> must have a 'type' attribute"); |
| 571 | return false; |
| 572 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | // Get the name of the resource. This will be checked later, because not all |
| 576 | // XML elements require a name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 577 | Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 578 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 579 | if (resource_type == "id") { |
| 580 | if (!maybe_name) { |
| 581 | diag_->Error(DiagMessage(out_resource->source) |
| 582 | << "<" << parser->element_name() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 583 | << "> missing 'name' attribute"); |
| 584 | return false; |
| 585 | } |
| 586 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 587 | out_resource->name.type = ResourceType::kId; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 588 | out_resource->name.entry = maybe_name.value().to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 589 | out_resource->value = util::make_unique<Id>(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 590 | return true; |
| 591 | } |
| 592 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 593 | if (can_be_item) { |
| 594 | const auto item_iter = elToItemMap.find(resource_type); |
| 595 | if (item_iter != elToItemMap.end()) { |
| 596 | // This is an item, record its type and format and start parsing. |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 597 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 598 | if (!maybe_name) { |
| 599 | diag_->Error(DiagMessage(out_resource->source) |
| 600 | << "<" << parser->element_name() << "> missing 'name' attribute"); |
| 601 | return false; |
| 602 | } |
| 603 | |
| 604 | out_resource->name.type = item_iter->second.type; |
| 605 | out_resource->name.entry = maybe_name.value().to_string(); |
| 606 | |
Adam Lesinski | e597d68 | 2017-06-01 17:16:44 -0700 | [diff] [blame] | 607 | // Only use the implied format of the type when there is no explicit format. |
| 608 | if (resource_format == 0u) { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 609 | resource_format = item_iter->second.format; |
| 610 | } |
| 611 | |
| 612 | if (!ParseItem(parser, out_resource, resource_format)) { |
| 613 | return false; |
| 614 | } |
| 615 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 616 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | // This might be a bag or something. |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 620 | if (can_be_bag) { |
| 621 | const auto bag_iter = elToBagMap.find(resource_type); |
| 622 | if (bag_iter != elToBagMap.end()) { |
| 623 | // Ensure we have a name (unless this is a <public-group>). |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 624 | if (resource_type != "public-group" && resource_type != "overlayable") { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 625 | if (!maybe_name) { |
| 626 | diag_->Error(DiagMessage(out_resource->source) |
| 627 | << "<" << parser->element_name() << "> missing 'name' attribute"); |
| 628 | return false; |
| 629 | } |
| 630 | |
| 631 | out_resource->name.entry = maybe_name.value().to_string(); |
| 632 | } |
| 633 | |
| 634 | // Call the associated parse method. The type will be filled in by the |
| 635 | // parse func. |
| 636 | if (!bag_iter->second(this, parser, out_resource)) { |
| 637 | return false; |
| 638 | } |
| 639 | return true; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | if (can_be_item) { |
| 644 | // Try parsing the elementName (or type) as a resource. These shall only be |
| 645 | // resources like 'layout' or 'xml' and they can only be references. |
| 646 | const ResourceType* parsed_type = ParseResourceType(resource_type); |
| 647 | if (parsed_type) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 648 | if (!maybe_name) { |
| 649 | diag_->Error(DiagMessage(out_resource->source) |
| 650 | << "<" << parser->element_name() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 651 | << "> missing 'name' attribute"); |
| 652 | return false; |
| 653 | } |
| 654 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 655 | out_resource->name.type = *parsed_type; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 656 | out_resource->name.entry = maybe_name.value().to_string(); |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 657 | out_resource->value = ParseXml(parser, android::ResTable_map::TYPE_REFERENCE, kNoRawString); |
| 658 | if (!out_resource->value) { |
| 659 | diag_->Error(DiagMessage(out_resource->source) |
| 660 | << "invalid value for type '" << *parsed_type << "'. Expected a reference"); |
| 661 | return false; |
| 662 | } |
| 663 | return true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 664 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 667 | diag_->Warn(DiagMessage(out_resource->source) |
| 668 | << "unknown resource type '" << parser->element_name() << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 669 | return false; |
| 670 | } |
| 671 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 672 | bool ResourceParser::ParseItem(xml::XmlPullParser* parser, |
| 673 | ParsedResource* out_resource, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 674 | const uint32_t format) { |
| 675 | if (format == android::ResTable_map::TYPE_STRING) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 676 | return ParseString(parser, out_resource); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 679 | out_resource->value = ParseXml(parser, format, kNoRawString); |
| 680 | if (!out_resource->value) { |
| 681 | diag_->Error(DiagMessage(out_resource->source) << "invalid " |
| 682 | << out_resource->name.type); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 683 | return false; |
| 684 | } |
| 685 | return true; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 686 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 687 | |
| 688 | /** |
| 689 | * Reads the entire XML subtree and attempts to parse it as some Item, |
| 690 | * with typeMask denoting which items it can be. If allowRawValue is |
| 691 | * true, a RawString is returned if the XML couldn't be parsed as |
| 692 | * an Item. If allowRawValue is false, nullptr is returned in this |
| 693 | * case. |
| 694 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 695 | std::unique_ptr<Item> ResourceParser::ParseXml(xml::XmlPullParser* parser, |
| 696 | const uint32_t type_mask, |
| 697 | const bool allow_raw_value) { |
| 698 | const size_t begin_xml_line = parser->line_number(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 699 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 700 | std::string raw_value; |
| 701 | StyleString style_string; |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 702 | std::vector<UntranslatableSection> untranslatable_sections; |
| 703 | if (!FlattenXmlSubtree(parser, &raw_value, &style_string, &untranslatable_sections)) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 704 | return {}; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 707 | if (!style_string.spans.empty()) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 708 | // This can only be a StyledString. |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 709 | std::unique_ptr<StyledString> styled_string = |
| 710 | util::make_unique<StyledString>(table_->string_pool.MakeRef( |
Adam Lesinski | 060b53d | 2017-07-28 17:10:35 -0700 | [diff] [blame] | 711 | style_string, StringPool::Context(StringPool::Context::kNormalPriority, config_))); |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 712 | styled_string->untranslatable_sections = std::move(untranslatable_sections); |
| 713 | return std::move(styled_string); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 716 | auto on_create_reference = [&](const ResourceName& name) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 717 | // name.package can be empty here, as it will assume the package name of the |
| 718 | // table. |
| 719 | std::unique_ptr<Id> id = util::make_unique<Id>(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 720 | id->SetSource(source_.WithLine(begin_xml_line)); |
| 721 | table_->AddResource(name, {}, {}, std::move(id), diag_); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 722 | }; |
| 723 | |
| 724 | // Process the raw value. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 725 | std::unique_ptr<Item> processed_item = |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 726 | ResourceUtils::TryParseItemForAttribute(raw_value, type_mask, on_create_reference); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 727 | if (processed_item) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 728 | // Fix up the reference. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 729 | if (Reference* ref = ValueCast<Reference>(processed_item.get())) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 730 | ResolvePackage(parser, ref); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 731 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 732 | return processed_item; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | // Try making a regular string. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 736 | if (type_mask & android::ResTable_map::TYPE_STRING) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 737 | // Use the trimmed, escaped string. |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 738 | std::unique_ptr<String> string = util::make_unique<String>( |
| 739 | table_->string_pool.MakeRef(style_string.str, StringPool::Context(config_))); |
| 740 | string->untranslatable_sections = std::move(untranslatable_sections); |
| 741 | return std::move(string); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Adam Lesinski | bab4ef5 | 2017-06-01 15:22:57 -0700 | [diff] [blame] | 744 | // If the text is empty, and the value is not allowed to be a string, encode it as a @null. |
| 745 | if (util::TrimWhitespace(raw_value).empty()) { |
| 746 | return ResourceUtils::MakeNull(); |
| 747 | } |
| 748 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 749 | if (allow_raw_value) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 750 | // We can't parse this so return a RawString if we are allowed. |
| 751 | return util::make_unique<RawString>( |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 752 | table_->string_pool.MakeRef(raw_value, StringPool::Context(config_))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 753 | } |
| 754 | return {}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 755 | } |
| 756 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 757 | bool ResourceParser::ParseString(xml::XmlPullParser* parser, |
| 758 | ParsedResource* out_resource) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 759 | bool formatted = true; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 760 | if (Maybe<StringPiece> formatted_attr = |
| 761 | xml::FindAttribute(parser, "formatted")) { |
| 762 | Maybe<bool> maybe_formatted = |
| 763 | ResourceUtils::ParseBool(formatted_attr.value()); |
| 764 | if (!maybe_formatted) { |
| 765 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 766 | << "invalid value for 'formatted'. Must be a boolean"); |
| 767 | return false; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 768 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 769 | formatted = maybe_formatted.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 770 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 771 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 772 | bool translatable = options_.translatable; |
| 773 | if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { |
| 774 | Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); |
| 775 | if (!maybe_translatable) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 776 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 777 | << "invalid value for 'translatable'. Must be a boolean"); |
| 778 | return false; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 779 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 780 | translatable = maybe_translatable.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 781 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 782 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 783 | out_resource->value = |
| 784 | ParseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString); |
| 785 | if (!out_resource->value) { |
| 786 | diag_->Error(DiagMessage(out_resource->source) << "not a valid string"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 787 | return false; |
| 788 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 789 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 790 | if (String* string_value = ValueCast<String>(out_resource->value.get())) { |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 791 | string_value->SetTranslatable(translatable); |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 792 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 793 | if (formatted && translatable) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 794 | if (!util::VerifyJavaStringFormat(*string_value->value)) { |
| 795 | DiagMessage msg(out_resource->source); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 796 | msg << "multiple substitutions specified in non-positional format; " |
| 797 | "did you mean to add the formatted=\"false\" attribute?"; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 798 | if (options_.error_on_positional_arguments) { |
| 799 | diag_->Error(msg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 800 | return false; |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 801 | } |
Adam Lesinski | 393b5f0 | 2015-12-17 13:03:11 -0800 | [diff] [blame] | 802 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 803 | diag_->Warn(msg); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 804 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 805 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 806 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 807 | } else if (StyledString* string_value = ValueCast<StyledString>(out_resource->value.get())) { |
| 808 | string_value->SetTranslatable(translatable); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 809 | } |
| 810 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 813 | bool ResourceParser::ParsePublic(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 814 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 815 | diag_->Warn(DiagMessage(out_resource->source) |
| 816 | << "ignoring configuration '" << out_resource->config << "' for <public> tag"); |
| 817 | } |
| 818 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 819 | Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); |
| 820 | if (!maybe_type) { |
| 821 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 822 | << "<public> must have a 'type' attribute"); |
| 823 | return false; |
| 824 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 825 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 826 | const ResourceType* parsed_type = ParseResourceType(maybe_type.value()); |
| 827 | if (!parsed_type) { |
| 828 | diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '" |
| 829 | << maybe_type.value() |
| 830 | << "' in <public>"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 831 | return false; |
| 832 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 833 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 834 | out_resource->name.type = *parsed_type; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 835 | |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 836 | if (Maybe<StringPiece> maybe_id_str = xml::FindNonEmptyAttribute(parser, "id")) { |
| 837 | Maybe<ResourceId> maybe_id = ResourceUtils::ParseResourceId(maybe_id_str.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 838 | if (!maybe_id) { |
Adam Lesinski | ceb9b2f | 2017-02-16 12:05:42 -0800 | [diff] [blame] | 839 | diag_->Error(DiagMessage(out_resource->source) |
| 840 | << "invalid resource ID '" << maybe_id_str.value() << "' in <public>"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 841 | return false; |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 842 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 843 | out_resource->id = maybe_id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 844 | } |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 845 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 846 | if (*parsed_type == ResourceType::kId) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 847 | // An ID marked as public is also the definition of an ID. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 848 | out_resource->value = util::make_unique<Id>(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 849 | } |
Adam Lesinski | bf0bd0f | 2016-06-01 15:31:50 -0700 | [diff] [blame] | 850 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 851 | out_resource->visibility_level = Visibility::Level::kPublic; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 852 | return true; |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 853 | } |
| 854 | |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 855 | bool ResourceParser::ParsePublicGroup(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 856 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 857 | diag_->Warn(DiagMessage(out_resource->source) |
| 858 | << "ignoring configuration '" << out_resource->config |
| 859 | << "' for <public-group> tag"); |
| 860 | } |
| 861 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 862 | Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); |
| 863 | if (!maybe_type) { |
| 864 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 865 | << "<public-group> must have a 'type' attribute"); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 866 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 867 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 868 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 869 | const ResourceType* parsed_type = ParseResourceType(maybe_type.value()); |
| 870 | if (!parsed_type) { |
| 871 | diag_->Error(DiagMessage(out_resource->source) << "invalid resource type '" |
| 872 | << maybe_type.value() |
| 873 | << "' in <public-group>"); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 874 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 877 | Maybe<StringPiece> maybe_id_str = |
| 878 | xml::FindNonEmptyAttribute(parser, "first-id"); |
| 879 | if (!maybe_id_str) { |
| 880 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 881 | << "<public-group> must have a 'first-id' attribute"); |
| 882 | return false; |
| 883 | } |
| 884 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 885 | Maybe<ResourceId> maybe_id = |
| 886 | ResourceUtils::ParseResourceId(maybe_id_str.value()); |
| 887 | if (!maybe_id) { |
| 888 | diag_->Error(DiagMessage(out_resource->source) << "invalid resource ID '" |
| 889 | << maybe_id_str.value() |
| 890 | << "' in <public-group>"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 891 | return false; |
| 892 | } |
| 893 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 894 | ResourceId next_id = maybe_id.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 895 | |
| 896 | std::string comment; |
| 897 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 898 | const size_t depth = parser->depth(); |
| 899 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 900 | if (parser->event() == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 901 | comment = util::TrimWhitespace(parser->comment()).to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 902 | continue; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 903 | } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 904 | // Skip text. |
| 905 | continue; |
| 906 | } |
| 907 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 908 | const Source item_source = source_.WithLine(parser->line_number()); |
| 909 | const std::string& element_namespace = parser->element_namespace(); |
| 910 | const std::string& element_name = parser->element_name(); |
| 911 | if (element_namespace.empty() && element_name == "public") { |
| 912 | Maybe<StringPiece> maybe_name = |
| 913 | xml::FindNonEmptyAttribute(parser, "name"); |
| 914 | if (!maybe_name) { |
| 915 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 916 | << "<public> must have a 'name' attribute"); |
| 917 | error = true; |
| 918 | continue; |
| 919 | } |
| 920 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 921 | if (xml::FindNonEmptyAttribute(parser, "id")) { |
| 922 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 923 | << "'id' is ignored within <public-group>"); |
| 924 | error = true; |
| 925 | continue; |
| 926 | } |
| 927 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 928 | if (xml::FindNonEmptyAttribute(parser, "type")) { |
| 929 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 930 | << "'type' is ignored within <public-group>"); |
| 931 | error = true; |
| 932 | continue; |
| 933 | } |
| 934 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 935 | ParsedResource child_resource; |
| 936 | child_resource.name.type = *parsed_type; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 937 | child_resource.name.entry = maybe_name.value().to_string(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 938 | child_resource.id = next_id; |
| 939 | child_resource.comment = std::move(comment); |
| 940 | child_resource.source = item_source; |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 941 | child_resource.visibility_level = Visibility::Level::kPublic; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 942 | out_resource->child_resources.push_back(std::move(child_resource)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 943 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 944 | next_id.id += 1; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 945 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 946 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 947 | diag_->Error(DiagMessage(item_source) << ":" << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 948 | error = true; |
| 949 | } |
| 950 | } |
| 951 | return !error; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 952 | } |
| 953 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 954 | bool ResourceParser::ParseSymbolImpl(xml::XmlPullParser* parser, |
| 955 | ParsedResource* out_resource) { |
| 956 | Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); |
| 957 | if (!maybe_type) { |
| 958 | diag_->Error(DiagMessage(out_resource->source) |
| 959 | << "<" << parser->element_name() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 960 | << "> must have a 'type' attribute"); |
| 961 | return false; |
| 962 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 963 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 964 | const ResourceType* parsed_type = ParseResourceType(maybe_type.value()); |
| 965 | if (!parsed_type) { |
| 966 | diag_->Error(DiagMessage(out_resource->source) |
| 967 | << "invalid resource type '" << maybe_type.value() << "' in <" |
| 968 | << parser->element_name() << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 969 | return false; |
| 970 | } |
| 971 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 972 | out_resource->name.type = *parsed_type; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 973 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 974 | } |
| 975 | |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 976 | bool ResourceParser::ParseSymbol(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 977 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 978 | diag_->Warn(DiagMessage(out_resource->source) |
| 979 | << "ignoring configuration '" << out_resource->config << "' for <" |
| 980 | << parser->element_name() << "> tag"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 981 | } |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 982 | |
| 983 | if (!ParseSymbolImpl(parser, out_resource)) { |
| 984 | return false; |
| 985 | } |
| 986 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 987 | out_resource->visibility_level = Visibility::Level::kPrivate; |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 988 | return true; |
| 989 | } |
| 990 | |
| 991 | bool ResourceParser::ParseOverlayable(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 992 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 993 | diag_->Warn(DiagMessage(out_resource->source) |
| 994 | << "ignoring configuration '" << out_resource->config << "' for <overlayable> tag"); |
| 995 | } |
| 996 | |
| 997 | if (Maybe<StringPiece> maybe_policy = xml::FindNonEmptyAttribute(parser, "policy")) { |
| 998 | const StringPiece& policy = maybe_policy.value(); |
| 999 | if (policy != "system") { |
| 1000 | diag_->Error(DiagMessage(out_resource->source) |
| 1001 | << "<overlayable> has invalid policy '" << policy << "'"); |
| 1002 | return false; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | bool error = false; |
| 1007 | const size_t depth = parser->depth(); |
| 1008 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1009 | if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
| 1010 | // Skip text/comments. |
| 1011 | continue; |
| 1012 | } |
| 1013 | |
| 1014 | const Source item_source = source_.WithLine(parser->line_number()); |
| 1015 | const std::string& element_namespace = parser->element_namespace(); |
| 1016 | const std::string& element_name = parser->element_name(); |
| 1017 | if (element_namespace.empty() && element_name == "item") { |
| 1018 | Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
| 1019 | if (!maybe_name) { |
| 1020 | diag_->Error(DiagMessage(item_source) |
| 1021 | << "<item> within an <overlayable> tag must have a 'name' attribute"); |
| 1022 | error = true; |
| 1023 | continue; |
| 1024 | } |
| 1025 | |
| 1026 | Maybe<StringPiece> maybe_type = xml::FindNonEmptyAttribute(parser, "type"); |
| 1027 | if (!maybe_type) { |
| 1028 | diag_->Error(DiagMessage(item_source) |
| 1029 | << "<item> within an <overlayable> tag must have a 'type' attribute"); |
| 1030 | error = true; |
| 1031 | continue; |
| 1032 | } |
| 1033 | |
| 1034 | const ResourceType* type = ParseResourceType(maybe_type.value()); |
| 1035 | if (type == nullptr) { |
| 1036 | diag_->Error(DiagMessage(out_resource->source) |
| 1037 | << "invalid resource type '" << maybe_type.value() |
| 1038 | << "' in <item> within an <overlayable>"); |
| 1039 | error = true; |
| 1040 | continue; |
| 1041 | } |
| 1042 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 1043 | ParsedResource child_resource; |
| 1044 | child_resource.name.type = *type; |
| 1045 | child_resource.name.entry = maybe_name.value().to_string(); |
| 1046 | child_resource.source = item_source; |
| 1047 | child_resource.overlayable = true; |
| 1048 | out_resource->child_resources.push_back(std::move(child_resource)); |
Adam Lesinski | 46c4d72 | 2017-08-23 13:03:56 -0700 | [diff] [blame] | 1049 | |
| 1050 | xml::XmlPullParser::SkipCurrentElement(parser); |
| 1051 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1052 | diag_->Error(DiagMessage(item_source) << ":" << element_name << ">"); |
| 1053 | error = true; |
| 1054 | } |
| 1055 | } |
| 1056 | return !error; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1057 | } |
| 1058 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 1059 | bool ResourceParser::ParseAddResource(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1060 | if (ParseSymbolImpl(parser, out_resource)) { |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 1061 | out_resource->visibility_level = Visibility::Level::kUndefined; |
Adam Lesinski | 4488f1c | 2017-05-26 17:33:38 -0700 | [diff] [blame] | 1062 | out_resource->allow_new = true; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1063 | return true; |
| 1064 | } |
| 1065 | return false; |
| 1066 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1067 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1068 | bool ResourceParser::ParseAttr(xml::XmlPullParser* parser, |
| 1069 | ParsedResource* out_resource) { |
| 1070 | return ParseAttrImpl(parser, out_resource, false); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1073 | bool ResourceParser::ParseAttrImpl(xml::XmlPullParser* parser, |
| 1074 | ParsedResource* out_resource, bool weak) { |
| 1075 | out_resource->name.type = ResourceType::kAttr; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1076 | |
| 1077 | // Attributes only end up in default configuration. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1078 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 1079 | diag_->Warn(DiagMessage(out_resource->source) |
| 1080 | << "ignoring configuration '" << out_resource->config |
| 1081 | << "' for attribute " << out_resource->name); |
| 1082 | out_resource->config = ConfigDescription::DefaultConfig(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1085 | uint32_t type_mask = 0; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1086 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1087 | Maybe<StringPiece> maybe_format = xml::FindAttribute(parser, "format"); |
| 1088 | if (maybe_format) { |
| 1089 | type_mask = ParseFormatAttribute(maybe_format.value()); |
| 1090 | if (type_mask == 0) { |
| 1091 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1092 | << "invalid attribute format '" << maybe_format.value() << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1093 | return false; |
| 1094 | } |
| 1095 | } |
| 1096 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1097 | Maybe<int32_t> maybe_min, maybe_max; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1098 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1099 | if (Maybe<StringPiece> maybe_min_str = xml::FindAttribute(parser, "min")) { |
| 1100 | StringPiece min_str = util::TrimWhitespace(maybe_min_str.value()); |
| 1101 | if (!min_str.empty()) { |
| 1102 | std::u16string min_str16 = util::Utf8ToUtf16(min_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1103 | android::Res_value value; |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1104 | if (android::ResTable::stringToInt(min_str16.data(), min_str16.size(), &value)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1105 | maybe_min = static_cast<int32_t>(value.data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1106 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1107 | } |
| 1108 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1109 | if (!maybe_min) { |
| 1110 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1111 | << "invalid 'min' value '" << min_str << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1112 | return false; |
| 1113 | } |
| 1114 | } |
| 1115 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1116 | if (Maybe<StringPiece> maybe_max_str = xml::FindAttribute(parser, "max")) { |
| 1117 | StringPiece max_str = util::TrimWhitespace(maybe_max_str.value()); |
| 1118 | if (!max_str.empty()) { |
| 1119 | std::u16string max_str16 = util::Utf8ToUtf16(max_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1120 | android::Res_value value; |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1121 | if (android::ResTable::stringToInt(max_str16.data(), max_str16.size(), &value)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1122 | maybe_max = static_cast<int32_t>(value.data); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1123 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1126 | if (!maybe_max) { |
| 1127 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1128 | << "invalid 'max' value '" << max_str << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1129 | return false; |
| 1130 | } |
| 1131 | } |
| 1132 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1133 | if ((maybe_min || maybe_max) && |
| 1134 | (type_mask & android::ResTable_map::TYPE_INTEGER) == 0) { |
| 1135 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1136 | << "'min' and 'max' can only be used when format='integer'"); |
| 1137 | return false; |
| 1138 | } |
| 1139 | |
| 1140 | struct SymbolComparator { |
Yi Kong | 087e2d4 | 2017-05-02 12:49:25 -0700 | [diff] [blame] | 1141 | bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1142 | return a.symbol.name.value() < b.symbol.name.value(); |
| 1143 | } |
| 1144 | }; |
| 1145 | |
| 1146 | std::set<Attribute::Symbol, SymbolComparator> items; |
| 1147 | |
| 1148 | std::string comment; |
| 1149 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1150 | const size_t depth = parser->depth(); |
| 1151 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1152 | if (parser->event() == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 1153 | comment = util::TrimWhitespace(parser->comment()).to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1154 | continue; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1155 | } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1156 | // Skip text. |
| 1157 | continue; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1158 | } |
| 1159 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1160 | const Source item_source = source_.WithLine(parser->line_number()); |
| 1161 | const std::string& element_namespace = parser->element_namespace(); |
| 1162 | const std::string& element_name = parser->element_name(); |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1163 | if (element_namespace.empty() && (element_name == "flag" || element_name == "enum")) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1164 | if (element_name == "enum") { |
| 1165 | if (type_mask & android::ResTable_map::TYPE_FLAGS) { |
| 1166 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1167 | << "can not define an <enum>; already defined a <flag>"); |
| 1168 | error = true; |
| 1169 | continue; |
| 1170 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1171 | type_mask |= android::ResTable_map::TYPE_ENUM; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1172 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1173 | } else if (element_name == "flag") { |
| 1174 | if (type_mask & android::ResTable_map::TYPE_ENUM) { |
| 1175 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1176 | << "can not define a <flag>; already defined an <enum>"); |
| 1177 | error = true; |
| 1178 | continue; |
| 1179 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1180 | type_mask |= android::ResTable_map::TYPE_FLAGS; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
| 1183 | if (Maybe<Attribute::Symbol> s = |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1184 | ParseEnumOrFlagItem(parser, element_name)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1185 | Attribute::Symbol& symbol = s.value(); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1186 | ParsedResource child_resource; |
| 1187 | child_resource.name = symbol.symbol.name.value(); |
| 1188 | child_resource.source = item_source; |
| 1189 | child_resource.value = util::make_unique<Id>(); |
| 1190 | out_resource->child_resources.push_back(std::move(child_resource)); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1191 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1192 | symbol.symbol.SetComment(std::move(comment)); |
| 1193 | symbol.symbol.SetSource(item_source); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1194 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1195 | auto insert_result = items.insert(std::move(symbol)); |
| 1196 | if (!insert_result.second) { |
| 1197 | const Attribute::Symbol& existing_symbol = *insert_result.first; |
| 1198 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1199 | << "duplicate symbol '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1200 | << existing_symbol.symbol.name.value().entry << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1201 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1202 | diag_->Note(DiagMessage(existing_symbol.symbol.GetSource()) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1203 | << "first defined here"); |
| 1204 | error = true; |
| 1205 | } |
| 1206 | } else { |
| 1207 | error = true; |
| 1208 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1209 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1210 | diag_->Error(DiagMessage(item_source) << ":" << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1211 | error = true; |
| 1212 | } |
| 1213 | |
| 1214 | comment = {}; |
| 1215 | } |
| 1216 | |
| 1217 | if (error) { |
| 1218 | return false; |
| 1219 | } |
| 1220 | |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1221 | std::unique_ptr<Attribute> attr = util::make_unique<Attribute>( |
| 1222 | type_mask ? type_mask : uint32_t{android::ResTable_map::TYPE_ANY}); |
| 1223 | attr->SetWeak(weak); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1224 | attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end()); |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1225 | attr->min_int = maybe_min.value_or_default(std::numeric_limits<int32_t>::min()); |
| 1226 | 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] | 1227 | out_resource->value = std::move(attr); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1228 | return true; |
| 1229 | } |
| 1230 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1231 | Maybe<Attribute::Symbol> ResourceParser::ParseEnumOrFlagItem( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1232 | xml::XmlPullParser* parser, const StringPiece& tag) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1233 | const Source source = source_.WithLine(parser->line_number()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1234 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1235 | Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
| 1236 | if (!maybe_name) { |
| 1237 | diag_->Error(DiagMessage(source) << "no attribute 'name' found for tag <" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1238 | << tag << ">"); |
| 1239 | return {}; |
| 1240 | } |
| 1241 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1242 | Maybe<StringPiece> maybe_value = xml::FindNonEmptyAttribute(parser, "value"); |
| 1243 | if (!maybe_value) { |
| 1244 | diag_->Error(DiagMessage(source) << "no attribute 'value' found for tag <" |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1245 | << tag << ">"); |
| 1246 | return {}; |
| 1247 | } |
| 1248 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1249 | std::u16string value16 = util::Utf8ToUtf16(maybe_value.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1250 | android::Res_value val; |
| 1251 | if (!android::ResTable::stringToInt(value16.data(), value16.size(), &val)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1252 | diag_->Error(DiagMessage(source) << "invalid value '" << maybe_value.value() |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1253 | << "' for <" << tag |
| 1254 | << ">; must be an integer"); |
| 1255 | return {}; |
| 1256 | } |
| 1257 | |
| 1258 | return Attribute::Symbol{ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1259 | Reference(ResourceNameRef({}, ResourceType::kId, maybe_name.value())), |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1260 | val.data}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1261 | } |
| 1262 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1263 | bool ResourceParser::ParseStyleItem(xml::XmlPullParser* parser, Style* style) { |
| 1264 | const Source source = source_.WithLine(parser->line_number()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1265 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1266 | Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
| 1267 | if (!maybe_name) { |
| 1268 | diag_->Error(DiagMessage(source) << "<item> must have a 'name' attribute"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1269 | return false; |
| 1270 | } |
| 1271 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1272 | Maybe<Reference> maybe_key = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1273 | if (!maybe_key) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1274 | diag_->Error(DiagMessage(source) << "invalid attribute name '" << maybe_name.value() << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1275 | return false; |
| 1276 | } |
| 1277 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1278 | ResolvePackage(parser, &maybe_key.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1279 | maybe_key.value().SetSource(source); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1280 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1281 | std::unique_ptr<Item> value = ParseXml(parser, 0, kAllowRawString); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1282 | if (!value) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1283 | diag_->Error(DiagMessage(source) << "could not parse style item"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1284 | return false; |
| 1285 | } |
| 1286 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1287 | 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] | 1288 | return true; |
| 1289 | } |
| 1290 | |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 1291 | bool ResourceParser::ParseStyle(const ResourceType type, xml::XmlPullParser* parser, |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1292 | ParsedResource* out_resource) { |
Adam Lesinski | 86d67df | 2017-01-31 13:47:27 -0800 | [diff] [blame] | 1293 | out_resource->name.type = type; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1294 | |
| 1295 | std::unique_ptr<Style> style = util::make_unique<Style>(); |
| 1296 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1297 | Maybe<StringPiece> maybe_parent = xml::FindAttribute(parser, "parent"); |
| 1298 | if (maybe_parent) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1299 | // 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] | 1300 | if (!maybe_parent.value().empty()) { |
| 1301 | std::string err_str; |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1302 | style->parent = ResourceUtils::ParseStyleParentReference(maybe_parent.value(), &err_str); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1303 | if (!style->parent) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1304 | diag_->Error(DiagMessage(out_resource->source) << err_str); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1305 | return false; |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1308 | // 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] | 1309 | // private if appropriate. |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1310 | ResolvePackage(parser, &style->parent.value()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1313 | } else { |
| 1314 | // No parent was specified, so try inferring it from the style name. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1315 | std::string style_name = out_resource->name.entry; |
| 1316 | size_t pos = style_name.find_last_of(u'.'); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1317 | if (pos != std::string::npos) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1318 | style->parent_inferred = true; |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1319 | style->parent = Reference(ResourceName({}, ResourceType::kStyle, style_name.substr(0, pos))); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1324 | const size_t depth = parser->depth(); |
| 1325 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1326 | if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1327 | // Skip text and comments. |
| 1328 | continue; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1329 | } |
| 1330 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1331 | const std::string& element_namespace = parser->element_namespace(); |
| 1332 | const std::string& element_name = parser->element_name(); |
| 1333 | if (element_namespace == "" && element_name == "item") { |
| 1334 | error |= !ParseStyleItem(parser, style.get()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1335 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1336 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1337 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1338 | << ":" << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1339 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1340 | } |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1341 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1342 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1343 | if (error) { |
| 1344 | return false; |
| 1345 | } |
| 1346 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1347 | out_resource->value = std::move(style); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1348 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 1351 | bool ResourceParser::ParseArray(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 1352 | uint32_t resource_format = android::ResTable_map::TYPE_ANY; |
| 1353 | if (Maybe<StringPiece> format_attr = xml::FindNonEmptyAttribute(parser, "format")) { |
| 1354 | resource_format = ParseFormatTypeNoEnumsOrFlags(format_attr.value()); |
| 1355 | if (resource_format == 0u) { |
| 1356 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1357 | << "'" << format_attr.value() << "' is an invalid format"); |
| 1358 | return false; |
| 1359 | } |
| 1360 | } |
| 1361 | return ParseArrayImpl(parser, out_resource, resource_format); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1362 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1363 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 1364 | bool ResourceParser::ParseIntegerArray(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 1365 | return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_INTEGER); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1366 | } |
| 1367 | |
Adam Lesinski | d5fd76a | 2017-05-16 12:18:08 -0700 | [diff] [blame] | 1368 | bool ResourceParser::ParseStringArray(xml::XmlPullParser* parser, ParsedResource* out_resource) { |
| 1369 | return ParseArrayImpl(parser, out_resource, android::ResTable_map::TYPE_STRING); |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1370 | } |
| 1371 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1372 | bool ResourceParser::ParseArrayImpl(xml::XmlPullParser* parser, |
| 1373 | ParsedResource* out_resource, |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1374 | const uint32_t typeMask) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1375 | out_resource->name.type = ResourceType::kArray; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1376 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1377 | std::unique_ptr<Array> array = util::make_unique<Array>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1378 | |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 1379 | bool translatable = options_.translatable; |
| 1380 | if (Maybe<StringPiece> translatable_attr = xml::FindAttribute(parser, "translatable")) { |
| 1381 | Maybe<bool> maybe_translatable = ResourceUtils::ParseBool(translatable_attr.value()); |
| 1382 | if (!maybe_translatable) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1383 | diag_->Error(DiagMessage(out_resource->source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1384 | << "invalid value for 'translatable'. Must be a boolean"); |
| 1385 | return false; |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 1386 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 1387 | translatable = maybe_translatable.value(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1388 | } |
Adam Lesinski | 7542162 | 2017-01-06 15:20:04 -0800 | [diff] [blame] | 1389 | array->SetTranslatable(translatable); |
Adam Lesinski | 458b877 | 2016-04-25 14:20:21 -0700 | [diff] [blame] | 1390 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1391 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1392 | const size_t depth = parser->depth(); |
| 1393 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1394 | if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1395 | // Skip text and comments. |
| 1396 | continue; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1399 | const Source item_source = source_.WithLine(parser->line_number()); |
| 1400 | const std::string& element_namespace = parser->element_namespace(); |
| 1401 | const std::string& element_name = parser->element_name(); |
| 1402 | if (element_namespace.empty() && element_name == "item") { |
| 1403 | std::unique_ptr<Item> item = ParseXml(parser, typeMask, kNoRawString); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1404 | if (!item) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1405 | diag_->Error(DiagMessage(item_source) << "could not parse array item"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1406 | error = true; |
| 1407 | continue; |
| 1408 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1409 | item->SetSource(item_source); |
Adam Lesinski | 4ffea04 | 2017-08-10 15:37:28 -0700 | [diff] [blame] | 1410 | array->elements.emplace_back(std::move(item)); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1411 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1412 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1413 | diag_->Error(DiagMessage(source_.WithLine(parser->line_number())) |
| 1414 | << "unknown tag <" << element_namespace << ":" |
| 1415 | << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1416 | error = true; |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | if (error) { |
| 1421 | return false; |
| 1422 | } |
| 1423 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1424 | out_resource->value = std::move(array); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1425 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1426 | } |
| 1427 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1428 | bool ResourceParser::ParsePlural(xml::XmlPullParser* parser, |
| 1429 | ParsedResource* out_resource) { |
| 1430 | out_resource->name.type = ResourceType::kPlurals; |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1431 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1432 | std::unique_ptr<Plural> plural = util::make_unique<Plural>(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1433 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1434 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1435 | const size_t depth = parser->depth(); |
| 1436 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1437 | if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1438 | // Skip text and comments. |
| 1439 | continue; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1440 | } |
| 1441 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1442 | const Source item_source = source_.WithLine(parser->line_number()); |
| 1443 | const std::string& element_namespace = parser->element_namespace(); |
| 1444 | const std::string& element_name = parser->element_name(); |
| 1445 | if (element_namespace.empty() && element_name == "item") { |
| 1446 | Maybe<StringPiece> maybe_quantity = |
| 1447 | xml::FindNonEmptyAttribute(parser, "quantity"); |
| 1448 | if (!maybe_quantity) { |
| 1449 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1450 | << "<item> in <plurals> requires attribute " |
| 1451 | << "'quantity'"); |
| 1452 | error = true; |
| 1453 | continue; |
| 1454 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1455 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1456 | StringPiece trimmed_quantity = |
| 1457 | util::TrimWhitespace(maybe_quantity.value()); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1458 | size_t index = 0; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1459 | if (trimmed_quantity == "zero") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1460 | index = Plural::Zero; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1461 | } else if (trimmed_quantity == "one") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1462 | index = Plural::One; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1463 | } else if (trimmed_quantity == "two") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1464 | index = Plural::Two; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1465 | } else if (trimmed_quantity == "few") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1466 | index = Plural::Few; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1467 | } else if (trimmed_quantity == "many") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1468 | index = Plural::Many; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1469 | } else if (trimmed_quantity == "other") { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1470 | index = Plural::Other; |
| 1471 | } else { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1472 | diag_->Error(DiagMessage(item_source) |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1473 | << "<item> in <plural> has invalid value '" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1474 | << trimmed_quantity << "' for attribute 'quantity'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1475 | error = true; |
| 1476 | continue; |
| 1477 | } |
| 1478 | |
| 1479 | if (plural->values[index]) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1480 | diag_->Error(DiagMessage(item_source) << "duplicate quantity '" |
| 1481 | << trimmed_quantity << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1482 | error = true; |
| 1483 | continue; |
| 1484 | } |
| 1485 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1486 | if (!(plural->values[index] = ParseXml( |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1487 | parser, android::ResTable_map::TYPE_STRING, kNoRawString))) { |
| 1488 | error = true; |
| 1489 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1490 | plural->values[index]->SetSource(item_source); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1491 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1492 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1493 | diag_->Error(DiagMessage(item_source) << "unknown tag <" |
| 1494 | << element_namespace << ":" |
| 1495 | << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1496 | error = true; |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | if (error) { |
| 1501 | return false; |
| 1502 | } |
| 1503 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1504 | out_resource->value = std::move(plural); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1505 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1506 | } |
| 1507 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1508 | bool ResourceParser::ParseDeclareStyleable(xml::XmlPullParser* parser, |
| 1509 | ParsedResource* out_resource) { |
| 1510 | out_resource->name.type = ResourceType::kStyleable; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1511 | |
Adam Lesinski | 71be705 | 2017-12-12 16:48:07 -0800 | [diff] [blame] | 1512 | // Declare-styleable is kPrivate by default, because it technically only exists in R.java. |
| 1513 | out_resource->visibility_level = Visibility::Level::kPublic; |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 1514 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1515 | // Declare-styleable only ends up in default config; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1516 | if (out_resource->config != ConfigDescription::DefaultConfig()) { |
| 1517 | diag_->Warn(DiagMessage(out_resource->source) |
| 1518 | << "ignoring configuration '" << out_resource->config |
| 1519 | << "' for styleable " << out_resource->name.entry); |
| 1520 | out_resource->config = ConfigDescription::DefaultConfig(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>(); |
| 1524 | |
| 1525 | std::string comment; |
| 1526 | bool error = false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1527 | const size_t depth = parser->depth(); |
| 1528 | while (xml::XmlPullParser::NextChildNode(parser, depth)) { |
| 1529 | if (parser->event() == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 1530 | comment = util::TrimWhitespace(parser->comment()).to_string(); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1531 | continue; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1532 | } else if (parser->event() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1533 | // Ignore text. |
| 1534 | continue; |
Adam Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 1535 | } |
| 1536 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1537 | const Source item_source = source_.WithLine(parser->line_number()); |
| 1538 | const std::string& element_namespace = parser->element_namespace(); |
| 1539 | const std::string& element_name = parser->element_name(); |
| 1540 | if (element_namespace.empty() && element_name == "attr") { |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1541 | Maybe<StringPiece> maybe_name = xml::FindNonEmptyAttribute(parser, "name"); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1542 | if (!maybe_name) { |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1543 | diag_->Error(DiagMessage(item_source) << "<attr> tag must have a 'name' attribute"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1544 | error = true; |
| 1545 | continue; |
| 1546 | } |
Adam Lesinski | 7ff3ee1 | 2015-12-14 16:08:50 -0800 | [diff] [blame] | 1547 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1548 | // If this is a declaration, the package name may be in the name. Separate |
| 1549 | // these out. |
| 1550 | // Eg. <attr name="android:text" /> |
Adam Lesinski | 73bff1e | 2017-12-08 16:06:10 -0800 | [diff] [blame] | 1551 | Maybe<Reference> maybe_ref = ResourceUtils::ParseXmlAttributeName(maybe_name.value()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1552 | if (!maybe_ref) { |
| 1553 | diag_->Error(DiagMessage(item_source) << "<attr> tag has invalid name '" |
| 1554 | << maybe_name.value() << "'"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1555 | error = true; |
| 1556 | continue; |
| 1557 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1558 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1559 | Reference& child_ref = maybe_ref.value(); |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 1560 | xml::ResolvePackage(parser, &child_ref); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1561 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1562 | // Create the ParsedResource that will add the attribute to the table. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1563 | ParsedResource child_resource; |
| 1564 | child_resource.name = child_ref.name.value(); |
| 1565 | child_resource.source = item_source; |
| 1566 | child_resource.comment = std::move(comment); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1567 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1568 | if (!ParseAttrImpl(parser, &child_resource, true)) { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1569 | error = true; |
| 1570 | continue; |
| 1571 | } |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1572 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1573 | // Create the reference to this attribute. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1574 | child_ref.SetComment(child_resource.comment); |
| 1575 | child_ref.SetSource(item_source); |
| 1576 | styleable->entries.push_back(std::move(child_ref)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1577 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1578 | out_resource->child_resources.push_back(std::move(child_resource)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1579 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1580 | } else if (!ShouldIgnoreElement(element_namespace, element_name)) { |
| 1581 | diag_->Error(DiagMessage(item_source) << "unknown tag <" |
| 1582 | << element_namespace << ":" |
| 1583 | << element_name << ">"); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1584 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1587 | comment = {}; |
| 1588 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1589 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1590 | if (error) { |
| 1591 | return false; |
| 1592 | } |
| 1593 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 1594 | out_resource->value = std::move(styleable); |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1595 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1596 | } |
| 1597 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 1598 | } // namespace aapt |