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 | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 18 | #include "ResourceTable.h" |
| 19 | #include "ResourceUtils.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include "ResourceValues.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 21 | #include "ValueVisitor.h" |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 22 | #include "util/Util.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 23 | #include "xml/XmlPullParser.h" |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 24 | |
Adam Lesinski | 769de98 | 2015-04-10 19:43:55 -0700 | [diff] [blame] | 25 | #include <sstream> |
| 26 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 27 | namespace aapt { |
| 28 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 29 | constexpr const char16_t* sXliffNamespaceUri = u"urn:oasis:names:tc:xliff:document:1.2"; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 30 | |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Returns true if the element is <skip> or <eat-comment> and can be safely ignored. |
| 33 | */ |
| 34 | static bool shouldIgnoreElement(const StringPiece16& ns, const StringPiece16& name) { |
| 35 | return ns.empty() && (name == u"skip" || name == u"eat-comment"); |
| 36 | } |
| 37 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 38 | ResourceParser::ResourceParser(IDiagnostics* diag, ResourceTable* table, const Source& source, |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 39 | const ConfigDescription& config, |
| 40 | const ResourceParserOptions& options) : |
| 41 | mDiag(diag), mTable(table), mSource(source), mConfig(config), mOptions(options) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Build a string from XML that converts nested elements into Span objects. |
| 46 | */ |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 47 | bool ResourceParser::flattenXmlSubtree(xml::XmlPullParser* parser, std::u16string* outRawString, |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | StyleString* outStyleString) { |
| 49 | std::vector<Span> spanStack; |
| 50 | |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 51 | bool error = false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 52 | outRawString->clear(); |
| 53 | outStyleString->spans.clear(); |
| 54 | util::StringBuilder builder; |
| 55 | size_t depth = 1; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 56 | while (xml::XmlPullParser::isGoodEvent(parser->next())) { |
| 57 | const xml::XmlPullParser::Event event = parser->getEvent(); |
| 58 | if (event == xml::XmlPullParser::Event::kEndElement) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 59 | if (!parser->getElementNamespace().empty()) { |
| 60 | // We already warned and skipped the start element, so just skip here too |
| 61 | continue; |
| 62 | } |
| 63 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 64 | depth--; |
| 65 | if (depth == 0) { |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | spanStack.back().lastChar = builder.str().size(); |
| 70 | outStyleString->spans.push_back(spanStack.back()); |
| 71 | spanStack.pop_back(); |
| 72 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 73 | } else if (event == xml::XmlPullParser::Event::kText) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | outRawString->append(parser->getText()); |
| 75 | builder.append(parser->getText()); |
| 76 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 77 | } else if (event == xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 78 | if (!parser->getElementNamespace().empty()) { |
| 79 | if (parser->getElementNamespace() != sXliffNamespaceUri) { |
| 80 | // Only warn if this isn't an xliff namespace. |
| 81 | mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 82 | << "skipping element '" |
| 83 | << parser->getElementName() |
| 84 | << "' with unknown namespace '" |
| 85 | << parser->getElementNamespace() |
| 86 | << "'"); |
| 87 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 88 | continue; |
| 89 | } |
| 90 | depth++; |
| 91 | |
| 92 | // Build a span object out of the nested element. |
| 93 | std::u16string spanName = parser->getElementName(); |
| 94 | const auto endAttrIter = parser->endAttributes(); |
| 95 | for (auto attrIter = parser->beginAttributes(); attrIter != endAttrIter; ++attrIter) { |
| 96 | spanName += u";"; |
| 97 | spanName += attrIter->name; |
| 98 | spanName += u"="; |
| 99 | spanName += attrIter->value; |
| 100 | } |
| 101 | |
| 102 | if (builder.str().size() > std::numeric_limits<uint32_t>::max()) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 103 | mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 104 | << "style string '" << builder.str() << "' is too long"); |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 105 | error = true; |
| 106 | } else { |
| 107 | spanStack.push_back(Span{ spanName, static_cast<uint32_t>(builder.str().size()) }); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 108 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 109 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 110 | } else if (event == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 111 | // Skip |
| 112 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 113 | assert(false); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | assert(spanStack.empty() && "spans haven't been fully processed"); |
| 117 | |
| 118 | outStyleString->str = builder.str(); |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 119 | return !error; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 122 | bool ResourceParser::parse(xml::XmlPullParser* parser) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 123 | bool error = false; |
| 124 | const size_t depth = parser->getDepth(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 125 | while (xml::XmlPullParser::nextChildNode(parser, depth)) { |
| 126 | if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 127 | // Skip comments and text. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 128 | continue; |
| 129 | } |
| 130 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 131 | if (!parser->getElementNamespace().empty() || parser->getElementName() != u"resources") { |
| 132 | mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 133 | << "root element must be <resources>"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 134 | return false; |
| 135 | } |
| 136 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 137 | error |= !parseResources(parser); |
| 138 | break; |
| 139 | }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 140 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 141 | if (parser->getEvent() == xml::XmlPullParser::Event::kBadDocument) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 142 | mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 143 | << "xml parser error: " << parser->getLastError()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 144 | return false; |
| 145 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 146 | return !error; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 149 | static bool shouldStripResource(const xml::XmlPullParser* parser, |
| 150 | const Maybe<std::u16string> productToMatch) { |
| 151 | assert(parser->getEvent() == xml::XmlPullParser::Event::kStartElement); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 152 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 153 | if (Maybe<StringPiece16> maybeProduct = xml::findNonEmptyAttribute(parser, u"product")) { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 154 | if (!productToMatch) { |
| 155 | if (maybeProduct.value() != u"default" && maybeProduct.value() != u"phone") { |
| 156 | // We didn't specify a product and this is not a default product, so skip. |
| 157 | return true; |
| 158 | } |
| 159 | } else { |
| 160 | if (productToMatch && maybeProduct.value() != productToMatch.value()) { |
| 161 | // We specified a product, but they don't match. |
| 162 | return true; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * A parsed resource ready to be added to the ResourceTable. |
| 171 | */ |
| 172 | struct ParsedResource { |
| 173 | ResourceName name; |
| 174 | Source source; |
| 175 | ResourceId id; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame^] | 176 | Maybe<SymbolState> symbolState; |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 177 | std::u16string comment; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 178 | std::unique_ptr<Value> value; |
| 179 | std::list<ParsedResource> childResources; |
| 180 | }; |
| 181 | |
| 182 | // Recursively adds resources to the ResourceTable. |
| 183 | static bool addResourcesToTable(ResourceTable* table, const ConfigDescription& config, |
| 184 | IDiagnostics* diag, ParsedResource* res) { |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame^] | 185 | if (res->symbolState) { |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 186 | Symbol symbol; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame^] | 187 | symbol.state = res->symbolState.value(); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 188 | symbol.source = res->source; |
| 189 | symbol.comment = res->comment; |
| 190 | if (!table->setSymbolState(res->name, res->id, symbol, diag)) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 191 | return false; |
| 192 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 195 | if (res->value) { |
| 196 | // Attach the comment, source and config to the value. |
| 197 | res->value->setComment(std::move(res->comment)); |
| 198 | res->value->setSource(std::move(res->source)); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 199 | |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 200 | if (!table->addResource(res->name, res->id, config, std::move(res->value), diag)) { |
| 201 | return false; |
| 202 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | bool error = false; |
| 206 | for (ParsedResource& child : res->childResources) { |
| 207 | error |= !addResourcesToTable(table, config, diag, &child); |
| 208 | } |
| 209 | return !error; |
| 210 | } |
| 211 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 212 | bool ResourceParser::parseResources(xml::XmlPullParser* parser) { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 213 | std::set<ResourceName> strippedResources; |
| 214 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 215 | bool error = false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 216 | std::u16string comment; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 217 | const size_t depth = parser->getDepth(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 218 | while (xml::XmlPullParser::nextChildNode(parser, depth)) { |
| 219 | const xml::XmlPullParser::Event event = parser->getEvent(); |
| 220 | if (event == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 221 | comment = parser->getComment(); |
| 222 | continue; |
| 223 | } |
| 224 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 225 | if (event == xml::XmlPullParser::Event::kText) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 226 | if (!util::trimWhitespace(parser->getText()).empty()) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 227 | mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 228 | << "plain text not allowed here"); |
| 229 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 230 | } |
| 231 | continue; |
| 232 | } |
| 233 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 234 | assert(event == xml::XmlPullParser::Event::kStartElement); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 235 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 236 | if (!parser->getElementNamespace().empty()) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 237 | // Skip unknown namespace. |
| 238 | continue; |
| 239 | } |
| 240 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 241 | std::u16string elementName = parser->getElementName(); |
| 242 | if (elementName == u"skip" || elementName == u"eat-comment") { |
| 243 | comment = u""; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 244 | continue; |
| 245 | } |
| 246 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 247 | if (elementName == u"item") { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 248 | // Items simply have their type encoded in the type attribute. |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 249 | if (Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type")) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 250 | elementName = maybeType.value().toString(); |
| 251 | } else { |
| 252 | mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 253 | << "<item> must have a 'type' attribute"); |
| 254 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 255 | continue; |
| 256 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 259 | ParsedResource parsedResource; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 260 | parsedResource.source = mSource.withLine(parser->getLineNumber()); |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 261 | parsedResource.comment = std::move(comment); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 262 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 263 | if (Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name")) { |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 264 | parsedResource.name.entry = maybeName.value().toString(); |
| 265 | |
| 266 | } else if (elementName != u"public-group") { |
| 267 | mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 268 | << "<" << elementName << "> tag must have a 'name' attribute"); |
| 269 | error = true; |
| 270 | continue; |
| 271 | } |
| 272 | |
| 273 | // Check if we should skip this product. |
| 274 | const bool stripResource = shouldStripResource(parser, mOptions.product); |
| 275 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 276 | bool result = true; |
| 277 | if (elementName == u"id") { |
| 278 | parsedResource.name.type = ResourceType::kId; |
| 279 | parsedResource.value = util::make_unique<Id>(); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 280 | } else if (elementName == u"string") { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 281 | parsedResource.name.type = ResourceType::kString; |
| 282 | result = parseString(parser, &parsedResource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 283 | } else if (elementName == u"color") { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 284 | parsedResource.name.type = ResourceType::kColor; |
| 285 | result = parseColor(parser, &parsedResource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 286 | } else if (elementName == u"drawable") { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 287 | parsedResource.name.type = ResourceType::kDrawable; |
| 288 | result = parseColor(parser, &parsedResource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 289 | } else if (elementName == u"bool") { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 290 | parsedResource.name.type = ResourceType::kBool; |
| 291 | result = parsePrimitive(parser, &parsedResource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 292 | } else if (elementName == u"integer") { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 293 | parsedResource.name.type = ResourceType::kInteger; |
| 294 | result = parsePrimitive(parser, &parsedResource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 295 | } else if (elementName == u"dimen") { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 296 | parsedResource.name.type = ResourceType::kDimen; |
| 297 | result = parsePrimitive(parser, &parsedResource); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 298 | } else if (elementName == u"fraction") { |
| 299 | parsedResource.name.type = ResourceType::kFraction; |
| 300 | result = parsePrimitive(parser, &parsedResource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 301 | } else if (elementName == u"style") { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 302 | parsedResource.name.type = ResourceType::kStyle; |
| 303 | result = parseStyle(parser, &parsedResource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 304 | } else if (elementName == u"plurals") { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 305 | parsedResource.name.type = ResourceType::kPlurals; |
| 306 | result = parsePlural(parser, &parsedResource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 307 | } else if (elementName == u"array") { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 308 | parsedResource.name.type = ResourceType::kArray; |
| 309 | result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_ANY); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 310 | } else if (elementName == u"string-array") { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 311 | parsedResource.name.type = ResourceType::kArray; |
| 312 | result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_STRING); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 313 | } else if (elementName == u"integer-array") { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 314 | parsedResource.name.type = ResourceType::kArray; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 315 | result = parseArray(parser, &parsedResource, android::ResTable_map::TYPE_INTEGER); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 316 | } else if (elementName == u"declare-styleable") { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 317 | parsedResource.name.type = ResourceType::kStyleable; |
| 318 | result = parseDeclareStyleable(parser, &parsedResource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 319 | } else if (elementName == u"attr") { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 320 | parsedResource.name.type = ResourceType::kAttr; |
| 321 | result = parseAttr(parser, &parsedResource); |
| 322 | } else if (elementName == u"public") { |
| 323 | result = parsePublic(parser, &parsedResource); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 324 | } else if (elementName == u"java-symbol" || elementName == u"symbol") { |
| 325 | result = parseSymbol(parser, &parsedResource); |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 326 | } else if (elementName == u"public-group") { |
| 327 | result = parsePublicGroup(parser, &parsedResource); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame^] | 328 | } else if (elementName == u"add-resource") { |
| 329 | result = parseAddResource(parser, &parsedResource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 330 | } else { |
Adam Lesinski | fa10505 | 2015-11-07 13:34:39 -0800 | [diff] [blame] | 331 | // Try parsing the elementName (or type) as a resource. These shall only be |
| 332 | // resources like 'layout' or 'xml' and they can only be references. |
| 333 | if (const ResourceType* type = parseResourceType(elementName)) { |
| 334 | parsedResource.name.type = *type; |
| 335 | parsedResource.value = parseXml(parser, android::ResTable_map::TYPE_REFERENCE, |
| 336 | false); |
| 337 | if (!parsedResource.value) { |
| 338 | mDiag->error(DiagMessage(parsedResource.source) << "invalid value for type '" |
| 339 | << *type << "'. Expected a reference"); |
| 340 | result = false; |
| 341 | } |
| 342 | } else { |
| 343 | mDiag->warn(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 344 | << "unknown resource type '" << elementName << "'"); |
| 345 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 346 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 347 | |
| 348 | if (result) { |
| 349 | // We successfully parsed the resource. |
| 350 | |
| 351 | if (stripResource) { |
| 352 | // Record that we stripped out this resource name. |
| 353 | // We will check that at least one variant of this resource was included. |
| 354 | strippedResources.insert(parsedResource.name); |
| 355 | } else { |
| 356 | error |= !addResourcesToTable(mTable, mConfig, mDiag, &parsedResource); |
| 357 | } |
| 358 | } else { |
| 359 | error = true; |
| 360 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 361 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 362 | |
| 363 | // Check that we included at least one variant of each stripped resource. |
| 364 | for (const ResourceName& strippedResource : strippedResources) { |
| 365 | if (!mTable->findResource(strippedResource)) { |
| 366 | // Failed to find the resource. |
| 367 | mDiag->error(DiagMessage(mSource) << "resource '" << strippedResource << "' " |
| 368 | "was filtered out but no product variant remains"); |
| 369 | error = true; |
| 370 | } |
| 371 | } |
| 372 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 373 | return !error; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 376 | enum { |
| 377 | kAllowRawString = true, |
| 378 | kNoRawString = false |
| 379 | }; |
| 380 | |
| 381 | /** |
| 382 | * Reads the entire XML subtree and attempts to parse it as some Item, |
| 383 | * with typeMask denoting which items it can be. If allowRawValue is |
| 384 | * true, a RawString is returned if the XML couldn't be parsed as |
| 385 | * an Item. If allowRawValue is false, nullptr is returned in this |
| 386 | * case. |
| 387 | */ |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 388 | std::unique_ptr<Item> ResourceParser::parseXml(xml::XmlPullParser* parser, const uint32_t typeMask, |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 389 | const bool allowRawValue) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 390 | const size_t beginXmlLine = parser->getLineNumber(); |
| 391 | |
| 392 | std::u16string rawValue; |
| 393 | StyleString styleString; |
| 394 | if (!flattenXmlSubtree(parser, &rawValue, &styleString)) { |
| 395 | return {}; |
| 396 | } |
| 397 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 398 | if (!styleString.spans.empty()) { |
| 399 | // This can only be a StyledString. |
| 400 | return util::make_unique<StyledString>( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 401 | mTable->stringPool.makeRef(styleString, StringPool::Context{ 1, mConfig })); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | auto onCreateReference = [&](const ResourceName& name) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 405 | // name.package can be empty here, as it will assume the package name of the table. |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 406 | std::unique_ptr<Id> id = util::make_unique<Id>(); |
| 407 | id->setSource(mSource.withLine(beginXmlLine)); |
| 408 | mTable->addResource(name, {}, std::move(id), mDiag); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 409 | }; |
| 410 | |
| 411 | // Process the raw value. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 412 | std::unique_ptr<Item> processedItem = ResourceUtils::parseItemForAttribute(rawValue, typeMask, |
| 413 | onCreateReference); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 414 | if (processedItem) { |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 415 | // Fix up the reference. |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 416 | if (Reference* ref = valueCast<Reference>(processedItem.get())) { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 417 | transformReferenceFromNamespace(parser, u"", ref); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 418 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 419 | return processedItem; |
| 420 | } |
| 421 | |
| 422 | // Try making a regular string. |
| 423 | if (typeMask & android::ResTable_map::TYPE_STRING) { |
| 424 | // Use the trimmed, escaped string. |
| 425 | return util::make_unique<String>( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 426 | mTable->stringPool.makeRef(styleString.str, StringPool::Context{ 1, mConfig })); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 427 | } |
| 428 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 429 | if (allowRawValue) { |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 430 | // We can't parse this so return a RawString if we are allowed. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 431 | return util::make_unique<RawString>( |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 432 | mTable->stringPool.makeRef(rawValue, StringPool::Context{ 1, mConfig })); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 433 | } |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 434 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 435 | return {}; |
| 436 | } |
| 437 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 438 | bool ResourceParser::parseString(xml::XmlPullParser* parser, ParsedResource* outResource) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 439 | const Source source = mSource.withLine(parser->getLineNumber()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 440 | |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 441 | bool formatted = true; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 442 | if (Maybe<StringPiece16> formattedAttr = xml::findAttribute(parser, u"formatted")) { |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 443 | if (!ResourceUtils::tryParseBool(formattedAttr.value(), &formatted)) { |
| 444 | mDiag->error(DiagMessage(source) << "invalid value for 'formatted'. Must be a boolean"); |
| 445 | return false; |
| 446 | } |
| 447 | } |
| 448 | |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 449 | bool translateable = mOptions.translatable; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 450 | if (Maybe<StringPiece16> translateableAttr = xml::findAttribute(parser, u"translatable")) { |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 451 | if (!ResourceUtils::tryParseBool(translateableAttr.value(), &translateable)) { |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 452 | mDiag->error(DiagMessage(source) |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 453 | << "invalid value for 'translatable'. Must be a boolean"); |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 454 | return false; |
| 455 | } |
| 456 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 457 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 458 | outResource->value = parseXml(parser, android::ResTable_map::TYPE_STRING, kNoRawString); |
| 459 | if (!outResource->value) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 460 | mDiag->error(DiagMessage(source) << "not a valid string"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 461 | return false; |
| 462 | } |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 463 | |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 464 | if (formatted && translateable) { |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 465 | if (String* stringValue = valueCast<String>(outResource->value.get())) { |
| 466 | if (!util::verifyJavaStringFormat(*stringValue->value)) { |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 467 | mDiag->error(DiagMessage(source) |
Adam Lesinski | b23f1e0 | 2015-11-03 12:24:17 -0800 | [diff] [blame] | 468 | << "multiple substitutions specified in non-positional format; " |
| 469 | "did you mean to add the formatted=\"false\" attribute?"); |
| 470 | return false; |
| 471 | } |
| 472 | } |
| 473 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 474 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 475 | } |
| 476 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 477 | bool ResourceParser::parseColor(xml::XmlPullParser* parser, ParsedResource* outResource) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 478 | const Source source = mSource.withLine(parser->getLineNumber()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 479 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 480 | outResource->value = parseXml(parser, android::ResTable_map::TYPE_COLOR, kNoRawString); |
| 481 | if (!outResource->value) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 482 | mDiag->error(DiagMessage(source) << "invalid color"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 483 | return false; |
| 484 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 485 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 488 | bool ResourceParser::parsePrimitive(xml::XmlPullParser* parser, ParsedResource* outResource) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 489 | const Source source = mSource.withLine(parser->getLineNumber()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 490 | |
| 491 | uint32_t typeMask = 0; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 492 | switch (outResource->name.type) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 493 | case ResourceType::kInteger: |
| 494 | typeMask |= android::ResTable_map::TYPE_INTEGER; |
| 495 | break; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 496 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 497 | case ResourceType::kFraction: |
| 498 | // fallthrough |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 499 | case ResourceType::kDimen: |
| 500 | typeMask |= android::ResTable_map::TYPE_DIMENSION |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 501 | | android::ResTable_map::TYPE_FLOAT |
| 502 | | android::ResTable_map::TYPE_FRACTION; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 503 | break; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 504 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 505 | case ResourceType::kBool: |
| 506 | typeMask |= android::ResTable_map::TYPE_BOOLEAN; |
| 507 | break; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 508 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 509 | default: |
| 510 | assert(false); |
| 511 | break; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 514 | outResource->value = parseXml(parser, typeMask, kNoRawString); |
| 515 | if (!outResource->value) { |
| 516 | mDiag->error(DiagMessage(source) << "invalid " << outResource->name.type); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 517 | return false; |
| 518 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 519 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 522 | bool ResourceParser::parsePublic(xml::XmlPullParser* parser, ParsedResource* outResource) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 523 | const Source source = mSource.withLine(parser->getLineNumber()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 524 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 525 | Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 526 | if (!maybeType) { |
| 527 | mDiag->error(DiagMessage(source) << "<public> must have a 'type' attribute"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 528 | return false; |
| 529 | } |
| 530 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 531 | const ResourceType* parsedType = parseResourceType(maybeType.value()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 532 | if (!parsedType) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 533 | mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value() |
| 534 | << "' in <public>"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 535 | return false; |
| 536 | } |
| 537 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 538 | outResource->name.type = *parsedType; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 539 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 540 | if (Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"id")) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 541 | android::Res_value val; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 542 | bool result = android::ResTable::stringToInt(maybeId.value().data(), |
| 543 | maybeId.value().size(), &val); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 544 | ResourceId resourceId(val.data); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 545 | if (!result || !resourceId.isValid()) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 546 | mDiag->error(DiagMessage(source) << "invalid resource ID '" << maybeId.value() |
| 547 | << "' in <public>"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 548 | return false; |
| 549 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 550 | outResource->id = resourceId; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | if (*parsedType == ResourceType::kId) { |
| 554 | // An ID marked as public is also the definition of an ID. |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 555 | outResource->value = util::make_unique<Id>(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 556 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 557 | |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 558 | outResource->symbolState = SymbolState::kPublic; |
| 559 | return true; |
| 560 | } |
| 561 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 562 | bool ResourceParser::parsePublicGroup(xml::XmlPullParser* parser, ParsedResource* outResource) { |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 563 | const Source source = mSource.withLine(parser->getLineNumber()); |
| 564 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 565 | Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type"); |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 566 | if (!maybeType) { |
| 567 | mDiag->error(DiagMessage(source) << "<public-group> must have a 'type' attribute"); |
| 568 | return false; |
| 569 | } |
| 570 | |
| 571 | const ResourceType* parsedType = parseResourceType(maybeType.value()); |
| 572 | if (!parsedType) { |
| 573 | mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value() |
| 574 | << "' in <public-group>"); |
| 575 | return false; |
| 576 | } |
| 577 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 578 | Maybe<StringPiece16> maybeId = xml::findNonEmptyAttribute(parser, u"first-id"); |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 579 | if (!maybeId) { |
| 580 | mDiag->error(DiagMessage(source) << "<public-group> must have a 'first-id' attribute"); |
| 581 | return false; |
| 582 | } |
| 583 | |
| 584 | android::Res_value val; |
| 585 | bool result = android::ResTable::stringToInt(maybeId.value().data(), |
| 586 | maybeId.value().size(), &val); |
| 587 | ResourceId nextId(val.data); |
| 588 | if (!result || !nextId.isValid()) { |
| 589 | mDiag->error(DiagMessage(source) << "invalid resource ID '" << maybeId.value() |
| 590 | << "' in <public-group>"); |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | std::u16string comment; |
| 595 | bool error = false; |
| 596 | const size_t depth = parser->getDepth(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 597 | while (xml::XmlPullParser::nextChildNode(parser, depth)) { |
| 598 | if (parser->getEvent() == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 599 | comment = util::trimWhitespace(parser->getComment()).toString(); |
| 600 | continue; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 601 | } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 602 | // Skip text. |
| 603 | continue; |
| 604 | } |
| 605 | |
| 606 | const Source itemSource = mSource.withLine(parser->getLineNumber()); |
| 607 | const std::u16string& elementNamespace = parser->getElementNamespace(); |
| 608 | const std::u16string& elementName = parser->getElementName(); |
| 609 | if (elementNamespace.empty() && elementName == u"public") { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 610 | Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name"); |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 611 | if (!maybeName) { |
| 612 | mDiag->error(DiagMessage(itemSource) << "<public> must have a 'name' attribute"); |
| 613 | error = true; |
| 614 | continue; |
| 615 | } |
| 616 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 617 | if (xml::findNonEmptyAttribute(parser, u"id")) { |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 618 | mDiag->error(DiagMessage(itemSource) << "'id' is ignored within <public-group>"); |
| 619 | error = true; |
| 620 | continue; |
| 621 | } |
| 622 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 623 | if (xml::findNonEmptyAttribute(parser, u"type")) { |
Adam Lesinski | 27afb9e | 2015-11-06 18:25:04 -0800 | [diff] [blame] | 624 | mDiag->error(DiagMessage(itemSource) << "'type' is ignored within <public-group>"); |
| 625 | error = true; |
| 626 | continue; |
| 627 | } |
| 628 | |
| 629 | ParsedResource childResource; |
| 630 | childResource.name.type = *parsedType; |
| 631 | childResource.name.entry = maybeName.value().toString(); |
| 632 | childResource.id = nextId; |
| 633 | childResource.comment = std::move(comment); |
| 634 | childResource.source = itemSource; |
| 635 | childResource.symbolState = SymbolState::kPublic; |
| 636 | outResource->childResources.push_back(std::move(childResource)); |
| 637 | |
| 638 | nextId.id += 1; |
| 639 | |
| 640 | } else if (!shouldIgnoreElement(elementNamespace, elementName)) { |
| 641 | mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">"); |
| 642 | error = true; |
| 643 | } |
| 644 | } |
| 645 | return !error; |
| 646 | } |
| 647 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame^] | 648 | bool ResourceParser::parseSymbolImpl(xml::XmlPullParser* parser, ParsedResource* outResource) { |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 649 | const Source source = mSource.withLine(parser->getLineNumber()); |
| 650 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 651 | Maybe<StringPiece16> maybeType = xml::findNonEmptyAttribute(parser, u"type"); |
Adam Lesinski | 9e10ac7 | 2015-10-16 14:37:48 -0700 | [diff] [blame] | 652 | if (!maybeType) { |
| 653 | mDiag->error(DiagMessage(source) << "<" << parser->getElementName() << "> must have a " |
| 654 | "'type' attribute"); |
| 655 | return false; |
| 656 | } |
| 657 | |
| 658 | const ResourceType* parsedType = parseResourceType(maybeType.value()); |
| 659 | if (!parsedType) { |
| 660 | mDiag->error(DiagMessage(source) << "invalid resource type '" << maybeType.value() |
| 661 | << "' in <" << parser->getElementName() << ">"); |
| 662 | return false; |
| 663 | } |
| 664 | |
| 665 | outResource->name.type = *parsedType; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 666 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 667 | } |
| 668 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame^] | 669 | bool ResourceParser::parseSymbol(xml::XmlPullParser* parser, ParsedResource* outResource) { |
| 670 | if (parseSymbolImpl(parser, outResource)) { |
| 671 | outResource->symbolState = SymbolState::kPrivate; |
| 672 | return true; |
| 673 | } |
| 674 | return false; |
| 675 | } |
| 676 | |
| 677 | bool ResourceParser::parseAddResource(xml::XmlPullParser* parser, ParsedResource* outResource) { |
| 678 | if (parseSymbolImpl(parser, outResource)) { |
| 679 | outResource->symbolState = SymbolState::kUndefined; |
| 680 | return true; |
| 681 | } |
| 682 | return false; |
| 683 | } |
| 684 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 685 | static uint32_t parseFormatType(const StringPiece16& piece) { |
| 686 | if (piece == u"reference") return android::ResTable_map::TYPE_REFERENCE; |
| 687 | else if (piece == u"string") return android::ResTable_map::TYPE_STRING; |
| 688 | else if (piece == u"integer") return android::ResTable_map::TYPE_INTEGER; |
| 689 | else if (piece == u"boolean") return android::ResTable_map::TYPE_BOOLEAN; |
| 690 | else if (piece == u"color") return android::ResTable_map::TYPE_COLOR; |
| 691 | else if (piece == u"float") return android::ResTable_map::TYPE_FLOAT; |
| 692 | else if (piece == u"dimension") return android::ResTable_map::TYPE_DIMENSION; |
| 693 | else if (piece == u"fraction") return android::ResTable_map::TYPE_FRACTION; |
| 694 | else if (piece == u"enum") return android::ResTable_map::TYPE_ENUM; |
| 695 | else if (piece == u"flags") return android::ResTable_map::TYPE_FLAGS; |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static uint32_t parseFormatAttribute(const StringPiece16& str) { |
| 700 | uint32_t mask = 0; |
| 701 | for (StringPiece16 part : util::tokenize(str, u'|')) { |
| 702 | StringPiece16 trimmedPart = util::trimWhitespace(part); |
| 703 | uint32_t type = parseFormatType(trimmedPart); |
| 704 | if (type == 0) { |
| 705 | return 0; |
| 706 | } |
| 707 | mask |= type; |
| 708 | } |
| 709 | return mask; |
| 710 | } |
| 711 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 712 | bool ResourceParser::parseAttr(xml::XmlPullParser* parser, ParsedResource* outResource) { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 713 | outResource->source = mSource.withLine(parser->getLineNumber()); |
| 714 | return parseAttrImpl(parser, outResource, false); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 715 | } |
| 716 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 717 | bool ResourceParser::parseAttrImpl(xml::XmlPullParser* parser, ParsedResource* outResource, |
| 718 | bool weak) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 719 | uint32_t typeMask = 0; |
| 720 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 721 | Maybe<StringPiece16> maybeFormat = xml::findAttribute(parser, u"format"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 722 | if (maybeFormat) { |
| 723 | typeMask = parseFormatAttribute(maybeFormat.value()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 724 | if (typeMask == 0) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 725 | mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 726 | << "invalid attribute format '" << maybeFormat.value() << "'"); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 727 | return false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 728 | } |
| 729 | } |
| 730 | |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 731 | Maybe<int32_t> maybeMin, maybeMax; |
| 732 | |
| 733 | if (Maybe<StringPiece16> maybeMinStr = xml::findAttribute(parser, u"min")) { |
| 734 | StringPiece16 minStr = util::trimWhitespace(maybeMinStr.value()); |
| 735 | if (!minStr.empty()) { |
| 736 | android::Res_value value; |
| 737 | if (android::ResTable::stringToInt(minStr.data(), minStr.size(), &value)) { |
| 738 | maybeMin = static_cast<int32_t>(value.data); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | if (!maybeMin) { |
| 743 | mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 744 | << "invalid 'min' value '" << minStr << "'"); |
| 745 | return false; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | if (Maybe<StringPiece16> maybeMaxStr = xml::findAttribute(parser, u"max")) { |
| 750 | StringPiece16 maxStr = util::trimWhitespace(maybeMaxStr.value()); |
| 751 | if (!maxStr.empty()) { |
| 752 | android::Res_value value; |
| 753 | if (android::ResTable::stringToInt(maxStr.data(), maxStr.size(), &value)) { |
| 754 | maybeMax = static_cast<int32_t>(value.data); |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | if (!maybeMax) { |
| 759 | mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 760 | << "invalid 'max' value '" << maxStr << "'"); |
| 761 | return false; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | if ((maybeMin || maybeMax) && (typeMask & android::ResTable_map::TYPE_INTEGER) == 0) { |
| 766 | mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 767 | << "'min' and 'max' can only be used when format='integer'"); |
| 768 | return false; |
| 769 | } |
| 770 | |
Adam Lesinski | abf83cb | 2015-11-16 15:59:10 -0800 | [diff] [blame] | 771 | struct SymbolComparator { |
| 772 | bool operator()(const Attribute::Symbol& a, const Attribute::Symbol& b) { |
| 773 | return a.symbol.name.value() < b.symbol.name.value(); |
| 774 | } |
| 775 | }; |
| 776 | |
| 777 | std::set<Attribute::Symbol, SymbolComparator> items; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 778 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 779 | std::u16string comment; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 780 | bool error = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 781 | const size_t depth = parser->getDepth(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 782 | while (xml::XmlPullParser::nextChildNode(parser, depth)) { |
| 783 | if (parser->getEvent() == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 784 | comment = util::trimWhitespace(parser->getComment()).toString(); |
| 785 | continue; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 786 | } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 787 | // Skip text. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 788 | continue; |
| 789 | } |
| 790 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 791 | const Source itemSource = mSource.withLine(parser->getLineNumber()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 792 | const std::u16string& elementNamespace = parser->getElementNamespace(); |
| 793 | const std::u16string& elementName = parser->getElementName(); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 794 | if (elementNamespace.empty() && (elementName == u"flag" || elementName == u"enum")) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 795 | if (elementName == u"enum") { |
| 796 | if (typeMask & android::ResTable_map::TYPE_FLAGS) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 797 | mDiag->error(DiagMessage(itemSource) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 798 | << "can not define an <enum>; already defined a <flag>"); |
| 799 | error = true; |
| 800 | continue; |
| 801 | } |
| 802 | typeMask |= android::ResTable_map::TYPE_ENUM; |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 803 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 804 | } else if (elementName == u"flag") { |
| 805 | if (typeMask & android::ResTable_map::TYPE_ENUM) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 806 | mDiag->error(DiagMessage(itemSource) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 807 | << "can not define a <flag>; already defined an <enum>"); |
| 808 | error = true; |
| 809 | continue; |
| 810 | } |
| 811 | typeMask |= android::ResTable_map::TYPE_FLAGS; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 812 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 813 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 814 | if (Maybe<Attribute::Symbol> s = parseEnumOrFlagItem(parser, elementName)) { |
Adam Lesinski | abf83cb | 2015-11-16 15:59:10 -0800 | [diff] [blame] | 815 | Attribute::Symbol& symbol = s.value(); |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 816 | ParsedResource childResource; |
Adam Lesinski | abf83cb | 2015-11-16 15:59:10 -0800 | [diff] [blame] | 817 | childResource.name = symbol.symbol.name.value(); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 818 | childResource.source = itemSource; |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 819 | childResource.value = util::make_unique<Id>(); |
| 820 | outResource->childResources.push_back(std::move(childResource)); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 821 | |
Adam Lesinski | abf83cb | 2015-11-16 15:59:10 -0800 | [diff] [blame] | 822 | symbol.symbol.setComment(std::move(comment)); |
| 823 | symbol.symbol.setSource(itemSource); |
| 824 | |
| 825 | auto insertResult = items.insert(std::move(symbol)); |
| 826 | if (!insertResult.second) { |
| 827 | const Attribute::Symbol& existingSymbol = *insertResult.first; |
| 828 | mDiag->error(DiagMessage(itemSource) |
| 829 | << "duplicate symbol '" << existingSymbol.symbol.name.value().entry |
| 830 | << "'"); |
| 831 | |
| 832 | mDiag->note(DiagMessage(existingSymbol.symbol.getSource()) |
| 833 | << "first defined here"); |
| 834 | error = true; |
| 835 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 836 | } else { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 837 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 838 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 839 | } else if (!shouldIgnoreElement(elementNamespace, elementName)) { |
| 840 | mDiag->error(DiagMessage(itemSource) << ":" << elementName << ">"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 841 | error = true; |
| 842 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 843 | |
| 844 | comment = {}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | if (error) { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 848 | return false; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | std::unique_ptr<Attribute> attr = util::make_unique<Attribute>(weak); |
Adam Lesinski | abf83cb | 2015-11-16 15:59:10 -0800 | [diff] [blame] | 852 | attr->symbols = std::vector<Attribute::Symbol>(items.begin(), items.end()); |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 853 | attr->typeMask = typeMask ? typeMask : uint32_t(android::ResTable_map::TYPE_ANY); |
Adam Lesinski | a587065 | 2015-11-20 15:32:30 -0800 | [diff] [blame] | 854 | if (maybeMin) { |
| 855 | attr->minInt = maybeMin.value(); |
| 856 | } |
| 857 | |
| 858 | if (maybeMax) { |
| 859 | attr->maxInt = maybeMax.value(); |
| 860 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 861 | outResource->value = std::move(attr); |
| 862 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 863 | } |
| 864 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 865 | Maybe<Attribute::Symbol> ResourceParser::parseEnumOrFlagItem(xml::XmlPullParser* parser, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 866 | const StringPiece16& tag) { |
| 867 | const Source source = mSource.withLine(parser->getLineNumber()); |
| 868 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 869 | Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 870 | if (!maybeName) { |
| 871 | mDiag->error(DiagMessage(source) << "no attribute 'name' found for tag <" << tag << ">"); |
| 872 | return {}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 873 | } |
| 874 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 875 | Maybe<StringPiece16> maybeValue = xml::findNonEmptyAttribute(parser, u"value"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 876 | if (!maybeValue) { |
| 877 | mDiag->error(DiagMessage(source) << "no attribute 'value' found for tag <" << tag << ">"); |
| 878 | return {}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | android::Res_value val; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 882 | if (!android::ResTable::stringToInt(maybeValue.value().data(), |
| 883 | maybeValue.value().size(), &val)) { |
| 884 | mDiag->error(DiagMessage(source) << "invalid value '" << maybeValue.value() |
| 885 | << "' for <" << tag << ">; must be an integer"); |
| 886 | return {}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 887 | } |
| 888 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 889 | return Attribute::Symbol{ |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame^] | 890 | Reference(ResourceNameRef({}, ResourceType::kId, maybeName.value())), |
Adam Lesinski | e78fd61 | 2015-10-22 12:48:43 -0700 | [diff] [blame] | 891 | val.data }; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 892 | } |
| 893 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 894 | static Maybe<Reference> parseXmlAttributeName(StringPiece16 str) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 895 | str = util::trimWhitespace(str); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 896 | const char16_t* start = str.data(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 897 | const char16_t* const end = start + str.size(); |
| 898 | const char16_t* p = start; |
| 899 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 900 | Reference ref; |
| 901 | if (p != end && *p == u'*') { |
| 902 | ref.privateReference = true; |
| 903 | start++; |
| 904 | p++; |
| 905 | } |
| 906 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 907 | StringPiece16 package; |
| 908 | StringPiece16 name; |
| 909 | while (p != end) { |
| 910 | if (*p == u':') { |
| 911 | package = StringPiece16(start, p - start); |
| 912 | name = StringPiece16(p + 1, end - (p + 1)); |
| 913 | break; |
| 914 | } |
| 915 | p++; |
| 916 | } |
| 917 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 918 | ref.name = ResourceName(package.toString(), ResourceType::kAttr, |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 919 | name.empty() ? str.toString() : name.toString()); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 920 | return Maybe<Reference>(std::move(ref)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 921 | } |
| 922 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 923 | bool ResourceParser::parseStyleItem(xml::XmlPullParser* parser, Style* style) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 924 | const Source source = mSource.withLine(parser->getLineNumber()); |
| 925 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 926 | Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 927 | if (!maybeName) { |
| 928 | mDiag->error(DiagMessage(source) << "<item> must have a 'name' attribute"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 929 | return false; |
| 930 | } |
| 931 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 932 | Maybe<Reference> maybeKey = parseXmlAttributeName(maybeName.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 933 | if (!maybeKey) { |
| 934 | mDiag->error(DiagMessage(source) << "invalid attribute name '" << maybeName.value() << "'"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 935 | return false; |
| 936 | } |
| 937 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 938 | transformReferenceFromNamespace(parser, u"", &maybeKey.value()); |
Adam Lesinski | 28cacf0 | 2015-11-23 14:22:47 -0800 | [diff] [blame] | 939 | maybeKey.value().setSource(source); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 940 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 941 | std::unique_ptr<Item> value = parseXml(parser, 0, kAllowRawString); |
| 942 | if (!value) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 943 | mDiag->error(DiagMessage(source) << "could not parse style item"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 944 | return false; |
| 945 | } |
| 946 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 947 | style->entries.push_back(Style::Entry{ std::move(maybeKey.value()), std::move(value) }); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 948 | return true; |
| 949 | } |
| 950 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 951 | bool ResourceParser::parseStyle(xml::XmlPullParser* parser, ParsedResource* outResource) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 952 | const Source source = mSource.withLine(parser->getLineNumber()); |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 953 | std::unique_ptr<Style> style = util::make_unique<Style>(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 954 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 955 | Maybe<StringPiece16> maybeParent = xml::findAttribute(parser, u"parent"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 956 | if (maybeParent) { |
| 957 | // If the parent is empty, we don't have a parent, but we also don't infer either. |
| 958 | if (!maybeParent.value().empty()) { |
| 959 | std::string errStr; |
| 960 | style->parent = ResourceUtils::parseStyleParentReference(maybeParent.value(), &errStr); |
| 961 | if (!style->parent) { |
| 962 | mDiag->error(DiagMessage(source) << errStr); |
| 963 | return false; |
| 964 | } |
| 965 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 966 | // Transform the namespace prefix to the actual package name, and mark the reference as |
| 967 | // private if appropriate. |
| 968 | transformReferenceFromNamespace(parser, u"", &style->parent.value()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 969 | } |
| 970 | |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 971 | } else { |
| 972 | // No parent was specified, so try inferring it from the style name. |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 973 | std::u16string styleName = outResource->name.entry; |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 974 | size_t pos = styleName.find_last_of(u'.'); |
| 975 | if (pos != std::string::npos) { |
| 976 | style->parentInferred = true; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 977 | style->parent = Reference(ResourceName({}, ResourceType::kStyle, |
| 978 | styleName.substr(0, pos))); |
Adam Lesinski | bdaa092 | 2015-05-08 20:16:23 -0700 | [diff] [blame] | 979 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 980 | } |
| 981 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 982 | bool error = false; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 983 | const size_t depth = parser->getDepth(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 984 | while (xml::XmlPullParser::nextChildNode(parser, depth)) { |
| 985 | if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 986 | // Skip text and comments. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 987 | continue; |
| 988 | } |
| 989 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 990 | const std::u16string& elementNamespace = parser->getElementNamespace(); |
| 991 | const std::u16string& elementName = parser->getElementName(); |
| 992 | if (elementNamespace == u"" && elementName == u"item") { |
| 993 | error |= !parseStyleItem(parser, style.get()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 994 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 995 | } else if (!shouldIgnoreElement(elementNamespace, elementName)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 996 | mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 997 | << ":" << elementName << ">"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 998 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 999 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | if (error) { |
| 1003 | return false; |
| 1004 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1005 | |
| 1006 | outResource->value = std::move(style); |
| 1007 | return true; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1008 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1009 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1010 | bool ResourceParser::parseArray(xml::XmlPullParser* parser, ParsedResource* outResource, |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1011 | uint32_t typeMask) { |
| 1012 | const Source source = mSource.withLine(parser->getLineNumber()); |
| 1013 | std::unique_ptr<Array> array = util::make_unique<Array>(); |
| 1014 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1015 | bool error = false; |
| 1016 | const size_t depth = parser->getDepth(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1017 | while (xml::XmlPullParser::nextChildNode(parser, depth)) { |
| 1018 | if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1019 | // Skip text and comments. |
| 1020 | continue; |
| 1021 | } |
| 1022 | |
| 1023 | const Source itemSource = mSource.withLine(parser->getLineNumber()); |
| 1024 | const std::u16string& elementNamespace = parser->getElementNamespace(); |
| 1025 | const std::u16string& elementName = parser->getElementName(); |
| 1026 | if (elementNamespace.empty() && elementName == u"item") { |
| 1027 | std::unique_ptr<Item> item = parseXml(parser, typeMask, kNoRawString); |
| 1028 | if (!item) { |
| 1029 | mDiag->error(DiagMessage(itemSource) << "could not parse array item"); |
| 1030 | error = true; |
| 1031 | continue; |
| 1032 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1033 | item->setSource(itemSource); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1034 | array->items.emplace_back(std::move(item)); |
| 1035 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1036 | } else if (!shouldIgnoreElement(elementNamespace, elementName)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1037 | mDiag->error(DiagMessage(mSource.withLine(parser->getLineNumber())) |
| 1038 | << "unknown tag <" << elementNamespace << ":" << elementName << ">"); |
| 1039 | error = true; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | if (error) { |
| 1044 | return false; |
| 1045 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1046 | |
| 1047 | outResource->value = std::move(array); |
| 1048 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1049 | } |
| 1050 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1051 | bool ResourceParser::parsePlural(xml::XmlPullParser* parser, ParsedResource* outResource) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1052 | const Source source = mSource.withLine(parser->getLineNumber()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1053 | std::unique_ptr<Plural> plural = util::make_unique<Plural>(); |
| 1054 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1055 | bool error = false; |
| 1056 | const size_t depth = parser->getDepth(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1057 | while (xml::XmlPullParser::nextChildNode(parser, depth)) { |
| 1058 | if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1059 | // Skip text and comments. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1060 | continue; |
| 1061 | } |
| 1062 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1063 | const Source itemSource = mSource.withLine(parser->getLineNumber()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1064 | const std::u16string& elementNamespace = parser->getElementNamespace(); |
| 1065 | const std::u16string& elementName = parser->getElementName(); |
| 1066 | if (elementNamespace.empty() && elementName == u"item") { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1067 | Maybe<StringPiece16> maybeQuantity = xml::findNonEmptyAttribute(parser, u"quantity"); |
| 1068 | if (!maybeQuantity) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1069 | mDiag->error(DiagMessage(itemSource) << "<item> in <plurals> requires attribute " |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1070 | << "'quantity'"); |
| 1071 | error = true; |
| 1072 | continue; |
| 1073 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1074 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1075 | StringPiece16 trimmedQuantity = util::trimWhitespace(maybeQuantity.value()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1076 | size_t index = 0; |
| 1077 | if (trimmedQuantity == u"zero") { |
| 1078 | index = Plural::Zero; |
| 1079 | } else if (trimmedQuantity == u"one") { |
| 1080 | index = Plural::One; |
| 1081 | } else if (trimmedQuantity == u"two") { |
| 1082 | index = Plural::Two; |
| 1083 | } else if (trimmedQuantity == u"few") { |
| 1084 | index = Plural::Few; |
| 1085 | } else if (trimmedQuantity == u"many") { |
| 1086 | index = Plural::Many; |
| 1087 | } else if (trimmedQuantity == u"other") { |
| 1088 | index = Plural::Other; |
| 1089 | } else { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1090 | mDiag->error(DiagMessage(itemSource) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1091 | << "<item> in <plural> has invalid value '" << trimmedQuantity |
| 1092 | << "' for attribute 'quantity'"); |
| 1093 | error = true; |
| 1094 | continue; |
| 1095 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1096 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1097 | if (plural->values[index]) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1098 | mDiag->error(DiagMessage(itemSource) |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1099 | << "duplicate quantity '" << trimmedQuantity << "'"); |
| 1100 | error = true; |
| 1101 | continue; |
| 1102 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1103 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1104 | if (!(plural->values[index] = parseXml(parser, android::ResTable_map::TYPE_STRING, |
| 1105 | kNoRawString))) { |
| 1106 | error = true; |
| 1107 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1108 | plural->values[index]->setSource(itemSource); |
| 1109 | |
| 1110 | } else if (!shouldIgnoreElement(elementNamespace, elementName)) { |
| 1111 | mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1112 | << elementName << ">"); |
| 1113 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1117 | if (error) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1118 | return false; |
| 1119 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1120 | |
| 1121 | outResource->value = std::move(plural); |
| 1122 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1125 | bool ResourceParser::parseDeclareStyleable(xml::XmlPullParser* parser, ParsedResource* outResource) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1126 | const Source source = mSource.withLine(parser->getLineNumber()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1127 | std::unique_ptr<Styleable> styleable = util::make_unique<Styleable>(); |
| 1128 | |
Adam Lesinski | b274e35 | 2015-11-06 15:14:35 -0800 | [diff] [blame] | 1129 | // Declare-styleable is kPrivate by default, because it technically only exists in R.java. |
Adam Lesinski | 9f22204 | 2015-11-04 13:51:45 -0800 | [diff] [blame] | 1130 | outResource->symbolState = SymbolState::kPublic; |
| 1131 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1132 | std::u16string comment; |
| 1133 | bool error = false; |
| 1134 | const size_t depth = parser->getDepth(); |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1135 | while (xml::XmlPullParser::nextChildNode(parser, depth)) { |
| 1136 | if (parser->getEvent() == xml::XmlPullParser::Event::kComment) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1137 | comment = util::trimWhitespace(parser->getComment()).toString(); |
| 1138 | continue; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1139 | } else if (parser->getEvent() != xml::XmlPullParser::Event::kStartElement) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1140 | // Ignore text. |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1141 | continue; |
| 1142 | } |
| 1143 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1144 | const Source itemSource = mSource.withLine(parser->getLineNumber()); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1145 | const std::u16string& elementNamespace = parser->getElementNamespace(); |
| 1146 | const std::u16string& elementName = parser->getElementName(); |
| 1147 | if (elementNamespace.empty() && elementName == u"attr") { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1148 | Maybe<StringPiece16> maybeName = xml::findNonEmptyAttribute(parser, u"name"); |
| 1149 | if (!maybeName) { |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1150 | mDiag->error(DiagMessage(itemSource) << "<attr> tag must have a 'name' attribute"); |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1151 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1152 | continue; |
| 1153 | } |
| 1154 | |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1155 | // If this is a declaration, the package name may be in the name. Separate these out. |
| 1156 | // Eg. <attr name="android:text" /> |
| 1157 | Maybe<Reference> maybeRef = parseXmlAttributeName(maybeName.value()); |
| 1158 | if (!maybeRef) { |
| 1159 | mDiag->error(DiagMessage(itemSource) << "<attr> tag has invalid name '" |
| 1160 | << maybeName.value() << "'"); |
| 1161 | error = true; |
| 1162 | continue; |
| 1163 | } |
| 1164 | |
| 1165 | Reference& childRef = maybeRef.value(); |
| 1166 | xml::transformReferenceFromNamespace(parser, u"", &childRef); |
| 1167 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1168 | // Create the ParsedResource that will add the attribute to the table. |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1169 | ParsedResource childResource; |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 1170 | childResource.name = childRef.name.value(); |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1171 | childResource.source = itemSource; |
| 1172 | childResource.comment = std::move(comment); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1173 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1174 | if (!parseAttrImpl(parser, &childResource, true)) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1175 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1176 | continue; |
| 1177 | } |
| 1178 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1179 | // Create the reference to this attribute. |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1180 | childRef.setComment(childResource.comment); |
| 1181 | childRef.setSource(itemSource); |
| 1182 | styleable->entries.push_back(std::move(childRef)); |
| 1183 | |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1184 | outResource->childResources.push_back(std::move(childResource)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1185 | |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1186 | } else if (!shouldIgnoreElement(elementNamespace, elementName)) { |
| 1187 | mDiag->error(DiagMessage(itemSource) << "unknown tag <" << elementNamespace << ":" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1188 | << elementName << ">"); |
| 1189 | error = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1190 | } |
Adam Lesinski | ca5638f | 2015-10-21 14:42:43 -0700 | [diff] [blame] | 1191 | |
| 1192 | comment = {}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1193 | } |
| 1194 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1195 | if (error) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1196 | return false; |
| 1197 | } |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame] | 1198 | |
| 1199 | outResource->value = std::move(styleable); |
| 1200 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | } // namespace aapt |