Handle pages/under/overlay JSON file in begin

...since they have to be handled before other options. It was working
because, in both cases, `file` was alphabetically before the other
keys, but this implementation gives a stronger guarantee.
This commit is contained in:
Jay Berkenbilt 2024-01-09 16:54:11 -05:00
parent 6488b156f7
commit 12f7a4461b
5 changed files with 50 additions and 7 deletions

View File

@ -57,6 +57,8 @@ namespace
bare_handler_t bindBare(void (Handlers::*f)());
json_handler_t bindJSON(void (Handlers::*f)(JSON));
void beginUnderOverlay(JSON const& j);
std::list<std::shared_ptr<JSONHandler>> json_handlers;
bool partial;
JSONHandler* jh{nullptr}; // points to last of json_handlers
@ -225,6 +227,24 @@ Handlers::handle(JSON& j)
this->json_handlers.back()->handle(".", j);
}
void
Handlers::beginUnderOverlay(JSON const& j)
{
// File has to be processed before items, so handle it here.
bool file_seen = false;
std::string file;
j.forEachDictItem([&](std::string const& key, JSON const& value) {
if (key == "file") {
file_seen = value.getString(file);
}
});
if (!file_seen) {
QTC::TC("qpdf", "QPDFJob json over/under no file");
usage("file is required in underlay/overlay specification");
}
c_uo->file(file);
}
void
Handlers::setupInputFile()
{
@ -468,16 +488,17 @@ void
Handlers::beginPages(JSON j)
{
bool file_seen = false;
std::string file;
j.forEachDictItem([&](std::string const& key, JSON const& value) {
if (key == "file") {
std::string v;
file_seen = value.getString(v);
file_seen = value.getString(file);
}
});
if (!file_seen) {
QTC::TC("qpdf", "QPDFJob json pages no file");
usage("file is required in page specification");
}
c_pages->file(file);
}
void
@ -489,7 +510,8 @@ Handlers::endPages()
void
Handlers::setupPagesFile()
{
addParameter([this](char const* p) { c_pages->file(p); });
// This is handled in beginPages since file() has to be called first.
ignoreItem();
}
void
@ -499,9 +521,10 @@ Handlers::setupPagesPassword()
}
void
Handlers::beginOverlay(JSON)
Handlers::beginOverlay(JSON j)
{
this->c_uo = c_main->overlay();
beginUnderOverlay(j);
}
void
@ -514,7 +537,8 @@ Handlers::endOverlay()
void
Handlers::setupOverlayFile()
{
addParameter([this](char const* p) { c_uo->file(p); });
// This is handled in beginOverlay since file() has to be called first.
ignoreItem();
}
void
@ -524,9 +548,10 @@ Handlers::setupOverlayPassword()
}
void
Handlers::beginUnderlay(JSON)
Handlers::beginUnderlay(JSON j)
{
this->c_uo = c_main->underlay();
beginUnderOverlay(j);
}
void
@ -539,7 +564,8 @@ Handlers::endUnderlay()
void
Handlers::setupUnderlayFile()
{
addParameter([this](char const* p) { c_uo->file(p); });
// This is handled in beginUnderlay since file() has to be called first.
ignoreItem();
}
void

View File

@ -691,3 +691,4 @@ QPDFPageDocumentHelper flatten resources missing or invalid 0
QPDF recover xref stream 0
QPDFJob misplaced page range 0
QPDFJob duplicated range 0
QPDFJob json over/under no file 0

View File

@ -0,0 +1,5 @@
{
"overlay": {
"to": "1-z"
}
}

View File

@ -0,0 +1,10 @@
qpdf: error with job-json file bad-json-overlay-no-file.json: file is required in underlay/overlay specification
Run qpdf --job-json-help for information on the file format.
For help:
qpdf --help=usage usage information
qpdf --help=topic help on a topic
qpdf --help=--option help on an option
qpdf --help general help and a topic list

View File

@ -26,6 +26,7 @@ my @bad_json = (
"encrypt-missing-password",
"encrypt-no-key-length",
"pages-no-file",
"overlay-no-file",
"schema-error",
"json-error"
);