Commit 4a2a8283dc55136ec9a91a42265bbe8ee1e2dd10
1 parent
83fa78e2
Refactor JSON schema key handling in QPDFJob::json_schema.
Centralize JSON schema dictionary member addition by introducing `add_if_want_key`. This reduces redundancy and improves maintainability by consolidating repeated logic. No changes to functionality were made.
Showing
1 changed file
with
36 additions
and
39 deletions
libqpdf/QPDFJob.cc
| ... | ... | @@ -1397,20 +1397,22 @@ QPDFJob::json_schema(int json_version, std::set<std::string>* keys) |
| 1397 | 1397 | "decodelevel": "decode level used to determine stream filterability" |
| 1398 | 1398 | })")); |
| 1399 | 1399 | |
| 1400 | - bool all_keys = !keys || keys->empty(); | |
| 1400 | + const bool all_keys = !keys || keys->empty(); | |
| 1401 | 1401 | |
| 1402 | - auto want_key = [&](std::string const& key) -> bool { return all_keys || keys->contains(key); }; | |
| 1402 | + auto add_if_want_key = [&](std::string const& key, std::string const& json) -> void { | |
| 1403 | + if (all_keys || keys->contains(key)) { | |
| 1404 | + (void)schema.addDictionaryMember(key, JSON::parse(json)); | |
| 1405 | + } | |
| 1406 | + }; | |
| 1403 | 1407 | |
| 1404 | 1408 | // The list of selectable top-level keys id duplicated in the following places: job.yml, |
| 1405 | 1409 | // QPDFJob::json_schema, and QPDFJob::doJSON. |
| 1406 | 1410 | if (json_version == 1) { |
| 1407 | - if (want_key("objects")) { | |
| 1408 | - (void)schema.addDictionaryMember("objects", JSON::parse(R"({ | |
| 1411 | + add_if_want_key("objects", R"({ | |
| 1409 | 1412 | "<n n R|trailer>": "json representation of object" |
| 1410 | -})")); | |
| 1411 | - } | |
| 1412 | - if (want_key("objectinfo")) { | |
| 1413 | - (void)schema.addDictionaryMember("objectinfo", JSON::parse(R"({ | |
| 1413 | +})"); | |
| 1414 | + | |
| 1415 | + add_if_want_key("objectinfo", R"({ | |
| 1414 | 1416 | "<object-id>": { |
| 1415 | 1417 | "stream": { |
| 1416 | 1418 | "filter": "if stream, its filters, otherwise null", |
| ... | ... | @@ -1418,11 +1420,10 @@ QPDFJob::json_schema(int json_version, std::set<std::string>* keys) |
| 1418 | 1420 | "length": "if stream, its length, otherwise null" |
| 1419 | 1421 | } |
| 1420 | 1422 | } |
| 1421 | -})")); | |
| 1422 | - } | |
| 1423 | +})"); | |
| 1424 | + | |
| 1423 | 1425 | } else { |
| 1424 | - if (want_key("qpdf")) { | |
| 1425 | - (void)schema.addDictionaryMember("qpdf", JSON::parse(R"([{ | |
| 1426 | + add_if_want_key("qpdf", R"([{ | |
| 1426 | 1427 | "jsonversion": "numeric JSON version", |
| 1427 | 1428 | "pdfversion": "PDF version as x.y", |
| 1428 | 1429 | "pushedinheritedpageresources": "whether inherited attributes were pushed to the page level", |
| ... | ... | @@ -1431,11 +1432,9 @@ QPDFJob::json_schema(int json_version, std::set<std::string>* keys) |
| 1431 | 1432 | }, |
| 1432 | 1433 | { |
| 1433 | 1434 | "<obj:n n R|trailer>": "json representation of object" |
| 1434 | -}])")); | |
| 1435 | - } | |
| 1435 | +}])"); | |
| 1436 | 1436 | } |
| 1437 | - if (want_key("pages")) { | |
| 1438 | - (void)schema.addDictionaryMember("pages", JSON::parse(R"([ | |
| 1437 | + add_if_want_key("pages", R"([ | |
| 1439 | 1438 | { |
| 1440 | 1439 | "contents": [ |
| 1441 | 1440 | "reference to each content stream" |
| ... | ... | @@ -1468,18 +1467,16 @@ QPDFJob::json_schema(int json_version, std::set<std::string>* keys) |
| 1468 | 1467 | ], |
| 1469 | 1468 | "pageposfrom1": "position of page in document numbering from 1" |
| 1470 | 1469 | } |
| 1471 | -])")); | |
| 1472 | - } | |
| 1473 | - if (want_key("pagelabels")) { | |
| 1474 | - (void)schema.addDictionaryMember("pagelabels", JSON::parse(R"([ | |
| 1470 | +])"); | |
| 1471 | + | |
| 1472 | + add_if_want_key("pagelabels", R"([ | |
| 1475 | 1473 | { |
| 1476 | 1474 | "index": "starting page position starting from zero", |
| 1477 | 1475 | "label": "page label dictionary" |
| 1478 | 1476 | } |
| 1479 | -])")); | |
| 1480 | - } | |
| 1481 | - if (want_key("outlines")) { | |
| 1482 | - (void)schema.addDictionaryMember("outlines", JSON::parse(R"([ | |
| 1477 | +])"); | |
| 1478 | + | |
| 1479 | + add_if_want_key("outlines", R"([ | |
| 1483 | 1480 | { |
| 1484 | 1481 | "dest": "outline destination dictionary", |
| 1485 | 1482 | "destpageposfrom1": "position of destination page in document numbered from 1; null if not known", |
| ... | ... | @@ -1488,10 +1485,9 @@ QPDFJob::json_schema(int json_version, std::set<std::string>* keys) |
| 1488 | 1485 | "open": "whether the outline is displayed expanded", |
| 1489 | 1486 | "title": "outline title" |
| 1490 | 1487 | } |
| 1491 | -])")); | |
| 1492 | - } | |
| 1493 | - if (want_key("acroform")) { | |
| 1494 | - (void)schema.addDictionaryMember("acroform", JSON::parse(R"({ | |
| 1488 | +])"); | |
| 1489 | + | |
| 1490 | + add_if_want_key("acroform", R"({ | |
| 1495 | 1491 | "fields": [ |
| 1496 | 1492 | { |
| 1497 | 1493 | "alternativename": "alternative name of field -- this is the one usually shown to users", |
| ... | ... | @@ -1520,16 +1516,18 @@ QPDFJob::json_schema(int json_version, std::set<std::string>* keys) |
| 1520 | 1516 | ], |
| 1521 | 1517 | "hasacroform": "whether the document has interactive forms", |
| 1522 | 1518 | "needappearances": "whether the form fields' appearance streams need to be regenerated" |
| 1523 | -})")); | |
| 1524 | - } | |
| 1519 | +})"); | |
| 1520 | + | |
| 1525 | 1521 | std::string MODIFY_ANNOTATIONS = |
| 1526 | 1522 | (json_version == 1 ? "moddifyannotations" : "modifyannotations"); |
| 1527 | - if (want_key("encrypt")) { | |
| 1528 | - (void)schema.addDictionaryMember("encrypt", JSON::parse(R"({ | |
| 1523 | + add_if_want_key( | |
| 1524 | + "encrypt", | |
| 1525 | + R"({ | |
| 1529 | 1526 | "capabilities": { |
| 1530 | 1527 | "accessibility": "allow extraction for accessibility?", |
| 1531 | 1528 | "extract": "allow extraction?", |
| 1532 | - ")" + MODIFY_ANNOTATIONS + R"(": "allow modifying annotations?", | |
| 1529 | + ")" + MODIFY_ANNOTATIONS + | |
| 1530 | + R"(": "allow modifying annotations?", | |
| 1533 | 1531 | "modify": "allow all modifications?", |
| 1534 | 1532 | "modifyassembly": "allow modifying document assembly?", |
| 1535 | 1533 | "modifyforms": "allow modifying forms?", |
| ... | ... | @@ -1552,10 +1550,9 @@ QPDFJob::json_schema(int json_version, std::set<std::string>* keys) |
| 1552 | 1550 | "stringmethod": "encryption method for string" |
| 1553 | 1551 | }, |
| 1554 | 1552 | "userpasswordmatched": "whether supplied password matched user password; always false for non-encrypted files" |
| 1555 | -})")); | |
| 1556 | - } | |
| 1557 | - if (want_key("attachments")) { | |
| 1558 | - (void)schema.addDictionaryMember("attachments", JSON::parse(R"({ | |
| 1553 | +})"); | |
| 1554 | + | |
| 1555 | + add_if_want_key("attachments", R"({ | |
| 1559 | 1556 | "<attachment-key>": { |
| 1560 | 1557 | "filespec": "object containing the file spec", |
| 1561 | 1558 | "preferredcontents": "most preferred embedded file stream", |
| ... | ... | @@ -1573,8 +1570,8 @@ QPDFJob::json_schema(int json_version, std::set<std::string>* keys) |
| 1573 | 1570 | } |
| 1574 | 1571 | } |
| 1575 | 1572 | } |
| 1576 | -})")); | |
| 1577 | - } | |
| 1573 | +})"); | |
| 1574 | + | |
| 1578 | 1575 | return schema; |
| 1579 | 1576 | } |
| 1580 | 1577 | ... | ... |