Validate XML with xerces.
[libdcp.git] / tools / dcpverify.cc
1 /*
2     Copyright (C) 2019 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 "verify.h"
35 #include "compose.hpp"
36 #include <boost/bind.hpp>
37 #include <boost/optional.hpp>
38 #include <boost/filesystem.hpp>
39 #include <boost/foreach.hpp>
40 #include <getopt.h>
41 #include <iostream>
42 #include <cstdlib>
43
44 using std::cout;
45 using std::cerr;
46 using std::string;
47 using std::vector;
48 using std::list;
49 using boost::bind;
50 using boost::optional;
51
52 static void
53 help (string n)
54 {
55         cerr << "Syntax: " << n << " [OPTION] <DCP>\n"
56              << "  -V, --version   show libdcp version\n"
57              << "  -h, --help      show this help\n";
58 }
59
60 void
61 stage (string s, optional<boost::filesystem::path> path)
62 {
63         if (path) {
64                 cout << s << ": " << path->string() << "\n";
65         } else {
66                 cout << s << "\n";
67         }
68 }
69
70 void
71 progress ()
72 {
73
74 }
75
76 int
77 main (int argc, char* argv[])
78 {
79         int option_index = 0;
80         while (true) {
81                 static struct option long_options[] = {
82                         { "version", no_argument, 0, 'V'},
83                         { "help", no_argument, 0, 'h'},
84                         { 0, 0, 0, 0 }
85                 };
86
87                 int c = getopt_long (argc, argv, "Vh", long_options, &option_index);
88
89                 if (c == -1) {
90                         break;
91                 }
92
93                 switch (c) {
94                 case 'V':
95                         cout << "dcpverify version " << LIBDCP_VERSION << "\n";
96                         exit (EXIT_SUCCESS);
97                 case 'h':
98                         help (argv[0]);
99                         exit (EXIT_SUCCESS);
100                 }
101         }
102
103         if (argc <= optind) {
104                 help (argv[0]);
105                 exit (EXIT_FAILURE);
106         }
107
108         if (!boost::filesystem::exists (argv[optind])) {
109                 cerr << argv[0] << ": DCP " << argv[optind] << " not found.\n";
110                 exit (EXIT_FAILURE);
111         }
112
113         vector<boost::filesystem::path> directories;
114         directories.push_back (argv[optind]);
115         /* XXX */
116         list<dcp::VerificationNote> notes = dcp::verify (directories, bind(&stage, _1, _2), bind(&progress), "xsd");
117
118         bool failed = false;
119         BOOST_FOREACH (dcp::VerificationNote i, notes) {
120                 switch (i.type()) {
121                 case dcp::VerificationNote::VERIFY_ERROR:
122                         cout << "Error: " << note_to_string(i) << "\n";
123                         failed = true;
124                         break;
125                 case dcp::VerificationNote::VERIFY_WARNING:
126                         cout << "Warning: " << note_to_string(i) << "\n";
127                         break;
128                 }
129         }
130
131         if (!failed) {
132                 cout << "DCP verified OK.\n";
133         }
134
135         exit (failed ? EXIT_FAILURE : EXIT_SUCCESS);
136 }