summaryrefslogtreecommitdiff
path: root/internal/xmpmeta/xml/deserializer_impl.h
blob: 65df2d6e5e8c75efbeda8a6be1965bd0a3a380db (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
#ifndef DYNAMIC_DEPTH_INTERNAL_XMPMETA_XML_DESERIALIZER_IMPL_H_  // NOLINT
#define DYNAMIC_DEPTH_INTERNAL_XMPMETA_XML_DESERIALIZER_IMPL_H_  // NOLINT

#include <libxml/tree.h>

#include <map>
#include <mutex>  // NOLINT(build/c++11)
#include <string>

#include "base/port.h"
#include "xmpmeta/xml/deserializer.h"

namespace photos_editing_formats {
namespace xml {

// Deserializes an XML node.
// Example:
//   xmlNodePtr device_node =
//       DepthFirstSearch(xmp.ExtendedSection(), "Device", "Description");
//   DeserializerImpl deserializer(device_node);
//   string revision;
//   deserializer.ParseString("Device", "Revision", &revision);
// TODO(miraleung): Add example for list node deserializer.
class DeserializerImpl : public Deserializer {
 public:
  // Creates a deserializer with a null rdf:Seq node.
  explicit DeserializerImpl(const xmlNodePtr node);

  // Returns a Deserializer.
  // If prefix is empty, the deserializer will be created on the first node
  // found with a name that matches child_name.
  // child_name is the name of the next node to deserialize.
  std::unique_ptr<Deserializer> CreateDeserializer(
      const string& prefix, const string& child_name) const override;

  // Returns a Deserializer from a list element node, if one is available as
  // a descendant of node_.
  // If prefix is empty, the deserializer will be created on the first node
  // found with a name that matches child_name.
  // Returns null if seq_node_ is null or if the index is out of range.
  std::unique_ptr<Deserializer> CreateDeserializerFromListElementAt(
      const string& prefix, const string& list_name, int index) const override;

  // Parsers for XML properties.
  // If prefix is empty, the node's namespace may be null. Otherwise, it must
  // not be null.
  bool ParseBase64(const string& prefix, const string& name,
                   string* value) const override;
  bool ParseIntArrayBase64(const string& prefix, const string& name,
                           std::vector<int>* values) const override;
  bool ParseFloatArrayBase64(const string& prefix, const string& name,
                             std::vector<float>* values) const override;
  bool ParseDoubleArrayBase64(const string& prefix, const string& name,
                              std::vector<double>* values) const override;
  bool ParseBoolean(const string& prefix, const string& name,
                    bool* value) const override;
  bool ParseDouble(const string& prefix, const string& name,
                   double* value) const override;
  bool ParseInt(const string& prefix, const string& name,
                int* value) const override;
  bool ParseFloat(const string& prefix, const string& name,
                  float* value) const override;
  bool ParseLong(const string& prefix, const string& name,
                 int64* value) const override;
  bool ParseString(const string& prefix, const string& name,
                   string* value) const override;

  // Parses the numbers in an rdf:Seq list into the values collection.
  // The given collection is cleared of any existing values, and the
  // parsed numbers are written to it.
  bool ParseIntArray(const string& prefix, const string& list_name,
                     std::vector<int>* values) const override;
  bool ParseDoubleArray(const string& prefix, const string& list_name,
                        std::vector<double>* values) const override;

  // Disallow copying.
  DeserializerImpl(const DeserializerImpl&) = delete;
  void operator=(const DeserializerImpl&) = delete;

 private:
  xmlNodePtr node_;
  // Remembers the parent node of the last deserializer created on the rdf:Seq
  // node. For performance reasons only, to avoid unnessarily traversing
  // the XML document tree.
  mutable xmlNodePtr list_node_;
  // Lock modifications of list_node_ in const functions to make it thread-safe.
  mutable std::mutex mtx_;
};

}  // namespace xml
}  // namespace photos_editing_formats

#endif // DYNAMIC_DEPTH_INTERNAL_XMPMETA_XML_DESERIALIZER_IMPL_H_  // NOLINT