File Coverage

blib/lib/Image/ExifTool/WriteXMP.pl
Criterion Covered Total %
statement 712 875 81.3
branch 453 646 70.1
condition 233 367 63.4
subroutine 20 22 90.9
pod 0 19 0.0
total 1418 1929 73.5


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 36     36   253 use strict;
  36         75  
  36         1255  
11 36     36   181 use vars qw(%specialStruct %dateTimeInfo %stdXlatNS);
  36         70  
  36         1801  
12              
13 36     36   189 use Image::ExifTool qw(:DataAccess :Utils);
  36         67  
  36         364669  
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 120     120 0 234 my $et = shift;
57 120         459 my $nv = $$et{NEW_VALUE}{$Image::ExifTool::XMP::x{xmptk}};
58 120         206 my $tk;
59 120 100       305 if (defined $nv) {
60 1         4 $tk = $et->GetNewValue($nv);
61 1 50       8 $et->VerboseValue(($tk ? '+' : '-') . ' XMP-x:XMPToolkit', $tk);
62 1         2 ++$$et{CHANGED};
63             } else {
64 119         344 $tk = "Image::ExifTool $Image::ExifTool::VERSION";
65             }
66 120 50       683 my $str = $tk ? (" x:xmptk='" . EscapeXML($tk) . "'") : '';
67 120         450 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 10 my ($xmpPt, $mode) = @_;
77 4         13 $$xmpPt =~ s/^\s*\s*//s; # remove leading comment if it exists
78 4 50       26 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       13 $mode = 'w' unless $mode;
84 4         9 my $end = substr($$xmpPt, -32, 32);
85             # check for proper xpacket trailer and set r/w mode if necessary
86 4 50       37 return '' unless $end =~ s/(e\0*n\0*d\0*=\0*['"]\0*)([rw])(\0*['"]\0*\?\0*>)/$1$mode$3/;
87 4 50       15 substr($$xmpPt, -32, 32) = $end if $2 ne $mode;
88 4         11 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 258 my $val = shift;
145 127         264 my ($y, $m, $d, $t, $tz);
146 127 100       856 if ($val =~ /(\d{4}):(\d{2}):(\d{2}) (\d{2}:\d{2}(?::\d{2}(?:\.\d*)?)?)(.*)/) {
    50          
    0          
147 99         530 ($y, $m, $d, $t, $tz) = ($1, $2, $3, $4, $5);
148 99         347 $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         89 $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       308 if ($tz) {
160 21 50       121 $tz =~ /^(Z|[+-]\d{2}:\d{2})$/ or return undef;
161 21         60 $val .= $tz;
162             }
163 127         701 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 2511     2511 0 4946 my ($et, $tagInfo, $valPtr, $convType) = @_;
174              
175 2511 100       5291 if ($$tagInfo{Struct}) {
176 95         6145 require 'Image/ExifTool/XMPStruct.pl';
177 95         234 my ($item, $err, $w, $warn);
178 95 100       296 unless (ref $$valPtr) {
179 73         326 ($$valPtr, $warn) = InflateStruct($valPtr);
180             # expect a structure HASH ref or ARRAY of structures
181 73 100       245 unless (ref $$valPtr) {
182 61 50       189 $$valPtr eq '' and $$valPtr = { }, return undef; # allow empty structures
183 61         222 return 'Improperly formed structure';
184             }
185             }
186 34 100       105 if (ref $$valPtr eq 'ARRAY') {
187 1 50       6 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         165 ($$valPtr, $err) = CheckStruct($et, $$valPtr, $$tagInfo{Struct});
203             }
204 33 50       93 $warn and $$et{CHECK_WARN} = $warn;
205 33         98 return $err;
206             }
207 2416         3672 my $format = $$tagInfo{Writable};
208             # (if no format specified, value is a simple string)
209 2416 100 100     8693 if (not $format or $format eq 'string' or $format eq 'lang-alt') {
      100        
210             # convert value to UTF8 if necessary
211 1298 100       3301 if ($$et{OPTIONS}{Charset} ne 'UTF8') {
212 4 50       16 if ($$valPtr =~ /[\x80-\xff]/) {
213             # convert from Charset to UTF-8
214 4         11 $$valPtr = $et->Encode($$valPtr,'UTF8');
215             }
216             } else {
217             # translate invalid XML characters to "."
218 1294         2597 $$valPtr =~ tr/\0-\x08\x0b\x0c\x0e-\x1f/./;
219             # fix any malformed UTF-8 characters
220 1294 50 33     3975 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 1298         3193 return undef; # success
226             }
227 1118 100 100     4580 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     1122 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         23 return 'Not a floating point number';
235             }
236 358 100       1055 if ($format eq 'rational') {
237 269         904 $$valPtr = join('/', Image::ExifTool::Rationalize($$valPtr));
238             }
239             } elsif ($format eq 'integer') {
240             # make sure the value is integer
241 591 100       1578 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         112 return 'Not an integer';
247             }
248             } elsif ($format eq 'date') {
249 92         320 my $newDate = FormatXMPDate($$valPtr);
250 92 50       267 return "Invalid date/time (use YYYY:mm:dd HH:MM:SS[.ss][+/-HH:MM|Z])" unless $newDate;
251 92         189 $$valPtr = $newDate;
252             } elsif ($format eq 'boolean') {
253             # (allow lower-case 'true' and 'false' if not setting PrintConv value)
254 68 100 66     499 if (not $$valPtr or $$valPtr =~ /false/i or $$valPtr =~ /^no$/i) {
    50 66        
      33        
      33        
255 43 0 33     237 if (not $$valPtr or $$valPtr ne 'false' or not $convType or $convType eq 'PrintConv') {
      33        
      33        
256 43         83 $$valPtr = 'False';
257             }
258             } elsif ($$valPtr ne 'true' or not $convType or $convType eq 'PrintConv') {
259 25         53 $$valPtr = 'True';
260             }
261             } elsif ($format eq '1') {
262             # this is the entire XMP data block
263 1 50       4 return 'Invalid XMP data' unless ValidateXMP($valPtr);
264             } else {
265 0         0 return "Unknown XMP format: $format";
266             }
267 1076         2791 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 8601     8601 0 11437 my $tagInfo = shift;
277 8601 100       20284 SetPropertyPath($$tagInfo{Table}, $$tagInfo{TagID}) unless $$tagInfo{PropertyPath};
278 8601         16107 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 3966     3966 0 6411 my ($tagTablePtr, $tagID, $parentID, $structPtr, $propList, $isType) = @_;
290 3966   66     7504 my $table = $structPtr || $tagTablePtr;
291 3966         6090 my $tagInfo = $$table{$tagID};
292 3966         4308 my $flatInfo;
293              
294 3966 50       7787 return if ref($tagInfo) ne 'HASH'; # (shouldn't happen)
295              
296 3966 100       6012 if ($structPtr) {
297 2296         3879 my $flatID = $parentID . ucfirst($tagID);
298 2296         5294 $flatInfo = $$tagTablePtr{$flatID};
299 2296 100       3799 if ($flatInfo) {
    50          
300 2244 50       4458 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         197 $flatInfo = { Name => ucfirst($flatID), Flat => 1 };
307 52         216 AddTagToTable($tagTablePtr, $flatID, $flatInfo);
308             }
309 2296 100       3657 $isType = 1 if $$structPtr{TYPE};
310             } else {
311             # don't override existing main table entry if already set by a Struct
312 1670 50       3419 return if $$tagInfo{PropertyPath};
313             # use property path from original tagInfo if this is an alternate-language tag
314 1670         2400 my $srcInfo = $$tagInfo{SrcTagInfo};
315 1670 100       2755 $$tagInfo{PropertyPath} = GetPropertyPath($srcInfo) if $srcInfo;
316 1670 100       2709 return if $$tagInfo{PropertyPath};
317             # set property path for all flattened tags in structure if necessary
318 1667 100       3237 if ($$tagInfo{RootTagInfo}) {
319 59         312 SetPropertyPath($tagTablePtr, $$tagInfo{RootTagInfo}{TagID});
320 59 50       256 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 3904   66     10716 my $ns = $$tagInfo{Namespace} || $$table{NAMESPACE};
326 3904 50       6105 $ns or warn("No namespace for $tagID\n"), return;
327 3904         4761 my (@propList, $listType);
328 3904 100       7529 $propList and @propList = @$propList;
329 3904         7756 push @propList, "$ns:$tagID";
330             # lang-alt lists are handled specially, signified by Writable='lang-alt'
331 3904 100 100     10290 if ($$tagInfo{Writable} and $$tagInfo{Writable} eq 'lang-alt') {
332 98         172 $listType = 'Alt';
333             # remove language code from property path if it exists
334 98 50       222 $propList[-1] =~ s/-$$tagInfo{LangCode}$// if $$tagInfo{LangCode};
335             # handle lists of lang-alt lists (eg. XMP-plus:Custom tags)
336 98 100 66     315 if ($$tagInfo{List} and $$tagInfo{List} ne '1') {
337 3         9 push @propList, "rdf:$$tagInfo{List}", 'rdf:li 10';
338             }
339             } else {
340 3806         4931 $listType = $$tagInfo{List};
341             }
342             # add required properties if this is a list
343 3904 100 66     7675 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 3904         5928 my $strTable = $$tagInfo{Struct};
346 3904 100 100     8202 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 229 100       753 RegisterNamespace($strTable) if ref $$strTable{NAMESPACE};
354 229         380 my $tag;
355 229         1237 foreach $tag (keys %$strTable) {
356             # ignore special fields and any lang-alt fields we may have added
357 2791 100 100     8380 next if $specialStruct{$tag} or $$strTable{$tag}{LangCode};
358 2296 100       4165 my $fullID = $parentID ? $parentID . ucfirst($tagID) : $tagID;
359 2296         3957 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 3904 100       6121 if ($structPtr) {
365 2296         2805 $tagInfo = $flatInfo;
366             # set StructType flag if any containing structure has a TYPE
367 2296 100       3561 $$tagInfo{StructType} = 1 if $isType;
368             }
369             # set property path for tagInfo in main table
370 3904         15285 $$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 1148     1148 0 1915 my ($et, $propList, $val, $attrs) = @_;
381 1148 50 33     3179 return unless defined $val and @$propList > 2;
382 1148 100 66     8189 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 1147 100       2651 return unless @$propList > 3;
388             # ignore empty list properties
389 1139 50       2188 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 1139         1626 my $capture = $$et{XMP_CAPTURE};
395 1139         2730 my $path = join('/', @$propList[3..$#$propList]);
396 1139 50       2259 if (defined $$capture{$path}) {
397 0         0 $$et{XMP_ERROR} = "Duplicate XMP property: $path";
398             } else {
399 1139   100     5019 $$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         4 $$et{XMP_NO_XMPMETA} = 1;
406             # add missing x:xmpmeta element and try again
407 1         4 unshift @$propList, 'x:xmpmeta';
408 1         8 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 58 my ($blankInfo, $propListPt, $val, $attrs) = @_;
425              
426 30         91 my $propPath = join '/', @$propListPt;
427 30         112 my @ids = ($propPath =~ m{ #([^ /]*)}g);
428 30         42 my $id;
429             # split the property path at each nodeID
430 30         50 foreach $id (@ids) {
431 30         310 my ($pre, $prop, $post) = ($propPath =~ m{^(.*?)/([^/]*) #$id((/.*)?)$});
432 30 50       78 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       59 unless ($prop eq $rdfDesc) {
436 12 100       22 if ($post) {
437 8         20 $post = "/$prop$post";
438             } else {
439 4         10 $pre = "$pre/$prop";
440             }
441             }
442 30         68 $$blankInfo{Prop}{$id}{Pre}{$pre} = 1;
443 30 100 66     122 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         125 $$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 10 my ($et, $tagTablePtr, $blankInfo, $isWriting) = @_;
457 4 100       15 $et->VPrint(1, " [Elements with nodeID set:]\n") unless $isWriting;
458 4         7 my ($id, $pre, $post);
459             # handle each nodeID separately
460 4         7 foreach $id (sort keys %{$$blankInfo{Prop}}) {
  4         19  
461 8         18 my $path = $$blankInfo{Prop}{$id};
462             # flag all resource names so we can warn later if some are unused
463 8         10 my %unused;
464 8         37 foreach $post (keys %{$$path{Post}}) {
  8         24  
465 26         37 $unused{$post} = 1;
466             }
467             # combine property paths for all possible paths through this node
468 8         14 foreach $pre (sort keys %{$$path{Pre}}) {
  8         25  
469             # there will be no description for the object of a blank node
470 16 100       70 next unless $pre =~ m{/$rdfDesc/};
471 8         18 foreach $post (sort keys %{$$path{Post}}) {
  8         32  
472 38         103 my @propList = split m{/}, "$pre$post";
473 38         48 my ($val, $attrs) = @{$$path{Post}{$post}};
  38         75  
474 38 100       61 if ($isWriting) {
475 19         31 CaptureXMP($et, \@propList, $val, $attrs);
476             } else {
477 19         47 FoundXMP($et, $tagTablePtr, \@propList, $val);
478             }
479 38         92 delete $unused{$post};
480             }
481             }
482             # save information from unused properties (if RDF is malformed like f-spot output)
483 8 100       25 if (%unused) {
484 4 50       12 $et->Options('Verbose') and $et->Warn('An XMP resource is about nothing');
485 4         14 foreach $post (sort keys %unused) {
486 8         9 my ($val, $attrs, $propPath) = @{$$path{Post}{$post}};
  8         18  
487 8         20 my @propList = split m{/}, $propPath;
488 8 100       15 if ($isWriting) {
489 4         7 CaptureXMP($et, \@propList, $val, $attrs);
490             } else {
491 4         12 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 8683     8683 0 13912 my ($et, $path) = @_;
506 8683         20897 my @propList = split('/',$path);
507 8683         13183 my $nsUsed = $$et{XMP_NS};
508 8683         9586 my $prop;
509 8683         11984 foreach $prop (@propList) {
510 23546         81338 my ($ns, $tag) = $prop =~ /(.+?):(.*)/;
511 23546 100 66     71714 next if not defined $ns or $$nsUsed{$ns};
512 13578         22572 my $uri = $nsURI{$ns};
513 13578 50       19229 unless ($uri) {
514 0         0 warn "No URI for namespace prefix $ns!\n";
515 0         0 next;
516             }
517 13578         16025 my $ns2;
518 13578         33256 foreach $ns2 (keys %$nsUsed) {
519 73165 50       117288 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 8683         23100 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 4 my ($et, $tagTablePtr, $capture, $path, $basePath) = @_;
536 1         5 my @props = split '/', $path;
537 1         2 my %doneID;
538 1         2 for (;;) {
539 5         7 pop @props;
540 5 50       9 last unless @props;
541 5         10 my $tagID = GetXMPTagID(\@props);
542 5 100       11 next if $doneID{$tagID};
543 2         5 $doneID{$tagID} = 1;
544 2         3 my $tagInfo = $$tagTablePtr{$tagID};
545 2 50       6 last unless ref $tagInfo eq 'HASH';
546 2 100       14 if ($$tagInfo{Struct}) {
547 1         4 my $type = $$tagInfo{Struct}{TYPE};
548 1 50       3 if ($type) {
549 1         2 my $pat = $$tagInfo{PropertyPath};
550 1 50       3 $pat or warn("Missing PropertyPath in AddStructType\n"), last;
551 1         4 $pat = ConformPathToNamespace($et, $pat);
552 1         6 $pat =~ s/ \d+/ \\d\+/g;
553 1 50       15 $path =~ /^($pat)/ or warn("Wrong path in AddStructType\n"), last;
554 1         6 my $p = $1 . '/rdf:type';
555 1 50       4 $p = "$basePath/$p" if $basePath;
556 1 50       6 $$capture{$p} = [ '', { 'rdf:resource' => $type } ] unless $$capture{$p};
557             }
558             }
559 2 100       9 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 22 my $chunkSize = 45;
601 8         49 my $len = length $_[0];
602 8         22 my $str = '';
603 8         16 my $i;
604 8         40 for ($i=0; $i<$len; $i+=$chunkSize) {
605 42         63 my $n = $len - $i;
606 42 100       86 $n = $chunkSize if $n > $chunkSize;
607             # add uuencoded data to output (minus size byte, but including trailing newline)
608 42         156 $str .= substr(pack('u', substr($_[0], $i, $n)), 1);
609             }
610             # convert to base64 (remember that "\0" may be encoded as ' ' or '`')
611 8         28 $str =~ tr/` -_/AA-Za-z0-9+\//;
612             # convert pad characters at the end (remember to account for trailing newline)
613 8         31 my $pad = 3 - ($len % 3);
614 8 50       50 substr($str, -$pad-1, $pad) = ('=' x $pad) if $pad < 3;
615 8 50       30 $str =~ tr/\n//d if $_[1]; # remove newlines if specified
616 8         71 return $str;
617             }
618              
619             #------------------------------------------------------------------------------
620             # sort tagInfo hash references by tag name
621             sub ByTagName
622             {
623 130407     130407 0 176787 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 7732 100   7732 0 12505 if ($a =~ /rdf:type$/) {
    100          
631 11 50       38 return substr($a, 0, -8) cmp $b unless $b =~ /rdf:type$/;
632             } elsif ($b =~ /rdf:type$/) {
633 17         44 return $a cmp substr($b, 0, -8);
634             }
635 7704         8887 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 121 my ($et, $dataPt, $maxLen, $about, $startPt, $extStart) = @_;
647              
648             # return straight away if it isn't too big
649 35 50       145 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\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 1051     1051 0 1663 my ($curPropList, $long, $short, $resFlag) = @_;
698              
699 1051         1456 my $prop = pop @$curPropList;
700 1051         1709 $prop =~ s/ .*//; # remove list index if it exists
701 1051         1724 my $pad = $sp x (scalar(@$curPropList) + 1);
702 1051 100       2616 if ($$resFlag[@$curPropList]) {
    100          
703             # close this XMP structure with possible shorthand properties
704 1 50       7 if (length $$short[-1]) {
705 1 50       4 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$nl";
710             } else {
711             # simply close empty property if all shorthand
712 1         3 $$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$nl" if length $$long[-1];
720 1         3 $$long[-2] .= $$short[-1] . $$long[-1];
721 1         2 pop @$short;
722 1         2 pop @$long;
723             } elsif (defined $$resFlag[@$curPropList]) {
724             # close this top level Description with possible shorthand values
725 6 100       14 if (length $$long[-1]) {
726 3         13 $$long[-2] .= $$short[-1] . ">$nl" . $$long[-1] . "$pad$nl";
727             } else {
728 3         8 $$long[-2] .= $$short[-1] . "/>$nl"; # empty element (ie. all shorthand)
729             }
730 6         13 $$short[-1] = $$long[-1] = '';
731             } else {
732             # close this property (no chance of shorthand)
733 1044         2136 $$long[-1] .= "$pad$nl";
734 1044 100       1749 unless (@$curPropList) {
735             # add properties now that this top-level Description is complete
736 298         1189 $$long[-2] .= ">$nl" . $$long[-1];
737 298         534 $$long[-1] = '';
738             }
739             }
740 1051         3111 $#$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 6079     6079 0 10259 my ($et, $dirInfo, $tagTablePtr) = @_;
757 6079 100       20155 $et or return 1; # allow dummy access to autoload this package
758 121         314 my $dataPt = $$dirInfo{DataPt};
759 121         250 my (%capture, %nsUsed, $xmpErr, $about);
760 121         228 my $changed = 0;
761 121         268 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 121   100     706 my $preferred = $xmpFile || ($$et{PreferredGroup} and $$et{PreferredGroup} eq 'XMP');
764 121         295 my $verbose = $$et{OPTIONS}{Verbose};
765 121         210 my %compact = ( %{$$et{OPTIONS}{Compact}} ); # (make a copy so we can change settings)
  121         438  
766 121         273 my $dirLen = $$dirInfo{DirLen};
767 121 100 100     656 $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 121         345 $$et{XMP_CAPTURE} = \%capture;
778 121         308 $$et{XMP_NS} = \%nsUsed;
779 121         253 delete $$et{XMP_NO_XMPMETA};
780 121         239 delete $$et{XMP_NO_XPACKET};
781 121         230 delete $$et{XMP_IS_XML};
782 121         220 delete $$et{XMP_IS_SVG};
783              
784             # set current padding characters
785 121 50       680 ($sp, $nl) = ($compact{NoIndent} ? '' : ' ', $compact{NoNewline} ? '' : "\n");
    50          
786              
787             # get value for new rdf:about
788 121         385 my $tagInfo = $Image::ExifTool::XMP::rdf{about};
789 121 100       447 if (defined $$et{NEW_VALUE}{$tagInfo}) {
790 1   50     6 $about = $et->GetNewValue($$et{NEW_VALUE}{$tagInfo}) || '';
791             }
792              
793 121 100 100     643 if ($xmpFile or $dirLen) {
    50          
794 71         145 delete $$et{XMP_ERROR};
795             # extract all existing XMP information (to the XMP_CAPTURE hash)
796 71         366 my $success = ProcessXMP($et, $dirInfo, $tagTablePtr);
797             # don't continue if there is nothing to parse or if we had a parsing error
798 71 100 66     454 unless ($success and not $$et{XMP_ERROR}) {
799 16   50     82 my $err = $$et{XMP_ERROR} || 'Error parsing XMP';
800             # may ignore this error only if we were successful
801 16 50       52 if ($xmpFile) {
802 16         56 my $raf = $$dirInfo{RAF};
803             # allow empty XMP data so we can create something from nothing
804 16 50 33     224 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 71 100       264 if (defined $about) {
821 1 50       4 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         4 $about = EscapeXML($about); # must escape for XML
827 1         2 ++$changed;
828             } else {
829 70   100     317 $about = $$et{XmpAbout} || '';
830             }
831 71         142 delete $$et{XMP_ERROR};
832              
833             # call InitWriteDirs to initialize FORCE_WRITE flags if necessary
834 71 50 66     340 $et->InitWriteDirs({}, 'XMP') if $xmpFile and $et->GetNewValue('ForceWrite');
835             # set changed if we are ForceWrite tag was set to "XMP"
836 71 50       231 ++$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 50         125 $about = '';
844             }
845             #
846             # handle writing XMP as a block to XMP file
847             #
848 121 100       603 if ($xmpFile) {
849 35         92 $tagInfo = $Image::ExifTool::Extra{XMP};
850 35 50 33     226 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 121 100 66     235 if (%{$$et{DEL_GROUP}} and (grep /^XMP-.+$/, keys %{$$et{DEL_GROUP}} or
  121   66     457  
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         34 my $del = $$et{DEL_GROUP};
870 12         26 my $path;
871 12         71 foreach $path (keys %capture) {
872 141         266 my @propList = split('/',$path); # get property list
873 141         270 my ($tag, $ns) = GetXMPTagID(\@propList);
874             # translate namespace if necessary
875 141 50       291 $ns = $stdXlatNS{$ns} if $stdXlatNS{$ns};
876 141         164 my ($grp, @g);
877             # no "XMP-" added to most groups in exiftool RDF/XML output file
878 141 100 66     610 if ($nsUsed{$ns} and (@g = ($nsUsed{$ns} =~ m{^http://ns.exiftool.(?:ca|org)/(.*?)/(.*?)/}))) {
879 78 100       164 if ($g[1] =~ /^\d/) {
880 20         30 $grp = "XML-$g[0]";
881             #(all XML-* groups stored as uppercase DEL_GROUP key)
882 20         29 my $ucg = uc $grp;
883 20 100 66     100 next unless $$del{$ucg} or ($$del{'XML-*'} and not $$del{"-$ucg"});
      66        
884             } else {
885 58         77 $grp = $g[1];
886 58 100 66     184 next unless $$del{$grp} or ($$del{$g[0]} and not $$del{"-$grp"});
      66        
887             }
888             } else {
889 63         90 $grp = "XMP-$ns";
890 63         89 my $ucg = uc $grp;
891 63 100 100     244 next unless $$del{$ucg} or ($$del{'XMP-*'} and not $$del{"-$ucg"});
      100        
892             }
893 91         351 $et->VerboseValue("- $grp:$tag", $capture{$path}->[0]);
894 91         234 delete $capture{$path};
895 91         154 ++$changed;
896             }
897             }
898             # delete HasExtendedXMP tag (we create it as needed)
899 121         317 my $hasExtTag = 'xmpNote:HasExtendedXMP';
900 121 100       365 if ($capture{$hasExtTag}) {
901 1         11 $et->VerboseValue("- XMP-$hasExtTag", $capture{$hasExtTag}->[0]);
902 1         3 delete $capture{$hasExtTag};
903             }
904             # set $xmpOpen now to to handle xmptk tag first
905 121 100       575 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 121         269 my (@tagInfoList, $delLangPath, %delLangPaths, %delAllLang, $firstNewPath);
913 121         278 my $writeGroup = $$dirInfo{WriteGroup};
914 121         496 foreach $tagInfo (sort ByTagName $et->GetNewTagInfoList()) {
915 14910 100       25611 next unless $et->GetGroup($tagInfo, 0) eq 'XMP';
916 4227 50       7895 next if $$tagInfo{Name} eq 'XMP'; # (ignore full XMP block if we didn't write it already)
917 4227 50 66     6446 next if $writeGroup and $writeGroup ne $$et{NEW_VALUE}{$tagInfo}{WriteGroup};
918 4227 100       5909 if ($$tagInfo{Struct}) {
919 194         591 unshift @tagInfoList, $tagInfo;
920             } else {
921 4033         6761 push @tagInfoList, $tagInfo;
922             }
923             }
924 121         1537 foreach $tagInfo (@tagInfoList) {
925 4227         5681 my @delPaths; # list of deleted paths
926 4227         10781 my $tag = $$tagInfo{TagID};
927 4227         8037 my $path = GetPropertyPath($tagInfo);
928 4227 50       8300 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 4227 100 100     11633 if ($path eq 'rdf:about' or $path eq 'x:xmptk') {
934 2         4 ++$changed;
935 2         5 next;
936             }
937 4225         6183 my $isStruct = $$tagInfo{Struct};
938             # change our property path namespace prefixes to conform
939             # to the ones used in this file
940 4225         7844 $path = ConformPathToNamespace($et, $path);
941             # find existing property
942 4225         6970 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 4225         7067 until ($cap) {
947             # find and fix all incorrect property names if this is a structure or a flattened tag
948 4095         5637 my @fixInfo;
949 4095 100 100     12222 if ($isStruct or defined $$tagInfo{Flat}) {
950             # get tagInfo for all containing (possibly nested) structures
951 2141         5914 my @props = split '/', $path;
952 2141         3910 my $tbl = $$tagInfo{Table};
953 2141         4489 while (@props) {
954 12055         26333 my $info = $$tbl{GetXMPTagID(\@props)};
955             unshift @fixInfo, $info if ref $info eq 'HASH' and $$info{Struct} and
956 12055 100 66     69104 (not @fixInfo or $fixInfo[0] ne $info);
      100        
      100        
957 12055         25662 pop @props;
958             }
959 2141 50       5589 $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 4095 100 100     11630 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 4095         5458 my $err;
966 4095         7780 while (@fixInfo) {
967 4361         6084 my $fixInfo = shift @fixInfo;
968 4361         8341 my $fixPath = ConformPathToNamespace($et, GetPropertyPath($fixInfo));
969 4361         8115 my $regex = quotemeta($fixPath);
970 4361         10111 $regex =~ s/ \d+/ \\d\+/g; # match any list index
971 4361         6484 my $ok = $regex;
972 4361         6953 my ($ok2, $match, $i, @fixed, %fixed, $fixed);
973             # check for incorrect list types
974 4361 100       13850 if ($regex =~ s{\\/rdf\\:(Bag|Seq|Alt)\\/}{/rdf:(Bag|Seq|Alt)/}g) {
    100          
975             # also look for missing bottom-level list
976 1936 100       6905 if ($regex =~ s{/rdf:\(Bag\|Seq\|Alt\)\/rdf\\:li\\ \\d\+$}{}) {
977 1923 100       4198 $regex .= '(/.*)?' unless @fixInfo;
978             }
979             } elsif (not @fixInfo) {
980 1956         2757 $ok2 = $regex;
981             # check for properties in lists that shouldn't be (ref forum4325)
982 1956         3313 $regex .= '(/rdf:(Bag|Seq|Alt)/rdf:li \d+)?';
983             }
984 4361 100       7721 if (@fixInfo) {
985 2052         3554 $regex .= '(/.*)?';
986 2052         3216 $ok .= '(/.*)?';
987             }
988 4361         195681 my @matches = sort grep m{^$regex$}i, keys %capture;
989 4361 100       19367 last unless @matches;
990 270 100       1614 if ($matches[0] =~ m{^$ok$}) {
991 265 50       557 unless (@fixInfo) {
992 0         0 $path = $matches[0];
993 0         0 $cap = $capture{$path};
994             }
995 265         821 next;
996             }
997             # needs fixing...
998 5         22 my @fixProps = split '/', $fixPath;
999 5         16 foreach $match (@matches) {
1000 7         22 my @matchProps = split '/', $match;
1001             # remove superfluous list properties if necessary
1002 7 100 66     27 $#matchProps = $#fixProps if $ok2 and $#matchProps > $#fixProps;
1003 7         22 for ($i=0; $i<@fixProps; ++$i) {
1004 19 50       42 defined $matchProps[$i] or $matchProps[$i] = $fixProps[$i], next;
1005 19 100 100     80 next if $matchProps[$i] =~ / \d+$/ or $matchProps[$i] eq $fixProps[$i];
1006 4         13 $matchProps[$i] = $fixProps[$i];
1007             }
1008 7         21 $fixed = join '/', @matchProps;
1009 7 50 66     37 $err = 1 if $fixed{$fixed} or ($capture{$fixed} and $match ne $fixed);
      33        
1010 7         15 push @fixed, $fixed;
1011 7         21 $fixed{$fixed} = 1;
1012             }
1013 5         21 my $tg = $et->GetGroup($fixInfo, 1) . ':' . $$fixInfo{Name};
1014 5 100       23 my $wrn = lc($fixed[0]) eq lc($matches[0]) ? 'tag ID case' : 'list type';
1015 5 50       12 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         11 foreach $fixed (@fixed) {
1021 7         11 my $match = shift @matches;
1022 7 100       18 next if $fixed eq $match;
1023 5         9 $capture{$fixed} = $capture{$match};
1024 5         9 delete $capture{$match};
1025             # remove xml:lang attribute from incorrect lang-alt list if necessary
1026 5 100 66     23 delete $capture{$fixed}[1]{'xml:lang'} if $ok2 and $match !~ /^$ok2$/;
1027 5         7 $didFix = 1;
1028             }
1029 5 100 66     30 $cap = $capture{$path} || $capture{$fixed[0]} unless @fixInfo;
1030 5 100       18 if ($didFix) {
1031 3         14 $et->Warn("Fixed incorrect $wrn for $tg", 1);
1032 3         12 ++$changed;
1033             }
1034             }
1035             }
1036 4095         6303 last;
1037             }
1038 4225         15054 my $nvHash = $et->GetNewValueHash($tagInfo);
1039 4225         10480 my $overwrite = $et->IsOverwriting($nvHash);
1040 4225   100     11624 my $writable = $$tagInfo{Writable} || '';
1041 4225         6278 my (%attrs, $deleted, $added, $existed, $newLang);
1042             # set up variables to save/restore paths of deleted lang-alt tags
1043 4225 100       7528 if ($writable eq 'lang-alt') {
1044 161   100     692 $newLang = lc($$tagInfo{LangCode} || 'x-default');
1045 161 100 100     578 if ($delLangPath and $delLangPath eq $path) {
1046             # restore paths of deleted entries for this language
1047 8 100       28 @delPaths = @{$delLangPaths{$newLang}} if $delLangPaths{$newLang};
  2         6  
1048             } else {
1049 153         330 undef %delLangPaths;
1050 153         244 $delLangPath = $path; # base path for deleted lang-alt tags
1051 153         265 undef %delAllLang;
1052 153         252 undef $firstNewPath; # reset first path for new lang-alt tag
1053             }
1054 161 100       335 if (%delAllLang) {
1055             # add missing paths to delete list for entries where all languages were deleted
1056 2         3 my ($prefix, $reSort);
1057 2         6 foreach $prefix (keys %delAllLang) {
1058 6 100       70 next if grep /^$prefix/, @delPaths;
1059 1         4 push @delPaths, "${prefix}10";
1060 1         3 $reSort = 1;
1061             }
1062 2 100       9 @delPaths = sort @delPaths if $reSort;
1063             }
1064             }
1065             # delete existing entry if necessary
1066 4225 100       8766 if ($isStruct) {
    100          
1067             # delete all structure (or pseudo-structure) elements
1068 194         1306 require 'Image/ExifTool/XMPStruct.pl';
1069 194         882 ($deleted, $added, $existed) = DeleteStruct($et, \%capture, \$path, $nvHash, \$changed);
1070 194 50 100     1125 next unless $deleted or $added or $et->IsOverwriting($nvHash);
      66        
1071 194 100 100     492 next if $existed and $$nvHash{CreateOnly};
1072             } elsif ($cap) {
1073 132 100       402 next if $$nvHash{CreateOnly}; # (necessary for List-type tags)
1074             # take attributes from old values if they exist
1075 130         202 %attrs = %{$$cap[1]};
  130         465  
1076 130 100       325 if ($overwrite) {
1077 126         241 my ($oldLang, $delLang, $addLang, @matchingPaths, $langPathPat, %langsHere);
1078             # check to see if this is an indexed list item
1079 126 100       358 if ($path =~ / /) {
1080 44         59 my $pp;
1081 44         211 ($pp = $path) =~ s/ \d+/ \\d\+/g;
1082 44         1505 @matchingPaths = sort grep(/^$pp$/, keys %capture);
1083             } else {
1084 82         206 push @matchingPaths, $path;
1085             }
1086 126         361 my $oldOverwrite = $overwrite;
1087 126         257 foreach $path (@matchingPaths) {
1088 181         292 my ($val, $attrs) = @{$capture{$path}};
  181         483  
1089 181 100       585 if ($writable eq 'lang-alt') {
    100          
1090             # get original language code (lc for comparisons)
1091 60   50     137 $oldLang = lc($$attrs{'xml:lang'} || 'x-default');
1092             # revert to original overwrite flag if this is in a different structure
1093 60 100 100     389 if (not $langPathPat or $path !~ /^$langPathPat$/) {
1094 38         51 $overwrite = $oldOverwrite;
1095 38         154 ($langPathPat = $path) =~ s/\d+$/\\d+/;
1096             }
1097             # remember languages in this lang-alt list
1098 60         165 $langsHere{$langPathPat}{$oldLang} = 1;
1099 60 100       116 unless (defined $addLang) {
1100             # add to lang-alt list by default if creating this tag from scratch
1101 24 100       59 $addLang = $$nvHash{IsCreating} ? 1 : 0;
1102             }
1103 60 100       107 if ($overwrite < 0) {
1104 13 100       30 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         25 $addLang = $et->IsOverwriting($nvHash, UnescapeXML($val));
1108 8 100       22 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     222 if ($oldLang eq 'x-default' and not $$tagInfo{LangCode}) {
    100 66        
1113 13         23 $delLang = 1; # delete all languages
1114 13         18 $overwrite = 1; # force overwrite
1115             } elsif ($$tagInfo{LangCode} and not $delLang) {
1116             # only overwrite specified language
1117 31 100       85 next unless lc($$tagInfo{LangCode}) eq $oldLang;
1118             }
1119             } elsif ($overwrite < 0) {
1120             # only overwrite specific values
1121 7 100       23 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     13 my $fmt = $$tagInfo{Writable} || '';
1125 3 100       13 if ($fmt eq 'rational') {
    50          
1126 1         7 ConvertRational($val);
1127             } elsif ($fmt eq 'date') {
1128 2         11 $val = ConvertXMPDate($val);
1129             }
1130             }
1131             # (note: this won't match if original XML contains CDATA!)
1132 7 100       29 next unless $et->IsOverwriting($nvHash, UnescapeXML($val));
1133             }
1134 143 50       341 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       474 %attrs = %$attrs unless @delPaths;
1144 143 100       369 if ($writable eq 'lang-alt') {
1145 23         41 $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     442 if ($writable ne 'lang-alt' or $oldLang eq $newLang) {
1150 137         248 push @delPaths, $path;
1151             } else {
1152 6 100       15 $delLangPaths{$oldLang} or $delLangPaths{$oldLang} = [ ];
1153 6         11 push @{$delLangPaths{$oldLang}}, $path;
  6         11  
1154             }
1155             # keep track of paths where we deleted all languages of a lang-alt tag
1156 143 100       304 if ($delLang) {
1157 19         31 my $p;
1158 19         75 ($p = $path) =~ s/\d+$//;
1159 19         46 $delAllLang{$p} = 1;
1160             }
1161             # delete this tag
1162 143         378 delete $capture{$path};
1163 143         216 ++$changed;
1164             # delete rdf:type tag if it is the only thing left in this structure
1165 143 50 66     839 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     409 next unless @delPaths or $$tagInfo{List} or $addLang;
      100        
1172 125 100       293 if (@delPaths) {
1173 118         236 $path = shift @delPaths;
1174             # make sure new path is unique
1175 118         416 while ($capture{$path}) {
1176 0 0       0 last unless $path =~ s/ \d(\d+)$/' '.length($1+1).($1+1)/e;
  0         0  
1177             }
1178 118         257 $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     27 next unless $$tagInfo{List} or $oldLang;
1183             # avoid adding duplicate entry to lang-alt in a list
1184 7 50 33     31 if ($writable eq 'lang-alt' and %langsHere) {
1185 7         25 foreach (sort keys %langsHere) {
1186 9 50       91 next unless $path =~ /^$_$/;
1187 9 100       32 last unless $langsHere{$_}{$newLang};
1188 3 50       14 $path =~ /(.* )\d(\d+)(.*? \d+)$/ or $et->Error('Internal error writing lang-alt list'), last;
1189 3         8 my $nxt = $2 + 1;
1190 3         13 $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       34 $path =~ m/.* (\d+)/g or warn "Internal error: no list index!\n", next;
1195 7         29 $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         9 my $pat = '.* (\d+)';
1201 4 100       13 if ($writable eq 'lang-alt') {
1202 2 100       6 if ($firstNewPath) {
1203 1         2 $path = $firstNewPath;
1204 1         3 $overwrite = 1; # necessary to put x-default entry first below
1205             } else {
1206 1         2 $pat = '.* (\d+)(.*? \d+)';
1207             }
1208             }
1209 4 50       53 if ($path =~ m/$pat/g) {
1210 4         11 $added = $1;
1211             # set position to end of matching index number
1212 4 100       16 pos($path) = pos($path) - length($2) if $2;
1213             }
1214             }
1215 129 100       382 if (defined $added) {
1216 11         17 my $len = length $added;
1217 11         18 my $pos = pos($path) - $len;
1218 11         25 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     71 if ($overwrite and $writable eq 'lang-alt' and (not $$tagInfo{LangCode} or
      100        
      100        
1222             $$tagInfo{LangCode} eq 'x-default'))
1223             {
1224 2         5 my $saveCap = $capture{$path};
1225 2         9 while ($saveCap) {
1226 1         2 my $p = $path;
1227 1         4 substr($p, $pos, $len) = length($nxt) . $nxt;
1228             # increment index in the path of the existing item
1229 1         3 my $nextCap = $capture{$p};
1230 1         2 $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         25 while ($capture{$path}) {
1238 16         32 my $try = length($nxt) . $nxt;
1239 16         31 substr($path, $pos, $len) = $try;
1240 16         22 $len = length $try;
1241 16         35 ++$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 4220   100     27241 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 4220 100 100     20451 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 2597 100       7966 my @newValues = $et->GetNewValue($nvHash) or next;
1260              
1261             # set language attribute for lang-alt lists
1262 865 100       1756 if ($writable eq 'lang-alt') {
1263 71   100     401 $attrs{'xml:lang'} = $$tagInfo{LangCode} || 'x-default';
1264 71 100       187 $firstNewPath = $path if defined $added; # save path of first lang-alt tag added
1265             }
1266             # add new value(s) to %capture hash
1267 865         1162 my $subIdx;
1268 865         1146 for (;;) {
1269 984         1443 my $newValue = shift @newValues;
1270 984 100       1704 if ($isStruct) {
1271             ++$changed if AddNewStruct($et, $tagInfo, \%capture,
1272 30 50       110 $path, $newValue, $$tagInfo{Struct});
1273             } else {
1274 954         2294 $newValue = EscapeXML($newValue);
1275 954         1348 for (;;) { # (a cheap 'goto')
1276 954 100       1954 if ($$tagInfo{Resource}) {
1277             # only store as a resource if it doesn't contain any illegal characters
1278 3 50       30 if ($newValue !~ /[^a-z0-9\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\.\-\_\~]/i) {
1279 3         18 $capture{$path} = [ '', { %attrs, 'rdf:resource' => $newValue } ];
1280 3         5 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 951         1602 delete $attrs{'rdf:value'};
1288 951         1270 delete $attrs{'rdf:resource'};
1289 951         3488 $capture{$path} = [ $newValue, \%attrs ];
1290 951         1409 last;
1291             }
1292 954 100       1828 if ($verbose > 1) {
1293 1         5 my $grp = $et->GetGroup($tagInfo, 1);
1294 1         8 $et->VerboseValue("+ $grp:$$tagInfo{Name}", $newValue);
1295             }
1296 954         1190 ++$changed;
1297             # add rdf:type if necessary
1298 954 50       1755 if ($$tagInfo{StructType}) {
1299 0         0 AddStructType($et, $$tagInfo{Table}, \%capture, $path);
1300             }
1301             }
1302 984 100       1918 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       333 my $pat = $writable eq 'lang-alt' ? '.* (\d+)(.*? \d+)' : '.* (\d+)';
1306 119         290 pos($path) = 0;
1307 119 50       729 $path =~ m/$pat/g or warn("Internal error: no list index for $tag ($path) ($pat)!\n"), next;
1308 119         267 my $idx = $1;
1309 119         204 my $len = length $1;
1310 119 100       307 my $pos = pos($path) - $len - ($2 ? length $2 : 0);
1311             # use sub-indices if necessary to store additional values in sequence
1312 119 100       287 if ($subIdx) {
    100          
1313 50         117 $idx = substr($idx, 0, -length($subIdx)); # remove old sub-index
1314 50         110 $subIdx = substr($subIdx, 1) + 1;
1315 50         114 $subIdx = length($subIdx) . $subIdx;
1316             } elsif (@delPaths) {
1317 19         32 $path = shift @delPaths;
1318             # make sure new path is unique
1319 19         64 while ($capture{$path}) {
1320 2 50       9 last unless $path =~ s/ \d(\d+)$/' '.length($1+1).($1+1)/e;
  2         15  
1321             }
1322 19         36 next;
1323             } else {
1324 50         99 $subIdx = '10';
1325             }
1326 100         316 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 865 100       2378 if (defined $$tagInfo{Flat}) {
1331 309         482 my $p = $path;
1332 309         1603 while ($p =~ s/\/[^\/]+$//) {
1333 411 50       1698 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 121         335 delete $$et{XMP_CAPTURE};
1342 121         267 delete $$et{XMP_NS};
1343              
1344 121         280 my $maxDataLen = $$dirInfo{MaxDataLen};
1345             # get DataPt again because it may have been set by ProcessXMP
1346 121         260 $dataPt = $$dirInfo{DataPt};
1347              
1348             # return now if we didn't change anything
1349 121 50 66     412 unless ($changed or ($maxDataLen and $dataPt and defined $$dataPt and
      66        
      33        
      66        
1350             length($$dataPt) > $maxDataLen))
1351             {
1352 16 50       166 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 105         213 my (@long, @short, @resFlag);
1361 105         304 $long[0] = $long[1] = $short[0] = '';
1362 105 100       305 if ($$et{XMP_NO_XPACKET}) {
1363             # write BOM if flag is set
1364 1 50       4 $long[-2] .= "\xef\xbb\xbf" if $$et{XMP_NO_XPACKET} == 2;
1365             } else {
1366 104         328 $long[-2] .= $pktOpen;
1367             }
1368 105 100       348 $long[-2] .= $xmlOpen if $$et{XMP_IS_XML};
1369 105         381 $long[-2] .= $xmpOpen . $rdfOpen;
1370              
1371             # initialize current property path list
1372 105         405 my (@curPropList, @writeLast, @descStart, $extStart);
1373 105         0 my (%nsCur, $prop, $n, $path);
1374 105         801 my @pathList = sort TypeFirst keys %capture;
1375             # order properties to write large values last if we have a MaxDataLen limit
1376 105 100 100     515 if ($maxDataLen and @pathList) {
1377 33         75 my @pathTmp;
1378 33         104 my ($lastProp, $lastNS, $propSize) = ('', '', 0);
1379 33         116 my @pathLoop = (@pathList, ''); # add empty path to end of list for loop
1380 33         84 undef @pathList;
1381 33         85 foreach $path (@pathLoop) {
1382 372         854 $path =~ /^((\w*)[^\/]*)/; # get path element ($1) and ns ($2)
1383 372 100       670 if ($1 eq $lastProp) {
1384 92         134 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     1054 if ($extendedRes{$lastProp} or $extendedRes{$lastNS} or
      66        
1388             $propSize > $newDescThresh)
1389             {
1390 14         24 push @writeLast, @pathTmp;
1391             } else {
1392 266         436 push @pathList, @pathTmp;
1393             }
1394 280 100       503 last unless $path; # all done if we hit empty path
1395 247         434 @pathTmp = ( $path );
1396 247         494 ($lastProp, $lastNS, $propSize) = ($1, $2, 0);
1397             }
1398 339         569 $propSize += length $capture{$path}->[0];
1399             }
1400             }
1401              
1402             # write out all properties
1403 105         191 for (;;) {
1404 1925         2484 my (%nsNew, $newDesc);
1405 1925 100       3134 unless (@pathList) {
1406 109 100       368 last unless @writeLast;
1407 4         14 @pathList = @writeLast;
1408 4         10 undef @writeLast;
1409 4         10 $newDesc = 2; # start with a new description for the extended data
1410             }
1411 1820         2508 $path = shift @pathList;
1412 1820         3714 my @propList = split('/',$path); # get property list
1413             # must open/close rdf:Description too
1414 1820         3002 unshift @propList, $rdfDesc;
1415             # make sure we have defined all necessary namespaces
1416 1820         2368 foreach $prop (@propList) {
1417 5476 50       12245 $prop =~ /(.*):/ or next;
1418 5476 100       10346 $1 eq 'rdf' and next; # rdf namespace already defined
1419 2380         3505 my $uri = $nsUsed{$1};
1420 2380 100       3685 unless ($uri) {
1421 1362         2020 $uri = $nsURI{$1}; # we must have added a namespace
1422 1362 50       2029 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 2380         3663 $nsNew{$1} = $uri;
1435             # need a new description if any new namespaces
1436 2380 100       4580 $newDesc = 1 unless $nsCur{$1};
1437             }
1438 1820         2253 my $closeTo = 0;
1439 1820 100       2554 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 304         422 my ($path2, $ns2);
1445 304         517 foreach $path2 (@pathList) {
1446 1715         6802 my @ns2s = ($path2 =~ m{(?:^|/)([^/]+?):}g);
1447 1715 50       2891 my $opening = $compact{OneDesc} ? 1 : 0;
1448 1715         2102 foreach $ns2 (@ns2s) {
1449 3229 100       4637 next if $ns2 eq 'rdf';
1450 2211 100       3699 $nsNew{$ns2} and ++$opening, next;
1451 211 100       427 last unless $opening;
1452             # get URI for this existing or new namespace
1453 12 50 66     50 my $uri = $nsUsed{$ns2} || $nsURI{$ns2} or last;
1454 12         28 $nsNew{$ns2} = $uri; # also open this namespace
1455             }
1456 1715 100       3058 last unless $opening;
1457             }
1458             } else {
1459             # find first property where the current path differs from the new path
1460 1516         2673 for ($closeTo=0; $closeTo<@curPropList; ++$closeTo) {
1461 2833 50       4089 last unless $closeTo < @propList;
1462 2833 100       5960 last unless $propList[$closeTo] eq $curPropList[$closeTo];
1463             }
1464             }
1465             # close out properties down to the common base path
1466 1820         3570 CloseProperty(\@curPropList, \@long, \@short, \@resFlag) while @curPropList > $closeTo;
1467              
1468             # open new description if necessary
1469 1820 100       2897 if ($newDesc) {
1470 304 50       643 $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 304 100       615 push @descStart, length($long[-2]) if $maxDataLen;
1473             # open the new description
1474 304         468 $prop = $rdfDesc;
1475 304         878 %nsCur = %nsNew; # save current namespaces
1476 304         786 my @ns = sort keys %nsCur;
1477 304         1047 $long[-2] .= "$nl$sp<$prop rdf:about='${about}'";
1478             # generate et:toolkit attribute if this is an exiftool RDF/XML output file
1479 304 100 66     1491 if (@ns and $nsCur{$ns[0]} =~ m{^http://ns.exiftool.(?:ca|org)/}) {
1480 5         19 $long[-2] .= "\n$sp${sp}xmlns:et='http://ns.exiftool.org/1.0/'" .
1481             " et:toolkit='Image::ExifTool $Image::ExifTool::VERSION'";
1482             }
1483 304         1286 $long[-2] .= "\n$sp${sp}xmlns:$_='$nsCur{$_}'" foreach @ns;
1484 304         554 push @curPropList, $prop;
1485             # set resFlag to 0 to indicate base description when Shorthand enabled
1486 304 100       701 $resFlag[0] = 0 if $compact{Shorthand};
1487             }
1488 1820         2150 my ($val, $attrs) = @{$capture{$path}};
  1820         3904  
1489 1820 50       2875 $debug and print "$path = $val\n";
1490             # open new properties if necessary
1491 1820         2105 my ($attr, $dummy);
1492 1820         3596 for ($n=@curPropList; $n<$#propList; ++$n) {
1493 750         1078 $prop = $propList[$n];
1494 750         1015 push @curPropList, $prop;
1495 750         1241 $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 750 50       1912 $long[-1] .= ($compact{NoIndent} ? '' : ' ' x scalar(@curPropList)) . "<$prop";
1499 750 100 100     3805 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       376 if ($propList[$n+1] =~ /:~dummy~$/) {
1504 3         8 $long[-1] .= " rdf:parseType='Resource'/>$nl";
1505 3         5 pop @curPropList;
1506 3         5 $dummy = 1;
1507 3         6 last;
1508             }
1509 131 100       273 if ($compact{Shorthand}) {
1510 1         4 $resFlag[$#curPropList] = 1;
1511 1         2 push @long, '';
1512 1         3 push @short, '';
1513             } else {
1514             # use rdf:parseType='Resource' to avoid new 'rdf:Description'
1515 130         402 $long[-1] .= " rdf:parseType='Resource'>$nl";
1516             }
1517             } else {
1518 616         1461 $long[-1] .= ">$nl"; # (will be no shorthand properties)
1519             }
1520             }
1521 1820         2462 my $prop2 = pop @propList; # get new property name
1522             # add element unless it was a dummy structure field
1523 1820 50 66     4740 unless ($dummy or ($val eq '' and $prop2 =~ /:~dummy~$/)) {
      66        
1524 1817         3136 $prop2 =~ s/ .*//; # remove list index if it exists
1525 1817 50       3621 my $pad = $compact{NoIndent} ? '' : ' ' x (scalar(@curPropList) + 1);
1526             # (can't write as shortcut if it has attributes or CDATA)
1527 1817 100 66     4192 if (defined $resFlag[$#curPropList] and not %$attrs and $val !~ /
      66        
1528 19         55 $short[-1] .= "\n$pad$prop2='${val}'";
1529             } else {
1530 1798         3016 $long[-1] .= "$pad<$prop2";
1531             # write out attributes
1532 1798         3885 foreach $attr (sort keys %$attrs) {
1533 209         348 my $attrVal = $$attrs{$attr};
1534 209 50       464 my $quot = ($attrVal =~ /'/) ? '"' : "'";
1535 209         522 $long[-1] .= " $attr=$quot$attrVal$quot";
1536             }
1537 1798 100       5455 $long[-1] .= length $val ? ">$val$nl" : "/>$nl";
1538             }
1539             }
1540             }
1541             # close out all open properties
1542 105         541 CloseProperty(\@curPropList, \@long, \@short, \@resFlag) while @curPropList;
1543              
1544             # limit XMP length and re-arrange if necessary to fit inside specified size
1545 105 100       330 if ($maxDataLen) {
1546             # adjust maxDataLen to allow room for closing elements
1547 35         109 $maxDataLen -= length($rdfClose) + length($xmpClose) + length($pktCloseW);
1548 35 50       134 $extStart or $extStart = length $long[-2];
1549 35         184 my @rtn = LimitXMPSize($et, \$long[-2], $maxDataLen, $about, \@descStart, $extStart);
1550             # return extended XMP information in $dirInfo
1551 35         119 $$dirInfo{ExtendedXMP} = $rtn[0];
1552 35         113 $$dirInfo{ExtendedGUID} = $rtn[1];
1553             # compact if necessary to fit
1554 35 50       171 $compact{NoPadding} = 1 if length($long[-2]) + 101 * $numPadLines > $maxDataLen;
1555             }
1556 105 50       372 $compact{NoPadding} = 1 if $$dirInfo{Compact};
1557             #
1558             # close out the XMP, clean up, and return our data
1559             #
1560 105         298 $long[-2] .= $rdfClose;
1561 105 100       407 $long[-2] .= $xmpClose unless $$et{XMP_NO_XMPMETA};
1562              
1563             # remove the ExifTool members we created
1564 105         245 delete $$et{XMP_CAPTURE};
1565 105         197 delete $$et{XMP_NS};
1566 105         206 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 105 100       317 unless ($$et{XMP_NO_XPACKET}) {
1571 104         218 my $pad = (' ' x 100) . "\n";
1572             # get current XMP length without padding
1573 104         254 my $len = length($long[-2]) + length($pktCloseW);
1574 104 50 0     1147 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 64         681 $long[-2] .= $pad x $numPadLines;
1590             }
1591 104 100       477 $long[-2] .= ($$dirInfo{ReadOnly} ? $pktCloseR : $pktCloseW);
1592             }
1593             # return empty data if no properties exist and this is allowed
1594 105 100 66     487 unless (%capture or $xmpFile or $$dirInfo{InPlace} or $$dirInfo{NoDelete}) {
      66        
      66        
1595 3         6 $long[-2] = '';
1596             }
1597 105 0       285 return($xmpFile ? -1 : undef) if $xmpErr;
    50          
1598 105         257 $$et{CHANGED} += $changed;
1599 105 0 33     305 $debug > 1 and $long[-2] and print $long[-2],"\n";
1600 105 100       1393 return $long[-2] unless $xmpFile;
1601 35 50       193 Write($$dirInfo{OutFile}, $long[-2]) or return -1;
1602 35         1636 return 1;
1603             }
1604              
1605              
1606             1; # end
1607              
1608             __END__