line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
2
|
|
|
|
|
|
|
# File: WriteXMP.pl |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Description: Write XMP meta information |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# Revisions: 12/19/2004 - P. Harvey Created |
7
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
8
|
|
|
|
|
|
|
package Image::ExifTool::XMP; |
9
|
|
|
|
|
|
|
|
10
|
37
|
|
|
37
|
|
313
|
use strict; |
|
37
|
|
|
|
|
107
|
|
|
37
|
|
|
|
|
1542
|
|
11
|
37
|
|
|
37
|
|
245
|
use vars qw(%specialStruct %dateTimeInfo %stdXlatNS); |
|
37
|
|
|
|
|
112
|
|
|
37
|
|
|
|
|
2360
|
|
12
|
|
|
|
|
|
|
|
13
|
37
|
|
|
37
|
|
258
|
use Image::ExifTool qw(:DataAccess :Utils); |
|
37
|
|
|
|
|
100
|
|
|
37
|
|
|
|
|
467898
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub CheckXMP($$$;$); |
16
|
|
|
|
|
|
|
sub CaptureXMP($$$;$); |
17
|
|
|
|
|
|
|
sub SetPropertyPath($$;$$$$); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $debug = 0; |
20
|
|
|
|
|
|
|
my $numPadLines = 24; # number of blank padding lines |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# when writing extended XMP, resources bigger than this get placed in their own |
23
|
|
|
|
|
|
|
# rdf:Description so they can be moved to the extended segments if necessary |
24
|
|
|
|
|
|
|
my $newDescThresh = 10240; # 10 kB |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# individual resources and namespaces to place last in separate rdf:Description's |
27
|
|
|
|
|
|
|
# so they can be moved to extended XMP segments if required (see Oct. 2008 XMP spec) |
28
|
|
|
|
|
|
|
my %extendedRes = ( |
29
|
|
|
|
|
|
|
'photoshop:History' => 1, |
30
|
|
|
|
|
|
|
'xap:Thumbnails' => 1, |
31
|
|
|
|
|
|
|
'xmp:Thumbnails' => 1, |
32
|
|
|
|
|
|
|
'crs' => 1, |
33
|
|
|
|
|
|
|
'crss' => 1, |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
my $rdfDesc = 'rdf:Description'; |
37
|
|
|
|
|
|
|
# |
38
|
|
|
|
|
|
|
# packet/xmp/rdf headers and trailers |
39
|
|
|
|
|
|
|
# |
40
|
|
|
|
|
|
|
my $pktOpen = "\n"; |
41
|
|
|
|
|
|
|
my $xmlOpen = "\n"; |
42
|
|
|
|
|
|
|
my $xmpOpenPrefix = "
|
43
|
|
|
|
|
|
|
my $rdfOpen = "\n"; |
44
|
|
|
|
|
|
|
my $rdfClose = "\n"; |
45
|
|
|
|
|
|
|
my $xmpClose = "\n"; |
46
|
|
|
|
|
|
|
my $pktCloseW = ""; # writable by default |
47
|
|
|
|
|
|
|
my $pktCloseR = ""; |
48
|
|
|
|
|
|
|
my ($sp, $nl); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
51
|
|
|
|
|
|
|
# Get XMP opening tag (and set x:xmptk appropriately) |
52
|
|
|
|
|
|
|
# Inputs: 0) ExifTool object ref |
53
|
|
|
|
|
|
|
# Returns: x:xmpmeta opening tag |
54
|
|
|
|
|
|
|
sub XMPOpen($) |
55
|
|
|
|
|
|
|
{ |
56
|
122
|
|
|
122
|
0
|
321
|
my $et = shift; |
57
|
122
|
|
|
|
|
593
|
my $nv = $$et{NEW_VALUE}{$Image::ExifTool::XMP::x{xmptk}}; |
58
|
122
|
|
|
|
|
265
|
my $tk; |
59
|
122
|
100
|
|
|
|
442
|
if (defined $nv) { |
60
|
1
|
|
|
|
|
5
|
$tk = $et->GetNewValue($nv); |
61
|
1
|
50
|
|
|
|
11
|
$et->VerboseValue(($tk ? '+' : '-') . ' XMP-x:XMPToolkit', $tk); |
62
|
1
|
|
|
|
|
2
|
++$$et{CHANGED}; |
63
|
|
|
|
|
|
|
} else { |
64
|
121
|
|
|
|
|
422
|
$tk = "Image::ExifTool $Image::ExifTool::VERSION"; |
65
|
|
|
|
|
|
|
} |
66
|
122
|
50
|
|
|
|
835
|
my $str = $tk ? (" x:xmptk='" . EscapeXML($tk) . "'") : ''; |
67
|
122
|
|
|
|
|
617
|
return "$xmpOpenPrefix$str>\n"; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
71
|
|
|
|
|
|
|
# Validate XMP packet and set read or read/write mode |
72
|
|
|
|
|
|
|
# Inputs: 0) XMP data reference, 1) 'r' = read only, 'w' or undef = read/write |
73
|
|
|
|
|
|
|
# Returns: true if XMP is good (and adds packet header/trailer if necessary) |
74
|
|
|
|
|
|
|
sub ValidateXMP($;$) |
75
|
|
|
|
|
|
|
{ |
76
|
4
|
|
|
4
|
0
|
15
|
my ($xmpPt, $mode) = @_; |
77
|
4
|
|
|
|
|
20
|
$$xmpPt =~ s/^\s*\s*//s; # remove leading comment if it exists |
78
|
4
|
50
|
|
|
|
37
|
unless ($$xmpPt =~ /^\0*<\0*\?\0*x\0*p\0*a\0*c\0*k\0*e\0*t/) { |
79
|
0
|
0
|
|
|
|
0
|
return '' unless $$xmpPt =~ /^
|
80
|
|
|
|
|
|
|
# add required xpacket header/trailer |
81
|
0
|
|
|
|
|
0
|
$$xmpPt = $pktOpen . $$xmpPt . $pktCloseW; |
82
|
|
|
|
|
|
|
} |
83
|
4
|
100
|
|
|
|
17
|
$mode = 'w' unless $mode; |
84
|
4
|
|
|
|
|
21
|
my $end = substr($$xmpPt, -32, 32); |
85
|
|
|
|
|
|
|
# check for proper xpacket trailer and set r/w mode if necessary |
86
|
4
|
50
|
|
|
|
54
|
return '' unless $end =~ s/(e\0*n\0*d\0*=\0*['"]\0*)([rw])(\0*['"]\0*\?\0*>)/$1$mode$3/; |
87
|
4
|
50
|
|
|
|
18
|
substr($$xmpPt, -32, 32) = $end if $2 ne $mode; |
88
|
4
|
|
|
|
|
13
|
return 1; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
92
|
|
|
|
|
|
|
# Validate XMP property |
93
|
|
|
|
|
|
|
# Inputs: 0) ExifTool ref, 1) validate hash ref, 2) attribute hash ref |
94
|
|
|
|
|
|
|
# - issues warnings if problems detected |
95
|
|
|
|
|
|
|
sub ValidateProperty($$;$) |
96
|
|
|
|
|
|
|
{ |
97
|
0
|
|
|
0
|
0
|
0
|
my ($et, $propList, $attr) = @_; |
98
|
|
|
|
|
|
|
|
99
|
0
|
0
|
0
|
|
|
0
|
if ($$et{XmpValidate} and @$propList > 2) { |
100
|
0
|
0
|
0
|
|
|
0
|
if ($$propList[0] =~ /^x:x[ma]pmeta$/ and |
|
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
101
|
|
|
|
|
|
|
$$propList[1] eq 'rdf:RDF' and |
102
|
|
|
|
|
|
|
$$propList[2] =~ /rdf:Description( |$)/) |
103
|
|
|
|
|
|
|
{ |
104
|
0
|
0
|
|
|
|
0
|
if (@$propList > 3) { |
105
|
0
|
0
|
|
|
|
0
|
if ($$propList[-1] =~ /^rdf:(Bag|Seq|Alt)$/) { |
106
|
0
|
|
|
|
|
0
|
$et->Warn("Ignored empty $$propList[-1] list for $$propList[-2]", 1); |
107
|
|
|
|
|
|
|
} else { |
108
|
0
|
0
|
0
|
|
|
0
|
if ($$propList[-2] eq 'rdf:Alt' and $attr) { |
109
|
0
|
|
|
|
|
0
|
my $lang = $$attr{'xml:lang'}; |
110
|
0
|
0
|
0
|
|
|
0
|
if ($lang and @$propList >= 5) { |
111
|
0
|
|
|
|
|
0
|
my $langPath = join('/', @$propList[3..($#$propList-2)]); |
112
|
0
|
|
0
|
|
|
0
|
my $valLang = $$et{XmpValidateLangAlt} || ($$et{XmpValidateLangAlt} = { }); |
113
|
0
|
0
|
|
|
|
0
|
$$valLang{$langPath} or $$valLang{$langPath} = { }; |
114
|
0
|
0
|
|
|
|
0
|
if ($$valLang{$langPath}{$lang}) { |
115
|
0
|
|
|
|
|
0
|
$et->WarnOnce("Duplicate language ($lang) in lang-alt list: $langPath"); |
116
|
|
|
|
|
|
|
} else { |
117
|
0
|
|
|
|
|
0
|
$$valLang{$langPath}{$lang} = 1; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
} |
121
|
0
|
|
|
|
|
0
|
my $xmpValidate = $$et{XmpValidate}; |
122
|
0
|
|
|
|
|
0
|
my $path = join('/', @$propList[3..$#$propList]); |
123
|
0
|
0
|
|
|
|
0
|
if (defined $$xmpValidate{$path}) { |
124
|
0
|
|
|
|
|
0
|
$et->Warn("Duplicate XMP property: $path"); |
125
|
|
|
|
|
|
|
} else { |
126
|
0
|
|
|
|
|
0
|
$$xmpValidate{$path} = 1; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
} elsif ($$propList[0] ne 'rdf:RDF' or |
131
|
|
|
|
|
|
|
$$propList[1] !~ /rdf:Description( |$)/) |
132
|
|
|
|
|
|
|
{ |
133
|
0
|
|
|
|
|
0
|
$et->Warn('Improperly enclosed XMP property: ' . join('/',@$propList)); |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
139
|
|
|
|
|
|
|
# Check XMP date values for validity and format accordingly |
140
|
|
|
|
|
|
|
# Inputs: 1) EXIF-format date string |
141
|
|
|
|
|
|
|
# Returns: XMP date/time string (or undef on error) |
142
|
|
|
|
|
|
|
sub FormatXMPDate($) |
143
|
|
|
|
|
|
|
{ |
144
|
127
|
|
|
127
|
0
|
374
|
my $val = shift; |
145
|
127
|
|
|
|
|
293
|
my ($y, $m, $d, $t, $tz); |
146
|
127
|
100
|
|
|
|
1051
|
if ($val =~ /(\d{4}):(\d{2}):(\d{2}) (\d{2}:\d{2}(?::\d{2}(?:\.\d*)?)?)(.*)/) { |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
147
|
99
|
|
|
|
|
652
|
($y, $m, $d, $t, $tz) = ($1, $2, $3, $4, $5); |
148
|
99
|
|
|
|
|
409
|
$val = "$y-$m-${d}T$t"; |
149
|
|
|
|
|
|
|
} elsif ($val =~ /^\s*\d{4}(:\d{2}){0,2}\s*$/) { |
150
|
|
|
|
|
|
|
# this is just a date (YYYY, YYYY-mm or YYYY-mm-dd) |
151
|
28
|
|
|
|
|
85
|
$val =~ tr/:/-/; |
152
|
|
|
|
|
|
|
} elsif ($val =~ /^\s*(\d{2}:\d{2}(?::\d{2}(?:\.\d*)?)?)(.*)\s*$/) { |
153
|
|
|
|
|
|
|
# this is just a time |
154
|
0
|
|
|
|
|
0
|
($t, $tz) = ($1, $2); |
155
|
0
|
|
|
|
|
0
|
$val = $t; |
156
|
|
|
|
|
|
|
} else { |
157
|
0
|
|
|
|
|
0
|
return undef; |
158
|
|
|
|
|
|
|
} |
159
|
127
|
100
|
|
|
|
387
|
if ($tz) { |
160
|
21
|
50
|
|
|
|
137
|
$tz =~ /^(Z|[+-]\d{2}:\d{2})$/ or return undef; |
161
|
21
|
|
|
|
|
67
|
$val .= $tz; |
162
|
|
|
|
|
|
|
} |
163
|
127
|
|
|
|
|
915
|
return $val; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
167
|
|
|
|
|
|
|
# Check XMP values for validity and format accordingly |
168
|
|
|
|
|
|
|
# Inputs: 0) ExifTool object ref, 1) tagInfo hash ref, 2) raw value ref, 3) conversion type |
169
|
|
|
|
|
|
|
# Returns: error string or undef (and may change value) on success |
170
|
|
|
|
|
|
|
# Note: copies structured information to avoid conflicts with calling code |
171
|
|
|
|
|
|
|
sub CheckXMP($$$;$) |
172
|
|
|
|
|
|
|
{ |
173
|
2528
|
|
|
2528
|
0
|
6440
|
my ($et, $tagInfo, $valPtr, $convType) = @_; |
174
|
|
|
|
|
|
|
|
175
|
2528
|
100
|
|
|
|
6387
|
if ($$tagInfo{Struct}) { |
176
|
95
|
|
|
|
|
7972
|
require 'Image/ExifTool/XMPStruct.pl'; |
177
|
95
|
|
|
|
|
291
|
my ($item, $err, $w, $warn); |
178
|
95
|
100
|
|
|
|
406
|
unless (ref $$valPtr) { |
179
|
73
|
|
|
|
|
358
|
($$valPtr, $warn) = InflateStruct($valPtr); |
180
|
|
|
|
|
|
|
# expect a structure HASH ref or ARRAY of structures |
181
|
73
|
100
|
|
|
|
306
|
unless (ref $$valPtr) { |
182
|
61
|
50
|
|
|
|
221
|
$$valPtr eq '' and $$valPtr = { }, return undef; # allow empty structures |
183
|
61
|
|
|
|
|
240
|
return 'Improperly formed structure'; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
} |
186
|
34
|
100
|
|
|
|
136
|
if (ref $$valPtr eq 'ARRAY') { |
187
|
1
|
50
|
|
|
|
8
|
return 'Not a list tag' unless $$tagInfo{List}; |
188
|
0
|
|
|
|
|
0
|
my @copy = ( @{$$valPtr} ); # copy the list for ExifTool to use |
|
0
|
|
|
|
|
0
|
|
189
|
0
|
|
|
|
|
0
|
$$valPtr = \@copy; # return the copy |
190
|
0
|
|
|
|
|
0
|
foreach $item (@copy) { |
191
|
0
|
0
|
|
|
|
0
|
unless (ref $item eq 'HASH') { |
192
|
0
|
|
|
|
|
0
|
($item, $w) = InflateStruct(\$item); # deserialize structure |
193
|
0
|
0
|
|
|
|
0
|
$w and $warn = $w; |
194
|
0
|
0
|
|
|
|
0
|
next if ref $item eq 'HASH'; |
195
|
0
|
|
|
|
|
0
|
$err = 'Improperly formed structure'; |
196
|
0
|
|
|
|
|
0
|
last; |
197
|
|
|
|
|
|
|
} |
198
|
0
|
|
|
|
|
0
|
($item, $err) = CheckStruct($et, $item, $$tagInfo{Struct}); |
199
|
0
|
0
|
|
|
|
0
|
last if $err; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
} else { |
202
|
33
|
|
|
|
|
178
|
($$valPtr, $err) = CheckStruct($et, $$valPtr, $$tagInfo{Struct}); |
203
|
|
|
|
|
|
|
} |
204
|
33
|
50
|
|
|
|
116
|
$warn and $$et{CHECK_WARN} = $warn; |
205
|
33
|
|
|
|
|
117
|
return $err; |
206
|
|
|
|
|
|
|
} |
207
|
2433
|
|
|
|
|
5077
|
my $format = $$tagInfo{Writable}; |
208
|
|
|
|
|
|
|
# (if no format specified, value is a simple string) |
209
|
2433
|
100
|
100
|
|
|
10932
|
if (not $format or $format eq 'string' or $format eq 'lang-alt') { |
|
|
|
100
|
|
|
|
|
210
|
|
|
|
|
|
|
# convert value to UTF8 if necessary |
211
|
1315
|
100
|
|
|
|
4155
|
if ($$et{OPTIONS}{Charset} ne 'UTF8') { |
212
|
4
|
50
|
|
|
|
20
|
if ($$valPtr =~ /[\x80-\xff]/) { |
213
|
|
|
|
|
|
|
# convert from Charset to UTF-8 |
214
|
4
|
|
|
|
|
17
|
$$valPtr = $et->Encode($$valPtr,'UTF8'); |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
} else { |
217
|
|
|
|
|
|
|
# translate invalid XML characters to "." |
218
|
1311
|
|
|
|
|
3411
|
$$valPtr =~ tr/\0-\x08\x0b\x0c\x0e-\x1f/./; |
219
|
|
|
|
|
|
|
# fix any malformed UTF-8 characters |
220
|
1311
|
50
|
33
|
|
|
5090
|
if (FixUTF8($valPtr) and not $$et{WarnBadUTF8}) { |
221
|
0
|
|
|
|
|
0
|
$et->Warn('Malformed UTF-8 character(s)'); |
222
|
0
|
|
|
|
|
0
|
$$et{WarnBadUTF8} = 1; |
223
|
|
|
|
|
|
|
} |
224
|
|
|
|
|
|
|
} |
225
|
1315
|
|
|
|
|
3834
|
return undef; # success |
226
|
|
|
|
|
|
|
} |
227
|
1118
|
100
|
100
|
|
|
5683
|
if ($format eq 'rational' or $format eq 'real') { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
228
|
|
|
|
|
|
|
# make sure the value is a valid floating point number |
229
|
366
|
100
|
33
|
|
|
1806
|
unless (Image::ExifTool::IsFloat($$valPtr) or |
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
230
|
|
|
|
|
|
|
# allow 'inf' and 'undef' rational values |
231
|
|
|
|
|
|
|
($format eq 'rational' and ($$valPtr eq 'inf' or |
232
|
|
|
|
|
|
|
$$valPtr eq 'undef' or Image::ExifTool::IsRational($$valPtr)))) |
233
|
|
|
|
|
|
|
{ |
234
|
8
|
|
|
|
|
28
|
return 'Not a floating point number'; |
235
|
|
|
|
|
|
|
} |
236
|
358
|
100
|
|
|
|
1238
|
if ($format eq 'rational') { |
237
|
269
|
|
|
|
|
1136
|
$$valPtr = join('/', Image::ExifTool::Rationalize($$valPtr)); |
238
|
|
|
|
|
|
|
} |
239
|
|
|
|
|
|
|
} elsif ($format eq 'integer') { |
240
|
|
|
|
|
|
|
# make sure the value is integer |
241
|
591
|
100
|
|
|
|
2286
|
if (Image::ExifTool::IsInt($$valPtr)) { |
|
|
50
|
|
|
|
|
|
242
|
|
|
|
|
|
|
# no conversion required (converting to 'int' would remove leading '+') |
243
|
|
|
|
|
|
|
} elsif (Image::ExifTool::IsHex($$valPtr)) { |
244
|
0
|
|
|
|
|
0
|
$$valPtr = hex($$valPtr); |
245
|
|
|
|
|
|
|
} else { |
246
|
34
|
|
|
|
|
156
|
return 'Not an integer'; |
247
|
|
|
|
|
|
|
} |
248
|
|
|
|
|
|
|
} elsif ($format eq 'date') { |
249
|
92
|
|
|
|
|
360
|
my $newDate = FormatXMPDate($$valPtr); |
250
|
92
|
50
|
|
|
|
353
|
return "Invalid date/time (use YYYY:mm:dd HH:MM:SS[.ss][+/-HH:MM|Z])" unless $newDate; |
251
|
92
|
|
|
|
|
219
|
$$valPtr = $newDate; |
252
|
|
|
|
|
|
|
} elsif ($format eq 'boolean') { |
253
|
|
|
|
|
|
|
# (allow lower-case 'true' and 'false' if not setting PrintConv value) |
254
|
68
|
100
|
66
|
|
|
653
|
if (not $$valPtr or $$valPtr =~ /false/i or $$valPtr =~ /^no$/i) { |
|
|
50
|
66
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
255
|
43
|
0
|
33
|
|
|
270
|
if (not $$valPtr or $$valPtr ne 'false' or not $convType or $convType eq 'PrintConv') { |
|
|
|
33
|
|
|
|
|
|
|
|
33
|
|
|
|
|
256
|
43
|
|
|
|
|
97
|
$$valPtr = 'False'; |
257
|
|
|
|
|
|
|
} |
258
|
|
|
|
|
|
|
} elsif ($$valPtr ne 'true' or not $convType or $convType eq 'PrintConv') { |
259
|
25
|
|
|
|
|
79
|
$$valPtr = 'True'; |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
} elsif ($format eq '1') { |
262
|
|
|
|
|
|
|
# this is the entire XMP data block |
263
|
1
|
50
|
|
|
|
6
|
return 'Invalid XMP data' unless ValidateXMP($valPtr); |
264
|
|
|
|
|
|
|
} else { |
265
|
0
|
|
|
|
|
0
|
return "Unknown XMP format: $format"; |
266
|
|
|
|
|
|
|
} |
267
|
1076
|
|
|
|
|
3509
|
return undef; # success! |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
271
|
|
|
|
|
|
|
# Get PropertyPath for specified tagInfo |
272
|
|
|
|
|
|
|
# Inputs: 0) tagInfo reference |
273
|
|
|
|
|
|
|
# Returns: PropertyPath string |
274
|
|
|
|
|
|
|
sub GetPropertyPath($) |
275
|
|
|
|
|
|
|
{ |
276
|
8726
|
|
|
8726
|
0
|
14123
|
my $tagInfo = shift; |
277
|
8726
|
100
|
|
|
|
23974
|
SetPropertyPath($$tagInfo{Table}, $$tagInfo{TagID}) unless $$tagInfo{PropertyPath}; |
278
|
8726
|
|
|
|
|
22146
|
return $$tagInfo{PropertyPath}; |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
282
|
|
|
|
|
|
|
# Set PropertyPath for specified tag (also for associated flattened tags and structure elements) |
283
|
|
|
|
|
|
|
# Inputs: 0) tagTable reference, 1) tagID, 2) tagID of parent structure, |
284
|
|
|
|
|
|
|
# 3) structure definition ref (or undef), 4) property list up to this point (or undef), |
285
|
|
|
|
|
|
|
# 5) flag set if any containing structure has a TYPE |
286
|
|
|
|
|
|
|
# Notes: also generates flattened tags if they don't already exist |
287
|
|
|
|
|
|
|
sub SetPropertyPath($$;$$$$) |
288
|
|
|
|
|
|
|
{ |
289
|
4021
|
|
|
4021
|
0
|
7894
|
my ($tagTablePtr, $tagID, $parentID, $structPtr, $propList, $isType) = @_; |
290
|
4021
|
|
66
|
|
|
8995
|
my $table = $structPtr || $tagTablePtr; |
291
|
4021
|
|
|
|
|
7549
|
my $tagInfo = $$table{$tagID}; |
292
|
4021
|
|
|
|
|
5586
|
my $flatInfo; |
293
|
|
|
|
|
|
|
|
294
|
4021
|
50
|
|
|
|
8950
|
return if ref($tagInfo) ne 'HASH'; # (shouldn't happen) |
295
|
|
|
|
|
|
|
|
296
|
4021
|
100
|
|
|
|
6869
|
if ($structPtr) { |
297
|
2327
|
|
|
|
|
4491
|
my $flatID = $parentID . ucfirst($tagID); |
298
|
2327
|
|
|
|
|
8105
|
$flatInfo = $$tagTablePtr{$flatID}; |
299
|
2327
|
100
|
|
|
|
3753
|
if ($flatInfo) { |
|
|
50
|
|
|
|
|
|
300
|
2275
|
50
|
|
|
|
5526
|
return if $$flatInfo{PropertyPath}; |
301
|
|
|
|
|
|
|
} elsif (@$propList > 50) { |
302
|
0
|
|
|
|
|
0
|
return; # avoid deep recursion |
303
|
|
|
|
|
|
|
} else { |
304
|
|
|
|
|
|
|
# flattened tag doesn't exist, so create it now |
305
|
|
|
|
|
|
|
# (could happen if we were just writing a structure) |
306
|
52
|
|
|
|
|
223
|
$flatInfo = { Name => ucfirst($flatID), Flat => 1 }; |
307
|
52
|
|
|
|
|
195
|
AddTagToTable($tagTablePtr, $flatID, $flatInfo); |
308
|
|
|
|
|
|
|
} |
309
|
2327
|
100
|
|
|
|
4552
|
$isType = 1 if $$structPtr{TYPE}; |
310
|
|
|
|
|
|
|
} else { |
311
|
|
|
|
|
|
|
# don't override existing main table entry if already set by a Struct |
312
|
1694
|
50
|
|
|
|
3693
|
return if $$tagInfo{PropertyPath}; |
313
|
|
|
|
|
|
|
# use property path from original tagInfo if this is an alternate-language tag |
314
|
1694
|
|
|
|
|
3002
|
my $srcInfo = $$tagInfo{SrcTagInfo}; |
315
|
1694
|
100
|
|
|
|
3102
|
$$tagInfo{PropertyPath} = GetPropertyPath($srcInfo) if $srcInfo; |
316
|
1694
|
100
|
|
|
|
3623
|
return if $$tagInfo{PropertyPath}; |
317
|
|
|
|
|
|
|
# set property path for all flattened tags in structure if necessary |
318
|
1691
|
100
|
|
|
|
4090
|
if ($$tagInfo{RootTagInfo}) { |
319
|
60
|
|
|
|
|
345
|
SetPropertyPath($tagTablePtr, $$tagInfo{RootTagInfo}{TagID}); |
320
|
60
|
50
|
|
|
|
290
|
return if $$tagInfo{PropertyPath}; |
321
|
0
|
|
|
|
|
0
|
warn "Internal Error: Didn't set path from root for $tagID\n"; |
322
|
0
|
|
|
|
|
0
|
warn "(Is the Struct NAMESPACE defined?)\n"; |
323
|
|
|
|
|
|
|
} |
324
|
|
|
|
|
|
|
} |
325
|
3958
|
|
66
|
|
|
13205
|
my $ns = $$tagInfo{Namespace} || $$table{NAMESPACE}; |
326
|
3958
|
50
|
|
|
|
7457
|
$ns or warn("No namespace for $tagID\n"), return; |
327
|
3958
|
|
|
|
|
6123
|
my (@propList, $listType); |
328
|
3958
|
100
|
|
|
|
9283
|
$propList and @propList = @$propList; |
329
|
3958
|
|
|
|
|
9781
|
push @propList, "$ns:$tagID"; |
330
|
|
|
|
|
|
|
# lang-alt lists are handled specially, signified by Writable='lang-alt' |
331
|
3958
|
100
|
100
|
|
|
12318
|
if ($$tagInfo{Writable} and $$tagInfo{Writable} eq 'lang-alt') { |
332
|
102
|
|
|
|
|
273
|
$listType = 'Alt'; |
333
|
|
|
|
|
|
|
# remove language code from property path if it exists |
334
|
102
|
50
|
|
|
|
309
|
$propList[-1] =~ s/-$$tagInfo{LangCode}$// if $$tagInfo{LangCode}; |
335
|
|
|
|
|
|
|
# handle lists of lang-alt lists (eg. XMP-plus:Custom tags) |
336
|
102
|
100
|
66
|
|
|
377
|
if ($$tagInfo{List} and $$tagInfo{List} ne '1') { |
337
|
3
|
|
|
|
|
12
|
push @propList, "rdf:$$tagInfo{List}", 'rdf:li 10'; |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
} else { |
340
|
3856
|
|
|
|
|
6840
|
$listType = $$tagInfo{List}; |
341
|
|
|
|
|
|
|
} |
342
|
|
|
|
|
|
|
# add required properties if this is a list |
343
|
3958
|
100
|
66
|
|
|
8789
|
push @propList, "rdf:$listType", 'rdf:li 10' if $listType and $listType ne '1'; |
344
|
|
|
|
|
|
|
# set PropertyPath for all flattened tags of this structure if necessary |
345
|
3958
|
|
|
|
|
6452
|
my $strTable = $$tagInfo{Struct}; |
346
|
3958
|
100
|
100
|
|
|
9576
|
if ($strTable and not ($parentID and |
|
|
|
100
|
|
|
|
|
347
|
|
|
|
|
|
|
# must test NoSubStruct flag to avoid infinite recursion |
348
|
|
|
|
|
|
|
(($$tagTablePtr{$parentID} and $$tagTablePtr{$parentID}{NoSubStruct}) or |
349
|
|
|
|
|
|
|
length $parentID > 500))) # avoid deep recursion |
350
|
|
|
|
|
|
|
{ |
351
|
|
|
|
|
|
|
# make sure the structure namespace has been registered |
352
|
|
|
|
|
|
|
# (user-defined namespaces may not have been) |
353
|
231
|
100
|
|
|
|
868
|
RegisterNamespace($strTable) if ref $$strTable{NAMESPACE}; |
354
|
231
|
|
|
|
|
882
|
my $tag; |
355
|
231
|
|
|
|
|
1466
|
foreach $tag (keys %$strTable) { |
356
|
|
|
|
|
|
|
# ignore special fields and any lang-alt fields we may have added |
357
|
2826
|
100
|
100
|
|
|
10125
|
next if $specialStruct{$tag} or $$strTable{$tag}{LangCode}; |
358
|
2327
|
100
|
|
|
|
4989
|
my $fullID = $parentID ? $parentID . ucfirst($tagID) : $tagID; |
359
|
2327
|
|
|
|
|
4803
|
SetPropertyPath($tagTablePtr, $tag, $fullID, $strTable, \@propList, $isType); |
360
|
|
|
|
|
|
|
} |
361
|
|
|
|
|
|
|
} |
362
|
|
|
|
|
|
|
# if this was a structure field and not a normal tag, |
363
|
|
|
|
|
|
|
# we set PropertyPath in the corresponding flattened tag |
364
|
3958
|
100
|
|
|
|
7063
|
if ($structPtr) { |
365
|
2327
|
|
|
|
|
3422
|
$tagInfo = $flatInfo; |
366
|
|
|
|
|
|
|
# set StructType flag if any containing structure has a TYPE |
367
|
2327
|
100
|
|
|
|
4441
|
$$tagInfo{StructType} = 1 if $isType; |
368
|
|
|
|
|
|
|
} |
369
|
|
|
|
|
|
|
# set property path for tagInfo in main table |
370
|
3958
|
|
|
|
|
20076
|
$$tagInfo{PropertyPath} = join '/', @propList; |
371
|
|
|
|
|
|
|
} |
372
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
374
|
|
|
|
|
|
|
# Save XMP property name/value for rewriting |
375
|
|
|
|
|
|
|
# Inputs: 0) ExifTool object reference |
376
|
|
|
|
|
|
|
# 1) reference to array of XMP property path (last is current property) |
377
|
|
|
|
|
|
|
# 2) property value, 3) optional reference to hash of property attributes |
378
|
|
|
|
|
|
|
sub CaptureXMP($$$;$) |
379
|
|
|
|
|
|
|
{ |
380
|
1149
|
|
|
1149
|
0
|
2671
|
my ($et, $propList, $val, $attrs) = @_; |
381
|
1149
|
50
|
33
|
|
|
3911
|
return unless defined $val and @$propList > 2; |
382
|
1149
|
100
|
66
|
|
|
10335
|
if ($$propList[0] =~ /^x:x[ma]pmeta$/ and |
|
|
50
|
66
|
|
|
|
|
|
|
|
33
|
|
|
|
|
383
|
|
|
|
|
|
|
$$propList[1] eq 'rdf:RDF' and |
384
|
|
|
|
|
|
|
$$propList[2] =~ /$rdfDesc( |$)/) |
385
|
|
|
|
|
|
|
{ |
386
|
|
|
|
|
|
|
# no properties to save yet if this is just the description |
387
|
1148
|
100
|
|
|
|
2862
|
return unless @$propList > 3; |
388
|
|
|
|
|
|
|
# ignore empty list properties |
389
|
1140
|
50
|
|
|
|
2751
|
if ($$propList[-1] =~ /^rdf:(Bag|Seq|Alt)$/) { |
390
|
0
|
|
|
|
|
0
|
$et->Warn("Ignored empty $$propList[-1] list for $$propList[-2]", 1); |
391
|
0
|
|
|
|
|
0
|
return; |
392
|
|
|
|
|
|
|
} |
393
|
|
|
|
|
|
|
# save information about this property |
394
|
1140
|
|
|
|
|
1901
|
my $capture = $$et{XMP_CAPTURE}; |
395
|
1140
|
|
|
|
|
3548
|
my $path = join('/', @$propList[3..$#$propList]); |
396
|
1140
|
50
|
|
|
|
2788
|
if (defined $$capture{$path}) { |
397
|
0
|
|
|
|
|
0
|
$$et{XMP_ERROR} = "Duplicate XMP property: $path"; |
398
|
|
|
|
|
|
|
} else { |
399
|
1140
|
|
100
|
|
|
6716
|
$$capture{$path} = [$val, $attrs || { }]; |
400
|
|
|
|
|
|
|
} |
401
|
|
|
|
|
|
|
} elsif ($$propList[0] eq 'rdf:RDF' and |
402
|
|
|
|
|
|
|
$$propList[1] =~ /$rdfDesc( |$)/) |
403
|
|
|
|
|
|
|
{ |
404
|
|
|
|
|
|
|
# set flag so we don't write x:xmpmeta element |
405
|
1
|
|
|
|
|
5
|
$$et{XMP_NO_XMPMETA} = 1; |
406
|
|
|
|
|
|
|
# add missing x:xmpmeta element and try again |
407
|
1
|
|
|
|
|
5
|
unshift @$propList, 'x:xmpmeta'; |
408
|
1
|
|
|
|
|
7
|
CaptureXMP($et, $propList, $val, $attrs); |
409
|
|
|
|
|
|
|
} else { |
410
|
0
|
|
|
|
|
0
|
$$et{XMP_ERROR} = 'Improperly enclosed XMP property: ' . join('/',@$propList); |
411
|
|
|
|
|
|
|
} |
412
|
|
|
|
|
|
|
} |
413
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
415
|
|
|
|
|
|
|
# Save information about resource containing blank node with nodeID |
416
|
|
|
|
|
|
|
# Inputs: 0) reference to blank node information hash |
417
|
|
|
|
|
|
|
# 1) reference to property list |
418
|
|
|
|
|
|
|
# 2) property value |
419
|
|
|
|
|
|
|
# 3) [optional] reference to attribute hash |
420
|
|
|
|
|
|
|
# Notes: This routine and ProcessBlankInfo() are also used for reading information, but |
421
|
|
|
|
|
|
|
# are uncommon so are put in this file to reduce compile time for the common case |
422
|
|
|
|
|
|
|
sub SaveBlankInfo($$$;$) |
423
|
|
|
|
|
|
|
{ |
424
|
30
|
|
|
30
|
0
|
75
|
my ($blankInfo, $propListPt, $val, $attrs) = @_; |
425
|
|
|
|
|
|
|
|
426
|
30
|
|
|
|
|
123
|
my $propPath = join '/', @$propListPt; |
427
|
30
|
|
|
|
|
159
|
my @ids = ($propPath =~ m{ #([^ /]*)}g); |
428
|
30
|
|
|
|
|
55
|
my $id; |
429
|
|
|
|
|
|
|
# split the property path at each nodeID |
430
|
30
|
|
|
|
|
59
|
foreach $id (@ids) { |
431
|
30
|
|
|
|
|
402
|
my ($pre, $prop, $post) = ($propPath =~ m{^(.*?)/([^/]*) #$id((/.*)?)$}); |
432
|
30
|
50
|
|
|
|
100
|
defined $pre or warn("internal error parsing nodeID's"), next; |
433
|
|
|
|
|
|
|
# the element with the nodeID should be in the path prefix for subject |
434
|
|
|
|
|
|
|
# nodes and the path suffix for object nodes |
435
|
30
|
100
|
|
|
|
70
|
unless ($prop eq $rdfDesc) { |
436
|
12
|
100
|
|
|
|
36
|
if ($post) { |
437
|
8
|
|
|
|
|
20
|
$post = "/$prop$post"; |
438
|
|
|
|
|
|
|
} else { |
439
|
4
|
|
|
|
|
17
|
$pre = "$pre/$prop"; |
440
|
|
|
|
|
|
|
} |
441
|
|
|
|
|
|
|
} |
442
|
30
|
|
|
|
|
106
|
$$blankInfo{Prop}{$id}{Pre}{$pre} = 1; |
443
|
30
|
100
|
66
|
|
|
174
|
if ((defined $post and length $post) or (defined $val and length $val)) { |
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
444
|
|
|
|
|
|
|
# save the property value and attributes for each unique path suffix |
445
|
26
|
|
|
|
|
167
|
$$blankInfo{Prop}{$id}{Post}{$post} = [ $val, $attrs, $propPath ]; |
446
|
|
|
|
|
|
|
} |
447
|
|
|
|
|
|
|
} |
448
|
|
|
|
|
|
|
} |
449
|
|
|
|
|
|
|
|
450
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
451
|
|
|
|
|
|
|
# Process blank-node information |
452
|
|
|
|
|
|
|
# Inputs: 0) ExifTool object ref, 1) tag table ref, |
453
|
|
|
|
|
|
|
# 2) blank node information hash ref, 3) flag set for writing |
454
|
|
|
|
|
|
|
sub ProcessBlankInfo($$$;$) |
455
|
|
|
|
|
|
|
{ |
456
|
4
|
|
|
4
|
0
|
14
|
my ($et, $tagTablePtr, $blankInfo, $isWriting) = @_; |
457
|
4
|
100
|
|
|
|
24
|
$et->VPrint(1, " [Elements with nodeID set:]\n") unless $isWriting; |
458
|
4
|
|
|
|
|
11
|
my ($id, $pre, $post); |
459
|
|
|
|
|
|
|
# handle each nodeID separately |
460
|
4
|
|
|
|
|
9
|
foreach $id (sort keys %{$$blankInfo{Prop}}) { |
|
4
|
|
|
|
|
43
|
|
461
|
8
|
|
|
|
|
21
|
my $path = $$blankInfo{Prop}{$id}; |
462
|
|
|
|
|
|
|
# flag all resource names so we can warn later if some are unused |
463
|
8
|
|
|
|
|
14
|
my %unused; |
464
|
8
|
|
|
|
|
14
|
foreach $post (keys %{$$path{Post}}) { |
|
8
|
|
|
|
|
39
|
|
465
|
26
|
|
|
|
|
49
|
$unused{$post} = 1; |
466
|
|
|
|
|
|
|
} |
467
|
|
|
|
|
|
|
# combine property paths for all possible paths through this node |
468
|
8
|
|
|
|
|
15
|
foreach $pre (sort keys %{$$path{Pre}}) { |
|
8
|
|
|
|
|
36
|
|
469
|
|
|
|
|
|
|
# there will be no description for the object of a blank node |
470
|
16
|
100
|
|
|
|
89
|
next unless $pre =~ m{/$rdfDesc/}; |
471
|
8
|
|
|
|
|
17
|
foreach $post (sort keys %{$$path{Post}}) { |
|
8
|
|
|
|
|
39
|
|
472
|
38
|
|
|
|
|
134
|
my @propList = split m{/}, "$pre$post"; |
473
|
38
|
|
|
|
|
62
|
my ($val, $attrs) = @{$$path{Post}{$post}}; |
|
38
|
|
|
|
|
93
|
|
474
|
38
|
100
|
|
|
|
93
|
if ($isWriting) { |
475
|
19
|
|
|
|
|
40
|
CaptureXMP($et, \@propList, $val, $attrs); |
476
|
|
|
|
|
|
|
} else { |
477
|
19
|
|
|
|
|
51
|
FoundXMP($et, $tagTablePtr, \@propList, $val); |
478
|
|
|
|
|
|
|
} |
479
|
38
|
|
|
|
|
119
|
delete $unused{$post}; |
480
|
|
|
|
|
|
|
} |
481
|
|
|
|
|
|
|
} |
482
|
|
|
|
|
|
|
# save information from unused properties (if RDF is malformed like f-spot output) |
483
|
8
|
100
|
|
|
|
34
|
if (%unused) { |
484
|
4
|
50
|
|
|
|
17
|
$et->Options('Verbose') and $et->Warn('An XMP resource is about nothing'); |
485
|
4
|
|
|
|
|
19
|
foreach $post (sort keys %unused) { |
486
|
8
|
|
|
|
|
13
|
my ($val, $attrs, $propPath) = @{$$path{Post}{$post}}; |
|
8
|
|
|
|
|
26
|
|
487
|
8
|
|
|
|
|
31
|
my @propList = split m{/}, $propPath; |
488
|
8
|
100
|
|
|
|
32
|
if ($isWriting) { |
489
|
4
|
|
|
|
|
10
|
CaptureXMP($et, \@propList, $val, $attrs); |
490
|
|
|
|
|
|
|
} else { |
491
|
4
|
|
|
|
|
14
|
FoundXMP($et, $tagTablePtr, \@propList, $val); |
492
|
|
|
|
|
|
|
} |
493
|
|
|
|
|
|
|
} |
494
|
|
|
|
|
|
|
} |
495
|
|
|
|
|
|
|
} |
496
|
|
|
|
|
|
|
} |
497
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
499
|
|
|
|
|
|
|
# Convert path to namespace used in file (this is a pain, but the XMP |
500
|
|
|
|
|
|
|
# spec only suggests 'preferred' namespace prefixes...) |
501
|
|
|
|
|
|
|
# Inputs: 0) ExifTool object reference, 1) property path |
502
|
|
|
|
|
|
|
# Returns: conforming property path |
503
|
|
|
|
|
|
|
sub ConformPathToNamespace($$) |
504
|
|
|
|
|
|
|
{ |
505
|
8808
|
|
|
8808
|
0
|
17228
|
my ($et, $path) = @_; |
506
|
8808
|
|
|
|
|
26348
|
my @propList = split('/',$path); |
507
|
8808
|
|
|
|
|
17290
|
my $nsUsed = $$et{XMP_NS}; |
508
|
8808
|
|
|
|
|
12462
|
my $prop; |
509
|
8808
|
|
|
|
|
16866
|
foreach $prop (@propList) { |
510
|
23842
|
|
|
|
|
101106
|
my ($ns, $tag) = $prop =~ /(.+?):(.*)/; |
511
|
23842
|
100
|
66
|
|
|
87069
|
next if not defined $ns or $$nsUsed{$ns}; |
512
|
13718
|
|
|
|
|
26366
|
my $uri = $nsURI{$ns}; |
513
|
13718
|
50
|
|
|
|
27073
|
unless ($uri) { |
514
|
0
|
|
|
|
|
0
|
warn "No URI for namespace prefix $ns!\n"; |
515
|
0
|
|
|
|
|
0
|
next; |
516
|
|
|
|
|
|
|
} |
517
|
13718
|
|
|
|
|
18312
|
my $ns2; |
518
|
13718
|
|
|
|
|
40403
|
foreach $ns2 (keys %$nsUsed) { |
519
|
73697
|
50
|
|
|
|
143974
|
next unless $$nsUsed{$ns2} eq $uri; |
520
|
|
|
|
|
|
|
# use the existing namespace prefix instead of ours |
521
|
0
|
|
|
|
|
0
|
$prop = "$ns2:$tag"; |
522
|
0
|
|
|
|
|
0
|
last; |
523
|
|
|
|
|
|
|
} |
524
|
|
|
|
|
|
|
} |
525
|
8808
|
|
|
|
|
30902
|
return join('/',@propList); |
526
|
|
|
|
|
|
|
} |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
529
|
|
|
|
|
|
|
# Add necessary rdf:type element when writing structure |
530
|
|
|
|
|
|
|
# Inputs: 0) ExifTool ref, 1) tag table ref, 2) capture hash ref, 3) path string |
531
|
|
|
|
|
|
|
# 4) optional base path (already conformed to namespace) for elements in |
532
|
|
|
|
|
|
|
# variable-namespace structures |
533
|
|
|
|
|
|
|
sub AddStructType($$$$;$) |
534
|
|
|
|
|
|
|
{ |
535
|
1
|
|
|
1
|
0
|
3
|
my ($et, $tagTablePtr, $capture, $path, $basePath) = @_; |
536
|
1
|
|
|
|
|
7
|
my @props = split '/', $path; |
537
|
1
|
|
|
|
|
3
|
my %doneID; |
538
|
1
|
|
|
|
|
2
|
for (;;) { |
539
|
5
|
|
|
|
|
9
|
pop @props; |
540
|
5
|
50
|
|
|
|
13
|
last unless @props; |
541
|
5
|
|
|
|
|
15
|
my $tagID = GetXMPTagID(\@props); |
542
|
5
|
100
|
|
|
|
14
|
next if $doneID{$tagID}; |
543
|
2
|
|
|
|
|
7
|
$doneID{$tagID} = 1; |
544
|
2
|
|
|
|
|
5
|
my $tagInfo = $$tagTablePtr{$tagID}; |
545
|
2
|
50
|
|
|
|
19
|
last unless ref $tagInfo eq 'HASH'; |
546
|
2
|
100
|
|
|
|
11
|
if ($$tagInfo{Struct}) { |
547
|
1
|
|
|
|
|
5
|
my $type = $$tagInfo{Struct}{TYPE}; |
548
|
1
|
50
|
|
|
|
5
|
if ($type) { |
549
|
1
|
|
|
|
|
4
|
my $pat = $$tagInfo{PropertyPath}; |
550
|
1
|
50
|
|
|
|
6
|
$pat or warn("Missing PropertyPath in AddStructType\n"), last; |
551
|
1
|
|
|
|
|
6
|
$pat = ConformPathToNamespace($et, $pat); |
552
|
1
|
|
|
|
|
3
|
$pat =~ s/ \d+/ \\d\+/g; |
553
|
1
|
50
|
|
|
|
27
|
$path =~ /^($pat)/ or warn("Wrong path in AddStructType\n"), last; |
554
|
1
|
|
|
|
|
5
|
my $p = $1 . '/rdf:type'; |
555
|
1
|
50
|
|
|
|
6
|
$p = "$basePath/$p" if $basePath; |
556
|
1
|
50
|
|
|
|
22
|
$$capture{$p} = [ '', { 'rdf:resource' => $type } ] unless $$capture{$p}; |
557
|
|
|
|
|
|
|
} |
558
|
|
|
|
|
|
|
} |
559
|
2
|
100
|
|
|
|
15
|
last unless $$tagInfo{StructType}; |
560
|
|
|
|
|
|
|
} |
561
|
|
|
|
|
|
|
} |
562
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
564
|
|
|
|
|
|
|
# Hack to use XMP writer for SphericalVideoXML |
565
|
|
|
|
|
|
|
# Inputs: 0) ExifTool ref, 1) dirInfo ref, 2) tag table ref |
566
|
|
|
|
|
|
|
# Returns: SphericalVideoXML data |
567
|
|
|
|
|
|
|
sub WriteGSpherical($$$) |
568
|
|
|
|
|
|
|
{ |
569
|
0
|
|
|
0
|
0
|
0
|
my ($et, $dirInfo, $tagTablePtr) = @_; |
570
|
|
|
|
|
|
|
$$dirInfo{Compact} = 1, |
571
|
0
|
|
|
|
|
0
|
my $dataPt = $$dirInfo{DataPt}; |
572
|
0
|
0
|
0
|
|
|
0
|
if ($dataPt and $$dataPt) { |
573
|
|
|
|
|
|
|
# make it look like XMP for writing |
574
|
0
|
|
|
|
|
0
|
my $buff = $$dataPt; |
575
|
0
|
|
|
|
|
0
|
$buff =~ s/\n
|
576
|
0
|
|
|
|
|
0
|
$buff =~ s/\s*xmlns:GSpherical/>\n
|
577
|
0
|
|
|
|
|
0
|
$buff =~ s/<\/rdf:SphericalVideo>/<\/rdf:Description>/; |
578
|
0
|
|
|
|
|
0
|
$buff .= ""; |
579
|
0
|
|
|
|
|
0
|
$$dirInfo{DataPt} = \$buff; |
580
|
0
|
|
0
|
|
|
0
|
$$dirInfo{DirLen} = length($buff) - ($$dirInfo{DirStart} || 0); |
581
|
|
|
|
|
|
|
} |
582
|
0
|
|
|
|
|
0
|
my $xmp = Image::ExifTool::XMP::WriteXMP($et, $dirInfo, $tagTablePtr); |
583
|
0
|
0
|
|
|
|
0
|
if ($xmp) { |
584
|
|
|
|
|
|
|
# change back to rdf:SphericalVideo structure |
585
|
0
|
|
|
|
|
0
|
$xmp =~ s/^<\?xpacket begin.*?
|
586
|
0
|
|
|
|
|
0
|
$xmp =~ s/>\s*
|
587
|
0
|
|
|
|
|
0
|
$xmp =~ s/\s*<\/rdf:Description>\s*(<\/rdf:RDF>)/\n<\/rdf:SphericalVideo>$1/s; |
588
|
0
|
|
|
|
|
0
|
$xmp =~ s/\s*<\/rdf:RDF>\s*<\/x:xmpmeta>.*//s; |
589
|
|
|
|
|
|
|
} |
590
|
0
|
|
|
|
|
0
|
return $xmp; |
591
|
|
|
|
|
|
|
} |
592
|
|
|
|
|
|
|
|
593
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
594
|
|
|
|
|
|
|
# Utility routine to encode data in base64 |
595
|
|
|
|
|
|
|
# Inputs: 0) binary data string, 1) flag to avoid inserting newlines |
596
|
|
|
|
|
|
|
# Returns: base64-encoded string |
597
|
|
|
|
|
|
|
sub EncodeBase64($;$) |
598
|
|
|
|
|
|
|
{ |
599
|
|
|
|
|
|
|
# encode the data in 45-byte chunks |
600
|
8
|
|
|
8
|
0
|
30
|
my $chunkSize = 45; |
601
|
8
|
|
|
|
|
31
|
my $len = length $_[0]; |
602
|
8
|
|
|
|
|
24
|
my $str = ''; |
603
|
8
|
|
|
|
|
20
|
my $i; |
604
|
8
|
|
|
|
|
48
|
for ($i=0; $i<$len; $i+=$chunkSize) { |
605
|
42
|
|
|
|
|
76
|
my $n = $len - $i; |
606
|
42
|
100
|
|
|
|
94
|
$n = $chunkSize if $n > $chunkSize; |
607
|
|
|
|
|
|
|
# add uuencoded data to output (minus size byte, but including trailing newline) |
608
|
42
|
|
|
|
|
205
|
$str .= substr(pack('u', substr($_[0], $i, $n)), 1); |
609
|
|
|
|
|
|
|
} |
610
|
|
|
|
|
|
|
# convert to base64 (remember that "\0" may be encoded as ' ' or '`') |
611
|
8
|
|
|
|
|
31
|
$str =~ tr/` -_/AA-Za-z0-9+\//; |
612
|
|
|
|
|
|
|
# convert pad characters at the end (remember to account for trailing newline) |
613
|
8
|
|
|
|
|
42
|
my $pad = 3 - ($len % 3); |
614
|
8
|
50
|
|
|
|
62
|
substr($str, -$pad-1, $pad) = ('=' x $pad) if $pad < 3; |
615
|
8
|
50
|
|
|
|
38
|
$str =~ tr/\n//d if $_[1]; # remove newlines if specified |
616
|
8
|
|
|
|
|
87
|
return $str; |
617
|
|
|
|
|
|
|
} |
618
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
620
|
|
|
|
|
|
|
# sort tagInfo hash references by tag name |
621
|
|
|
|
|
|
|
sub ByTagName |
622
|
|
|
|
|
|
|
{ |
623
|
131168
|
|
|
131168
|
0
|
214687
|
return $$a{Name} cmp $$b{Name}; |
624
|
|
|
|
|
|
|
} |
625
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
627
|
|
|
|
|
|
|
# sort alphabetically, but with rdf:type first in the structure |
628
|
|
|
|
|
|
|
sub TypeFirst |
629
|
|
|
|
|
|
|
{ |
630
|
7782
|
100
|
|
7782
|
0
|
16219
|
if ($a =~ /rdf:type$/) { |
|
|
100
|
|
|
|
|
|
631
|
20
|
100
|
|
|
|
67
|
return substr($a, 0, -8) cmp $b unless $b =~ /rdf:type$/; |
632
|
|
|
|
|
|
|
} elsif ($b =~ /rdf:type$/) { |
633
|
14
|
|
|
|
|
33
|
return $a cmp substr($b, 0, -8); |
634
|
|
|
|
|
|
|
} |
635
|
7750
|
|
|
|
|
10932
|
return $a cmp $b; |
636
|
|
|
|
|
|
|
} |
637
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
639
|
|
|
|
|
|
|
# Limit size of XMP |
640
|
|
|
|
|
|
|
# Inputs: 0) ExifTool object ref, 1) XMP data ref (written up to start of $rdfClose), |
641
|
|
|
|
|
|
|
# 2) max XMP len, 3) rdf:about string, 4) list ref for description start offsets |
642
|
|
|
|
|
|
|
# 5) start offset of first description recommended for extended XMP |
643
|
|
|
|
|
|
|
# Returns: 0) extended XMP ref, 1) GUID and updates $$dataPt (or undef if no extended XMP) |
644
|
|
|
|
|
|
|
sub LimitXMPSize($$$$$$) |
645
|
|
|
|
|
|
|
{ |
646
|
35
|
|
|
35
|
0
|
161
|
my ($et, $dataPt, $maxLen, $about, $startPt, $extStart) = @_; |
647
|
|
|
|
|
|
|
|
648
|
|
|
|
|
|
|
# return straight away if it isn't too big |
649
|
35
|
50
|
|
|
|
207
|
return undef if length($$dataPt) < $maxLen; |
650
|
|
|
|
|
|
|
|
651
|
0
|
|
|
|
|
0
|
push @$startPt, length($$dataPt); # add end offset to list |
652
|
0
|
|
|
|
|
0
|
my $newData = substr($$dataPt, 0, $$startPt[0]); |
653
|
0
|
|
|
|
|
0
|
my $guid = '0' x 32; |
654
|
|
|
|
|
|
|
# write the required xmpNote:HasExtendedXMP property |
655
|
0
|
|
|
|
|
0
|
$newData .= "$nl$sp<$rdfDesc rdf:about='${about}'\n$sp${sp}xmlns:xmpNote='$nsURI{xmpNote}'"; |
656
|
0
|
0
|
|
|
|
0
|
if ($$et{OPTIONS}{Compact}{Shorthand}) { |
657
|
0
|
|
|
|
|
0
|
$newData .= "\n$sp${sp}xmpNote:HasExtendedXMP='${guid}'/>\n"; |
658
|
|
|
|
|
|
|
} else { |
659
|
0
|
|
|
|
|
0
|
$newData .= ">$nl$sp$sp$guid$nl$sp$rdfDesc>\n"; |
660
|
|
|
|
|
|
|
} |
661
|
|
|
|
|
|
|
|
662
|
0
|
|
|
|
|
0
|
my ($i, %descSize, $start); |
663
|
|
|
|
|
|
|
# calculate all description block sizes |
664
|
0
|
|
|
|
|
0
|
for ($i=1; $i<@$startPt; ++$i) { |
665
|
0
|
|
|
|
|
0
|
$descSize{$$startPt[$i-1]} = $$startPt[$i] - $$startPt[$i-1]; |
666
|
|
|
|
|
|
|
} |
667
|
0
|
|
|
|
|
0
|
pop @$startPt; # remove end offset |
668
|
|
|
|
|
|
|
# write the descriptions from smallest to largest, as many in main XMP as possible |
669
|
0
|
|
|
|
|
0
|
my @descStart = sort { $descSize{$a} <=> $descSize{$b} } @$startPt; |
|
0
|
|
|
|
|
0
|
|
670
|
0
|
|
|
|
|
0
|
my $extData = XMPOpen($et) . $rdfOpen; |
671
|
0
|
|
|
|
|
0
|
for ($i=0; $i<2; ++$i) { |
672
|
0
|
|
|
|
|
0
|
foreach $start (@descStart) { |
673
|
|
|
|
|
|
|
# write main XMP first (in order of size), then extended XMP afterwards (in order) |
674
|
0
|
0
|
0
|
|
|
0
|
next if $i xor $start >= $extStart; |
675
|
0
|
0
|
|
|
|
0
|
my $pt = (length($newData) + $descSize{$start} > $maxLen) ? \$extData : \$newData; |
676
|
0
|
|
|
|
|
0
|
$$pt .= substr($$dataPt, $start, $descSize{$start}); |
677
|
|
|
|
|
|
|
} |
678
|
|
|
|
|
|
|
} |
679
|
0
|
|
|
|
|
0
|
$extData .= $rdfClose . $xmpClose; # close rdf:RDF and x:xmpmeta |
680
|
|
|
|
|
|
|
# calculate GUID from MD5 of extended XMP data |
681
|
0
|
0
|
|
|
|
0
|
if (eval { require Digest::MD5 }) { |
|
0
|
|
|
|
|
0
|
|
682
|
0
|
|
|
|
|
0
|
$guid = uc unpack('H*', Digest::MD5::md5($extData)); |
683
|
0
|
|
|
|
|
0
|
$newData =~ s/0{32}/$guid/; # update GUID in main XMP segment |
684
|
|
|
|
|
|
|
} |
685
|
0
|
|
|
|
|
0
|
$et->VerboseValue('+ XMP-xmpNote:HasExtendedXMP', $guid); |
686
|
0
|
|
|
|
|
0
|
$$dataPt = $newData; # return main XMP block |
687
|
0
|
|
|
|
|
0
|
return (\$extData, $guid); # return extended XMP and its GUID |
688
|
|
|
|
|
|
|
} |
689
|
|
|
|
|
|
|
|
690
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
691
|
|
|
|
|
|
|
# Close out bottom-level property |
692
|
|
|
|
|
|
|
# Inputs: 0) current property path list ref, 1) longhand properties at each resource |
693
|
|
|
|
|
|
|
# level, 2) shorthand properties at each resource level, 3) resource flag for |
694
|
|
|
|
|
|
|
# each property path level (set only if Shorthand is enabled) |
695
|
|
|
|
|
|
|
sub CloseProperty($$$$) |
696
|
|
|
|
|
|
|
{ |
697
|
1059
|
|
|
1059
|
0
|
2166
|
my ($curPropList, $long, $short, $resFlag) = @_; |
698
|
|
|
|
|
|
|
|
699
|
1059
|
|
|
|
|
1914
|
my $prop = pop @$curPropList; |
700
|
1059
|
|
|
|
|
2153
|
$prop =~ s/ .*//; # remove list index if it exists |
701
|
1059
|
|
|
|
|
2156
|
my $pad = $sp x (scalar(@$curPropList) + 1); |
702
|
1059
|
100
|
|
|
|
2801
|
if ($$resFlag[@$curPropList]) { |
|
|
100
|
|
|
|
|
|
703
|
|
|
|
|
|
|
# close this XMP structure with possible shorthand properties |
704
|
1
|
50
|
|
|
|
6
|
if (length $$short[-1]) { |
705
|
1
|
50
|
|
|
|
6
|
if (length $$long[-1]) { |
706
|
|
|
|
|
|
|
# require a new Description if both longhand and shorthand properties |
707
|
0
|
|
|
|
|
0
|
$$long[-2] .= ">$nl$pad<$rdfDesc"; |
708
|
0
|
|
|
|
|
0
|
$$short[-1] .= ">$nl"; |
709
|
0
|
|
|
|
|
0
|
$$long[-1] .= "$pad$rdfDesc>$nl"; |
710
|
|
|
|
|
|
|
} else { |
711
|
|
|
|
|
|
|
# simply close empty property if all shorthand |
712
|
1
|
|
|
|
|
5
|
$$short[-1] .= "/>$nl"; |
713
|
|
|
|
|
|
|
} |
714
|
|
|
|
|
|
|
} else { |
715
|
|
|
|
|
|
|
# use "parseType" instead of opening a new Description |
716
|
0
|
|
|
|
|
0
|
$$long[-2] .= ' rdf:parseType="Resource"'; |
717
|
0
|
0
|
|
|
|
0
|
$$short[-1] = length $$long[-1] ? ">$nl" : "/>$nl"; |
718
|
|
|
|
|
|
|
} |
719
|
1
|
50
|
|
|
|
5
|
$$long[-1] .= "$pad$prop>$nl" if length $$long[-1]; |
720
|
1
|
|
|
|
|
5
|
$$long[-2] .= $$short[-1] . $$long[-1]; |
721
|
1
|
|
|
|
|
2
|
pop @$short; |
722
|
1
|
|
|
|
|
3
|
pop @$long; |
723
|
|
|
|
|
|
|
} elsif (defined $$resFlag[@$curPropList]) { |
724
|
|
|
|
|
|
|
# close this top level Description with possible shorthand values |
725
|
6
|
100
|
|
|
|
16
|
if (length $$long[-1]) { |
726
|
3
|
|
|
|
|
18
|
$$long[-2] .= $$short[-1] . ">$nl" . $$long[-1] . "$pad$prop>$nl"; |
727
|
|
|
|
|
|
|
} else { |
728
|
3
|
|
|
|
|
11
|
$$long[-2] .= $$short[-1] . "/>$nl"; # empty element (ie. all shorthand) |
729
|
|
|
|
|
|
|
} |
730
|
6
|
|
|
|
|
14
|
$$short[-1] = $$long[-1] = ''; |
731
|
|
|
|
|
|
|
} else { |
732
|
|
|
|
|
|
|
# close this property (no chance of shorthand) |
733
|
1052
|
|
|
|
|
2610
|
$$long[-1] .= "$pad$prop>$nl"; |
734
|
1052
|
100
|
|
|
|
5394
|
unless (@$curPropList) { |
735
|
|
|
|
|
|
|
# add properties now that this top-level Description is complete |
736
|
300
|
|
|
|
|
1454
|
$$long[-2] .= ">$nl" . $$long[-1]; |
737
|
300
|
|
|
|
|
634
|
$$long[-1] = ''; |
738
|
|
|
|
|
|
|
} |
739
|
|
|
|
|
|
|
} |
740
|
1059
|
|
|
|
|
3778
|
$#$resFlag = $#$curPropList; # remove expired resource flags |
741
|
|
|
|
|
|
|
} |
742
|
|
|
|
|
|
|
|
743
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
744
|
|
|
|
|
|
|
# Write XMP information |
745
|
|
|
|
|
|
|
# Inputs: 0) ExifTool ref, 1) source dirInfo ref (with optional WriteGroup), |
746
|
|
|
|
|
|
|
# 2) [optional] tag table ref |
747
|
|
|
|
|
|
|
# Returns: with tag table: new XMP data (may be empty if no XMP data) or undef on error |
748
|
|
|
|
|
|
|
# without tag table: 1 on success, 0 if not valid XMP file, -1 on write error |
749
|
|
|
|
|
|
|
# Notes: May set dirInfo InPlace flag to rewrite with specified DirLen (=2 to allow larger) |
750
|
|
|
|
|
|
|
# May set dirInfo ReadOnly flag to write as read-only XMP ('r' mode and no padding) |
751
|
|
|
|
|
|
|
# May set dirInfo Compact flag to force compact (drops 2kB of padding) |
752
|
|
|
|
|
|
|
# May set dirInfo MaxDataLen to limit output data length -- this causes ExtendedXMP |
753
|
|
|
|
|
|
|
# and ExtendedGUID to be returned in dirInfo if extended XMP was required |
754
|
|
|
|
|
|
|
sub WriteXMP($$;$) |
755
|
|
|
|
|
|
|
{ |
756
|
6141
|
|
|
6141
|
0
|
12230
|
my ($et, $dirInfo, $tagTablePtr) = @_; |
757
|
6141
|
100
|
|
|
|
23898
|
$et or return 1; # allow dummy access to autoload this package |
758
|
123
|
|
|
|
|
430
|
my $dataPt = $$dirInfo{DataPt}; |
759
|
123
|
|
|
|
|
352
|
my (%capture, %nsUsed, $xmpErr, $about); |
760
|
123
|
|
|
|
|
267
|
my $changed = 0; |
761
|
123
|
|
|
|
|
328
|
my $xmpFile = (not $tagTablePtr); # this is an XMP data file if no $tagTablePtr |
762
|
|
|
|
|
|
|
# prefer XMP over other metadata formats in some types of files |
763
|
123
|
|
100
|
|
|
909
|
my $preferred = $xmpFile || ($$et{PreferredGroup} and $$et{PreferredGroup} eq 'XMP'); |
764
|
123
|
|
|
|
|
404
|
my $verbose = $$et{OPTIONS}{Verbose}; |
765
|
123
|
|
|
|
|
305
|
my %compact = ( %{$$et{OPTIONS}{Compact}} ); # (make a copy so we can change settings) |
|
123
|
|
|
|
|
598
|
|
766
|
123
|
|
|
|
|
312
|
my $dirLen = $$dirInfo{DirLen}; |
767
|
123
|
100
|
100
|
|
|
983
|
$dirLen = length($$dataPt) if not defined $dirLen and $dataPt; |
768
|
|
|
|
|
|
|
# |
769
|
|
|
|
|
|
|
# extract existing XMP information into %capture hash |
770
|
|
|
|
|
|
|
# |
771
|
|
|
|
|
|
|
# define hash in ExifTool object to capture XMP information (also causes |
772
|
|
|
|
|
|
|
# CaptureXMP() instead of FoundXMP() to be called from ParseXMPElement()) |
773
|
|
|
|
|
|
|
# |
774
|
|
|
|
|
|
|
# The %capture hash is keyed on the complete property path beginning after |
775
|
|
|
|
|
|
|
# rdf:RDF/rdf:Description/. The values are array references with the |
776
|
|
|
|
|
|
|
# following entries: 0) value, 1) attribute hash reference. |
777
|
123
|
|
|
|
|
457
|
$$et{XMP_CAPTURE} = \%capture; |
778
|
123
|
|
|
|
|
430
|
$$et{XMP_NS} = \%nsUsed; |
779
|
123
|
|
|
|
|
349
|
delete $$et{XMP_NO_XMPMETA}; |
780
|
123
|
|
|
|
|
341
|
delete $$et{XMP_NO_XPACKET}; |
781
|
123
|
|
|
|
|
320
|
delete $$et{XMP_IS_XML}; |
782
|
123
|
|
|
|
|
278
|
delete $$et{XMP_IS_SVG}; |
783
|
|
|
|
|
|
|
|
784
|
|
|
|
|
|
|
# set current padding characters |
785
|
123
|
50
|
|
|
|
899
|
($sp, $nl) = ($compact{NoIndent} ? '' : ' ', $compact{NoNewline} ? '' : "\n"); |
|
|
50
|
|
|
|
|
|
786
|
|
|
|
|
|
|
|
787
|
|
|
|
|
|
|
# get value for new rdf:about |
788
|
123
|
|
|
|
|
461
|
my $tagInfo = $Image::ExifTool::XMP::rdf{about}; |
789
|
123
|
100
|
|
|
|
600
|
if (defined $$et{NEW_VALUE}{$tagInfo}) { |
790
|
1
|
|
50
|
|
|
7
|
$about = $et->GetNewValue($$et{NEW_VALUE}{$tagInfo}) || ''; |
791
|
|
|
|
|
|
|
} |
792
|
|
|
|
|
|
|
|
793
|
123
|
100
|
100
|
|
|
847
|
if ($xmpFile or $dirLen) { |
|
|
50
|
|
|
|
|
|
794
|
72
|
|
|
|
|
181
|
delete $$et{XMP_ERROR}; |
795
|
|
|
|
|
|
|
# extract all existing XMP information (to the XMP_CAPTURE hash) |
796
|
72
|
|
|
|
|
443
|
my $success = ProcessXMP($et, $dirInfo, $tagTablePtr); |
797
|
|
|
|
|
|
|
# don't continue if there is nothing to parse or if we had a parsing error |
798
|
72
|
100
|
66
|
|
|
654
|
unless ($success and not $$et{XMP_ERROR}) { |
799
|
16
|
|
50
|
|
|
122
|
my $err = $$et{XMP_ERROR} || 'Error parsing XMP'; |
800
|
|
|
|
|
|
|
# may ignore this error only if we were successful |
801
|
16
|
50
|
|
|
|
59
|
if ($xmpFile) { |
802
|
16
|
|
|
|
|
47
|
my $raf = $$dirInfo{RAF}; |
803
|
|
|
|
|
|
|
# allow empty XMP data so we can create something from nothing |
804
|
16
|
50
|
33
|
|
|
102
|
if ($success or not $raf->Seek(0,2) or $raf->Tell()) { |
|
|
|
33
|
|
|
|
|
805
|
|
|
|
|
|
|
# no error message if not an XMP file |
806
|
0
|
0
|
|
|
|
0
|
return 0 unless $$et{XMP_ERROR}; |
807
|
0
|
0
|
|
|
|
0
|
if ($et->Error($err, $success)) { |
808
|
0
|
|
|
|
|
0
|
delete $$et{XMP_CAPTURE}; |
809
|
0
|
|
|
|
|
0
|
return 0; |
810
|
|
|
|
|
|
|
} |
811
|
|
|
|
|
|
|
} |
812
|
|
|
|
|
|
|
} else { |
813
|
0
|
0
|
0
|
|
|
0
|
$success = 2 if $success and $success eq '1'; |
814
|
0
|
0
|
|
|
|
0
|
if ($et->Warn($err, $success)) { |
815
|
0
|
|
|
|
|
0
|
delete $$et{XMP_CAPTURE}; |
816
|
0
|
|
|
|
|
0
|
return undef; |
817
|
|
|
|
|
|
|
} |
818
|
|
|
|
|
|
|
} |
819
|
|
|
|
|
|
|
} |
820
|
72
|
100
|
|
|
|
281
|
if (defined $about) { |
821
|
1
|
50
|
|
|
|
5
|
if ($verbose > 1) { |
822
|
0
|
|
|
|
|
0
|
my $wasAbout = $$et{XmpAbout}; |
823
|
0
|
0
|
|
|
|
0
|
$et->VerboseValue('- XMP-rdf:About', UnescapeXML($wasAbout)) if defined $wasAbout; |
824
|
0
|
|
|
|
|
0
|
$et->VerboseValue('+ XMP-rdf:About', $about); |
825
|
|
|
|
|
|
|
} |
826
|
1
|
|
|
|
|
5
|
$about = EscapeXML($about); # must escape for XML |
827
|
1
|
|
|
|
|
2
|
++$changed; |
828
|
|
|
|
|
|
|
} else { |
829
|
71
|
|
100
|
|
|
393
|
$about = $$et{XmpAbout} || ''; |
830
|
|
|
|
|
|
|
} |
831
|
72
|
|
|
|
|
183
|
delete $$et{XMP_ERROR}; |
832
|
|
|
|
|
|
|
|
833
|
|
|
|
|
|
|
# call InitWriteDirs to initialize FORCE_WRITE flags if necessary |
834
|
72
|
50
|
66
|
|
|
433
|
$et->InitWriteDirs({}, 'XMP') if $xmpFile and $et->GetNewValue('ForceWrite'); |
835
|
|
|
|
|
|
|
# set changed if we are ForceWrite tag was set to "XMP" |
836
|
72
|
50
|
|
|
|
365
|
++$changed if $$et{FORCE_WRITE}{XMP}; |
837
|
|
|
|
|
|
|
|
838
|
|
|
|
|
|
|
} elsif (defined $about) { |
839
|
0
|
|
|
|
|
0
|
$et->VerboseValue('+ XMP-rdf:About', $about); |
840
|
0
|
|
|
|
|
0
|
$about = EscapeXML($about); # must escape for XML |
841
|
|
|
|
|
|
|
# (don't increment $changed here because we need another tag to be written) |
842
|
|
|
|
|
|
|
} else { |
843
|
51
|
|
|
|
|
184
|
$about = ''; |
844
|
|
|
|
|
|
|
} |
845
|
|
|
|
|
|
|
# |
846
|
|
|
|
|
|
|
# handle writing XMP as a block to XMP file |
847
|
|
|
|
|
|
|
# |
848
|
123
|
100
|
|
|
|
476
|
if ($xmpFile) { |
849
|
35
|
|
|
|
|
105
|
$tagInfo = $Image::ExifTool::Extra{XMP}; |
850
|
35
|
50
|
33
|
|
|
248
|
if ($tagInfo and $$et{NEW_VALUE}{$tagInfo}) { |
851
|
0
|
|
|
|
|
0
|
my $rtnVal = 1; |
852
|
0
|
|
|
|
|
0
|
my $newVal = $et->GetNewValue($$et{NEW_VALUE}{$tagInfo}); |
853
|
0
|
0
|
0
|
|
|
0
|
if (defined $newVal and length $newVal) { |
854
|
0
|
|
|
|
|
0
|
$et->VPrint(0, " Writing XMP as a block\n"); |
855
|
0
|
|
|
|
|
0
|
++$$et{CHANGED}; |
856
|
0
|
0
|
|
|
|
0
|
Write($$dirInfo{OutFile}, $newVal) or $rtnVal = -1; |
857
|
|
|
|
|
|
|
} |
858
|
0
|
|
|
|
|
0
|
delete $$et{XMP_CAPTURE}; |
859
|
0
|
|
|
|
|
0
|
return $rtnVal; |
860
|
|
|
|
|
|
|
} |
861
|
|
|
|
|
|
|
} |
862
|
|
|
|
|
|
|
# |
863
|
|
|
|
|
|
|
# delete groups in family 1 if requested |
864
|
|
|
|
|
|
|
# |
865
|
123
|
100
|
66
|
|
|
248
|
if (%{$$et{DEL_GROUP}} and (grep /^XMP-.+$/, keys %{$$et{DEL_GROUP}} or |
|
123
|
|
66
|
|
|
527
|
|
866
|
|
|
|
|
|
|
# (logic is a bit more complex for group names in exiftool XML files) |
867
|
|
|
|
|
|
|
grep m{^http://ns.exiftool.(?:ca|org)/}, values %nsUsed)) |
868
|
|
|
|
|
|
|
{ |
869
|
12
|
|
|
|
|
48
|
my $del = $$et{DEL_GROUP}; |
870
|
12
|
|
|
|
|
31
|
my $path; |
871
|
12
|
|
|
|
|
78
|
foreach $path (keys %capture) { |
872
|
141
|
|
|
|
|
319
|
my @propList = split('/',$path); # get property list |
873
|
141
|
|
|
|
|
332
|
my ($tag, $ns) = GetXMPTagID(\@propList); |
874
|
|
|
|
|
|
|
# translate namespace if necessary |
875
|
141
|
50
|
|
|
|
330
|
$ns = $stdXlatNS{$ns} if $stdXlatNS{$ns}; |
876
|
141
|
|
|
|
|
223
|
my ($grp, @g); |
877
|
|
|
|
|
|
|
# no "XMP-" added to most groups in exiftool RDF/XML output file |
878
|
141
|
100
|
66
|
|
|
737
|
if ($nsUsed{$ns} and (@g = ($nsUsed{$ns} =~ m{^http://ns.exiftool.(?:ca|org)/(.*?)/(.*?)/}))) { |
879
|
78
|
100
|
|
|
|
183
|
if ($g[1] =~ /^\d/) { |
880
|
20
|
|
|
|
|
36
|
$grp = "XML-$g[0]"; |
881
|
|
|
|
|
|
|
#(all XML-* groups stored as uppercase DEL_GROUP key) |
882
|
20
|
|
|
|
|
34
|
my $ucg = uc $grp; |
883
|
20
|
100
|
66
|
|
|
117
|
next unless $$del{$ucg} or ($$del{'XML-*'} and not $$del{"-$ucg"}); |
|
|
|
66
|
|
|
|
|
884
|
|
|
|
|
|
|
} else { |
885
|
58
|
|
|
|
|
88
|
$grp = $g[1]; |
886
|
58
|
100
|
66
|
|
|
218
|
next unless $$del{$grp} or ($$del{$g[0]} and not $$del{"-$grp"}); |
|
|
|
100
|
|
|
|
|
887
|
|
|
|
|
|
|
} |
888
|
|
|
|
|
|
|
} else { |
889
|
63
|
|
|
|
|
114
|
$grp = "XMP-$ns"; |
890
|
63
|
|
|
|
|
107
|
my $ucg = uc $grp; |
891
|
63
|
100
|
100
|
|
|
284
|
next unless $$del{$ucg} or ($$del{'XMP-*'} and not $$del{"-$ucg"}); |
|
|
|
100
|
|
|
|
|
892
|
|
|
|
|
|
|
} |
893
|
91
|
|
|
|
|
402
|
$et->VerboseValue("- $grp:$tag", $capture{$path}->[0]); |
894
|
91
|
|
|
|
|
237
|
delete $capture{$path}; |
895
|
91
|
|
|
|
|
189
|
++$changed; |
896
|
|
|
|
|
|
|
} |
897
|
|
|
|
|
|
|
} |
898
|
|
|
|
|
|
|
# delete HasExtendedXMP tag (we create it as needed) |
899
|
123
|
|
|
|
|
427
|
my $hasExtTag = 'xmpNote:HasExtendedXMP'; |
900
|
123
|
100
|
|
|
|
859
|
if ($capture{$hasExtTag}) { |
901
|
1
|
|
|
|
|
9
|
$et->VerboseValue("- XMP-$hasExtTag", $capture{$hasExtTag}->[0]); |
902
|
1
|
|
|
|
|
4
|
delete $capture{$hasExtTag}; |
903
|
|
|
|
|
|
|
} |
904
|
|
|
|
|
|
|
# set $xmpOpen now to to handle xmptk tag first |
905
|
123
|
100
|
|
|
|
774
|
my $xmpOpen = $$et{XMP_NO_XMPMETA} ? '' : XMPOpen($et); |
906
|
|
|
|
|
|
|
# |
907
|
|
|
|
|
|
|
# add, delete or change information as specified |
908
|
|
|
|
|
|
|
# |
909
|
|
|
|
|
|
|
# get hash of all information we want to change |
910
|
|
|
|
|
|
|
# (sorted by tag name so alternate languages come last, but with structures |
911
|
|
|
|
|
|
|
# first so flattened tags may be used to override individual structure elements) |
912
|
123
|
|
|
|
|
365
|
my (@tagInfoList, $delLangPath, %delLangPaths, %delAllLang, $firstNewPath); |
913
|
123
|
|
|
|
|
338
|
my $writeGroup = $$dirInfo{WriteGroup}; |
914
|
123
|
|
|
|
|
681
|
foreach $tagInfo (sort ByTagName $et->GetNewTagInfoList()) { |
915
|
14982
|
100
|
|
|
|
31221
|
next unless $et->GetGroup($tagInfo, 0) eq 'XMP'; |
916
|
4285
|
50
|
|
|
|
9535
|
next if $$tagInfo{Name} eq 'XMP'; # (ignore full XMP block if we didn't write it already) |
917
|
4285
|
50
|
66
|
|
|
7959
|
next if $writeGroup and $writeGroup ne $$et{NEW_VALUE}{$tagInfo}{WriteGroup}; |
918
|
4285
|
100
|
|
|
|
7177
|
if ($$tagInfo{Struct}) { |
919
|
196
|
|
|
|
|
649
|
unshift @tagInfoList, $tagInfo; |
920
|
|
|
|
|
|
|
} else { |
921
|
4089
|
|
|
|
|
8850
|
push @tagInfoList, $tagInfo; |
922
|
|
|
|
|
|
|
} |
923
|
|
|
|
|
|
|
} |
924
|
123
|
|
|
|
|
2034
|
foreach $tagInfo (@tagInfoList) { |
925
|
4285
|
|
|
|
|
7880
|
my @delPaths; # list of deleted paths |
926
|
4285
|
|
|
|
|
16364
|
my $tag = $$tagInfo{TagID}; |
927
|
4285
|
|
|
|
|
9986
|
my $path = GetPropertyPath($tagInfo); |
928
|
4285
|
50
|
|
|
|
9062
|
unless ($path) { |
929
|
0
|
|
|
|
|
0
|
$et->Warn("Can't write XMP:$tag (namespace unknown)"); |
930
|
0
|
|
|
|
|
0
|
next; |
931
|
|
|
|
|
|
|
} |
932
|
|
|
|
|
|
|
# skip tags that were handled specially |
933
|
4285
|
100
|
100
|
|
|
14946
|
if ($path eq 'rdf:about' or $path eq 'x:xmptk') { |
934
|
2
|
|
|
|
|
4
|
++$changed; |
935
|
2
|
|
|
|
|
4
|
next; |
936
|
|
|
|
|
|
|
} |
937
|
4283
|
|
|
|
|
8394
|
my $isStruct = $$tagInfo{Struct}; |
938
|
|
|
|
|
|
|
# change our property path namespace prefixes to conform |
939
|
|
|
|
|
|
|
# to the ones used in this file |
940
|
4283
|
|
|
|
|
9477
|
$path = ConformPathToNamespace($et, $path); |
941
|
|
|
|
|
|
|
# find existing property |
942
|
4283
|
|
|
|
|
8871
|
my $cap = $capture{$path}; |
943
|
|
|
|
|
|
|
# MicrosoftPhoto screws up the case of some tags, and some other software, |
944
|
|
|
|
|
|
|
# including Adobe software, has been known to write the wrong list type or |
945
|
|
|
|
|
|
|
# not properly enclose properties in a list, so we check for this |
946
|
4283
|
|
|
|
|
9289
|
until ($cap) { |
947
|
|
|
|
|
|
|
# find and fix all incorrect property names if this is a structure or a flattened tag |
948
|
4153
|
|
|
|
|
6897
|
my @fixInfo; |
949
|
4153
|
100
|
100
|
|
|
16765
|
if ($isStruct or defined $$tagInfo{Flat}) { |
950
|
|
|
|
|
|
|
# get tagInfo for all containing (possibly nested) structures |
951
|
2173
|
|
|
|
|
7481
|
my @props = split '/', $path; |
952
|
2173
|
|
|
|
|
5278
|
my $tbl = $$tagInfo{Table}; |
953
|
2173
|
|
|
|
|
5413
|
while (@props) { |
954
|
12171
|
|
|
|
|
31064
|
my $info = $$tbl{GetXMPTagID(\@props)}; |
955
|
|
|
|
|
|
|
unshift @fixInfo, $info if ref $info eq 'HASH' and $$info{Struct} and |
956
|
12171
|
100
|
66
|
|
|
77274
|
(not @fixInfo or $fixInfo[0] ne $info); |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
957
|
12171
|
|
|
|
|
31208
|
pop @props; |
958
|
|
|
|
|
|
|
} |
959
|
2173
|
50
|
|
|
|
5629
|
$et->WarnOnce("Error finding parent structure for $$tagInfo{Name}") unless @fixInfo; |
960
|
|
|
|
|
|
|
} |
961
|
|
|
|
|
|
|
# fix property path for this tag (last in the @fixInfo list) |
962
|
4153
|
100
|
100
|
|
|
14346
|
push @fixInfo, $tagInfo unless @fixInfo and $isStruct; |
963
|
|
|
|
|
|
|
# start from outermost containing structure, fixing incorrect list types, etc, |
964
|
|
|
|
|
|
|
# finally fixing the actual tag properties after all containing structures |
965
|
4153
|
|
|
|
|
6572
|
my $err; |
966
|
4153
|
|
|
|
|
9447
|
while (@fixInfo) { |
967
|
4428
|
|
|
|
|
7823
|
my $fixInfo = shift @fixInfo; |
968
|
4428
|
|
|
|
|
13397
|
my $fixPath = ConformPathToNamespace($et, GetPropertyPath($fixInfo)); |
969
|
4428
|
|
|
|
|
10554
|
my $regex = quotemeta($fixPath); |
970
|
4428
|
|
|
|
|
12642
|
$regex =~ s/ \d+/ \\d\+/g; # match any list index |
971
|
4428
|
|
|
|
|
7739
|
my $ok = $regex; |
972
|
4428
|
|
|
|
|
7156
|
my ($ok2, $match, $i, @fixed, %fixed, $fixed); |
973
|
|
|
|
|
|
|
# check for incorrect list types |
974
|
4428
|
100
|
|
|
|
17517
|
if ($regex =~ s{\\/rdf\\:(Bag|Seq|Alt)\\/}{/rdf:(Bag|Seq|Alt)/}g) { |
|
|
100
|
|
|
|
|
|
975
|
|
|
|
|
|
|
# also look for missing bottom-level list |
976
|
1967
|
100
|
|
|
|
7960
|
if ($regex =~ s{/rdf:\(Bag\|Seq\|Alt\)\/rdf\\:li\\ \\d\+$}{}) { |
977
|
1954
|
100
|
|
|
|
4971
|
$regex .= '(/.*)?' unless @fixInfo; |
978
|
|
|
|
|
|
|
} |
979
|
|
|
|
|
|
|
} elsif (not @fixInfo) { |
980
|
1983
|
|
|
|
|
3421
|
$ok2 = $regex; |
981
|
|
|
|
|
|
|
# check for properties in lists that shouldn't be (ref forum4325) |
982
|
1983
|
|
|
|
|
3778
|
$regex .= '(/rdf:(Bag|Seq|Alt)/rdf:li \d+)?'; |
983
|
|
|
|
|
|
|
} |
984
|
4428
|
100
|
|
|
|
9649
|
if (@fixInfo) { |
985
|
2083
|
|
|
|
|
4262
|
$regex .= '(/.*)?'; |
986
|
2083
|
|
|
|
|
3417
|
$ok .= '(/.*)?'; |
987
|
|
|
|
|
|
|
} |
988
|
4428
|
|
|
|
|
232475
|
my @matches = sort grep m{^$regex$}i, keys %capture; |
989
|
4428
|
100
|
|
|
|
24089
|
last unless @matches; |
990
|
279
|
100
|
|
|
|
2033
|
if ($matches[0] =~ m{^$ok$}) { |
991
|
274
|
50
|
|
|
|
777
|
unless (@fixInfo) { |
992
|
0
|
|
|
|
|
0
|
$path = $matches[0]; |
993
|
0
|
|
|
|
|
0
|
$cap = $capture{$path}; |
994
|
|
|
|
|
|
|
} |
995
|
274
|
|
|
|
|
1049
|
next; |
996
|
|
|
|
|
|
|
} |
997
|
|
|
|
|
|
|
# needs fixing... |
998
|
5
|
|
|
|
|
24
|
my @fixProps = split '/', $fixPath; |
999
|
5
|
|
|
|
|
16
|
foreach $match (@matches) { |
1000
|
7
|
|
|
|
|
35
|
my @matchProps = split '/', $match; |
1001
|
|
|
|
|
|
|
# remove superfluous list properties if necessary |
1002
|
7
|
100
|
66
|
|
|
37
|
$#matchProps = $#fixProps if $ok2 and $#matchProps > $#fixProps; |
1003
|
7
|
|
|
|
|
27
|
for ($i=0; $i<@fixProps; ++$i) { |
1004
|
19
|
50
|
|
|
|
46
|
defined $matchProps[$i] or $matchProps[$i] = $fixProps[$i], next; |
1005
|
19
|
100
|
100
|
|
|
118
|
next if $matchProps[$i] =~ / \d+$/ or $matchProps[$i] eq $fixProps[$i]; |
1006
|
4
|
|
|
|
|
15
|
$matchProps[$i] = $fixProps[$i]; |
1007
|
|
|
|
|
|
|
} |
1008
|
7
|
|
|
|
|
30
|
$fixed = join '/', @matchProps; |
1009
|
7
|
50
|
66
|
|
|
47
|
$err = 1 if $fixed{$fixed} or ($capture{$fixed} and $match ne $fixed); |
|
|
|
33
|
|
|
|
|
1010
|
7
|
|
|
|
|
17
|
push @fixed, $fixed; |
1011
|
7
|
|
|
|
|
32
|
$fixed{$fixed} = 1; |
1012
|
|
|
|
|
|
|
} |
1013
|
5
|
|
|
|
|
35
|
my $tg = $et->GetGroup($fixInfo, 1) . ':' . $$fixInfo{Name}; |
1014
|
5
|
100
|
|
|
|
32
|
my $wrn = lc($fixed[0]) eq lc($matches[0]) ? 'tag ID case' : 'list type'; |
1015
|
5
|
50
|
|
|
|
14
|
if ($err) { |
1016
|
0
|
|
|
|
|
0
|
$et->Warn("Incorrect $wrn for existing $tg (not changed)"); |
1017
|
|
|
|
|
|
|
} else { |
1018
|
|
|
|
|
|
|
# fix the incorrect property paths for all values of this tag |
1019
|
5
|
|
|
|
|
10
|
my $didFix; |
1020
|
5
|
|
|
|
|
16
|
foreach $fixed (@fixed) { |
1021
|
7
|
|
|
|
|
14
|
my $match = shift @matches; |
1022
|
7
|
100
|
|
|
|
29
|
next if $fixed eq $match; |
1023
|
5
|
|
|
|
|
13
|
$capture{$fixed} = $capture{$match}; |
1024
|
5
|
|
|
|
|
11
|
delete $capture{$match}; |
1025
|
|
|
|
|
|
|
# remove xml:lang attribute from incorrect lang-alt list if necessary |
1026
|
5
|
100
|
66
|
|
|
30
|
delete $capture{$fixed}[1]{'xml:lang'} if $ok2 and $match !~ /^$ok2$/; |
1027
|
5
|
|
|
|
|
11
|
$didFix = 1; |
1028
|
|
|
|
|
|
|
} |
1029
|
5
|
100
|
66
|
|
|
36
|
$cap = $capture{$path} || $capture{$fixed[0]} unless @fixInfo; |
1030
|
5
|
100
|
|
|
|
27
|
if ($didFix) { |
1031
|
3
|
|
|
|
|
19
|
$et->Warn("Fixed incorrect $wrn for $tg", 1); |
1032
|
3
|
|
|
|
|
16
|
++$changed; |
1033
|
|
|
|
|
|
|
} |
1034
|
|
|
|
|
|
|
} |
1035
|
|
|
|
|
|
|
} |
1036
|
4153
|
|
|
|
|
8423
|
last; |
1037
|
|
|
|
|
|
|
} |
1038
|
4283
|
|
|
|
|
17189
|
my $nvHash = $et->GetNewValueHash($tagInfo); |
1039
|
4283
|
|
|
|
|
13108
|
my $overwrite = $et->IsOverwriting($nvHash); |
1040
|
4283
|
|
100
|
|
|
14750
|
my $writable = $$tagInfo{Writable} || ''; |
1041
|
4283
|
|
|
|
|
8020
|
my (%attrs, $deleted, $added, $existed, $newLang); |
1042
|
|
|
|
|
|
|
# set up variables to save/restore paths of deleted lang-alt tags |
1043
|
4283
|
100
|
|
|
|
9266
|
if ($writable eq 'lang-alt') { |
1044
|
169
|
|
100
|
|
|
889
|
$newLang = lc($$tagInfo{LangCode} || 'x-default'); |
1045
|
169
|
100
|
100
|
|
|
857
|
if ($delLangPath and $delLangPath eq $path) { |
1046
|
|
|
|
|
|
|
# restore paths of deleted entries for this language |
1047
|
8
|
100
|
|
|
|
31
|
@delPaths = @{$delLangPaths{$newLang}} if $delLangPaths{$newLang}; |
|
2
|
|
|
|
|
9
|
|
1048
|
|
|
|
|
|
|
} else { |
1049
|
161
|
|
|
|
|
367
|
undef %delLangPaths; |
1050
|
161
|
|
|
|
|
318
|
$delLangPath = $path; # base path for deleted lang-alt tags |
1051
|
161
|
|
|
|
|
297
|
undef %delAllLang; |
1052
|
161
|
|
|
|
|
296
|
undef $firstNewPath; # reset first path for new lang-alt tag |
1053
|
|
|
|
|
|
|
} |
1054
|
169
|
100
|
|
|
|
480
|
if (%delAllLang) { |
1055
|
|
|
|
|
|
|
# add missing paths to delete list for entries where all languages were deleted |
1056
|
2
|
|
|
|
|
4
|
my ($prefix, $reSort); |
1057
|
2
|
|
|
|
|
9
|
foreach $prefix (keys %delAllLang) { |
1058
|
6
|
100
|
|
|
|
87
|
next if grep /^$prefix/, @delPaths; |
1059
|
1
|
|
|
|
|
5
|
push @delPaths, "${prefix}10"; |
1060
|
1
|
|
|
|
|
3
|
$reSort = 1; |
1061
|
|
|
|
|
|
|
} |
1062
|
2
|
100
|
|
|
|
11
|
@delPaths = sort @delPaths if $reSort; |
1063
|
|
|
|
|
|
|
} |
1064
|
|
|
|
|
|
|
} |
1065
|
|
|
|
|
|
|
# delete existing entry if necessary |
1066
|
4283
|
100
|
|
|
|
12715
|
if ($isStruct) { |
|
|
100
|
|
|
|
|
|
1067
|
|
|
|
|
|
|
# delete all structure (or pseudo-structure) elements |
1068
|
196
|
|
|
|
|
1155
|
require 'Image/ExifTool/XMPStruct.pl'; |
1069
|
196
|
|
|
|
|
828
|
($deleted, $added, $existed) = DeleteStruct($et, \%capture, \$path, $nvHash, \$changed); |
1070
|
196
|
50
|
100
|
|
|
1172
|
next unless $deleted or $added or $et->IsOverwriting($nvHash); |
|
|
|
66
|
|
|
|
|
1071
|
196
|
100
|
100
|
|
|
559
|
next if $existed and $$nvHash{CreateOnly}; |
1072
|
|
|
|
|
|
|
} elsif ($cap) { |
1073
|
132
|
100
|
|
|
|
437
|
next if $$nvHash{CreateOnly}; # (necessary for List-type tags) |
1074
|
|
|
|
|
|
|
# take attributes from old values if they exist |
1075
|
130
|
|
|
|
|
239
|
%attrs = %{$$cap[1]}; |
|
130
|
|
|
|
|
592
|
|
1076
|
130
|
100
|
|
|
|
360
|
if ($overwrite) { |
1077
|
126
|
|
|
|
|
285
|
my ($oldLang, $delLang, $addLang, @matchingPaths, $langPathPat, %langsHere); |
1078
|
|
|
|
|
|
|
# check to see if this is an indexed list item |
1079
|
126
|
100
|
|
|
|
427
|
if ($path =~ / /) { |
1080
|
44
|
|
|
|
|
107
|
my $pp; |
1081
|
44
|
|
|
|
|
281
|
($pp = $path) =~ s/ \d+/ \\d\+/g; |
1082
|
44
|
|
|
|
|
1680
|
@matchingPaths = sort grep(/^$pp$/, keys %capture); |
1083
|
|
|
|
|
|
|
} else { |
1084
|
82
|
|
|
|
|
198
|
push @matchingPaths, $path; |
1085
|
|
|
|
|
|
|
} |
1086
|
126
|
|
|
|
|
365
|
my $oldOverwrite = $overwrite; |
1087
|
126
|
|
|
|
|
296
|
foreach $path (@matchingPaths) { |
1088
|
181
|
|
|
|
|
599
|
my ($val, $attrs) = @{$capture{$path}}; |
|
181
|
|
|
|
|
564
|
|
1089
|
181
|
100
|
|
|
|
623
|
if ($writable eq 'lang-alt') { |
|
|
100
|
|
|
|
|
|
1090
|
|
|
|
|
|
|
# get original language code (lc for comparisons) |
1091
|
60
|
|
50
|
|
|
187
|
$oldLang = lc($$attrs{'xml:lang'} || 'x-default'); |
1092
|
|
|
|
|
|
|
# revert to original overwrite flag if this is in a different structure |
1093
|
60
|
100
|
100
|
|
|
534
|
if (not $langPathPat or $path !~ /^$langPathPat$/) { |
1094
|
38
|
|
|
|
|
68
|
$overwrite = $oldOverwrite; |
1095
|
38
|
|
|
|
|
200
|
($langPathPat = $path) =~ s/\d+$/\\d+/; |
1096
|
|
|
|
|
|
|
} |
1097
|
|
|
|
|
|
|
# remember languages in this lang-alt list |
1098
|
60
|
|
|
|
|
225
|
$langsHere{$langPathPat}{$oldLang} = 1; |
1099
|
60
|
100
|
|
|
|
159
|
unless (defined $addLang) { |
1100
|
|
|
|
|
|
|
# add to lang-alt list by default if creating this tag from scratch |
1101
|
24
|
100
|
|
|
|
80
|
$addLang = $$nvHash{IsCreating} ? 1 : 0; |
1102
|
|
|
|
|
|
|
} |
1103
|
60
|
100
|
|
|
|
150
|
if ($overwrite < 0) { |
1104
|
13
|
100
|
|
|
|
46
|
next unless $oldLang eq $newLang; |
1105
|
|
|
|
|
|
|
# only add new tag if we are overwriting this one |
1106
|
|
|
|
|
|
|
# (note: this won't match if original XML contains CDATA!) |
1107
|
8
|
|
|
|
|
39
|
$addLang = $et->IsOverwriting($nvHash, UnescapeXML($val)); |
1108
|
8
|
100
|
|
|
|
34
|
next unless $addLang; |
1109
|
|
|
|
|
|
|
} |
1110
|
|
|
|
|
|
|
# delete all if deleting "x-default" and writing with no LangCode |
1111
|
|
|
|
|
|
|
# (XMP spec requires x-default language exist and be first in list) |
1112
|
50
|
100
|
100
|
|
|
320
|
if ($oldLang eq 'x-default' and not $$tagInfo{LangCode}) { |
|
|
100
|
66
|
|
|
|
|
1113
|
13
|
|
|
|
|
22
|
$delLang = 1; # delete all languages |
1114
|
13
|
|
|
|
|
34
|
$overwrite = 1; # force overwrite |
1115
|
|
|
|
|
|
|
} elsif ($$tagInfo{LangCode} and not $delLang) { |
1116
|
|
|
|
|
|
|
# only overwrite specified language |
1117
|
31
|
100
|
|
|
|
110
|
next unless lc($$tagInfo{LangCode}) eq $oldLang; |
1118
|
|
|
|
|
|
|
} |
1119
|
|
|
|
|
|
|
} elsif ($overwrite < 0) { |
1120
|
|
|
|
|
|
|
# only overwrite specific values |
1121
|
7
|
100
|
|
|
|
26
|
if ($$nvHash{Shift}) { |
1122
|
|
|
|
|
|
|
# values to be shifted are checked (hence re-formatted) late, |
1123
|
|
|
|
|
|
|
# so we must un-format the to-be-shifted value for IsOverwriting() |
1124
|
3
|
|
50
|
|
|
14
|
my $fmt = $$tagInfo{Writable} || ''; |
1125
|
3
|
100
|
|
|
|
23
|
if ($fmt eq 'rational') { |
|
|
50
|
|
|
|
|
|
1126
|
1
|
|
|
|
|
10
|
ConvertRational($val); |
1127
|
|
|
|
|
|
|
} elsif ($fmt eq 'date') { |
1128
|
2
|
|
|
|
|
16
|
$val = ConvertXMPDate($val); |
1129
|
|
|
|
|
|
|
} |
1130
|
|
|
|
|
|
|
} |
1131
|
|
|
|
|
|
|
# (note: this won't match if original XML contains CDATA!) |
1132
|
7
|
100
|
|
|
|
36
|
next unless $et->IsOverwriting($nvHash, UnescapeXML($val)); |
1133
|
|
|
|
|
|
|
} |
1134
|
143
|
50
|
|
|
|
358
|
if ($verbose > 1) { |
1135
|
0
|
|
|
|
|
0
|
my $grp = $et->GetGroup($tagInfo, 1); |
1136
|
0
|
|
|
|
|
0
|
my $tagName = $$tagInfo{Name}; |
1137
|
0
|
0
|
|
|
|
0
|
$tagName =~ s/-$$tagInfo{LangCode}$// if $$tagInfo{LangCode}; |
1138
|
0
|
0
|
|
|
|
0
|
$tagName .= '-' . $$attrs{'xml:lang'} if $$attrs{'xml:lang'}; |
1139
|
0
|
|
|
|
|
0
|
$et->VerboseValue("- $grp:$tagName", $val); |
1140
|
|
|
|
|
|
|
} |
1141
|
|
|
|
|
|
|
# save attributes and path from first deleted property |
1142
|
|
|
|
|
|
|
# so we can replace it exactly |
1143
|
143
|
100
|
|
|
|
476
|
%attrs = %$attrs unless @delPaths; |
1144
|
143
|
100
|
|
|
|
414
|
if ($writable eq 'lang-alt') { |
1145
|
23
|
|
|
|
|
57
|
$langsHere{$langPathPat}{$oldLang} = 0; # (lang was deleted) |
1146
|
|
|
|
|
|
|
} |
1147
|
|
|
|
|
|
|
# save deleted paths so we can replace the same elements |
1148
|
|
|
|
|
|
|
# (separately for each language of a lang-alt list) |
1149
|
143
|
100
|
100
|
|
|
483
|
if ($writable ne 'lang-alt' or $oldLang eq $newLang) { |
1150
|
137
|
|
|
|
|
295
|
push @delPaths, $path; |
1151
|
|
|
|
|
|
|
} else { |
1152
|
6
|
100
|
|
|
|
24
|
$delLangPaths{$oldLang} or $delLangPaths{$oldLang} = [ ]; |
1153
|
6
|
|
|
|
|
13
|
push @{$delLangPaths{$oldLang}}, $path; |
|
6
|
|
|
|
|
14
|
|
1154
|
|
|
|
|
|
|
} |
1155
|
|
|
|
|
|
|
# keep track of paths where we deleted all languages of a lang-alt tag |
1156
|
143
|
100
|
|
|
|
339
|
if ($delLang) { |
1157
|
19
|
|
|
|
|
1254
|
my $p; |
1158
|
19
|
|
|
|
|
92
|
($p = $path) =~ s/\d+$//; |
1159
|
19
|
|
|
|
|
67
|
$delAllLang{$p} = 1; |
1160
|
|
|
|
|
|
|
} |
1161
|
|
|
|
|
|
|
# delete this tag |
1162
|
143
|
|
|
|
|
865
|
delete $capture{$path}; |
1163
|
143
|
|
|
|
|
249
|
++$changed; |
1164
|
|
|
|
|
|
|
# delete rdf:type tag if it is the only thing left in this structure |
1165
|
143
|
50
|
66
|
|
|
921
|
if ($path =~ /^(.*)\// and $capture{"$1/rdf:type"}) { |
1166
|
0
|
|
|
|
|
0
|
my $pp = $1; |
1167
|
0
|
|
|
|
|
0
|
my @a = grep /^\Q$pp\E\/[^\/]+/, keys %capture; |
1168
|
0
|
0
|
|
|
|
0
|
delete $capture{"$pp/rdf:type"} if @a == 1; |
1169
|
|
|
|
|
|
|
} |
1170
|
|
|
|
|
|
|
} |
1171
|
126
|
100
|
100
|
|
|
458
|
next unless @delPaths or $$tagInfo{List} or $addLang; |
|
|
|
100
|
|
|
|
|
1172
|
125
|
100
|
|
|
|
362
|
if (@delPaths) { |
1173
|
118
|
|
|
|
|
270
|
$path = shift @delPaths; |
1174
|
|
|
|
|
|
|
# make sure new path is unique |
1175
|
118
|
|
|
|
|
393
|
while ($capture{$path}) { |
1176
|
0
|
0
|
|
|
|
0
|
last unless $path =~ s/ \d(\d+)$/' '.length($1+1).($1+1)/e; |
|
0
|
|
|
|
|
0
|
|
1177
|
|
|
|
|
|
|
} |
1178
|
118
|
|
|
|
|
309
|
$deleted = 1; |
1179
|
|
|
|
|
|
|
} else { |
1180
|
|
|
|
|
|
|
# don't change tag if we couldn't delete old copy |
1181
|
|
|
|
|
|
|
# unless this is a list or an lang-alt tag |
1182
|
7
|
50
|
66
|
|
|
39
|
next unless $$tagInfo{List} or $oldLang; |
1183
|
|
|
|
|
|
|
# avoid adding duplicate entry to lang-alt in a list |
1184
|
7
|
50
|
33
|
|
|
48
|
if ($writable eq 'lang-alt' and %langsHere) { |
1185
|
7
|
|
|
|
|
32
|
foreach (sort keys %langsHere) { |
1186
|
9
|
50
|
|
|
|
125
|
next unless $path =~ /^$_$/; |
1187
|
9
|
100
|
|
|
|
46
|
last unless $langsHere{$_}{$newLang}; |
1188
|
3
|
50
|
|
|
|
18
|
$path =~ /(.* )\d(\d+)(.*? \d+)$/ or $et->Error('Internal error writing lang-alt list'), last; |
1189
|
3
|
|
|
|
|
10
|
my $nxt = $2 + 1; |
1190
|
3
|
|
|
|
|
14
|
$path = $1 . length($nxt) . ($nxt) . $3; # step to next index |
1191
|
|
|
|
|
|
|
} |
1192
|
|
|
|
|
|
|
} |
1193
|
|
|
|
|
|
|
# (match last index to put in same lang-alt list for Bag of lang-alt items) |
1194
|
7
|
50
|
|
|
|
45
|
$path =~ m/.* (\d+)/g or warn "Internal error: no list index!\n", next; |
1195
|
7
|
|
|
|
|
39
|
$added = $1; |
1196
|
|
|
|
|
|
|
} |
1197
|
|
|
|
|
|
|
} else { |
1198
|
|
|
|
|
|
|
# we are never overwriting, so we must be adding to a list |
1199
|
|
|
|
|
|
|
# match the last index unless this is a list of lang-alt lists |
1200
|
4
|
|
|
|
|
25
|
my $pat = '.* (\d+)'; |
1201
|
4
|
100
|
|
|
|
14
|
if ($writable eq 'lang-alt') { |
1202
|
2
|
100
|
|
|
|
7
|
if ($firstNewPath) { |
1203
|
1
|
|
|
|
|
3
|
$path = $firstNewPath; |
1204
|
1
|
|
|
|
|
2
|
$overwrite = 1; # necessary to put x-default entry first below |
1205
|
|
|
|
|
|
|
} else { |
1206
|
1
|
|
|
|
|
3
|
$pat = '.* (\d+)(.*? \d+)'; |
1207
|
|
|
|
|
|
|
} |
1208
|
|
|
|
|
|
|
} |
1209
|
4
|
50
|
|
|
|
66
|
if ($path =~ m/$pat/g) { |
1210
|
4
|
|
|
|
|
16
|
$added = $1; |
1211
|
|
|
|
|
|
|
# set position to end of matching index number |
1212
|
4
|
100
|
|
|
|
20
|
pos($path) = pos($path) - length($2) if $2; |
1213
|
|
|
|
|
|
|
} |
1214
|
|
|
|
|
|
|
} |
1215
|
129
|
100
|
|
|
|
360
|
if (defined $added) { |
1216
|
11
|
|
|
|
|
33
|
my $len = length $added; |
1217
|
11
|
|
|
|
|
29
|
my $pos = pos($path) - $len; |
1218
|
11
|
|
|
|
|
34
|
my $nxt = substr($added, 1) + 1; |
1219
|
|
|
|
|
|
|
# always insert x-default lang-alt entry first (as per XMP spec) |
1220
|
|
|
|
|
|
|
# (need to test $overwrite because this will be a new lang-alt entry otherwise) |
1221
|
11
|
100
|
66
|
|
|
109
|
if ($overwrite and $writable eq 'lang-alt' and (not $$tagInfo{LangCode} or |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
1222
|
|
|
|
|
|
|
$$tagInfo{LangCode} eq 'x-default')) |
1223
|
|
|
|
|
|
|
{ |
1224
|
2
|
|
|
|
|
13
|
my $saveCap = $capture{$path}; |
1225
|
2
|
|
|
|
|
11
|
while ($saveCap) { |
1226
|
1
|
|
|
|
|
3
|
my $p = $path; |
1227
|
1
|
|
|
|
|
8
|
substr($p, $pos, $len) = length($nxt) . $nxt; |
1228
|
|
|
|
|
|
|
# increment index in the path of the existing item |
1229
|
1
|
|
|
|
|
4
|
my $nextCap = $capture{$p}; |
1230
|
1
|
|
|
|
|
3
|
$capture{$p} = $saveCap; |
1231
|
1
|
50
|
|
|
|
5
|
last unless $nextCap; |
1232
|
0
|
|
|
|
|
0
|
$saveCap = $nextCap; |
1233
|
0
|
|
|
|
|
0
|
++$nxt; |
1234
|
|
|
|
|
|
|
} |
1235
|
|
|
|
|
|
|
} else { |
1236
|
|
|
|
|
|
|
# add to end of list |
1237
|
9
|
|
|
|
|
35
|
while ($capture{$path}) { |
1238
|
16
|
|
|
|
|
42
|
my $try = length($nxt) . $nxt; |
1239
|
16
|
|
|
|
|
34
|
substr($path, $pos, $len) = $try; |
1240
|
16
|
|
|
|
|
28
|
$len = length $try; |
1241
|
16
|
|
|
|
|
45
|
++$nxt; |
1242
|
|
|
|
|
|
|
} |
1243
|
|
|
|
|
|
|
} |
1244
|
|
|
|
|
|
|
} |
1245
|
|
|
|
|
|
|
} |
1246
|
|
|
|
|
|
|
# check to see if we want to create this tag |
1247
|
|
|
|
|
|
|
# (create non-avoided tags in XMP data files by default) |
1248
|
|
|
|
|
|
|
my $isCreating = ($$nvHash{IsCreating} or (($isStruct or |
1249
|
|
|
|
|
|
|
($preferred and not $$tagInfo{Avoid} and |
1250
|
4278
|
|
100
|
|
|
30008
|
not defined $$nvHash{Shift})) and not $$nvHash{EditOnly})); |
1251
|
|
|
|
|
|
|
|
1252
|
|
|
|
|
|
|
# don't add new values unless... |
1253
|
|
|
|
|
|
|
# ...tag existed before and was deleted, or we added it to a list |
1254
|
4278
|
100
|
100
|
|
|
25322
|
next unless $deleted or defined $added or |
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
1255
|
|
|
|
|
|
|
# ...tag didn't exist before and we are creating it |
1256
|
|
|
|
|
|
|
(not $cap and $isCreating); |
1257
|
|
|
|
|
|
|
|
1258
|
|
|
|
|
|
|
# get list of new values (all done if no new values specified) |
1259
|
2643
|
100
|
|
|
|
9187
|
my @newValues = $et->GetNewValue($nvHash) or next; |
1260
|
|
|
|
|
|
|
|
1261
|
|
|
|
|
|
|
# set language attribute for lang-alt lists |
1262
|
876
|
100
|
|
|
|
2289
|
if ($writable eq 'lang-alt') { |
1263
|
73
|
|
100
|
|
|
484
|
$attrs{'xml:lang'} = $$tagInfo{LangCode} || 'x-default'; |
1264
|
73
|
100
|
|
|
|
236
|
$firstNewPath = $path if defined $added; # save path of first lang-alt tag added |
1265
|
|
|
|
|
|
|
} |
1266
|
|
|
|
|
|
|
# add new value(s) to %capture hash |
1267
|
876
|
|
|
|
|
1440
|
my $subIdx; |
1268
|
876
|
|
|
|
|
1677
|
for (;;) { |
1269
|
995
|
|
|
|
|
2096
|
my $newValue = shift @newValues; |
1270
|
995
|
100
|
|
|
|
2052
|
if ($isStruct) { |
1271
|
|
|
|
|
|
|
++$changed if AddNewStruct($et, $tagInfo, \%capture, |
1272
|
30
|
50
|
|
|
|
155
|
$path, $newValue, $$tagInfo{Struct}); |
1273
|
|
|
|
|
|
|
} else { |
1274
|
965
|
|
|
|
|
2941
|
$newValue = EscapeXML($newValue); |
1275
|
965
|
|
|
|
|
1655
|
for (;;) { # (a cheap 'goto') |
1276
|
965
|
100
|
|
|
|
2333
|
if ($$tagInfo{Resource}) { |
1277
|
|
|
|
|
|
|
# only store as a resource if it doesn't contain any illegal characters |
1278
|
3
|
50
|
|
|
|
18
|
if ($newValue !~ /[^a-z0-9\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\.\-\_\~]/i) { |
1279
|
3
|
|
|
|
|
36
|
$capture{$path} = [ '', { %attrs, 'rdf:resource' => $newValue } ]; |
1280
|
3
|
|
|
|
|
10
|
last; |
1281
|
|
|
|
|
|
|
} |
1282
|
0
|
|
|
|
|
0
|
my $grp = $et->GetGroup($tagInfo, 1); |
1283
|
0
|
|
|
|
|
0
|
$et->Warn("$grp:$$tagInfo{Name} written as a literal because value is not a valid URI", 1); |
1284
|
|
|
|
|
|
|
# fall through to write as a string literal |
1285
|
|
|
|
|
|
|
} |
1286
|
|
|
|
|
|
|
# remove existing value and/or resource attribute if they exist |
1287
|
962
|
|
|
|
|
1556
|
delete $attrs{'rdf:value'}; |
1288
|
962
|
|
|
|
|
1423
|
delete $attrs{'rdf:resource'}; |
1289
|
962
|
|
|
|
|
3952
|
$capture{$path} = [ $newValue, \%attrs ]; |
1290
|
962
|
|
|
|
|
1720
|
last; |
1291
|
|
|
|
|
|
|
} |
1292
|
965
|
100
|
|
|
|
2340
|
if ($verbose > 1) { |
1293
|
1
|
|
|
|
|
5
|
my $grp = $et->GetGroup($tagInfo, 1); |
1294
|
1
|
|
|
|
|
9
|
$et->VerboseValue("+ $grp:$$tagInfo{Name}", $newValue); |
1295
|
|
|
|
|
|
|
} |
1296
|
965
|
|
|
|
|
1433
|
++$changed; |
1297
|
|
|
|
|
|
|
# add rdf:type if necessary |
1298
|
965
|
50
|
|
|
|
2080
|
if ($$tagInfo{StructType}) { |
1299
|
0
|
|
|
|
|
0
|
AddStructType($et, $$tagInfo{Table}, \%capture, $path); |
1300
|
|
|
|
|
|
|
} |
1301
|
|
|
|
|
|
|
} |
1302
|
995
|
100
|
|
|
|
2275
|
last unless @newValues; |
1303
|
|
|
|
|
|
|
# match last index except for lang-alt items where we want to put each |
1304
|
|
|
|
|
|
|
# item in a different lang-alt list (so match the 2nd-last for these) |
1305
|
119
|
100
|
|
|
|
364
|
my $pat = $writable eq 'lang-alt' ? '.* (\d+)(.*? \d+)' : '.* (\d+)'; |
1306
|
119
|
|
|
|
|
349
|
pos($path) = 0; |
1307
|
119
|
50
|
|
|
|
909
|
$path =~ m/$pat/g or warn("Internal error: no list index for $tag ($path) ($pat)!\n"), next; |
1308
|
119
|
|
|
|
|
347
|
my $idx = $1; |
1309
|
119
|
|
|
|
|
250
|
my $len = length $1; |
1310
|
119
|
100
|
|
|
|
397
|
my $pos = pos($path) - $len - ($2 ? length $2 : 0); |
1311
|
|
|
|
|
|
|
# use sub-indices if necessary to store additional values in sequence |
1312
|
119
|
100
|
|
|
|
357
|
if ($subIdx) { |
|
|
100
|
|
|
|
|
|
1313
|
50
|
|
|
|
|
221
|
$idx = substr($idx, 0, -length($subIdx)); # remove old sub-index |
1314
|
50
|
|
|
|
|
134
|
$subIdx = substr($subIdx, 1) + 1; |
1315
|
50
|
|
|
|
|
127
|
$subIdx = length($subIdx) . $subIdx; |
1316
|
|
|
|
|
|
|
} elsif (@delPaths) { |
1317
|
19
|
|
|
|
|
37
|
$path = shift @delPaths; |
1318
|
|
|
|
|
|
|
# make sure new path is unique |
1319
|
19
|
|
|
|
|
68
|
while ($capture{$path}) { |
1320
|
2
|
50
|
|
|
|
12
|
last unless $path =~ s/ \d(\d+)$/' '.length($1+1).($1+1)/e; |
|
2
|
|
|
|
|
19
|
|
1321
|
|
|
|
|
|
|
} |
1322
|
19
|
|
|
|
|
43
|
next; |
1323
|
|
|
|
|
|
|
} else { |
1324
|
50
|
|
|
|
|
144
|
$subIdx = '10'; |
1325
|
|
|
|
|
|
|
} |
1326
|
100
|
|
|
|
|
418
|
substr($path, $pos, $len) = $idx . $subIdx; |
1327
|
|
|
|
|
|
|
} |
1328
|
|
|
|
|
|
|
# make sure any empty structures are deleted |
1329
|
|
|
|
|
|
|
# (ExifTool shouldn't write these, but other software may) |
1330
|
876
|
100
|
|
|
|
3026
|
if (defined $$tagInfo{Flat}) { |
1331
|
318
|
|
|
|
|
640
|
my $p = $path; |
1332
|
318
|
|
|
|
|
2064
|
while ($p =~ s/\/[^\/]+$//) { |
1333
|
420
|
50
|
|
|
|
2275
|
next unless $capture{$p}; |
1334
|
|
|
|
|
|
|
# it is an error if this property has a value |
1335
|
0
|
0
|
|
|
|
0
|
$et->Error("Improperly structured XMP ($p)",1) if $capture{$p}[0] =~ /\S/; |
1336
|
0
|
|
|
|
|
0
|
delete $capture{$p}; # delete the (hopefully) empty structure |
1337
|
|
|
|
|
|
|
} |
1338
|
|
|
|
|
|
|
} |
1339
|
|
|
|
|
|
|
} |
1340
|
|
|
|
|
|
|
# remove the ExifTool members we created |
1341
|
123
|
|
|
|
|
496
|
delete $$et{XMP_CAPTURE}; |
1342
|
123
|
|
|
|
|
338
|
delete $$et{XMP_NS}; |
1343
|
|
|
|
|
|
|
|
1344
|
123
|
|
|
|
|
340
|
my $maxDataLen = $$dirInfo{MaxDataLen}; |
1345
|
|
|
|
|
|
|
# get DataPt again because it may have been set by ProcessXMP |
1346
|
123
|
|
|
|
|
340
|
$dataPt = $$dirInfo{DataPt}; |
1347
|
|
|
|
|
|
|
|
1348
|
|
|
|
|
|
|
# return now if we didn't change anything |
1349
|
123
|
50
|
66
|
|
|
534
|
unless ($changed or ($maxDataLen and $dataPt and defined $$dataPt and |
|
|
|
66
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
66
|
|
|
|
|
1350
|
|
|
|
|
|
|
length($$dataPt) > $maxDataLen)) |
1351
|
|
|
|
|
|
|
{ |
1352
|
16
|
50
|
|
|
|
190
|
return undef unless $xmpFile; # just rewrite original XMP |
1353
|
0
|
0
|
0
|
|
|
0
|
Write($$dirInfo{OutFile}, $$dataPt) or return -1 if $dataPt and defined $$dataPt; |
|
|
|
0
|
|
|
|
|
1354
|
0
|
|
|
|
|
0
|
return 1; |
1355
|
|
|
|
|
|
|
} |
1356
|
|
|
|
|
|
|
# |
1357
|
|
|
|
|
|
|
# write out the new XMP information (serialize it) |
1358
|
|
|
|
|
|
|
# |
1359
|
|
|
|
|
|
|
# start writing the XMP data |
1360
|
107
|
|
|
|
|
293
|
my (@long, @short, @resFlag); |
1361
|
107
|
|
|
|
|
399
|
$long[0] = $long[1] = $short[0] = ''; |
1362
|
107
|
100
|
|
|
|
372
|
if ($$et{XMP_NO_XPACKET}) { |
1363
|
|
|
|
|
|
|
# write BOM if flag is set |
1364
|
1
|
50
|
|
|
|
6
|
$long[-2] .= "\xef\xbb\xbf" if $$et{XMP_NO_XPACKET} == 2; |
1365
|
|
|
|
|
|
|
} else { |
1366
|
106
|
|
|
|
|
438
|
$long[-2] .= $pktOpen; |
1367
|
|
|
|
|
|
|
} |
1368
|
107
|
100
|
|
|
|
446
|
$long[-2] .= $xmlOpen if $$et{XMP_IS_XML}; |
1369
|
107
|
|
|
|
|
459
|
$long[-2] .= $xmpOpen . $rdfOpen; |
1370
|
|
|
|
|
|
|
|
1371
|
|
|
|
|
|
|
# initialize current property path list |
1372
|
107
|
|
|
|
|
509
|
my (@curPropList, @writeLast, @descStart, $extStart); |
1373
|
107
|
|
|
|
|
0
|
my (%nsCur, $prop, $n, $path); |
1374
|
107
|
|
|
|
|
1109
|
my @pathList = sort TypeFirst keys %capture; |
1375
|
|
|
|
|
|
|
# order properties to write large values last if we have a MaxDataLen limit |
1376
|
107
|
100
|
100
|
|
|
769
|
if ($maxDataLen and @pathList) { |
1377
|
33
|
|
|
|
|
86
|
my @pathTmp; |
1378
|
33
|
|
|
|
|
181
|
my ($lastProp, $lastNS, $propSize) = ('', '', 0); |
1379
|
33
|
|
|
|
|
147
|
my @pathLoop = (@pathList, ''); # add empty path to end of list for loop |
1380
|
33
|
|
|
|
|
102
|
undef @pathList; |
1381
|
33
|
|
|
|
|
111
|
foreach $path (@pathLoop) { |
1382
|
372
|
|
|
|
|
1075
|
$path =~ /^((\w*)[^\/]*)/; # get path element ($1) and ns ($2) |
1383
|
372
|
100
|
|
|
|
869
|
if ($1 eq $lastProp) { |
1384
|
92
|
|
|
|
|
183
|
push @pathTmp, $path; # accumulate all paths with same root |
1385
|
|
|
|
|
|
|
} else { |
1386
|
|
|
|
|
|
|
# put in list to write last if recommended or values are too large |
1387
|
280
|
100
|
66
|
|
|
1338
|
if ($extendedRes{$lastProp} or $extendedRes{$lastNS} or |
|
|
|
66
|
|
|
|
|
1388
|
|
|
|
|
|
|
$propSize > $newDescThresh) |
1389
|
|
|
|
|
|
|
{ |
1390
|
14
|
|
|
|
|
31
|
push @writeLast, @pathTmp; |
1391
|
|
|
|
|
|
|
} else { |
1392
|
266
|
|
|
|
|
579
|
push @pathList, @pathTmp; |
1393
|
|
|
|
|
|
|
} |
1394
|
280
|
100
|
|
|
|
640
|
last unless $path; # all done if we hit empty path |
1395
|
247
|
|
|
|
|
483
|
@pathTmp = ( $path ); |
1396
|
247
|
|
|
|
|
638
|
($lastProp, $lastNS, $propSize) = ($1, $2, 0); |
1397
|
|
|
|
|
|
|
} |
1398
|
339
|
|
|
|
|
780
|
$propSize += length $capture{$path}->[0]; |
1399
|
|
|
|
|
|
|
} |
1400
|
|
|
|
|
|
|
} |
1401
|
|
|
|
|
|
|
|
1402
|
|
|
|
|
|
|
# write out all properties |
1403
|
107
|
|
|
|
|
297
|
for (;;) { |
1404
|
1939
|
|
|
|
|
3087
|
my (%nsNew, $newDesc); |
1405
|
1939
|
100
|
|
|
|
3984
|
unless (@pathList) { |
1406
|
111
|
100
|
|
|
|
575
|
last unless @writeLast; |
1407
|
4
|
|
|
|
|
21
|
@pathList = @writeLast; |
1408
|
4
|
|
|
|
|
17
|
undef @writeLast; |
1409
|
4
|
|
|
|
|
21
|
$newDesc = 2; # start with a new description for the extended data |
1410
|
|
|
|
|
|
|
} |
1411
|
1832
|
|
|
|
|
3141
|
$path = shift @pathList; |
1412
|
1832
|
|
|
|
|
4637
|
my @propList = split('/',$path); # get property list |
1413
|
|
|
|
|
|
|
# must open/close rdf:Description too |
1414
|
1832
|
|
|
|
|
3764
|
unshift @propList, $rdfDesc; |
1415
|
|
|
|
|
|
|
# make sure we have defined all necessary namespaces |
1416
|
1832
|
|
|
|
|
3441
|
foreach $prop (@propList) { |
1417
|
5515
|
50
|
|
|
|
15676
|
$prop =~ /(.*):/ or next; |
1418
|
5515
|
100
|
|
|
|
12425
|
$1 eq 'rdf' and next; # rdf namespace already defined |
1419
|
2401
|
|
|
|
|
4428
|
my $uri = $nsUsed{$1}; |
1420
|
2401
|
100
|
|
|
|
4330
|
unless ($uri) { |
1421
|
1381
|
|
|
|
|
2532
|
$uri = $nsURI{$1}; # we must have added a namespace |
1422
|
1381
|
50
|
|
|
|
2496
|
unless ($uri) { |
1423
|
|
|
|
|
|
|
# (namespace prefix may be empty if trying to write empty XMP structure, forum12384) |
1424
|
0
|
0
|
|
|
|
0
|
if (length $1) { |
1425
|
0
|
|
|
|
|
0
|
my $err = "Undefined XMP namespace: $1"; |
1426
|
0
|
0
|
0
|
|
|
0
|
if (not $xmpErr or $err ne $xmpErr) { |
1427
|
0
|
0
|
|
|
|
0
|
$xmpFile ? $et->Error($err) : $et->Warn($err); |
1428
|
0
|
|
|
|
|
0
|
$xmpErr = $err; |
1429
|
|
|
|
|
|
|
} |
1430
|
|
|
|
|
|
|
} |
1431
|
0
|
|
|
|
|
0
|
next; |
1432
|
|
|
|
|
|
|
} |
1433
|
|
|
|
|
|
|
} |
1434
|
2401
|
|
|
|
|
4447
|
$nsNew{$1} = $uri; |
1435
|
|
|
|
|
|
|
# need a new description if any new namespaces |
1436
|
2401
|
100
|
|
|
|
5535
|
$newDesc = 1 unless $nsCur{$1}; |
1437
|
|
|
|
|
|
|
} |
1438
|
1832
|
|
|
|
|
2675
|
my $closeTo = 0; |
1439
|
1832
|
100
|
|
|
|
3418
|
if ($newDesc) { |
1440
|
|
|
|
|
|
|
# look forward to see if we will want to also open other namespaces |
1441
|
|
|
|
|
|
|
# at this level (this is necessary to keep lists and structures from |
1442
|
|
|
|
|
|
|
# being broken if a property introduces a new namespace; plus it |
1443
|
|
|
|
|
|
|
# improves formatting) |
1444
|
306
|
|
|
|
|
539
|
my ($path2, $ns2); |
1445
|
306
|
|
|
|
|
666
|
foreach $path2 (@pathList) { |
1446
|
1725
|
|
|
|
|
8737
|
my @ns2s = ($path2 =~ m{(?:^|/)([^/]+?):}g); |
1447
|
1725
|
50
|
|
|
|
3552
|
my $opening = $compact{OneDesc} ? 1 : 0; |
1448
|
1725
|
|
|
|
|
2717
|
foreach $ns2 (@ns2s) { |
1449
|
3250
|
100
|
|
|
|
5786
|
next if $ns2 eq 'rdf'; |
1450
|
2230
|
100
|
|
|
|
4428
|
$nsNew{$ns2} and ++$opening, next; |
1451
|
211
|
100
|
|
|
|
572
|
last unless $opening; |
1452
|
|
|
|
|
|
|
# get URI for this existing or new namespace |
1453
|
12
|
50
|
66
|
|
|
75
|
my $uri = $nsUsed{$ns2} || $nsURI{$ns2} or last; |
1454
|
12
|
|
|
|
|
56
|
$nsNew{$ns2} = $uri; # also open this namespace |
1455
|
|
|
|
|
|
|
} |
1456
|
1725
|
100
|
|
|
|
3903
|
last unless $opening; |
1457
|
|
|
|
|
|
|
} |
1458
|
|
|
|
|
|
|
} else { |
1459
|
|
|
|
|
|
|
# find first property where the current path differs from the new path |
1460
|
1526
|
|
|
|
|
3494
|
for ($closeTo=0; $closeTo<@curPropList; ++$closeTo) { |
1461
|
2853
|
50
|
|
|
|
5347
|
last unless $closeTo < @propList; |
1462
|
2853
|
100
|
|
|
|
7855
|
last unless $propList[$closeTo] eq $curPropList[$closeTo]; |
1463
|
|
|
|
|
|
|
} |
1464
|
|
|
|
|
|
|
} |
1465
|
|
|
|
|
|
|
# close out properties down to the common base path |
1466
|
1832
|
|
|
|
|
4665
|
CloseProperty(\@curPropList, \@long, \@short, \@resFlag) while @curPropList > $closeTo; |
1467
|
|
|
|
|
|
|
|
1468
|
|
|
|
|
|
|
# open new description if necessary |
1469
|
1832
|
100
|
|
|
|
3495
|
if ($newDesc) { |
1470
|
306
|
50
|
|
|
|
991
|
$extStart = length($long[-2]) if $newDesc == 2; # extended data starts after this |
1471
|
|
|
|
|
|
|
# save rdf:Description start positions so we can reorder them if necessary |
1472
|
306
|
100
|
|
|
|
872
|
push @descStart, length($long[-2]) if $maxDataLen; |
1473
|
|
|
|
|
|
|
# open the new description |
1474
|
306
|
|
|
|
|
571
|
$prop = $rdfDesc; |
1475
|
306
|
|
|
|
|
1116
|
%nsCur = %nsNew; # save current namespaces |
1476
|
306
|
|
|
|
|
1019
|
my @ns = sort keys %nsCur; |
1477
|
306
|
|
|
|
|
1316
|
$long[-2] .= "$nl$sp<$prop rdf:about='${about}'"; |
1478
|
|
|
|
|
|
|
# generate et:toolkit attribute if this is an exiftool RDF/XML output file |
1479
|
306
|
100
|
66
|
|
|
1954
|
if (@ns and $nsCur{$ns[0]} =~ m{^http://ns.exiftool.(?:ca|org)/}) { |
1480
|
5
|
|
|
|
|
21
|
$long[-2] .= "\n$sp${sp}xmlns:et='http://ns.exiftool.org/1.0/'" . |
1481
|
|
|
|
|
|
|
" et:toolkit='Image::ExifTool $Image::ExifTool::VERSION'"; |
1482
|
|
|
|
|
|
|
} |
1483
|
306
|
|
|
|
|
1620
|
$long[-2] .= "\n$sp${sp}xmlns:$_='$nsCur{$_}'" foreach @ns; |
1484
|
306
|
|
|
|
|
663
|
push @curPropList, $prop; |
1485
|
|
|
|
|
|
|
# set resFlag to 0 to indicate base description when Shorthand enabled |
1486
|
306
|
100
|
|
|
|
868
|
$resFlag[0] = 0 if $compact{Shorthand}; |
1487
|
|
|
|
|
|
|
} |
1488
|
1832
|
|
|
|
|
2631
|
my ($val, $attrs) = @{$capture{$path}}; |
|
1832
|
|
|
|
|
5047
|
|
1489
|
1832
|
50
|
|
|
|
3505
|
$debug and print "$path = $val\n"; |
1490
|
|
|
|
|
|
|
# open new properties if necessary |
1491
|
1832
|
|
|
|
|
2737
|
my ($attr, $dummy); |
1492
|
1832
|
|
|
|
|
4378
|
for ($n=@curPropList; $n<$#propList; ++$n) { |
1493
|
756
|
|
|
|
|
1411
|
$prop = $propList[$n]; |
1494
|
756
|
|
|
|
|
1367
|
push @curPropList, $prop; |
1495
|
756
|
|
|
|
|
1567
|
$prop =~ s/ .*//; # remove list index if it exists |
1496
|
|
|
|
|
|
|
# (we may add parseType and shorthand properties later, |
1497
|
|
|
|
|
|
|
# so leave off the trailing ">" for now) |
1498
|
756
|
50
|
|
|
|
2553
|
$long[-1] .= ($compact{NoIndent} ? '' : ' ' x scalar(@curPropList)) . "<$prop"; |
1499
|
756
|
100
|
100
|
|
|
4848
|
if ($prop ne $rdfDesc and ($propList[$n+1] !~ /^rdf:/ or |
|
|
|
66
|
|
|
|
|
1500
|
|
|
|
|
|
|
($propList[$n+1] eq 'rdf:type' and $n+1 == $#propList))) |
1501
|
|
|
|
|
|
|
{ |
1502
|
|
|
|
|
|
|
# check for empty structure |
1503
|
134
|
100
|
|
|
|
539
|
if ($propList[$n+1] =~ /:~dummy~$/) { |
1504
|
3
|
|
|
|
|
10
|
$long[-1] .= " rdf:parseType='Resource'/>$nl"; |
1505
|
3
|
|
|
|
|
6
|
pop @curPropList; |
1506
|
3
|
|
|
|
|
6
|
$dummy = 1; |
1507
|
3
|
|
|
|
|
6
|
last; |
1508
|
|
|
|
|
|
|
} |
1509
|
131
|
100
|
|
|
|
331
|
if ($compact{Shorthand}) { |
1510
|
1
|
|
|
|
|
4
|
$resFlag[$#curPropList] = 1; |
1511
|
1
|
|
|
|
|
3
|
push @long, ''; |
1512
|
1
|
|
|
|
|
5
|
push @short, ''; |
1513
|
|
|
|
|
|
|
} else { |
1514
|
|
|
|
|
|
|
# use rdf:parseType='Resource' to avoid new 'rdf:Description' |
1515
|
130
|
|
|
|
|
494
|
$long[-1] .= " rdf:parseType='Resource'>$nl"; |
1516
|
|
|
|
|
|
|
} |
1517
|
|
|
|
|
|
|
} else { |
1518
|
622
|
|
|
|
|
1898
|
$long[-1] .= ">$nl"; # (will be no shorthand properties) |
1519
|
|
|
|
|
|
|
} |
1520
|
|
|
|
|
|
|
} |
1521
|
1832
|
|
|
|
|
3076
|
my $prop2 = pop @propList; # get new property name |
1522
|
|
|
|
|
|
|
# add element unless it was a dummy structure field |
1523
|
1832
|
50
|
66
|
|
|
5885
|
unless ($dummy or ($val eq '' and $prop2 =~ /:~dummy~$/)) { |
|
|
|
66
|
|
|
|
|
1524
|
1829
|
|
|
|
|
4030
|
$prop2 =~ s/ .*//; # remove list index if it exists |
1525
|
1829
|
50
|
|
|
|
4626
|
my $pad = $compact{NoIndent} ? '' : ' ' x (scalar(@curPropList) + 1); |
1526
|
|
|
|
|
|
|
# (can't write as shortcut if it has attributes or CDATA) |
1527
|
1829
|
100
|
66
|
|
|
5129
|
if (defined $resFlag[$#curPropList] and not %$attrs and $val !~ /
|
|
|
|
66
|
|
|
|
|
1528
|
19
|
|
|
|
|
71
|
$short[-1] .= "\n$pad$prop2='${val}'"; |
1529
|
|
|
|
|
|
|
} else { |
1530
|
1810
|
|
|
|
|
3874
|
$long[-1] .= "$pad<$prop2"; |
1531
|
|
|
|
|
|
|
# write out attributes |
1532
|
1810
|
|
|
|
|
5212
|
foreach $attr (sort keys %$attrs) { |
1533
|
211
|
|
|
|
|
531
|
my $attrVal = $$attrs{$attr}; |
1534
|
211
|
50
|
|
|
|
610
|
my $quot = ($attrVal =~ /'/) ? '"' : "'"; |
1535
|
211
|
|
|
|
|
652
|
$long[-1] .= " $attr=$quot$attrVal$quot"; |
1536
|
|
|
|
|
|
|
} |
1537
|
1810
|
100
|
|
|
|
6914
|
$long[-1] .= length $val ? ">$val$prop2>$nl" : "/>$nl"; |
1538
|
|
|
|
|
|
|
} |
1539
|
|
|
|
|
|
|
} |
1540
|
|
|
|
|
|
|
} |
1541
|
|
|
|
|
|
|
# close out all open properties |
1542
|
107
|
|
|
|
|
736
|
CloseProperty(\@curPropList, \@long, \@short, \@resFlag) while @curPropList; |
1543
|
|
|
|
|
|
|
|
1544
|
|
|
|
|
|
|
# limit XMP length and re-arrange if necessary to fit inside specified size |
1545
|
107
|
100
|
|
|
|
511
|
if ($maxDataLen) { |
1546
|
|
|
|
|
|
|
# adjust maxDataLen to allow room for closing elements |
1547
|
35
|
|
|
|
|
131
|
$maxDataLen -= length($rdfClose) + length($xmpClose) + length($pktCloseW); |
1548
|
35
|
50
|
|
|
|
138
|
$extStart or $extStart = length $long[-2]; |
1549
|
35
|
|
|
|
|
183
|
my @rtn = LimitXMPSize($et, \$long[-2], $maxDataLen, $about, \@descStart, $extStart); |
1550
|
|
|
|
|
|
|
# return extended XMP information in $dirInfo |
1551
|
35
|
|
|
|
|
161
|
$$dirInfo{ExtendedXMP} = $rtn[0]; |
1552
|
35
|
|
|
|
|
112
|
$$dirInfo{ExtendedGUID} = $rtn[1]; |
1553
|
|
|
|
|
|
|
# compact if necessary to fit |
1554
|
35
|
50
|
|
|
|
179
|
$compact{NoPadding} = 1 if length($long[-2]) + 101 * $numPadLines > $maxDataLen; |
1555
|
|
|
|
|
|
|
} |
1556
|
107
|
50
|
|
|
|
472
|
$compact{NoPadding} = 1 if $$dirInfo{Compact}; |
1557
|
|
|
|
|
|
|
# |
1558
|
|
|
|
|
|
|
# close out the XMP, clean up, and return our data |
1559
|
|
|
|
|
|
|
# |
1560
|
107
|
|
|
|
|
408
|
$long[-2] .= $rdfClose; |
1561
|
107
|
100
|
|
|
|
518
|
$long[-2] .= $xmpClose unless $$et{XMP_NO_XMPMETA}; |
1562
|
|
|
|
|
|
|
|
1563
|
|
|
|
|
|
|
# remove the ExifTool members we created |
1564
|
107
|
|
|
|
|
260
|
delete $$et{XMP_CAPTURE}; |
1565
|
107
|
|
|
|
|
271
|
delete $$et{XMP_NS}; |
1566
|
107
|
|
|
|
|
249
|
delete $$et{XMP_NO_XMPMETA}; |
1567
|
|
|
|
|
|
|
|
1568
|
|
|
|
|
|
|
# (the XMP standard recommends writing 2k-4k of white space before the |
1569
|
|
|
|
|
|
|
# packet trailer, with a newline every 100 characters) |
1570
|
107
|
100
|
|
|
|
428
|
unless ($$et{XMP_NO_XPACKET}) { |
1571
|
106
|
|
|
|
|
328
|
my $pad = (' ' x 100) . "\n"; |
1572
|
|
|
|
|
|
|
# get current XMP length without padding |
1573
|
106
|
|
|
|
|
352
|
my $len = length($long[-2]) + length($pktCloseW); |
1574
|
106
|
50
|
0
|
|
|
1436
|
if ($$dirInfo{InPlace} and not ($$dirInfo{InPlace} == 2 and $len > $dirLen)) { |
|
|
100
|
33
|
|
|
|
|
|
|
|
100
|
|
|
|
|
1575
|
|
|
|
|
|
|
# pad to specified DirLen |
1576
|
0
|
0
|
|
|
|
0
|
if ($len > $dirLen) { |
1577
|
0
|
|
|
|
|
0
|
my $str = 'Not enough room to edit XMP in place'; |
1578
|
0
|
0
|
|
|
|
0
|
$str .= '. Try Shorthand feature' unless $compact{Shorthand}; |
1579
|
0
|
|
|
|
|
0
|
$et->Warn($str); |
1580
|
0
|
|
|
|
|
0
|
return undef; |
1581
|
|
|
|
|
|
|
} |
1582
|
0
|
|
|
|
|
0
|
my $num = int(($dirLen - $len) / length($pad)); |
1583
|
0
|
0
|
|
|
|
0
|
if ($num) { |
1584
|
0
|
|
|
|
|
0
|
$long[-2] .= $pad x $num; |
1585
|
0
|
|
|
|
|
0
|
$len += length($pad) * $num; |
1586
|
|
|
|
|
|
|
} |
1587
|
0
|
0
|
|
|
|
0
|
$len < $dirLen and $long[-2] .= (' ' x ($dirLen - $len - 1)) . "\n"; |
1588
|
|
|
|
|
|
|
} elsif (not $compact{NoPadding} and not $xmpFile and not $$dirInfo{ReadOnly}) { |
1589
|
66
|
|
|
|
|
822
|
$long[-2] .= $pad x $numPadLines; |
1590
|
|
|
|
|
|
|
} |
1591
|
106
|
100
|
|
|
|
660
|
$long[-2] .= ($$dirInfo{ReadOnly} ? $pktCloseR : $pktCloseW); |
1592
|
|
|
|
|
|
|
} |
1593
|
|
|
|
|
|
|
# return empty data if no properties exist and this is allowed |
1594
|
107
|
100
|
66
|
|
|
589
|
unless (%capture or $xmpFile or $$dirInfo{InPlace} or $$dirInfo{NoDelete}) { |
|
|
|
66
|
|
|
|
|
|
|
|
66
|
|
|
|
|
1595
|
3
|
|
|
|
|
10
|
$long[-2] = ''; |
1596
|
|
|
|
|
|
|
} |
1597
|
107
|
0
|
|
|
|
368
|
return($xmpFile ? -1 : undef) if $xmpErr; |
|
|
50
|
|
|
|
|
|
1598
|
107
|
|
|
|
|
357
|
$$et{CHANGED} += $changed; |
1599
|
107
|
0
|
33
|
|
|
457
|
$debug > 1 and $long[-2] and print $long[-2],"\n"; |
1600
|
107
|
100
|
|
|
|
1802
|
return $long[-2] unless $xmpFile; |
1601
|
35
|
50
|
|
|
|
244
|
Write($$dirInfo{OutFile}, $long[-2]) or return -1; |
1602
|
35
|
|
|
|
|
1887
|
return 1; |
1603
|
|
|
|
|
|
|
} |
1604
|
|
|
|
|
|
|
|
1605
|
|
|
|
|
|
|
|
1606
|
|
|
|
|
|
|
1; # end |
1607
|
|
|
|
|
|
|
|
1608
|
|
|
|
|
|
|
__END__ |