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,8 +73,20 @@ class QPDFJob | ||
| 73 | void initializeFromArgv(int argc, char* argv[], | 73 | void initializeFromArgv(int argc, char* argv[], |
| 74 | char const* progname_env = nullptr); | 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 | QPDF_DLL | 88 | QPDF_DLL |
| 77 | - void initializeFromJson(std::string const& json); | 89 | + void initializeFromJson(std::string const& json, bool partial = false); |
| 78 | 90 | ||
| 79 | // Set name that is used to prefix verbose messages, progress | 91 | // Set name that is used to prefix verbose messages, progress |
| 80 | // messages, and other things that the library writes to output | 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,7 +730,7 @@ QPDFJob::Config::jobJsonFile(char const* parameter) | ||
| 730 | QUtil::read_file_into_memory(parameter, file_buf, size); | 730 | QUtil::read_file_into_memory(parameter, file_buf, size); |
| 731 | try | 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 | catch (std::exception& e) | 735 | catch (std::exception& e) |
| 736 | { | 736 | { |
libqpdf/QPDFJob_json.cc
| @@ -15,7 +15,7 @@ namespace | @@ -15,7 +15,7 @@ namespace | ||
| 15 | class Handlers | 15 | class Handlers |
| 16 | { | 16 | { |
| 17 | public: | 17 | public: |
| 18 | - Handlers(std::shared_ptr<QPDFJob::Config> c_main); | 18 | + Handlers(bool partial, std::shared_ptr<QPDFJob::Config> c_main); |
| 19 | void handle(JSON&); | 19 | void handle(JSON&); |
| 20 | 20 | ||
| 21 | private: | 21 | private: |
| @@ -47,6 +47,7 @@ namespace | @@ -47,6 +47,7 @@ namespace | ||
| 47 | setup_handler_t bindSetup(void (Handlers::*f)(std::string const&)); | 47 | setup_handler_t bindSetup(void (Handlers::*f)(std::string const&)); |
| 48 | 48 | ||
| 49 | std::list<std::shared_ptr<JSONHandler>> json_handlers; | 49 | std::list<std::shared_ptr<JSONHandler>> json_handlers; |
| 50 | + bool partial; | ||
| 50 | JSONHandler* jh; // points to last of json_handlers | 51 | JSONHandler* jh; // points to last of json_handlers |
| 51 | std::shared_ptr<QPDFJob::Config> c_main; | 52 | std::shared_ptr<QPDFJob::Config> c_main; |
| 52 | std::shared_ptr<QPDFJob::CopyAttConfig> c_copy_att; | 53 | std::shared_ptr<QPDFJob::CopyAttConfig> c_copy_att; |
| @@ -57,7 +58,8 @@ namespace | @@ -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 | jh(nullptr), | 63 | jh(nullptr), |
| 62 | c_main(c_main) | 64 | c_main(c_main) |
| 63 | { | 65 | { |
| @@ -95,7 +97,12 @@ Handlers::initHandlers() | @@ -95,7 +97,12 @@ Handlers::initHandlers() | ||
| 95 | this->jh = this->json_handlers.back().get(); | 97 | this->jh = this->json_handlers.back().get(); |
| 96 | jh->addDictHandlers( | 98 | jh->addDictHandlers( |
| 97 | [](std::string const&, JSON){}, | 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 | # include <qpdf/auto_job_json_init.hh> | 107 | # include <qpdf/auto_job_json_init.hh> |
| 101 | 108 | ||
| @@ -623,7 +630,7 @@ Handlers::setupOptionsUnderlayPassword(std::string const& key) | @@ -623,7 +630,7 @@ Handlers::setupOptionsUnderlayPassword(std::string const& key) | ||
| 623 | } | 630 | } |
| 624 | 631 | ||
| 625 | void | 632 | void |
| 626 | -QPDFJob::initializeFromJson(std::string const& json) | 633 | +QPDFJob::initializeFromJson(std::string const& json, bool partial) |
| 627 | { | 634 | { |
| 628 | std::list<std::string> errors; | 635 | std::list<std::string> errors; |
| 629 | JSON j = JSON::parse(json); | 636 | JSON j = JSON::parse(json); |
| @@ -639,5 +646,5 @@ QPDFJob::initializeFromJson(std::string const& json) | @@ -639,5 +646,5 @@ QPDFJob::initializeFromJson(std::string const& json) | ||
| 639 | throw std::runtime_error(msg.str()); | 646 | throw std::runtime_error(msg.str()); |
| 640 | } | 647 | } |
| 641 | 648 | ||
| 642 | - Handlers(config()).handle(j); | 649 | + Handlers(partial, config()).handle(j); |
| 643 | } | 650 | } |