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