1 #ifndef DYNAMIC_DEPTH_INTERNAL_XMPMETA_XML_SERIALIZER_H_  // NOLINT
2 #define DYNAMIC_DEPTH_INTERNAL_XMPMETA_XML_SERIALIZER_H_  // NOLINT
3 
4 #include <memory>
5 #include <string>
6 #include <vector>
7 
8 #include "base/port.h"
9 
10 namespace dynamic_depth {
11 namespace xmpmeta {
12 namespace xml {
13 
14 // Serializes properties for a hierarchy of objects.
15 // Example:
16 //  BookSerializer serializer();
17 //  // Serialize a list of objects.
18 //  std::unique_ptr<Serializer> book_list_serializer =
19 //      serializer->CreateListSerializer("Books");
20 //  for (Book *book : book_list) {
21 //    std::unique_ptr<Serializer> book_serializer =
22 //        cameras_serializer->CreateItemSerializer("Book");
23 //    success &= book->Serialize(book_serializer.get());
24 //
25 //    // Write properties in an object.
26 //    // This would be called from the Book class.
27 //    string book_name("Book");
28 //    std::unique_ptr<Serializer> book_info_serializer =
29 //        book_serializer->CreateSerializer("Info");
30 //    book_info_serializer->WriteProperty("Author", "Cereal Eyser");
31 //    book_info_serializer->WriteProperty("ISBN", "314159265359");
32 //    std::unique_ptr<Serializer> genre_serializer =
33 //        book_serializer->CreateSeralizer("Genre", true);
34 //    std::unique_ptr<Serializer> fantasy_serializer =
35 //        genre_serializer->CreateSerialzer("Fantasy");
36 //    // Serialize genre properties here.
37 //  }
38 
39 class Serializer {
40  public:
~Serializer()41   virtual ~Serializer() {}
42 
43   // Returns a Serializer for an object that is an item in a list.
44   virtual std::unique_ptr<Serializer> CreateItemSerializer(
45       const string& prefix, const string& item_name) const = 0;
46 
47   // Returns a Serializer for a list of objects.
48   virtual std::unique_ptr<Serializer> CreateListSerializer(
49       const string& prefix, const string& list_name) const = 0;
50 
51   // Creates a serializer from the current serializer.
52   // node_ns_name is the XML namespace to which the newly created node belongs.
53   // If this parameter is an empty string, the new node will not belong to a
54   // namespace.
55   // node_name is the name of the new node. This parameter cannot be an empty
56   // string.
57   virtual std::unique_ptr<Serializer> CreateSerializer(
58       const string& node_ns_name, const string& node_name) const = 0;
59 
60   // Serializes a property with the given prefix.
61   // Example: <NodeName PropertyPrefix:PropertyName="PropertyValue" />
62   virtual bool WriteBoolProperty(const string& prefix, const string& name,
63                                  bool value) const = 0;
64   virtual bool WriteProperty(const string& prefix, const string& name,
65                              const string& value) const = 0;
66 
67   // Serializes the collection of values.
68   virtual bool WriteIntArray(const string& prefix, const string& array_name,
69                              const std::vector<int>& values) const = 0;
70   virtual bool WriteDoubleArray(const string& prefix, const string& array_name,
71                                 const std::vector<double>& values) const = 0;
72 };
73 
74 }  // namespace xml
75 }  // namespace xmpmeta
76 }  // namespace dynamic_depth
77 
78 #endif // DYNAMIC_DEPTH_INTERNAL_XMPMETA_XML_SERIALIZER_H_  // NOLINT
79