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