File Coverage

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