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