aboutsummaryrefslogtreecommitdiff
path: root/src/ui/mapper/style/StyleRuleMapper.cpp
blob: 2eb5b0a28d928fcfffbc3321366de7a6c9a750cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "cru/ui/mapper/style/StyleRuleMapper.h"
#include "cru/base/ClonablePtr.h"
#include "cru/base/Exception.h"
#include "cru/base/log/Logger.h"
#include "cru/ui/mapper/MapperRegistry.h"
#include "cru/ui/mapper/style/IConditionMapper.h"
#include "cru/ui/mapper/style/IStylerMapper.h"
#include "cru/ui/style/Condition.h"
#include "cru/ui/style/StyleRule.h"
#include "cru/ui/style/Styler.h"

namespace cru::ui::mapper::style {
using namespace ui::style;
bool StyleRuleMapper::XmlElementIsOfThisType(xml::XmlElementNode* node) {
  return node->GetTag().CaseInsensitiveEqual(u"StyleRule");
}

ClonablePtr<ui::style::StyleRule> StyleRuleMapper::DoMapFromXml(
    xml::XmlElementNode* node) {
  std::vector<IConditionMapper*> condition_mappers =
      MapperRegistry::GetInstance()->GetMappersByInterface<IConditionMapper>();
  std::vector<IStylerMapper*> styler_mappers =
      MapperRegistry::GetInstance()->GetMappersByInterface<IStylerMapper>();

  std::vector<ClonablePtr<Condition>> conditions;
  std::vector<ClonablePtr<Styler>> stylers;

  for (auto child : node->GetChildren()) {
    if (child->GetType() == xml::XmlNode::Type::Element) {
      auto c = child->AsElement();

      bool resolved = false;
      for (auto condition_mapper : condition_mappers) {
        if (condition_mapper->XmlElementIsOfThisType(c)) {
          conditions.push_back(condition_mapper->MapConditionFromXml(c));
          resolved = true;
          break;
        }
      }

      if (!resolved) {
        for (auto styler_mapper : styler_mappers) {
          if (styler_mapper->XmlElementIsOfThisType(c)) {
            stylers.push_back(styler_mapper->MapStylerFromXml(c));
            resolved = true;
            break;
          }
        }
      }

      if (!resolved) {
        throw Exception(u"Unknown element in StyleRule: " + c->GetTag());
      }
    }
  }

  return StyleRule::Create(AndCondition::Create(std::move(conditions)),
                           CompoundStyler::Create(std::move(stylers)));
}
}  // namespace cru::ui::mapper::style