Commit b3da5a2cba7645de4db6c0b66bc31b963dff8066
1 parent
7985c773
Switch json args and structure
Showing
1 changed file
with
64 additions
and
29 deletions
qpdf/qpdf.cc
| ... | ... | @@ -119,7 +119,7 @@ struct Options |
| 119 | 119 | show_filtered_stream_data(false), |
| 120 | 120 | show_pages(false), |
| 121 | 121 | show_page_images(false), |
| 122 | - show_json(false), | |
| 122 | + json(false), | |
| 123 | 123 | check(false), |
| 124 | 124 | require_outfile(true), |
| 125 | 125 | infilename(0), |
| ... | ... | @@ -193,7 +193,7 @@ struct Options |
| 193 | 193 | bool show_filtered_stream_data; |
| 194 | 194 | bool show_pages; |
| 195 | 195 | bool show_page_images; |
| 196 | - bool show_json; | |
| 196 | + bool json; | |
| 197 | 197 | bool check; |
| 198 | 198 | std::vector<PageSpec> page_specs; |
| 199 | 199 | std::map<std::string, RotationSpec> rotations; |
| ... | ... | @@ -329,7 +329,7 @@ class ArgParser |
| 329 | 329 | void argShowNpages(); |
| 330 | 330 | void argShowPages(); |
| 331 | 331 | void argWithImages(); |
| 332 | - void argShowJson(); | |
| 332 | + void argJson(); | |
| 333 | 333 | void argCheck(); |
| 334 | 334 | void arg40Print(char* parameter); |
| 335 | 335 | void arg40Modify(char* parameter); |
| ... | ... | @@ -517,7 +517,7 @@ ArgParser::initOptionTable() |
| 517 | 517 | (*t)["show-npages"] = oe_bare(&ArgParser::argShowNpages); |
| 518 | 518 | (*t)["show-pages"] = oe_bare(&ArgParser::argShowPages); |
| 519 | 519 | (*t)["with-images"] = oe_bare(&ArgParser::argWithImages); |
| 520 | - (*t)["show-json"] = oe_bare(&ArgParser::argShowJson); | |
| 520 | + (*t)["json"] = oe_bare(&ArgParser::argJson); | |
| 521 | 521 | (*t)["check"] = oe_bare(&ArgParser::argCheck); |
| 522 | 522 | |
| 523 | 523 | t = &this->encrypt40_option_table; |
| ... | ... | @@ -980,9 +980,9 @@ ArgParser::argWithImages() |
| 980 | 980 | } |
| 981 | 981 | |
| 982 | 982 | void |
| 983 | -ArgParser::argShowJson() | |
| 983 | +ArgParser::argJson() | |
| 984 | 984 | { |
| 985 | - o.show_json = true; | |
| 985 | + o.json = true; | |
| 986 | 986 | o.require_outfile = false; |
| 987 | 987 | } |
| 988 | 988 | |
| ... | ... | @@ -1510,7 +1510,7 @@ ArgParser::usage(std::string const& message) |
| 1510 | 1510 | } |
| 1511 | 1511 | } |
| 1512 | 1512 | |
| 1513 | -static JSON json_schema() | |
| 1513 | +static JSON json_schema(Options& o) | |
| 1514 | 1514 | { |
| 1515 | 1515 | // This JSON object doubles as a schema and as documentation for |
| 1516 | 1516 | // our JSON output. Any schema mismatch is a bug in qpdf. This |
| ... | ... | @@ -1522,6 +1522,11 @@ static JSON json_schema() |
| 1522 | 1522 | schema.addDictionaryMember( |
| 1523 | 1523 | "version", JSON::makeString( |
| 1524 | 1524 | "JSON format serial number; increased for non-compatible changes")); |
| 1525 | + JSON j_params = schema.addDictionaryMember( | |
| 1526 | + "parameters", JSON::makeDictionary()); | |
| 1527 | + j_params.addDictionaryMember( | |
| 1528 | + "decodeLevel", JSON::makeString( | |
| 1529 | + "decode level used to determine stream filterability")); | |
| 1525 | 1530 | schema.addDictionaryMember( |
| 1526 | 1531 | "objects", JSON::makeString( |
| 1527 | 1532 | "Original objects; keys are 'trailer' or 'n n R'")); |
| ... | ... | @@ -1574,7 +1579,7 @@ static JSON json_schema() |
| 1574 | 1579 | "title", |
| 1575 | 1580 | JSON::makeString("outline title")); |
| 1576 | 1581 | outline.addDictionaryMember( |
| 1577 | - "destination", | |
| 1582 | + "dest", | |
| 1578 | 1583 | JSON::makeString("outline destination dictionary")); |
| 1579 | 1584 | return schema; |
| 1580 | 1585 | } |
| ... | ... | @@ -2486,17 +2491,8 @@ static void do_show_pages(QPDF& pdf, Options& o) |
| 2486 | 2491 | } |
| 2487 | 2492 | } |
| 2488 | 2493 | |
| 2489 | -static void do_show_json(QPDF& pdf, Options& o) | |
| 2494 | +static void do_json_objects(QPDF& pdf, Options& o, JSON& j) | |
| 2490 | 2495 | { |
| 2491 | - JSON j = JSON::makeDictionary(); | |
| 2492 | - // This version is updated every time a non-backward-compatible | |
| 2493 | - // change is made to the JSON format. Clients of the JSON are to | |
| 2494 | - // ignore unrecognized keys, so we only update the version of a | |
| 2495 | - // key disappears or if its value changes meaning. | |
| 2496 | - j.addDictionaryMember("version", JSON::makeInt(1)); | |
| 2497 | - | |
| 2498 | - // Objects | |
| 2499 | - | |
| 2500 | 2496 | // Add all objects. Do this first before other code below modifies |
| 2501 | 2497 | // things by doing stuff like calling |
| 2502 | 2498 | // pushInheritedAttributesToPage. |
| ... | ... | @@ -2509,15 +2505,16 @@ static void do_show_json(QPDF& pdf, Options& o) |
| 2509 | 2505 | j_objects.addDictionaryMember( |
| 2510 | 2506 | (*iter).unparse(), (*iter).getJSON(true)); |
| 2511 | 2507 | } |
| 2508 | +} | |
| 2512 | 2509 | |
| 2513 | - // Pages | |
| 2514 | - | |
| 2510 | +static void do_json_pages(QPDF& pdf, Options& o, JSON& j) | |
| 2511 | +{ | |
| 2515 | 2512 | JSON j_pages = j.addDictionaryMember("pages", JSON::makeArray()); |
| 2516 | - QPDFPageDocumentHelper dh(pdf); | |
| 2513 | + QPDFPageDocumentHelper pdh(pdf); | |
| 2517 | 2514 | QPDFPageLabelDocumentHelper pldh(pdf); |
| 2518 | 2515 | QPDFOutlineDocumentHelper odh(pdf); |
| 2519 | - dh.pushInheritedAttributesToPage(); | |
| 2520 | - std::vector<QPDFPageObjectHelper> pages = dh.getAllPages(); | |
| 2516 | + pdh.pushInheritedAttributesToPage(); | |
| 2517 | + std::vector<QPDFPageObjectHelper> pages = pdh.getAllPages(); | |
| 2521 | 2518 | size_t pageno = 0; |
| 2522 | 2519 | for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin(); |
| 2523 | 2520 | iter != pages.end(); ++iter, ++pageno) |
| ... | ... | @@ -2592,13 +2589,17 @@ static void do_show_json(QPDF& pdf, Options& o) |
| 2592 | 2589 | j_outline.addDictionaryMember( |
| 2593 | 2590 | "title", JSON::makeString((*oiter).getTitle())); |
| 2594 | 2591 | j_outline.addDictionaryMember( |
| 2595 | - "destination", (*oiter).getDest().getJSON(true)); | |
| 2592 | + "dest", (*oiter).getDest().getJSON(true)); | |
| 2596 | 2593 | } |
| 2597 | 2594 | } |
| 2595 | +} | |
| 2598 | 2596 | |
| 2599 | - // Page labels | |
| 2600 | - | |
| 2597 | +static void do_json_page_labels(QPDF& pdf, Options& o, JSON& j) | |
| 2598 | +{ | |
| 2601 | 2599 | JSON j_labels = j.addDictionaryMember("pagelabels", JSON::makeArray()); |
| 2600 | + QPDFPageLabelDocumentHelper pldh(pdf); | |
| 2601 | + QPDFPageDocumentHelper pdh(pdf); | |
| 2602 | + std::vector<QPDFPageObjectHelper> pages = pdh.getAllPages(); | |
| 2602 | 2603 | if (pldh.hasPageLabels()) |
| 2603 | 2604 | { |
| 2604 | 2605 | std::vector<QPDFObjectHandle> labels; |
| ... | ... | @@ -2621,10 +2622,44 @@ static void do_show_json(QPDF& pdf, Options& o) |
| 2621 | 2622 | j_label.addDictionaryMember("label", (*iter).getJSON()); |
| 2622 | 2623 | } |
| 2623 | 2624 | } |
| 2625 | +} | |
| 2626 | + | |
| 2627 | +static void do_json(QPDF& pdf, Options& o) | |
| 2628 | +{ | |
| 2629 | + JSON j = JSON::makeDictionary(); | |
| 2630 | + // This version is updated every time a non-backward-compatible | |
| 2631 | + // change is made to the JSON format. Clients of the JSON are to | |
| 2632 | + // ignore unrecognized keys, so we only update the version of a | |
| 2633 | + // key disappears or if its value changes meaning. | |
| 2634 | + j.addDictionaryMember("version", JSON::makeInt(1)); | |
| 2635 | + JSON j_params = j.addDictionaryMember( | |
| 2636 | + "parameters", JSON::makeDictionary()); | |
| 2637 | + std::string decode_level_str; | |
| 2638 | + switch (o.decode_level) | |
| 2639 | + { | |
| 2640 | + case qpdf_dl_none: | |
| 2641 | + decode_level_str = "none"; | |
| 2642 | + break; | |
| 2643 | + case qpdf_dl_generalized: | |
| 2644 | + decode_level_str = "generalized"; | |
| 2645 | + break; | |
| 2646 | + case qpdf_dl_specialized: | |
| 2647 | + decode_level_str = "specialized"; | |
| 2648 | + break; | |
| 2649 | + case qpdf_dl_all: | |
| 2650 | + decode_level_str = "all"; | |
| 2651 | + break; | |
| 2652 | + } | |
| 2653 | + j_params.addDictionaryMember( | |
| 2654 | + "decodeLevel", JSON::makeString(decode_level_str)); | |
| 2655 | + | |
| 2656 | + do_json_objects(pdf, o, j); | |
| 2657 | + do_json_pages(pdf, o, j); | |
| 2658 | + do_json_page_labels(pdf, o, j); | |
| 2624 | 2659 | |
| 2625 | 2660 | // Check against schema |
| 2626 | 2661 | |
| 2627 | - JSON schema = json_schema(); | |
| 2662 | + JSON schema = json_schema(o); | |
| 2628 | 2663 | std::list<std::string> errors; |
| 2629 | 2664 | if (! j.checkSchema(schema, errors)) |
| 2630 | 2665 | { |
| ... | ... | @@ -2651,9 +2686,9 @@ static void do_inspection(QPDF& pdf, Options& o) |
| 2651 | 2686 | { |
| 2652 | 2687 | do_check(pdf, o, exit_code); |
| 2653 | 2688 | } |
| 2654 | - if (o.show_json) | |
| 2689 | + if (o.json) | |
| 2655 | 2690 | { |
| 2656 | - do_show_json(pdf, o); | |
| 2691 | + do_json(pdf, o); | |
| 2657 | 2692 | } |
| 2658 | 2693 | if (o.show_npages) |
| 2659 | 2694 | { | ... | ... |