2022-01-26 21:48:18 +00:00
|
|
|
#include <qpdf/QPDFJob.hh>
|
|
|
|
#include <qpdf/JSONHandler.hh>
|
2022-01-29 17:29:38 +00:00
|
|
|
#include <qpdf/QPDFUsage.hh>
|
2022-01-26 21:48:18 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <sstream>
|
2022-01-29 17:29:38 +00:00
|
|
|
#include <cstring>
|
2022-01-26 21:48:18 +00:00
|
|
|
|
|
|
|
static JSON JOB_SCHEMA = JSON::parse(QPDFJob::json_job_schema_v1().c_str());
|
|
|
|
|
2022-01-29 13:54:08 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class Handlers
|
|
|
|
{
|
|
|
|
public:
|
2022-01-29 14:01:20 +00:00
|
|
|
Handlers(std::shared_ptr<QPDFJob::Config> c_main);
|
2022-01-29 13:54:08 +00:00
|
|
|
void handle(JSON&);
|
|
|
|
|
|
|
|
private:
|
2022-01-29 14:08:45 +00:00
|
|
|
# include <qpdf/auto_job_json_decl.hh>
|
2022-01-29 13:54:08 +00:00
|
|
|
|
|
|
|
void usage(std::string const& message);
|
|
|
|
void initHandlers();
|
|
|
|
|
2022-01-29 17:29:38 +00:00
|
|
|
typedef std::function<void()> bare_arg_handler_t;
|
|
|
|
typedef std::function<void(char const*)> param_arg_handler_t;
|
|
|
|
|
|
|
|
void addBare(std::string const& key, bare_arg_handler_t);
|
|
|
|
void addParameter(std::string const& key, bool required,
|
|
|
|
param_arg_handler_t);
|
|
|
|
void addChoices(std::string const& key,
|
|
|
|
bool required, char const** choices,
|
|
|
|
param_arg_handler_t);
|
|
|
|
void beginDict(std::string const& key,
|
|
|
|
bare_arg_handler_t start_fn,
|
|
|
|
bare_arg_handler_t end_fn);
|
|
|
|
void endDict();
|
|
|
|
|
|
|
|
std::list<std::shared_ptr<JSONHandler>> json_handlers;
|
|
|
|
JSONHandler* jh; // points to last of json_handlers
|
2022-01-29 13:54:08 +00:00
|
|
|
std::shared_ptr<QPDFJob::Config> c_main;
|
|
|
|
std::shared_ptr<QPDFJob::CopyAttConfig> c_copy_att;
|
|
|
|
std::shared_ptr<QPDFJob::AttConfig> c_att;
|
|
|
|
std::shared_ptr<QPDFJob::PagesConfig> c_pages;
|
|
|
|
std::shared_ptr<QPDFJob::UOConfig> c_uo;
|
|
|
|
std::shared_ptr<QPDFJob::EncConfig> c_enc;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-29 14:01:20 +00:00
|
|
|
Handlers::Handlers(std::shared_ptr<QPDFJob::Config> c_main) :
|
2022-01-29 17:29:38 +00:00
|
|
|
jh(nullptr),
|
2022-01-29 13:54:08 +00:00
|
|
|
c_main(c_main)
|
|
|
|
{
|
|
|
|
initHandlers();
|
|
|
|
}
|
|
|
|
|
2022-01-29 17:29:38 +00:00
|
|
|
void
|
|
|
|
Handlers::usage(std::string const& message)
|
|
|
|
{
|
|
|
|
throw QPDFUsage(message);
|
|
|
|
}
|
|
|
|
|
2022-01-29 13:54:08 +00:00
|
|
|
void
|
|
|
|
Handlers::initHandlers()
|
|
|
|
{
|
2022-01-29 17:29:38 +00:00
|
|
|
this->json_handlers.push_back(std::make_shared<JSONHandler>());
|
|
|
|
this->jh = this->json_handlers.back().get();
|
|
|
|
jh->addDictHandlers(
|
2022-01-29 13:54:08 +00:00
|
|
|
[](std::string const&){},
|
2022-01-29 14:01:20 +00:00
|
|
|
[this](std::string const&){c_main->checkConfiguration();});
|
2022-01-29 13:54:08 +00:00
|
|
|
|
2022-01-29 17:29:38 +00:00
|
|
|
# include <qpdf/auto_job_json_init.hh>
|
2022-01-29 13:54:08 +00:00
|
|
|
|
2022-01-29 17:29:38 +00:00
|
|
|
// QXXXQ
|
|
|
|
auto empty = [](){};
|
|
|
|
beginDict("input", empty, empty);
|
|
|
|
beginDict("file", empty, empty);
|
|
|
|
addParameter("name", true, [this](char const* p) {
|
|
|
|
c_main->inputFile(p);
|
|
|
|
});
|
|
|
|
endDict(); // input.file
|
|
|
|
endDict(); // input
|
|
|
|
beginDict("output", empty, empty);
|
|
|
|
beginDict("file", empty, empty);
|
|
|
|
addParameter("name", true, [this](char const* p) {
|
|
|
|
c_main->outputFile(p);
|
|
|
|
});
|
|
|
|
endDict(); // output.file
|
|
|
|
beginDict("options", empty, empty);
|
|
|
|
addBare("qdf", [this]() {
|
|
|
|
c_main->qdf();
|
|
|
|
});
|
|
|
|
char const* choices[] = {"disable", "preserve", "generate", 0};
|
|
|
|
addChoices("objectStreams", true, choices, [this](char const* p) {
|
|
|
|
c_main->objectStreams(p);
|
|
|
|
});
|
|
|
|
endDict(); // output.options
|
|
|
|
endDict(); // output
|
|
|
|
// /QXXXQ
|
|
|
|
|
|
|
|
if (this->json_handlers.size() != 1)
|
|
|
|
{
|
|
|
|
throw std::logic_error("QPDFJob_json: json_handlers size != 1 at end");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Handlers::addBare(std::string const& key, bare_arg_handler_t fn)
|
|
|
|
{
|
|
|
|
auto h = std::make_shared<JSONHandler>();
|
|
|
|
h->addBoolHandler([this, fn](std::string const& path, bool v){
|
|
|
|
if (! v)
|
|
|
|
{
|
|
|
|
usage(path + ": value must be true");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fn();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
jh->addDictKeyHandler(key, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Handlers::addParameter(std::string const& key,
|
|
|
|
bool required,
|
|
|
|
param_arg_handler_t fn)
|
|
|
|
{
|
|
|
|
auto h = std::make_shared<JSONHandler>();
|
|
|
|
h->addStringHandler(
|
|
|
|
[fn](std::string const& path, std::string const& parameter){
|
|
|
|
fn(parameter.c_str());
|
2022-01-29 13:54:08 +00:00
|
|
|
});
|
2022-01-29 17:29:38 +00:00
|
|
|
if (! required)
|
|
|
|
{
|
|
|
|
h->addNullHandler(
|
|
|
|
[fn](std::string const& path){
|
|
|
|
fn(nullptr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
jh->addDictKeyHandler(key, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Handlers::addChoices(std::string const& key,
|
|
|
|
bool required, char const** choices,
|
|
|
|
param_arg_handler_t fn)
|
|
|
|
{
|
|
|
|
auto h = std::make_shared<JSONHandler>();
|
|
|
|
h->addStringHandler(
|
|
|
|
[fn, choices, this](
|
|
|
|
std::string const& path, std::string const& parameter){
|
|
|
|
|
|
|
|
char const* p = parameter.c_str();
|
|
|
|
bool matches = false;
|
|
|
|
for (char const** i = choices; *i; ++i)
|
|
|
|
{
|
|
|
|
if (strcmp(*i, p) == 0)
|
|
|
|
{
|
|
|
|
matches = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (! matches)
|
|
|
|
{
|
|
|
|
std::ostringstream msg;
|
|
|
|
msg << path + ": unexpected value; expected one of ";
|
|
|
|
bool first = true;
|
|
|
|
for (char const** i = choices; *i; ++i)
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msg << ", ";
|
|
|
|
}
|
|
|
|
msg << *i;
|
|
|
|
}
|
|
|
|
usage(msg.str());
|
|
|
|
}
|
|
|
|
fn(parameter.c_str());
|
2022-01-29 13:54:08 +00:00
|
|
|
});
|
2022-01-29 17:29:38 +00:00
|
|
|
if (! required)
|
|
|
|
{
|
|
|
|
h->addNullHandler(
|
|
|
|
[fn](std::string const& path){
|
|
|
|
fn(nullptr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
jh->addDictKeyHandler(key, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Handlers::beginDict(std::string const& key,
|
|
|
|
bare_arg_handler_t start_fn,
|
|
|
|
bare_arg_handler_t end_fn)
|
|
|
|
{
|
|
|
|
auto new_jh = std::make_shared<JSONHandler>();
|
|
|
|
new_jh->addDictHandlers(
|
|
|
|
[start_fn](std::string const&){ start_fn(); },
|
|
|
|
[end_fn](std::string const&){ end_fn(); });
|
|
|
|
this->jh->addDictKeyHandler(key, new_jh);
|
|
|
|
this->json_handlers.push_back(new_jh);
|
|
|
|
this->jh = new_jh.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Handlers::endDict()
|
|
|
|
{
|
|
|
|
this->json_handlers.pop_back();
|
|
|
|
this->jh = this->json_handlers.back().get();
|
2022-01-29 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Handlers::handle(JSON& j)
|
|
|
|
{
|
2022-01-29 17:29:38 +00:00
|
|
|
this->json_handlers.back()->handle(".", j);
|
2022-01-29 13:54:08 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 21:48:18 +00:00
|
|
|
void
|
|
|
|
QPDFJob::initializeFromJson(std::string const& json)
|
|
|
|
{
|
|
|
|
std::list<std::string> errors;
|
|
|
|
JSON j = JSON::parse(json);
|
|
|
|
if (! j.checkSchema(JOB_SCHEMA, JSON::f_optional, errors))
|
|
|
|
{
|
|
|
|
std::ostringstream msg;
|
|
|
|
msg << this->m->message_prefix
|
|
|
|
<< ": job json has errors:";
|
|
|
|
for (auto const& error: errors)
|
|
|
|
{
|
|
|
|
msg << std::endl << " " << error;
|
|
|
|
}
|
|
|
|
throw std::runtime_error(msg.str());
|
|
|
|
}
|
|
|
|
|
2022-01-29 14:01:20 +00:00
|
|
|
Handlers(config()).handle(j);
|
2022-01-26 21:48:18 +00:00
|
|
|
}
|