Image/ExifTool/iWork.pm

filter: Statementstmt 82.3% 70 / 85 Branchbran 40.0% 20 / 50 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 3 totaltotal 65.5% 99 / 151 CC 28 SCAR 36.0
CC
28
Cov
66%
Process_iWork
58.1
FoundTag
7.2
GetTagID
3.0
CRAP 36.5
« Image/ExifTool/NikonCapture.pm ↑ Summary Image/ExifTool/SigmaRaw.pm »
1 #------------------------------------------------------------------------------
2 # File: iWork.pm
3 #
4 # Description: Read Apple iWork '09 XML+ZIP files
5 #
6 # Revisions: 2009/11/11 - P. Harvey Created
7 #------------------------------------------------------------------------------
8  
9 package Image::ExifTool::iWork;
10  
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Image::ExifTool qw(:DataAccess :Utils);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Image::ExifTool::XMP;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Image::ExifTool::ZIP;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 $VERSION = '1.06';
18  
19 # test for recognized iWork document extensions and outer XML elements
20 1 my %iWorkType = (
21 # file extensions
22 NUMBERS => 'NUMBERS',
23 PAGES => 'PAGES',
24 KEY => 'KEY',
25 KTH => 'KTH',
26 NMBTEMPLATE => 'NMBTEMPLATE',
27 # we don't support double extensions --
28 # "PAGES.TEMPLATE" => 'Apple Pages Template',
29 # outer XML elements
30 'ls:document' => 'NUMBERS',
31 'sl:document' => 'PAGES',
32 'key:presentation' => 'KEY',
33 );
34  
35 # MIME types for iWork files (Apple has not registered these yet, but these
36 # are my best guess after doing some googling. I'm not 100% sure what "sff"
37 # indicates, but I think it refers to the new "flattened" package format)
38 1 my %mimeType = (
39 'NUMBERS' => 'application/x-iwork-numbers-sffnumbers',
40 'PAGES' => 'application/x-iwork-pages-sffpages',
41 'KEY' => 'application/x-iWork-keynote-sffkey',
42 'NMBTEMPLATE' => 'application/x-iwork-numbers-sfftemplate',
43 'PAGES.TEMPLATE'=> 'application/x-iwork-pages-sfftemplate',
44 'KTH' => 'application/x-iWork-keynote-sffkth',
45 );
46  
47 # iWork tags
48 1 %Image::ExifTool::iWork::Main = (
49 GROUPS => { 0 => 'XML', 1 => 'XML', 2 => 'Document' },
50 PROCESS_PROC => \&Image::ExifTool::XMP::ProcessXMP,
51 VARS => { ID_FMT => 'none' },
52 NOTES => q{
53 The Apple iWork '09 file format is a ZIP archive containing XML files
54 similar to the Office Open XML (OOXML) format. Metadata tags in iWork
55 files are extracted even if they don't appear below.
56 },
57 authors => { Name => 'Author', Groups => { 2 => 'Author' } },
58 comment => { },
59 copyright => { Groups => { 2 => 'Author' } },
60 keywords => { },
61 projects => { List => 1 },
62 title => { },
63 );
64  
65 #------------------------------------------------------------------------------
66 # Generate a tag ID for this XML tag
67 # Inputs: 0) tag property name list ref
68 # Returns: tagID
69 sub GetTagID($)
70 8 ▶{
Subroutine GetTagID: called CC=3 SCAR=11.1
Pod undocumented
71 8 my $props = shift;
72 8 ▶ return 0 if $$props[-1] =~ /^\w+:ID$/; # ignore ID tags
Branch: 1 branch
BranchTrueFalseTotal
if $props->[-1] =~ /^\w+:ID$/ 1 7 8
73 7 ▶ return $$props[0] =~ /^.*?:(.*)/ ? $1 : $$props[0];
Branch: 1 branch
BranchTrueFalseTotal
$props->[0] =~ /^.*?:(.*)/ ? : 7 0 7
74 }
75  
76 #------------------------------------------------------------------------------
77 # We found an XMP property name/value
78 # Inputs: 0) ExifTool object ref, 1) tag table ref
79 # 2) reference to array of XMP property names (last is current property)
80 # 3) property value, 4) attribute hash ref (not used here)
81 # Returns: 1 if valid tag was found
82 sub FoundTag($$$$;$)
83 8 ▶{
Subroutine FoundTag: called CC=6 SCAR=19.7
Pod undocumented
84 8 my ($et, $tagTablePtr, $props, $val, $attrs) = @_;
85 8 ▶ return 0 unless @$props;
Branch: 1 branch
BranchTrueFalseTotal
unless @$props 0 8 8
86 8 my $verbose = $et->Options('Verbose');
87  
88 8 ▶ $et->VPrint(0, " | - Tag '", join('/',@$props), "'\n") if $verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose > 1 0 8 8
89  
90 # un-escape XML character entities
91 8 $val = Image::ExifTool::XMP::UnescapeXML($val);
92 # convert from UTF8 to ExifTool Charset
93 8 $val = $et->Decode($val, 'UTF8');
94 8 ▶ my $tag = GetTagID($props) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $tag = &GetTagID($props) or return 0 7 1 8
95  
96 # add any unknown tags to table
97 7 ▶ unless ($$tagTablePtr{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tagTablePtr->{$tag}) 0 7 7
98 0 ▶ $et->VPrint(0, " [adding $tag]\n") if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
99 0 AddTagToTable($tagTablePtr, $tag, { Name => ucfirst $tag });
100 }
101 # save the tag
102 7 $et->HandleTag($tagTablePtr, $tag, $val);
103  
104 7 return 1;
105 }
106  
107 #------------------------------------------------------------------------------
108 # Extract information from an iWork file
109 # Inputs: 0) ExifTool object reference, 1) dirInfo reference
110 # Returns: 1
111 # Notes: Upon entry to this routine, the file type has already been verified
112 # as ZIP and the dirInfo hash contains a 'ZIP' Archive::Zip object reference
113 sub Process_iWork($$)
114 1 ▶{
Subroutine Process_iWork: called CC=21 SCAR=40.6
Pod undocumented
115 1 my ($et, $dirInfo) = @_;
116 1 my $zip = $$dirInfo{ZIP};
117 1 my ($type, $index, $indexFile, $status);
118  
119 # try to determine the file type
120 1 local $SIG{'__WARN__'} = \&Image::ExifTool::ZIP::WarnProc;
121 # trust type given by file extension if available
122 1 ▶ $type = $iWorkType{$$et{FILE_EXT}} if $$et{FILE_EXT};
Branch: 1 branch
BranchTrueFalseTotal
if $et->{"FILE_EXT"} 1 0 1
123 1 ▶ unless ($type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 1 1
124 # read the index file
125 0 my @members = $zip->membersMatching('^index\.(xml|apxl)$');
126 0 ▶ if (@members) {
Branch: 1 branch
BranchTrueFalseTotal
if (@members) { } 0 0 0
127 0 ($index, $status) = $zip->contents($members[0]);
128 0 ▶ unless ($status) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($status) 0 0 0
129 0 $indexFile = $members[0]->fileName();
130 0 ▶ if ($index =~ /^\s*<\?xml version=[^<]+<(\w+:\w+)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index =~ /^\s*<\?xml version=[^<]+<(\w+:\w+)/s) 0 0 0
131 0 ▶ $type = $iWorkType{$1} if $iWorkType{$1};
Branch: 1 branch
BranchTrueFalseTotal
if $iWorkType{$1} 0 0 0
132 }
133 }
134 } else {
135 0 @members = $zip->membersMatching('(?i)^.*\.(pages|numbers|key)/Index.*');
136 0 ▶ if (@members) {
Branch: 1 branch
BranchTrueFalseTotal
if (@members) 0 0 0
137 0 my $tmp = $members[0]->fileName();
138 0 ▶ $type = $iWorkType{uc $1} if $tmp =~ /\.(pages|numbers|key)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp =~ /\.(pages|numbers|key)/i 0 0 0
139 }
140 }
141 0 ▶ $type or $type = 'ZIP'; # assume ZIP by default
Branch: 1 branch
BranchTrueFalseTotal
$type or $type = "ZIP" 0 0 0
142 }
143 1 $et->SetFileType($type, $mimeType{$type});
144  
145 1 my @members = $zip->members();
146 1 my $docNum = 0;
147 1 my $member;
148 1 foreach $member (@members) {
149 # get filename of this ZIP member
150 4 my $file = $member->fileName();
151 4 ▶ next unless defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 0 4 4
152 4 $et->VPrint(0, "File: $file\n");
153 # set the document number and extract ZIP tags
154 4 $$et{DOC_NUM} = ++$docNum;
155 4 Image::ExifTool::ZIP::HandleMember($et, $member);
156  
157 # process only the index XML and JPEG thumbnail/preview files
158 4 ▶ next unless $file =~ m{^(index\.(xml|apxl)|QuickLook/Thumbnail\.jpg|[^/]+/preview(-micro|-web)?.jpg)$}i;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ m[^(index\.(xml|apxl)|QuickLook/Thumbnail\.jpg|[^/]+/preview(-micro|-web)?.jpg)$]i 2 2 4
159 # get the file contents if necessary
160 # (CAREFUL! $buff MUST be local since we hand off a value ref to PreviewImage)
161 2 my ($buff, $buffPt);
162 2 ▶ if ($indexFile and $indexFile eq $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($indexFile and $indexFile eq $file) { } 0 2 2
Condition33%1/3
$indexFile and $indexFile eq $file
!ll&&!rl&&r
200
MC/DC0%0/2
$indexFile and $indexFile eq $file
$indexFile $indexFile eq $file
Truth table33%1/3
A and B
A= $indexFileB= $indexFile eq $file
ABresult
0X0
100
111
163 # use the index file we already loaded
164 0 $buffPt = \$index;
165 } else {
166 2 ($buff, $status) = $zip->contents($member);
167 2 ▶ $status and $et->Warn("Error extracting $file"), next;
Branch: 1 branch
BranchTrueFalseTotal
$status and $et->Warn("Error extracting $file"), next 0 2 2
168 2 $buffPt = \$buff;
169 }
170 # extract JPEG as PreviewImage (should only be QuickLook/Thumbnail.jpg)
171 2 ▶ if ($file =~ /\.jpg$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ /\.jpg$/) 1 1 2
172 1 ▶ my $type = ($file =~ /preview-(\w+)/) ? ($1 eq 'web' ? 'Other' : 'Thumbnail') : 'Preview';
Branch: 2 branches
BranchTrueFalseTotal
$file =~ /preview-(\w+)/ ? : 0 1 1
$1 eq 'web' ? : 0 0 0
173 1 $et->FoundTag($type . 'Image', $buffPt);
174 1 next;
175 }
176 # process "metadata" section of XML index file
177 1 ▶ next unless $$buffPt =~ /<(\w+):metadata>/g;
Branch: 1 branch
BranchTrueFalseTotal
unless $$buffPt =~ /<(\w+):metadata>/g 0 1 1
178 1 my $ns = $1;
179 1 my $p1 = pos $$buffPt;
180 1 ▶ next unless $$buffPt =~ m{</${ns}:metadata>}g;
Branch: 1 branch
BranchTrueFalseTotal
unless $$buffPt =~ m[</$ns:metadata>]g 0 1 1
181 # construct XML data from "metadata" section only
182 1 $$buffPt = '<?xml version="1.0"?>' . substr($$buffPt, $p1, pos($$buffPt)-$p1);
183 1 my %dirInfo = (
184 DataPt => $buffPt,
185 DirLen => length $$buffPt,
186 DataLen => length $$buffPt,
187 XMPParseOpts => {
188 FoundProc => \&FoundTag,
189 },
190 );
191 1 my $tagTablePtr = GetTagTable('Image::ExifTool::iWork::Main');
192 1 $et->ProcessDirectory(\%dirInfo, $tagTablePtr);
193 1 undef $$buffPt; # (free memory now)
194 }
195 1 delete $$et{DOC_NUM};
196 1 return 1;
197 }
198  
199 1 1; # end
200  
201 __END__
« Image/ExifTool/NikonCapture.pm ↑ Summary Image/ExifTool/SigmaRaw.pm »