Fully indent PKL/CPL.
[libdcp.git] / src / pkl.cc
1 /*
2     Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34 #include "pkl.h"
35 #include "exceptions.h"
36 #include "util.h"
37 #include "raw_convert.h"
38 #include "dcp_assert.h"
39 #include <libxml++/libxml++.h>
40 #include <boost/foreach.hpp>
41 #include <iostream>
42
43 using std::string;
44 using boost::shared_ptr;
45 using boost::optional;
46 using namespace dcp;
47
48 static string const pkl_interop_ns = "http://www.digicine.com/PROTO-ASDCP-PKL-20040311#";
49 static string const pkl_smpte_ns   = "http://www.smpte-ra.org/schemas/429-8/2007/PKL";
50
51 PKL::PKL (boost::filesystem::path file)
52 {
53         cxml::Document pkl ("PackingList");
54         pkl.read_file (file);
55
56         if (pkl.namespace_uri() == pkl_interop_ns) {
57                 _standard = INTEROP;
58         } else if (pkl.namespace_uri() == pkl_smpte_ns) {
59                 _standard = SMPTE;
60         } else {
61                 boost::throw_exception (XMLError ("Unrecognised packing list namesapce " + pkl.namespace_uri()));
62         }
63
64         _id = remove_urn_uuid (pkl.string_child ("Id"));
65         _annotation_text = pkl.optional_string_child ("AnnotationText");
66         _issue_date = pkl.string_child ("IssueDate");
67         _issuer = pkl.string_child ("Issuer");
68         _creator = pkl.string_child ("Creator");
69
70         BOOST_FOREACH (cxml::ConstNodePtr i, pkl.node_child("AssetList")->node_children("Asset")) {
71                 _asset_list.push_back (shared_ptr<Asset> (new Asset (i)));
72         }
73 }
74
75 void
76 PKL::add_asset (std::string id, boost::optional<std::string> annotation_text, std::string hash, int64_t size, std::string type)
77 {
78         _asset_list.push_back (shared_ptr<Asset> (new Asset (id, annotation_text, hash, size, type)));
79 }
80
81 void
82 PKL::write (boost::filesystem::path file, shared_ptr<const CertificateChain> signer) const
83 {
84         xmlpp::Document doc;
85         xmlpp::Element* pkl;
86         if (_standard == INTEROP) {
87                 pkl = doc.create_root_node("PackingList", pkl_interop_ns);
88         } else {
89                 pkl = doc.create_root_node("PackingList", pkl_smpte_ns);
90         }
91
92         pkl->add_child("Id")->add_child_text ("urn:uuid:" + _id);
93         if (_annotation_text) {
94                 pkl->add_child("AnnotationText")->add_child_text (*_annotation_text);
95         }
96         pkl->add_child("IssueDate")->add_child_text (_issue_date);
97         pkl->add_child("Issuer")->add_child_text (_issuer);
98         pkl->add_child("Creator")->add_child_text (_creator);
99
100         xmlpp::Element* asset_list = pkl->add_child("AssetList");
101         BOOST_FOREACH (shared_ptr<Asset> i, _asset_list) {
102                 xmlpp::Element* asset = asset_list->add_child("Asset");
103                 asset->add_child("Id")->add_child_text ("urn:uuid:" + i->id());
104                 if (i->annotation_text) {
105                         asset->add_child("AnnotationText")->add_child_text (*i->annotation_text);
106                 }
107                 asset->add_child("Hash")->add_child_text (i->hash);
108                 asset->add_child("Size")->add_child_text (raw_convert<string> (i->size));
109                 asset->add_child("Type")->add_child_text (i->type);
110         }
111
112         indent (pkl, 0);
113
114         if (signer) {
115                 signer->sign (pkl, _standard);
116         }
117
118         doc.write_to_file_formatted (file.string(), "UTF-8");
119 }
120
121 optional<string>
122 PKL::hash (string id) const
123 {
124         BOOST_FOREACH (shared_ptr<Asset> i, _asset_list) {
125                 if (i->id() == id) {
126                         return i->hash;
127                 }
128         }
129
130         return optional<string>();
131 }
132
133 optional<string>
134 PKL::type (string id) const
135 {
136         BOOST_FOREACH (shared_ptr<Asset> i, _asset_list) {
137                 if (i->id() == id) {
138                         return i->type;
139                 }
140         }
141
142         return optional<string>();
143 }