Commit 1355d95d0811942758036e23d64ed6b9fb7c8317
1 parent
cd30f626
QPDFJob: partial mode for initializeFromJson
Showing
3 changed files
with
26 additions
and
7 deletions
include/qpdf/QPDFJob.hh
| ... | ... | @@ -73,8 +73,20 @@ class QPDFJob |
| 73 | 73 | void initializeFromArgv(int argc, char* argv[], |
| 74 | 74 | char const* progname_env = nullptr); |
| 75 | 75 | |
| 76 | + // Initialize a QPDFJob from json. Passing partial = true prevents | |
| 77 | + // this method from doing the final checks (calling | |
| 78 | + // checkConfiguration) after processing the json file. This makes | |
| 79 | + // it possible to initialze QPDFJob in stages using multiple json | |
| 80 | + // files or to have a json file that can be processed from the CLI | |
| 81 | + // with --job-json-file and be combined with other arguments. For | |
| 82 | + // example, you might include only encryption parameters, leaving | |
| 83 | + // it up to the rest of the command-line arguments to provide | |
| 84 | + // input and output files. initializeFromJson is called with | |
| 85 | + // partial = true when invoked from the command line. To make sure | |
| 86 | + // that the json file is fully valid on its own, just don't | |
| 87 | + // specify any other command-line flags. | |
| 76 | 88 | QPDF_DLL |
| 77 | - void initializeFromJson(std::string const& json); | |
| 89 | + void initializeFromJson(std::string const& json, bool partial = false); | |
| 78 | 90 | |
| 79 | 91 | // Set name that is used to prefix verbose messages, progress |
| 80 | 92 | // messages, and other things that the library writes to output | ... | ... |
libqpdf/QPDFJob_config.cc
| ... | ... | @@ -730,7 +730,7 @@ QPDFJob::Config::jobJsonFile(char const* parameter) |
| 730 | 730 | QUtil::read_file_into_memory(parameter, file_buf, size); |
| 731 | 731 | try |
| 732 | 732 | { |
| 733 | - o.initializeFromJson(std::string(file_buf.getPointer(), size)); | |
| 733 | + o.initializeFromJson(std::string(file_buf.getPointer(), size), true); | |
| 734 | 734 | } |
| 735 | 735 | catch (std::exception& e) |
| 736 | 736 | { | ... | ... |
libqpdf/QPDFJob_json.cc
| ... | ... | @@ -15,7 +15,7 @@ namespace |
| 15 | 15 | class Handlers |
| 16 | 16 | { |
| 17 | 17 | public: |
| 18 | - Handlers(std::shared_ptr<QPDFJob::Config> c_main); | |
| 18 | + Handlers(bool partial, std::shared_ptr<QPDFJob::Config> c_main); | |
| 19 | 19 | void handle(JSON&); |
| 20 | 20 | |
| 21 | 21 | private: |
| ... | ... | @@ -47,6 +47,7 @@ namespace |
| 47 | 47 | setup_handler_t bindSetup(void (Handlers::*f)(std::string const&)); |
| 48 | 48 | |
| 49 | 49 | std::list<std::shared_ptr<JSONHandler>> json_handlers; |
| 50 | + bool partial; | |
| 50 | 51 | JSONHandler* jh; // points to last of json_handlers |
| 51 | 52 | std::shared_ptr<QPDFJob::Config> c_main; |
| 52 | 53 | std::shared_ptr<QPDFJob::CopyAttConfig> c_copy_att; |
| ... | ... | @@ -57,7 +58,8 @@ namespace |
| 57 | 58 | }; |
| 58 | 59 | } |
| 59 | 60 | |
| 60 | -Handlers::Handlers(std::shared_ptr<QPDFJob::Config> c_main) : | |
| 61 | +Handlers::Handlers(bool partial, std::shared_ptr<QPDFJob::Config> c_main) : | |
| 62 | + partial(partial), | |
| 61 | 63 | jh(nullptr), |
| 62 | 64 | c_main(c_main) |
| 63 | 65 | { |
| ... | ... | @@ -95,7 +97,12 @@ Handlers::initHandlers() |
| 95 | 97 | this->jh = this->json_handlers.back().get(); |
| 96 | 98 | jh->addDictHandlers( |
| 97 | 99 | [](std::string const&, JSON){}, |
| 98 | - [this](std::string const&){c_main->checkConfiguration();}); | |
| 100 | + [this](std::string const&){ | |
| 101 | + if (! this->partial) | |
| 102 | + { | |
| 103 | + c_main->checkConfiguration(); | |
| 104 | + } | |
| 105 | + }); | |
| 99 | 106 | |
| 100 | 107 | # include <qpdf/auto_job_json_init.hh> |
| 101 | 108 | |
| ... | ... | @@ -623,7 +630,7 @@ Handlers::setupOptionsUnderlayPassword(std::string const& key) |
| 623 | 630 | } |
| 624 | 631 | |
| 625 | 632 | void |
| 626 | -QPDFJob::initializeFromJson(std::string const& json) | |
| 633 | +QPDFJob::initializeFromJson(std::string const& json, bool partial) | |
| 627 | 634 | { |
| 628 | 635 | std::list<std::string> errors; |
| 629 | 636 | JSON j = JSON::parse(json); |
| ... | ... | @@ -639,5 +646,5 @@ QPDFJob::initializeFromJson(std::string const& json) |
| 639 | 646 | throw std::runtime_error(msg.str()); |
| 640 | 647 | } |
| 641 | 648 | |
| 642 | - Handlers(config()).handle(j); | |
| 649 | + Handlers(partial, config()).handle(j); | |
| 643 | 650 | } | ... | ... |