summaryrefslogtreecommitdiff
path: root/internal/xmpmeta/xml/serializer_impl.cc
blob: c8a6038bac603c7397b083c76bd31d402902fd60 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "xmpmeta/xml/serializer_impl.h"

#include <libxml/tree.h>

#include "base/integral_types.h"
#include "android-base/logging.h"
#include "strings/numbers.h"
#include "xmpmeta/xml/const.h"
#include "xmpmeta/xml/utils.h"

namespace photos_editing_formats {
namespace xml {

// Methods specific to SerializerImpl.
SerializerImpl::SerializerImpl(
    const std::unordered_map<string, xmlNsPtr>& namespaces, xmlNodePtr node)
    : node_(node), namespaces_(namespaces) {
  CHECK(node_ != nullptr) << "Node cannot be null";
  CHECK(node_->name != nullptr) << "Name in the XML node cannot be null";
}

bool SerializerImpl::SerializeNamespaces() {
  if (namespaces_.empty()) {
    return true;
  }
  if (node_->ns == nullptr && !namespaces_.empty()) {
    return false;
  }
  // Check that the namespaces all have hrefs and that there is a value
  // for the key node_name.
  // Set the namespaces in the root node.
  xmlNsPtr node_ns = node_->ns;
  for (const auto& entry : namespaces_) {
    CHECK(entry.second->href != nullptr) << "Namespace href cannot be null";
    if (node_ns != nullptr) {
      node_ns->next = entry.second;
    }
    node_ns = entry.second;
  }
  return true;
}

std::unique_ptr<SerializerImpl> SerializerImpl::FromDataAndSerializeNamespaces(
    const std::unordered_map<string, xmlNsPtr>& namespaces, xmlNodePtr node) {
  std::unique_ptr<SerializerImpl> serializer =
      std::unique_ptr<SerializerImpl>(            // NOLINT
          new SerializerImpl(namespaces, node));  // NOLINT
  if (!serializer->SerializeNamespaces()) {
    LOG(ERROR) << "Could not serialize namespaces";
    return nullptr;
  }
  return serializer;
}

// Implemented methods.
std::unique_ptr<Serializer> SerializerImpl::CreateSerializer(
    const string& node_ns_name, const string& node_name) const {
  if (node_name.empty()) {
    LOG(ERROR) << "Node name is empty";
    return nullptr;
  }

  if (namespaces_.count(node_ns_name) == 0 && !node_ns_name.empty()) {
    LOG(ERROR) << "Prefix " << node_ns_name << " not found in prefix list";
    return nullptr;
  }

  xmlNodePtr new_node =
      xmlNewNode(node_ns_name.empty() ? nullptr : namespaces_.at(node_ns_name),
                 ToXmlChar(node_name.data()));
  xmlAddChild(node_, new_node);
  return std::unique_ptr<Serializer>(
      new SerializerImpl(namespaces_, new_node));  // NOLINT
}

std::unique_ptr<Serializer> SerializerImpl::CreateItemSerializer(
    const string& prefix, const string& item_name) const {
  if (namespaces_.count(XmlConst::RdfPrefix()) == 0 ||
      namespaces_.at(XmlConst::RdfPrefix()) == nullptr) {
    LOG(ERROR) << "No RDF prefix namespace found";
    return nullptr;
  }
  if (!prefix.empty() && !namespaces_.count(prefix)) {
    LOG(ERROR) << "No namespace found for " << prefix;
    return nullptr;
  }
  if (strcmp(XmlConst::RdfSeq(), FromXmlChar(node_->name)) != 0) {
    LOG(ERROR) << "No rdf:Seq node for serializing this item";
    return nullptr;
  }

  xmlNsPtr rdf_prefix_ns = namespaces_.at(string(XmlConst::RdfPrefix()));
  xmlNodePtr li_node = xmlNewNode(nullptr, ToXmlChar(XmlConst::RdfLi()));
  xmlNodePtr new_node =
      xmlNewNode(prefix.empty() ? nullptr : namespaces_.at(prefix),
                 ToXmlChar(item_name.data()));
  xmlSetNs(li_node, rdf_prefix_ns);
  xmlAddChild(node_, li_node);
  xmlAddChild(li_node, new_node);
  return std::unique_ptr<Serializer>(
      new SerializerImpl(namespaces_, new_node));  // NOLINT
}

std::unique_ptr<Serializer> SerializerImpl::CreateListSerializer(
    const string& prefix, const string& list_name) const {
  if (namespaces_.count(XmlConst::RdfPrefix()) == 0 ||
      namespaces_.at(XmlConst::RdfPrefix()) == nullptr) {
    LOG(ERROR) << "No RDF prefix namespace found";
    return nullptr;
  }
  if (!prefix.empty() && !namespaces_.count(prefix)) {
    LOG(ERROR) << "No namespace found for " << prefix;
    return nullptr;
  }

  xmlNodePtr list_node =
      xmlNewNode(prefix.empty() ? nullptr : namespaces_.at(prefix),
                 ToXmlChar(list_name.data()));
  xmlNsPtr rdf_prefix_ns = namespaces_.at(string(XmlConst::RdfPrefix()));
  xmlNodePtr seq_node = xmlNewNode(nullptr, ToXmlChar(XmlConst::RdfSeq()));
  xmlSetNs(seq_node, rdf_prefix_ns);
  xmlAddChild(list_node, seq_node);
  xmlAddChild(node_, list_node);
  return std::unique_ptr<Serializer>(
      new SerializerImpl(namespaces_, seq_node));  // NOLINT
}

bool SerializerImpl::WriteBoolProperty(const string& prefix, const string& name,
                                       bool value) const {
  const string& bool_str = (value ? "true" : "false");
  return WriteProperty(prefix, name, bool_str);
}

bool SerializerImpl::WriteProperty(const string& prefix, const string& name,
                                   const string& value) const {
  if (!strcmp(XmlConst::RdfSeq(), FromXmlChar(node_->name))) {
    LOG(ERROR) << "Cannot write a property on an rdf:Seq node";
    return false;
  }
  if (name.empty()) {
    LOG(ERROR) << "Property name is empty";
    return false;
  }

  // Check that prefix has a corresponding namespace href.
  if (!prefix.empty() && namespaces_.count(prefix) == 0) {
    LOG(ERROR) << "No namespace found for prefix " << prefix;
    return false;
  }

  // Serialize the property in the format Prefix:Name="Value".
  xmlSetNsProp(node_, prefix.empty() ? nullptr : namespaces_.at(prefix),
               ToXmlChar(name.data()), ToXmlChar(value.data()));
  return true;
}

bool SerializerImpl::WriteIntArray(const string& prefix,
                                   const string& array_name,
                                   const std::vector<int>& values) const {
  if (!strcmp(XmlConst::RdfSeq(), FromXmlChar(node_->name))) {
    LOG(ERROR) << "Cannot write a property on an rdf:Seq node";
    return false;
  }
  if (values.empty()) {
    LOG(WARNING) << "No values to write";
    return false;
  }
  if (namespaces_.count(XmlConst::RdfPrefix()) == 0 ||
      namespaces_.at(XmlConst::RdfPrefix()) == nullptr) {
    LOG(ERROR) << "No RDF prefix found";
    return false;
  }
  if (!prefix.empty() && !namespaces_.count(prefix)) {
    LOG(ERROR) << "No namespace found for " << prefix;
    return false;
  }
  if (array_name.empty()) {
    LOG(ERROR) << "Parent name cannot be empty";
    return false;
  }

  xmlNodePtr array_parent_node =
      xmlNewNode(prefix.empty() ? nullptr : namespaces_.at(prefix),
                 ToXmlChar(array_name.data()));
  xmlAddChild(node_, array_parent_node);

  xmlNsPtr rdf_prefix_ns = namespaces_.at(XmlConst::RdfPrefix());
  xmlNodePtr seq_node = xmlNewNode(nullptr, ToXmlChar(XmlConst::RdfSeq()));
  xmlSetNs(seq_node, rdf_prefix_ns);
  xmlAddChild(array_parent_node, seq_node);
  for (int value : values) {
    xmlNodePtr li_node = xmlNewNode(nullptr, ToXmlChar(XmlConst::RdfLi()));
    xmlSetNs(li_node, rdf_prefix_ns);
    xmlAddChild(seq_node, li_node);
    xmlNodeSetContent(li_node, ToXmlChar(std::to_string(value).c_str()));
  }

  return true;
}

bool SerializerImpl::WriteDoubleArray(const string& prefix,
                                      const string& array_name,
                                      const std::vector<double>& values) const {
  if (!strcmp(XmlConst::RdfSeq(), FromXmlChar(node_->name))) {
    LOG(ERROR) << "Cannot write a property on an rdf:Seq node";
    return false;
  }
  if (values.empty()) {
    LOG(WARNING) << "No values to write";
    return false;
  }
  if (namespaces_.count(XmlConst::RdfPrefix()) == 0 ||
      namespaces_.at(XmlConst::RdfPrefix()) == nullptr) {
    LOG(ERROR) << "No RDF prefix found";
    return false;
  }
  if (!prefix.empty() && !namespaces_.count(prefix)) {
    LOG(ERROR) << "No namespace found for " << prefix;
    return false;
  }
  if (array_name.empty()) {
    LOG(ERROR) << "Parent name cannot be empty";
    return false;
  }

  xmlNodePtr array_parent_node =
      xmlNewNode(prefix.empty() ? nullptr : namespaces_.at(prefix),
                 ToXmlChar(array_name.data()));
  xmlAddChild(node_, array_parent_node);

  xmlNsPtr rdf_prefix_ns = namespaces_.at(XmlConst::RdfPrefix());
  xmlNodePtr seq_node = xmlNewNode(nullptr, ToXmlChar(XmlConst::RdfSeq()));
  xmlSetNs(seq_node, rdf_prefix_ns);
  xmlAddChild(array_parent_node, seq_node);
  for (float value : values) {
    xmlNodePtr li_node = xmlNewNode(nullptr, ToXmlChar(XmlConst::RdfLi()));
    xmlSetNs(li_node, rdf_prefix_ns);
    xmlAddChild(seq_node, li_node);
    xmlNodeSetContent(
        li_node, ToXmlChar(dynamic_depth::strings::SimpleFtoa(value).c_str()));
  }

  return true;
}

}  // namespace xml
}  // namespace photos_editing_formats