source: main/trunk/greenstone2/perllib/cpan/Image/ExifTool/JSON.pm@ 34921

Last change on this file since 34921 was 34921, checked in by anupama, 3 years ago

Committing the improvements to EmbeddedMetaPlugin's processing of Keywords vs other metadata fields. Keywords were literally stored as arrays of words rather than phrases in PDFs (at least in Diego's sample PDF), whereas other meta fields like Subjects and Creators stored them as arrays of phrases. To get both to work, Kathy updated EXIF to a newer version, to retrieve the actual EXIF values stored in the PDF. And Kathy and Dr Bainbridge came up with a new option that I added called apply_join_before_split_to_metafields that's a regex which can list the metadata fields to apply the join_before_split to and whcih previously always got applied to all metadata fields. Now it's applied to any *Keywords metafields by default, as that's the metafield we have experience of that behaves differently to the others, as it stores by word instead of phrases. Tested on Diego's sample PDF. Diego has double-checked it to works on his sample PDF too, setting the split char to ; and turning on the join_before_split and leaving apply_join_before_split_to_metafields at its default of .*Keywords. File changes are strings.properties for the tooltip, the plugin introducing the option and working with it and Kathy's EXIF updates affecting cpan/File and cpan/Image.

File size: 5.0 KB
Line 
1#------------------------------------------------------------------------------
2# File: JSON.pm
3#
4# Description: Read JSON files
5#
6# Notes: Set ExifTool MissingTagValue to "null" to ignore JSON nulls
7#
8# Revisions: 2017/03/13 - P. Harvey Created
9#------------------------------------------------------------------------------
10
11package Image::ExifTool::JSON;
12use strict;
13use vars qw($VERSION);
14use Image::ExifTool qw(:DataAccess :Utils);
15use Image::ExifTool::Import;
16
17$VERSION = '1.02';
18
19sub ProcessTag($$$$%);
20
21%Image::ExifTool::JSON::Main = (
22 GROUPS => { 0 => 'JSON', 1 => 'JSON', 2 => 'Other' },
23 VARS => { NO_ID => 1 },
24 NOTES => q{
25 Other than a few tags in the table below, JSON tags have not been
26 pre-defined. However, ExifTool will read any existing tags from basic
27 JSON-formatted files.
28 },
29 # ON1 settings tags
30 ON1_SettingsData => {
31 RawConv => q{
32 require Image::ExifTool::XMP;
33 $val = Image::ExifTool::XMP::DecodeBase64($val);
34 },
35 SubDirectory => { TagTable => 'Image::ExifTool::PLIST::Main' },
36 },
37 ON1_SettingsMetadataCreated => { Groups => { 2 => 'Time' } },
38 ON1_SettingsMetadataModified => { Groups => { 2 => 'Time' } },
39 ON1_SettingsMetadataName => { },
40 ON1_SettingsMetadataPluginID => { },
41 ON1_SettingsMetadataTimestamp => { Groups => { 2 => 'Time' } },
42 ON1_SettingsMetadataUsage => { },
43 ON1_SettingsMetadataVisibleToUser=>{ },
44);
45
46#------------------------------------------------------------------------------
47# Store a tag value
48# Inputs: 0) ExifTool ref, 1) tag table, 2) tag ID, 3) value, 4) tagInfo flags
49sub FoundTag($$$$%)
50{
51 my ($et, $tagTablePtr, $tag, $val, %flags) = @_;
52
53 # special case to reformat ON1 tag names
54 if ($tag =~ s/^settings\w{8}-\w{4}-\w{4}-\w{4}-\w{12}(Data|Metadata.+)$/ON1_Settings$1/) {
55 $et->OverrideFileType('ONP','application/on1') if $$et{FILE_TYPE} eq 'JSON';
56 }
57
58 # avoid conflict with special table entries
59 $tag .= '!' if $Image::ExifTool::specialTags{$tag};
60
61 AddTagToTable($tagTablePtr, $tag, {
62 Name => Image::ExifTool::MakeTagName($tag),
63 %flags,
64 Temporary => 1,
65 }) unless $$tagTablePtr{$tag};
66
67 $et->HandleTag($tagTablePtr, $tag, $val);
68}
69
70#------------------------------------------------------------------------------
71# Process a JSON tag
72# Inputs: 0) ExifTool ref, 1) tag table, 2) tag ID, 3) value, 4) tagInfo flags
73# - expands structures into flattened tags as required
74sub ProcessTag($$$$%)
75{
76 local $_;
77 my ($et, $tagTablePtr, $tag, $val, %flags) = @_;
78
79 if (ref $val eq 'HASH') {
80 if ($et->Options('Struct')) {
81 FoundTag($et, $tagTablePtr, $tag, $val, %flags, Struct => 1);
82 return unless $et->Options('Struct') > 1;
83 }
84 foreach (sort keys %$val) {
85 ProcessTag($et, $tagTablePtr, $tag . ucfirst, $$val{$_}, %flags, Flat => 1);
86 }
87 } elsif (ref $val eq 'ARRAY') {
88 foreach (@$val) {
89 ProcessTag($et, $tagTablePtr, $tag, $_, %flags, List => 1);
90 }
91 } elsif (defined $val) {
92 FoundTag($et, $tagTablePtr, $tag, $val, %flags);
93 }
94}
95
96#------------------------------------------------------------------------------
97# Extract meta information from a JSON file
98# Inputs: 0) ExifTool object reference, 1) dirInfo reference
99# Returns: 1 on success, 0 if this wasn't a recognized JSON file
100sub ProcessJSON($$)
101{
102 local $_;
103 my ($et, $dirInfo) = @_;
104 my $raf = $$dirInfo{RAF};
105 my $structOpt = $et->Options('Struct');
106 my (%database, $key, $tag);
107
108 # read information from JSON file into database structure
109 my $err = Image::ExifTool::Import::ReadJSON($raf, \%database,
110 $et->Options('MissingTagValue'), $et->Options('Charset'));
111
112 return 0 if $err or not %database;
113
114 $et->SetFileType();
115
116 my $tagTablePtr = GetTagTable('Image::ExifTool::JSON::Main');
117
118 # remove any old tag definitions in case they change flags
119 foreach $key (TagTableKeys($tagTablePtr)) {
120 delete $$tagTablePtr{$key} if $$tagTablePtr{$key}{Temporary};
121 }
122
123 # extract tags from JSON database
124 foreach $key (sort keys %database) {
125 foreach $tag (sort keys %{$database{$key}}) {
126 my $val = $database{$key}{$tag};
127 # (ignore SourceFile if generated automatically by ReadJSON)
128 next if $tag eq 'SourceFile' and defined $val and $val eq '*';
129 ProcessTag($et, $tagTablePtr, $tag, $val);
130 }
131 }
132 return 1;
133}
134
1351; # end
136
137__END__
138
139=head1 NAME
140
141Image::ExifTool::JSON - Read JSON files
142
143=head1 SYNOPSIS
144
145This module is used by Image::ExifTool
146
147=head1 DESCRIPTION
148
149This module contains definitions required by Image::ExifTool read
150information from JSON files.
151
152=head1 AUTHOR
153
154Copyright 2003-2021, Phil Harvey (philharvey66 at gmail.com)
155
156This library is free software; you can redistribute it and/or modify it
157under the same terms as Perl itself.
158
159=head1 SEE ALSO
160
161L<Image::ExifTool::TagNames/JSON Tags>,
162L<Image::ExifTool(3pm)|Image::ExifTool>
163
164=cut
165
Note: See TracBrowser for help on using the repository browser.