Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | #include "configuration/ConfigurationParser.h" |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <functional> |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 21 | #include <map> |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 22 | #include <memory> |
| 23 | #include <utility> |
| 24 | |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 25 | #include <android-base/file.h> |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 26 | #include <android-base/logging.h> |
| 27 | |
| 28 | #include "ConfigDescription.h" |
| 29 | #include "Diagnostics.h" |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 30 | #include "io/File.h" |
| 31 | #include "io/FileSystem.h" |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 32 | #include "util/Maybe.h" |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 33 | #include "util/Util.h" |
| 34 | #include "xml/XmlActionExecutor.h" |
| 35 | #include "xml/XmlDom.h" |
| 36 | #include "xml/XmlUtil.h" |
| 37 | |
| 38 | namespace aapt { |
| 39 | |
| 40 | namespace { |
| 41 | |
| 42 | using ::aapt::configuration::Abi; |
| 43 | using ::aapt::configuration::AndroidManifest; |
| 44 | using ::aapt::configuration::AndroidSdk; |
| 45 | using ::aapt::configuration::Artifact; |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 46 | using ::aapt::configuration::PostProcessingConfiguration; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 47 | using ::aapt::configuration::GlTexture; |
| 48 | using ::aapt::configuration::Group; |
| 49 | using ::aapt::configuration::Locale; |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 50 | using ::aapt::io::IFile; |
| 51 | using ::aapt::io::RegularFile; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 52 | using ::aapt::util::TrimWhitespace; |
| 53 | using ::aapt::xml::Element; |
| 54 | using ::aapt::xml::FindRootElement; |
| 55 | using ::aapt::xml::NodeCast; |
| 56 | using ::aapt::xml::XmlActionExecutor; |
| 57 | using ::aapt::xml::XmlActionExecutorPolicy; |
| 58 | using ::aapt::xml::XmlNodeAction; |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 59 | using ::android::base::ReadFileToString; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 60 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 61 | const std::unordered_map<std::string, Abi> kStringToAbiMap = { |
| 62 | {"armeabi", Abi::kArmeV6}, {"armeabi-v7a", Abi::kArmV7a}, {"arm64-v8a", Abi::kArm64V8a}, |
| 63 | {"x86", Abi::kX86}, {"x86_64", Abi::kX86_64}, {"mips", Abi::kMips}, |
| 64 | {"mips64", Abi::kMips64}, {"universal", Abi::kUniversal}, |
| 65 | }; |
| 66 | const std::map<Abi, std::string> kAbiToStringMap = { |
| 67 | {Abi::kArmeV6, "armeabi"}, {Abi::kArmV7a, "armeabi-v7a"}, {Abi::kArm64V8a, "arm64-v8a"}, |
| 68 | {Abi::kX86, "x86"}, {Abi::kX86_64, "x86_64"}, {Abi::kMips, "mips"}, |
| 69 | {Abi::kMips64, "mips64"}, {Abi::kUniversal, "universal"}, |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | constexpr const char* kAaptXmlNs = "http://schemas.android.com/tools/aapt"; |
| 73 | |
| 74 | /** A default noop diagnostics context. */ |
| 75 | class NoopDiagnostics : public IDiagnostics { |
| 76 | public: |
| 77 | void Log(Level level, DiagMessageActual& actualMsg) override {} |
| 78 | }; |
| 79 | NoopDiagnostics noop_; |
| 80 | |
| 81 | std::string GetLabel(const Element* element, IDiagnostics* diag) { |
| 82 | std::string label; |
| 83 | for (const auto& attr : element->attributes) { |
| 84 | if (attr.name == "label") { |
| 85 | label = attr.value; |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (label.empty()) { |
| 91 | diag->Error(DiagMessage() << "No label found for element " << element->name); |
| 92 | } |
| 93 | return label; |
| 94 | } |
| 95 | |
| 96 | /** XML node visitor that removes all of the namespace URIs from the node and all children. */ |
| 97 | class NamespaceVisitor : public xml::Visitor { |
| 98 | public: |
| 99 | void Visit(xml::Element* node) override { |
| 100 | node->namespace_uri.clear(); |
| 101 | VisitChildren(node); |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | } // namespace |
| 106 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 107 | namespace configuration { |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 108 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 109 | const std::string& AbiToString(Abi abi) { |
| 110 | return kAbiToStringMap.find(abi)->second; |
| 111 | } |
| 112 | |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Attempts to replace the placeholder in the name string with the provided value. Returns true on |
| 115 | * success, or false if the either the placeholder is not found in the name, or the value is not |
| 116 | * present and the placeholder was. |
| 117 | */ |
| 118 | static bool ReplacePlaceholder(const std::string& placeholder, const Maybe<std::string>& value, |
| 119 | std::string* name, IDiagnostics* diag) { |
| 120 | size_t offset = name->find(placeholder); |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame^] | 121 | bool found = (offset != std::string::npos); |
| 122 | |
| 123 | // Make sure the placeholder was present if the desired value is present. |
| 124 | if (!found) { |
| 125 | if (value) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 126 | diag->Error(DiagMessage() << "Missing placeholder for artifact: " << placeholder); |
| 127 | return false; |
| 128 | } |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame^] | 132 | DCHECK(found) << "Missing return path for placeholder not found"; |
| 133 | |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 134 | // Make sure the placeholder was not present if the desired value was not present. |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame^] | 135 | if (!value) { |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 136 | diag->Error(DiagMessage() << "Placeholder present but no value for artifact: " << placeholder); |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame^] | 137 | return false; |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 138 | } |
Shane Farmer | 1a21b8c | 2017-07-21 09:42:42 -0700 | [diff] [blame^] | 139 | |
| 140 | name->replace(offset, placeholder.length(), value.value()); |
| 141 | |
| 142 | // Make sure there was only one instance of the placeholder. |
| 143 | if (name->find(placeholder) != std::string::npos) { |
| 144 | diag->Error(DiagMessage() << "Placeholder present multiple times: " << placeholder); |
| 145 | return false; |
| 146 | } |
| 147 | return true; |
Shane Farmer | 9f0e7f1 | 2017-06-22 12:26:44 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | Maybe<std::string> Artifact::ToArtifactName(const std::string& format, IDiagnostics* diag) const { |
| 151 | std::string result = format; |
| 152 | |
| 153 | if (!ReplacePlaceholder("{abi}", abi_group, &result, diag)) { |
| 154 | return {}; |
| 155 | } |
| 156 | |
| 157 | if (!ReplacePlaceholder("{density}", screen_density_group, &result, diag)) { |
| 158 | return {}; |
| 159 | } |
| 160 | |
| 161 | if (!ReplacePlaceholder("{locale}", locale_group, &result, diag)) { |
| 162 | return {}; |
| 163 | } |
| 164 | |
| 165 | if (!ReplacePlaceholder("{sdk}", android_sdk_group, &result, diag)) { |
| 166 | return {}; |
| 167 | } |
| 168 | |
| 169 | if (!ReplacePlaceholder("{feature}", device_feature_group, &result, diag)) { |
| 170 | return {}; |
| 171 | } |
| 172 | |
| 173 | if (!ReplacePlaceholder("{gl}", gl_texture_group, &result, diag)) { |
| 174 | return {}; |
| 175 | } |
| 176 | |
| 177 | return result; |
| 178 | } |
| 179 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 180 | } // namespace configuration |
Shane Farmer | b102727 | 2017-06-14 09:10:28 -0700 | [diff] [blame] | 181 | |
| 182 | /** Returns a ConfigurationParser for the file located at the provided path. */ |
| 183 | Maybe<ConfigurationParser> ConfigurationParser::ForPath(const std::string& path) { |
| 184 | std::string contents; |
| 185 | if (!ReadFileToString(path, &contents, true)) { |
| 186 | return {}; |
| 187 | } |
| 188 | return ConfigurationParser(contents); |
| 189 | } |
| 190 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 191 | ConfigurationParser::ConfigurationParser(std::string contents) |
| 192 | : contents_(std::move(contents)), |
| 193 | diag_(&noop_) { |
| 194 | } |
| 195 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 196 | Maybe<PostProcessingConfiguration> ConfigurationParser::Parse() { |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 197 | std::istringstream in(contents_); |
| 198 | |
| 199 | auto doc = xml::Inflate(&in, diag_, Source("config.xml")); |
| 200 | if (!doc) { |
| 201 | return {}; |
| 202 | } |
| 203 | |
| 204 | // Strip any namespaces from the XML as the XmlActionExecutor ignores anything with a namespace. |
| 205 | auto* root = FindRootElement(doc.get()); |
| 206 | if (root == nullptr) { |
| 207 | diag_->Error(DiagMessage() << "Could not find the root element in the XML document"); |
| 208 | return {}; |
| 209 | } |
| 210 | |
| 211 | std::string& xml_ns = root->namespace_uri; |
| 212 | if (!xml_ns.empty()) { |
| 213 | if (xml_ns != kAaptXmlNs) { |
| 214 | diag_->Error(DiagMessage() << "Unknown namespace found on root element: " << xml_ns); |
| 215 | return {}; |
| 216 | } |
| 217 | |
| 218 | xml_ns.clear(); |
| 219 | NamespaceVisitor visitor; |
| 220 | root->Accept(&visitor); |
| 221 | } |
| 222 | |
| 223 | XmlActionExecutor executor; |
| 224 | XmlNodeAction& root_action = executor["post-process"]; |
| 225 | XmlNodeAction& artifacts_action = root_action["artifacts"]; |
| 226 | XmlNodeAction& groups_action = root_action["groups"]; |
| 227 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 228 | PostProcessingConfiguration config; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 229 | |
| 230 | // Helper to bind a static method to an action handler in the DOM executor. |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 231 | auto bind_handler = |
| 232 | [&config](std::function<bool(PostProcessingConfiguration*, Element*, IDiagnostics*)> h) |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 233 | -> XmlNodeAction::ActionFuncWithDiag { |
| 234 | return std::bind(h, &config, std::placeholders::_1, std::placeholders::_2); |
| 235 | }; |
| 236 | |
| 237 | // Parse the artifact elements. |
| 238 | artifacts_action["artifact"].Action(bind_handler(artifact_handler_)); |
| 239 | artifacts_action["artifact-format"].Action(bind_handler(artifact_format_handler_)); |
| 240 | |
| 241 | // Parse the different configuration groups. |
| 242 | groups_action["abi-group"].Action(bind_handler(abi_group_handler_)); |
| 243 | groups_action["screen-density-group"].Action(bind_handler(screen_density_group_handler_)); |
| 244 | groups_action["locale-group"].Action(bind_handler(locale_group_handler_)); |
| 245 | groups_action["android-sdk-group"].Action(bind_handler(android_sdk_group_handler_)); |
| 246 | groups_action["gl-texture-group"].Action(bind_handler(gl_texture_group_handler_)); |
| 247 | groups_action["device-feature-group"].Action(bind_handler(device_feature_group_handler_)); |
| 248 | |
| 249 | if (!executor.Execute(XmlActionExecutorPolicy::kNone, diag_, doc.get())) { |
| 250 | diag_->Error(DiagMessage() << "Could not process XML document"); |
| 251 | return {}; |
| 252 | } |
| 253 | |
Shane Farmer | 5766943 | 2017-06-19 12:52:04 -0700 | [diff] [blame] | 254 | // TODO: Validate all references in the configuration are valid. It should be safe to assume from |
| 255 | // this point on that any references from one section to another will be present. |
| 256 | |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 257 | return {config}; |
| 258 | } |
| 259 | |
| 260 | ConfigurationParser::ActionHandler ConfigurationParser::artifact_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 261 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 262 | Artifact artifact{}; |
| 263 | for (const auto& attr : root_element->attributes) { |
| 264 | if (attr.name == "name") { |
| 265 | artifact.name = attr.value; |
| 266 | } else if (attr.name == "abi-group") { |
| 267 | artifact.abi_group = {attr.value}; |
| 268 | } else if (attr.name == "screen-density-group") { |
| 269 | artifact.screen_density_group = {attr.value}; |
| 270 | } else if (attr.name == "locale-group") { |
| 271 | artifact.locale_group = {attr.value}; |
| 272 | } else if (attr.name == "android-sdk-group") { |
| 273 | artifact.android_sdk_group = {attr.value}; |
| 274 | } else if (attr.name == "gl-texture-group") { |
| 275 | artifact.gl_texture_group = {attr.value}; |
| 276 | } else if (attr.name == "device-feature-group") { |
| 277 | artifact.device_feature_group = {attr.value}; |
| 278 | } else { |
| 279 | diag->Note(DiagMessage() << "Unknown artifact attribute: " << attr.name << " = " |
| 280 | << attr.value); |
| 281 | } |
| 282 | } |
| 283 | config->artifacts.push_back(artifact); |
| 284 | return true; |
| 285 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 286 | |
| 287 | ConfigurationParser::ActionHandler ConfigurationParser::artifact_format_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 288 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 289 | for (auto& node : root_element->children) { |
| 290 | xml::Text* t; |
| 291 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 292 | config->artifact_format = TrimWhitespace(t->text).to_string(); |
| 293 | break; |
| 294 | } |
| 295 | } |
| 296 | return true; |
| 297 | }; |
| 298 | |
| 299 | ConfigurationParser::ActionHandler ConfigurationParser::abi_group_handler_ = |
| 300 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 301 | std::string label = GetLabel(root_element, diag); |
| 302 | if (label.empty()) { |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | auto& group = config->abi_groups[label]; |
| 307 | bool valid = true; |
| 308 | |
| 309 | for (auto* child : root_element->GetChildElements()) { |
| 310 | if (child->name != "abi") { |
| 311 | diag->Error(DiagMessage() << "Unexpected element in ABI group: " << child->name); |
| 312 | valid = false; |
| 313 | } else { |
| 314 | for (auto& node : child->children) { |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 315 | xml::Text* t; |
| 316 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 317 | group.push_back(kStringToAbiMap.at(TrimWhitespace(t->text).to_string())); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 318 | break; |
| 319 | } |
| 320 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 323 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 324 | return valid; |
| 325 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 326 | |
| 327 | ConfigurationParser::ActionHandler ConfigurationParser::screen_density_group_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 328 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 329 | std::string label = GetLabel(root_element, diag); |
| 330 | if (label.empty()) { |
| 331 | return false; |
| 332 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 333 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 334 | auto& group = config->screen_density_groups[label]; |
| 335 | bool valid = true; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 336 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 337 | for (auto* child : root_element->GetChildElements()) { |
| 338 | if (child->name != "screen-density") { |
| 339 | diag->Error(DiagMessage() << "Unexpected root_element in screen density group: " |
| 340 | << child->name); |
| 341 | valid = false; |
| 342 | } else { |
| 343 | for (auto& node : child->children) { |
| 344 | xml::Text* t; |
| 345 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 346 | ConfigDescription config_descriptor; |
| 347 | const android::StringPiece& text = TrimWhitespace(t->text); |
| 348 | if (ConfigDescription::Parse(text, &config_descriptor)) { |
| 349 | // Copy the density with the minimum SDK version stripped out. |
| 350 | group.push_back(config_descriptor.CopyWithoutSdkVersion()); |
| 351 | } else { |
| 352 | diag->Error(DiagMessage() |
| 353 | << "Could not parse config descriptor for screen-density: " << text); |
| 354 | valid = false; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 355 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 356 | break; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 359 | } |
| 360 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 361 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 362 | return valid; |
| 363 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 364 | |
| 365 | ConfigurationParser::ActionHandler ConfigurationParser::locale_group_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 366 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 367 | std::string label = GetLabel(root_element, diag); |
| 368 | if (label.empty()) { |
| 369 | return false; |
| 370 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 371 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 372 | auto& group = config->locale_groups[label]; |
| 373 | bool valid = true; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 374 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 375 | for (auto* child : root_element->GetChildElements()) { |
| 376 | if (child->name != "locale") { |
| 377 | diag->Error(DiagMessage() << "Unexpected root_element in screen density group: " |
| 378 | << child->name); |
| 379 | valid = false; |
| 380 | } else { |
| 381 | Locale entry; |
| 382 | for (const auto& attr : child->attributes) { |
| 383 | if (attr.name == "lang") { |
| 384 | entry.lang = {attr.value}; |
| 385 | } else if (attr.name == "region") { |
| 386 | entry.region = {attr.value}; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 387 | } else { |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 388 | diag->Warn(DiagMessage() << "Unknown attribute: " << attr.name << " = " << attr.value); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 391 | group.push_back(entry); |
| 392 | } |
| 393 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 394 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 395 | return valid; |
| 396 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 397 | |
| 398 | ConfigurationParser::ActionHandler ConfigurationParser::android_sdk_group_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 399 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 400 | std::string label = GetLabel(root_element, diag); |
| 401 | if (label.empty()) { |
| 402 | return false; |
| 403 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 404 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 405 | auto& group = config->android_sdk_groups[label]; |
| 406 | bool valid = true; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 407 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 408 | for (auto* child : root_element->GetChildElements()) { |
| 409 | if (child->name != "android-sdk") { |
| 410 | diag->Error(DiagMessage() << "Unexpected root_element in ABI group: " << child->name); |
| 411 | valid = false; |
| 412 | } else { |
| 413 | AndroidSdk entry; |
| 414 | for (const auto& attr : child->attributes) { |
| 415 | if (attr.name == "minSdkVersion") { |
| 416 | entry.min_sdk_version = {attr.value}; |
| 417 | } else if (attr.name == "targetSdkVersion") { |
| 418 | entry.target_sdk_version = {attr.value}; |
| 419 | } else if (attr.name == "maxSdkVersion") { |
| 420 | entry.max_sdk_version = {attr.value}; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 421 | } else { |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 422 | diag->Warn(DiagMessage() << "Unknown attribute: " << attr.name << " = " << attr.value); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 426 | // TODO: Fill in the manifest details when they are finalised. |
| 427 | for (auto node : child->GetChildElements()) { |
| 428 | if (node->name == "manifest") { |
| 429 | if (entry.manifest) { |
| 430 | diag->Warn(DiagMessage() << "Found multiple manifest tags. Ignoring duplicates."); |
| 431 | continue; |
| 432 | } |
| 433 | entry.manifest = {AndroidManifest()}; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | group.push_back(entry); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | return valid; |
| 442 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 443 | |
| 444 | ConfigurationParser::ActionHandler ConfigurationParser::gl_texture_group_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 445 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 446 | std::string label = GetLabel(root_element, diag); |
| 447 | if (label.empty()) { |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | auto& group = config->gl_texture_groups[label]; |
| 452 | bool valid = true; |
| 453 | |
| 454 | GlTexture result; |
| 455 | for (auto* child : root_element->GetChildElements()) { |
| 456 | if (child->name != "gl-texture") { |
| 457 | diag->Error(DiagMessage() << "Unexpected element in GL texture group: " << child->name); |
| 458 | valid = false; |
| 459 | } else { |
| 460 | for (const auto& attr : child->attributes) { |
| 461 | if (attr.name == "name") { |
| 462 | result.name = attr.value; |
| 463 | break; |
| 464 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 467 | for (auto* element : child->GetChildElements()) { |
| 468 | if (element->name != "texture-path") { |
| 469 | diag->Error(DiagMessage() << "Unexpected element in gl-texture element: " << child->name); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 470 | valid = false; |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 471 | continue; |
| 472 | } |
| 473 | for (auto& node : element->children) { |
| 474 | xml::Text* t; |
| 475 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 476 | result.texture_paths.push_back(TrimWhitespace(t->text).to_string()); |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 479 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 480 | } |
| 481 | group.push_back(result); |
| 482 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 483 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 484 | return valid; |
| 485 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 486 | |
| 487 | ConfigurationParser::ActionHandler ConfigurationParser::device_feature_group_handler_ = |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 488 | [](PostProcessingConfiguration* config, Element* root_element, IDiagnostics* diag) -> bool { |
| 489 | std::string label = GetLabel(root_element, diag); |
| 490 | if (label.empty()) { |
| 491 | return false; |
| 492 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 493 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 494 | auto& group = config->device_feature_groups[label]; |
| 495 | bool valid = true; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 496 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 497 | for (auto* child : root_element->GetChildElements()) { |
| 498 | if (child->name != "supports-feature") { |
| 499 | diag->Error(DiagMessage() << "Unexpected root_element in device feature group: " |
| 500 | << child->name); |
| 501 | valid = false; |
| 502 | } else { |
| 503 | for (auto& node : child->children) { |
| 504 | xml::Text* t; |
| 505 | if ((t = NodeCast<xml::Text>(node.get())) != nullptr) { |
| 506 | group.push_back(TrimWhitespace(t->text).to_string()); |
| 507 | break; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 510 | } |
| 511 | } |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 512 | |
Shane Farmer | 280be34 | 2017-06-21 15:20:15 -0700 | [diff] [blame] | 513 | return valid; |
| 514 | }; |
Shane Farmer | 74cdea3 | 2017-05-12 16:22:36 -0700 | [diff] [blame] | 515 | |
| 516 | } // namespace aapt |