Commit b3da5a2cba7645de4db6c0b66bc31b963dff8066

Authored by Jay Berkenbilt
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,7 +119,7 @@ struct Options
119 show_filtered_stream_data(false), 119 show_filtered_stream_data(false),
120 show_pages(false), 120 show_pages(false),
121 show_page_images(false), 121 show_page_images(false),
122 - show_json(false), 122 + json(false),
123 check(false), 123 check(false),
124 require_outfile(true), 124 require_outfile(true),
125 infilename(0), 125 infilename(0),
@@ -193,7 +193,7 @@ struct Options @@ -193,7 +193,7 @@ struct Options
193 bool show_filtered_stream_data; 193 bool show_filtered_stream_data;
194 bool show_pages; 194 bool show_pages;
195 bool show_page_images; 195 bool show_page_images;
196 - bool show_json; 196 + bool json;
197 bool check; 197 bool check;
198 std::vector<PageSpec> page_specs; 198 std::vector<PageSpec> page_specs;
199 std::map<std::string, RotationSpec> rotations; 199 std::map<std::string, RotationSpec> rotations;
@@ -329,7 +329,7 @@ class ArgParser @@ -329,7 +329,7 @@ class ArgParser
329 void argShowNpages(); 329 void argShowNpages();
330 void argShowPages(); 330 void argShowPages();
331 void argWithImages(); 331 void argWithImages();
332 - void argShowJson(); 332 + void argJson();
333 void argCheck(); 333 void argCheck();
334 void arg40Print(char* parameter); 334 void arg40Print(char* parameter);
335 void arg40Modify(char* parameter); 335 void arg40Modify(char* parameter);
@@ -517,7 +517,7 @@ ArgParser::initOptionTable() @@ -517,7 +517,7 @@ ArgParser::initOptionTable()
517 (*t)["show-npages"] = oe_bare(&ArgParser::argShowNpages); 517 (*t)["show-npages"] = oe_bare(&ArgParser::argShowNpages);
518 (*t)["show-pages"] = oe_bare(&ArgParser::argShowPages); 518 (*t)["show-pages"] = oe_bare(&ArgParser::argShowPages);
519 (*t)["with-images"] = oe_bare(&ArgParser::argWithImages); 519 (*t)["with-images"] = oe_bare(&ArgParser::argWithImages);
520 - (*t)["show-json"] = oe_bare(&ArgParser::argShowJson); 520 + (*t)["json"] = oe_bare(&ArgParser::argJson);
521 (*t)["check"] = oe_bare(&ArgParser::argCheck); 521 (*t)["check"] = oe_bare(&ArgParser::argCheck);
522 522
523 t = &this->encrypt40_option_table; 523 t = &this->encrypt40_option_table;
@@ -980,9 +980,9 @@ ArgParser::argWithImages() @@ -980,9 +980,9 @@ ArgParser::argWithImages()
980 } 980 }
981 981
982 void 982 void
983 -ArgParser::argShowJson() 983 +ArgParser::argJson()
984 { 984 {
985 - o.show_json = true; 985 + o.json = true;
986 o.require_outfile = false; 986 o.require_outfile = false;
987 } 987 }
988 988
@@ -1510,7 +1510,7 @@ ArgParser::usage(std::string const&amp; message) @@ -1510,7 +1510,7 @@ ArgParser::usage(std::string const&amp; message)
1510 } 1510 }
1511 } 1511 }
1512 1512
1513 -static JSON json_schema() 1513 +static JSON json_schema(Options& o)
1514 { 1514 {
1515 // This JSON object doubles as a schema and as documentation for 1515 // This JSON object doubles as a schema and as documentation for
1516 // our JSON output. Any schema mismatch is a bug in qpdf. This 1516 // our JSON output. Any schema mismatch is a bug in qpdf. This
@@ -1522,6 +1522,11 @@ static JSON json_schema() @@ -1522,6 +1522,11 @@ static JSON json_schema()
1522 schema.addDictionaryMember( 1522 schema.addDictionaryMember(
1523 "version", JSON::makeString( 1523 "version", JSON::makeString(
1524 "JSON format serial number; increased for non-compatible changes")); 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 schema.addDictionaryMember( 1530 schema.addDictionaryMember(
1526 "objects", JSON::makeString( 1531 "objects", JSON::makeString(
1527 "Original objects; keys are 'trailer' or 'n n R'")); 1532 "Original objects; keys are 'trailer' or 'n n R'"));
@@ -1574,7 +1579,7 @@ static JSON json_schema() @@ -1574,7 +1579,7 @@ static JSON json_schema()
1574 "title", 1579 "title",
1575 JSON::makeString("outline title")); 1580 JSON::makeString("outline title"));
1576 outline.addDictionaryMember( 1581 outline.addDictionaryMember(
1577 - "destination", 1582 + "dest",
1578 JSON::makeString("outline destination dictionary")); 1583 JSON::makeString("outline destination dictionary"));
1579 return schema; 1584 return schema;
1580 } 1585 }
@@ -2486,17 +2491,8 @@ static void do_show_pages(QPDF&amp; pdf, Options&amp; o) @@ -2486,17 +2491,8 @@ static void do_show_pages(QPDF&amp; pdf, Options&amp; 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 // Add all objects. Do this first before other code below modifies 2496 // Add all objects. Do this first before other code below modifies
2501 // things by doing stuff like calling 2497 // things by doing stuff like calling
2502 // pushInheritedAttributesToPage. 2498 // pushInheritedAttributesToPage.
@@ -2509,15 +2505,16 @@ static void do_show_json(QPDF&amp; pdf, Options&amp; o) @@ -2509,15 +2505,16 @@ static void do_show_json(QPDF&amp; pdf, Options&amp; o)
2509 j_objects.addDictionaryMember( 2505 j_objects.addDictionaryMember(
2510 (*iter).unparse(), (*iter).getJSON(true)); 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 JSON j_pages = j.addDictionaryMember("pages", JSON::makeArray()); 2512 JSON j_pages = j.addDictionaryMember("pages", JSON::makeArray());
2516 - QPDFPageDocumentHelper dh(pdf); 2513 + QPDFPageDocumentHelper pdh(pdf);
2517 QPDFPageLabelDocumentHelper pldh(pdf); 2514 QPDFPageLabelDocumentHelper pldh(pdf);
2518 QPDFOutlineDocumentHelper odh(pdf); 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 size_t pageno = 0; 2518 size_t pageno = 0;
2522 for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin(); 2519 for (std::vector<QPDFPageObjectHelper>::iterator iter = pages.begin();
2523 iter != pages.end(); ++iter, ++pageno) 2520 iter != pages.end(); ++iter, ++pageno)
@@ -2592,13 +2589,17 @@ static void do_show_json(QPDF&amp; pdf, Options&amp; o) @@ -2592,13 +2589,17 @@ static void do_show_json(QPDF&amp; pdf, Options&amp; o)
2592 j_outline.addDictionaryMember( 2589 j_outline.addDictionaryMember(
2593 "title", JSON::makeString((*oiter).getTitle())); 2590 "title", JSON::makeString((*oiter).getTitle()));
2594 j_outline.addDictionaryMember( 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 JSON j_labels = j.addDictionaryMember("pagelabels", JSON::makeArray()); 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 if (pldh.hasPageLabels()) 2603 if (pldh.hasPageLabels())
2603 { 2604 {
2604 std::vector<QPDFObjectHandle> labels; 2605 std::vector<QPDFObjectHandle> labels;
@@ -2621,10 +2622,44 @@ static void do_show_json(QPDF&amp; pdf, Options&amp; o) @@ -2621,10 +2622,44 @@ static void do_show_json(QPDF&amp; pdf, Options&amp; o)
2621 j_label.addDictionaryMember("label", (*iter).getJSON()); 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 // Check against schema 2660 // Check against schema
2626 2661
2627 - JSON schema = json_schema(); 2662 + JSON schema = json_schema(o);
2628 std::list<std::string> errors; 2663 std::list<std::string> errors;
2629 if (! j.checkSchema(schema, errors)) 2664 if (! j.checkSchema(schema, errors))
2630 { 2665 {
@@ -2651,9 +2686,9 @@ static void do_inspection(QPDF&amp; pdf, Options&amp; o) @@ -2651,9 +2686,9 @@ static void do_inspection(QPDF&amp; pdf, Options&amp; o)
2651 { 2686 {
2652 do_check(pdf, o, exit_code); 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 if (o.show_npages) 2693 if (o.show_npages)
2659 { 2694 {