File Coverage

blib/lib/Image/ExifTool/WriteXMP.pl
Criterion Covered Total %
statement 719 888 80.9
branch 461 656 70.2
condition 245 379 64.6
subroutine 20 23 86.9
pod 0 20 0.0
total 1445 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   321 use strict;
  39         181  
  39         2417  
11 39     39   255 use vars qw(%specialStruct %dateTimeInfo %stdXlatNS);
  39         85  
  39         3066  
12              
13 39     39   241 use Image::ExifTool qw(:DataAccess :Utils);
  39         92  
  39         652674  
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 291 my $et = shift;
57 129         753 my $nv = $$et{NEW_VALUE}{$Image::ExifTool::XMP::x{xmptk}};
58 129         275 my $tk;
59 129 100       407 if (defined $nv) {
60 1         3 $tk = $et->GetNewValue($nv);
61 1 50       7 $et->VerboseValue(($tk ? '+' : '-') . ' XMP-x:XMPToolkit', $tk);
62 1         2 ++$$et{CHANGED};
63             } else {
64 128         429 $tk = "Image::ExifTool $Image::ExifTool::VERSION";
65             }
66 129 50       915 my $str = $tk ? (" x:xmptk='" . EscapeXML($tk) . "'") : '';
67 129         551 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         16 $$xmpPt =~ s/^\s*\s*//s; # remove leading comment if it exists
78 4 50       34 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       29 $mode = 'w' unless $mode;
84 4         14 my $end = substr($$xmpPt, -32, 32);
85             # check for proper xpacket trailer and set r/w mode if necessary
86 4 50       55 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         12 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 323 my $val = shift;
145 129         334 my ($y, $m, $d, $t, $tz);
146 129 100 66     1467 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         832 ($y, $m, $d, $t, $tz) = ($1, $2, $3, $4, $5);
150 101         298 $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         104 $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       448 if ($tz) {
162 22 50       164 $tz =~ /^(Z|[+-]\d{2}:\d{2})$/ or return undef;
163 22         69 $val .= $tz;
164             }
165 129         1030 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 2613     2613 0 8061 my ($et, $tagInfo, $valPtr, $convType) = @_;
176              
177 2613 100       7595 if ($$tagInfo{Struct}) {
178 103         12003 require 'Image/ExifTool/XMPStruct.pl';
179 103         328 my ($item, $err, $w, $warn);
180 103 100       380 unless (ref $$valPtr) {
181 81         457 ($$valPtr, $warn) = InflateStruct($et, $valPtr);
182             # expect a structure HASH ref or ARRAY of structures
183 81 100       276 unless (ref $$valPtr) {
184 69 50       234 $$valPtr eq '' and $$valPtr = { }, return undef; # allow empty structures
185 69         390 return 'Improperly formed structure';
186             }
187             }
188 34 100       101 if (ref $$valPtr eq 'ARRAY') {
189 1 50       9 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         160 ($$valPtr, $err) = CheckStruct($et, $$valPtr, $$tagInfo{Struct});
205             }
206 33 50       101 $warn and $$et{CHECK_WARN} = $warn;
207 33         105 return $err;
208             }
209 2510         5745 my $format = $$tagInfo{Writable};
210             # (if no format specified, value is a simple string)
211 2510 100 100     12503 if (not $format or $format eq 'string' or $format eq 'lang-alt') {
      100        
212             # convert value to UTF8 if necessary
213 1372 100       5397 if ($$et{OPTIONS}{Charset} ne 'UTF8') {
214 4 50       16 if ($$valPtr =~ /[\x80-\xff]/) {
215             # convert from Charset to UTF-8
216 4         13 $$valPtr = $et->Encode($$valPtr,'UTF8');
217             }
218             } else {
219             # translate invalid XML characters to "."
220 1368         3785 $$valPtr =~ tr/\0-\x08\x0b\x0c\x0e-\x1f/./;
221             # fix any malformed UTF-8 characters
222 1368 50 33     6277 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 1372         5064 return undef; # success
228             }
229 1138 100 100     6135 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     1641 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         39 return 'Not a floating point number';
237             }
238 374 100       1260 if ($format eq 'rational') {
239 270         1467 $$valPtr = join('/', Image::ExifTool::Rationalize($$valPtr,0xffffffff));
240             }
241             } elsif ($format eq 'integer') {
242             # make sure the value is integer
243 592 100       2142 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         130 return 'Not an integer';
249             }
250             } elsif ($format eq 'date') {
251 94         451 my $newDate = FormatXMPDate($$valPtr);
252 94 50       527 return "Invalid date/time (use YYYY:mm:dd HH:MM:SS[.ss][+/-HH:MM|Z])" unless $newDate;
253 94         230 $$valPtr = $newDate;
254             } elsif ($format eq 'boolean') {
255             # (allow lower-case 'true' and 'false' if not setting PrintConv value)
256 69 100 66     698 if (not $$valPtr or $$valPtr =~ /false/i or $$valPtr =~ /^no$/i) {
    50 66        
      33        
      33        
257 43 0 33     346 if (not $$valPtr or $$valPtr ne 'false' or not $convType or $convType eq 'PrintConv') {
      33        
      33        
258 43         103 $$valPtr = 'False';
259             }
260             } elsif ($$valPtr ne 'true' or not $convType or $convType eq 'PrintConv') {
261 26         82 $$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         4095 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 9625     9625 0 13266 my $tagInfo = shift;
279 9625 100       26694 SetPropertyPath($$tagInfo{Table}, $$tagInfo{TagID}) unless $$tagInfo{PropertyPath};
280 9625         23079 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 4426     4426 0 8092 my ($tagTablePtr, $tagID, $parentID, $structPtr, $propList, $isType) = @_;
292 4426   66     9349 my $table = $structPtr || $tagTablePtr;
293 4426         7059 my $tagInfo = $$table{$tagID};
294 4426         5908 my $flatInfo;
295              
296 4426 50       8515 return if ref($tagInfo) ne 'HASH'; # (shouldn't happen)
297              
298 4426 100       6626 if ($structPtr) {
299 2635         3464 my $flatID = $parentID . ucfirst($tagID);
300 2635         4837 $flatInfo = $$tagTablePtr{$flatID};
301 2635 100       3520 if ($flatInfo) {
    50          
302 2578 50       4673 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         229 $flatInfo = { Name => ucfirst($flatID), Flat => 1 };
309 57         182 AddTagToTable($tagTablePtr, $flatID, $flatInfo);
310             }
311 2635 100       4560 $isType = 1 if $$structPtr{TYPE};
312             } else {
313             # don't override existing main table entry if already set by a Struct
314 1791 50       3805 return if $$tagInfo{PropertyPath};
315             # use property path from original tagInfo if this is an alternate-language tag
316 1791         2998 my $srcInfo = $$tagInfo{SrcTagInfo};
317 1791 100       3193 $$tagInfo{PropertyPath} = GetPropertyPath($srcInfo) if $srcInfo;
318 1791 100       3711 return if $$tagInfo{PropertyPath};
319             # set property path for all flattened tags in structure if necessary
320 1788 100       4685 if ($$tagInfo{RootTagInfo}) {
321 62         344 SetPropertyPath($tagTablePtr, $$tagInfo{RootTagInfo}{TagID});
322 62 50       316 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 4361   66     13447 my $ns = $$tagInfo{Namespace} || $$table{NAMESPACE};
328 4361 50       7316 $ns or warn("No namespace for $tagID\n"), return;
329 4361         5410 my (@propList, $listType);
330 4361 100       8964 $propList and @propList = @$propList;
331 4361         8255 push @propList, "$ns:$tagID";
332             # lang-alt lists are handled specially, signified by Writable='lang-alt'
333 4361 100 100     11273 if ($$tagInfo{Writable} and $$tagInfo{Writable} eq 'lang-alt') {
334 102         191 $listType = 'Alt';
335             # remove language code from property path if it exists
336 102 50       260 $propList[-1] =~ s/-$$tagInfo{LangCode}$// if $$tagInfo{LangCode};
337             # handle lists of lang-alt lists (eg. XMP-plus:Custom tags)
338 102 100 66     323 if ($$tagInfo{List} and $$tagInfo{List} ne '1') {
339 3         11 push @propList, "rdf:$$tagInfo{List}", 'rdf:li 10';
340             }
341             } else {
342 4259         5946 $listType = $$tagInfo{List};
343             }
344             # add required properties if this is a list
345 4361 100 66     9089 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 4361         6144 my $strTable = $$tagInfo{Struct};
348 4361 100 100     8753 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 252 100       758 RegisterNamespace($strTable) if ref $$strTable{NAMESPACE};
356 252         358 my $tag;
357 252         1288 foreach $tag (keys %$strTable) {
358             # ignore special fields and any lang-alt fields we may have added
359 3180 100 100     9039 next if $specialStruct{$tag} or $$strTable{$tag}{LangCode};
360 2635 100       4243 my $fullID = $parentID ? $parentID . ucfirst($tagID) : $tagID;
361 2635         4503 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 4361 100       7481 if ($structPtr) {
367 2635         3122 $tagInfo = $flatInfo;
368             # set StructType flag if any containing structure has a TYPE
369 2635 100       3986 $$tagInfo{StructType} = 1 if $isType;
370             }
371             # set property path for tagInfo in main table
372 4361         20125 $$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 2344 my ($et, $propList, $val, $attrs) = @_;
383 1167 50 33     3827 return unless defined $val and @$propList > 2;
384 1167 100 66     11716 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       2651 return unless @$propList > 3;
390             # ignore empty list properties
391 1158 50       2785 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         1838 my $capture = $$et{XMP_CAPTURE};
397 1158         3723 my $path = join('/', @$propList[3..$#$propList]);
398 1158 50       2591 if (defined $$capture{$path}) {
399 0         0 $$et{XMP_ERROR} = "Duplicate XMP property: $path";
400             } else {
401 1158   100     5946 $$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         6 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 60 my ($blankInfo, $propListPt, $val, $attrs) = @_;
427              
428 30         100 my $propPath = join '/', @$propListPt;
429 30         109 my @ids = ($propPath =~ m{ #([^ /]*)}g);
430 30         39 my $id;
431             # split the property path at each nodeID
432 30         47 foreach $id (@ids) {
433 30         374 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       63 unless ($prop eq $rdfDesc) {
438 12 100       23 if ($post) {
439 8         16 $post = "/$prop$post";
440             } else {
441 4         9 $pre = "$pre/$prop";
442             }
443             }
444 30         88 $$blankInfo{Prop}{$id}{Pre}{$pre} = 1;
445 30 100 66     132 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         145 $$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 7 my ($et, $tagTablePtr, $blankInfo, $isWriting) = @_;
459 4 100       30 $et->VPrint(1, " [Elements with nodeID set:]\n") unless $isWriting;
460 4         6 my ($id, $pre, $post);
461             # handle each nodeID separately
462 4         7 foreach $id (sort keys %{$$blankInfo{Prop}}) {
  4         19  
463 8         17 my $path = $$blankInfo{Prop}{$id};
464             # flag all resource names so we can warn later if some are unused
465 8         11 my %unused;
466 8         11 foreach $post (keys %{$$path{Post}}) {
  8         22  
467 26         38 $unused{$post} = 1;
468             }
469             # combine property paths for all possible paths through this node
470 8         12 foreach $pre (sort keys %{$$path{Pre}}) {
  8         39  
471             # there will be no description for the object of a blank node
472 16 100       75 next unless $pre =~ m{/$rdfDesc/};
473 8         11 foreach $post (sort keys %{$$path{Post}}) {
  8         53  
474 38         101 my @propList = split m{/}, "$pre$post";
475 38         38 my ($val, $attrs) = @{$$path{Post}{$post}};
  38         73  
476 38 100       53 if ($isWriting) {
477 19         25 CaptureXMP($et, \@propList, $val, $attrs);
478             } else {
479 19         39 FoundXMP($et, $tagTablePtr, \@propList, $val);
480             }
481 38         90 delete $unused{$post};
482             }
483             }
484             # save information from unused properties (if RDF is malformed like f-spot output)
485 8 100       30 if (%unused) {
486 4 50       12 $et->Options('Verbose') and $et->Warn('An XMP resource is about nothing');
487 4         9 foreach $post (sort keys %unused) {
488 8         14 my ($val, $attrs, $propPath) = @{$$path{Post}{$post}};
  8         18  
489 8         22 my @propList = split m{/}, $propPath;
490 8 100       15 if ($isWriting) {
491 4         9 CaptureXMP($et, \@propList, $val, $attrs);
492             } else {
493 4         10 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 9707     9707 0 17390 my ($et, $path) = @_;
508 9707         26752 my @propList = split('/',$path);
509 9707         19254 my $nsUsed = $$et{XMP_NS};
510 9707         11618 my $prop;
511 9707         14658 foreach $prop (@propList) {
512 26927         96025 my ($ns, $tag) = $prop =~ /(.+?):(.*)/;
513 26927 100 66     81121 next if not defined $ns or $$nsUsed{$ns};
514 15453         33313 my $uri = $nsURI{$ns};
515 15453 50       24284 unless ($uri) {
516 0         0 warn "No URI for namespace prefix $ns!\n";
517 0         0 next;
518             }
519 15453         17908 my $ns2;
520 15453         40024 foreach $ns2 (keys %$nsUsed) {
521 84607 50       144463 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 9707         30284 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         2 for (;;) {
541 5         6 pop @props;
542 5 50       9 last unless @props;
543 5         9 my $tagID = GetXMPTagID(\@props);
544 5 100       7 next if $doneID{$tagID};
545 2         4 $doneID{$tagID} = 1;
546 2         3 my $tagInfo = $$tagTablePtr{$tagID};
547 2 50       5 last unless ref $tagInfo eq 'HASH';
548 2 100       5 if ($$tagInfo{Struct}) {
549 1         17 my $type = $$tagInfo{Struct}{TYPE};
550 1 50       3 if ($type) {
551 1         2 my $pat = $$tagInfo{PropertyPath};
552 1 50       3 $pat or warn("Missing PropertyPath in AddStructType\n"), last;
553 1         8 $pat = ConformPathToNamespace($et, $pat);
554 1         2 $pat =~ s/ \d+/ \\d\+/g;
555 1 50       28 $path =~ /^($pat)/ or warn("Wrong path in AddStructType\n"), last;
556 1         7 my $p = $1 . '/rdf:type';
557 1 50       6 $p = "$basePath/$p" if $basePath;
558 1 50       10 $$capture{$p} = [ '', { 'rdf:resource' => $type } ] unless $$capture{$p};
559             }
560             }
561 2 100       15 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 27 my $chunkSize = 45;
617 8         24 my $len = length $_[0];
618 8         25 my $str = '';
619 8         20 my $i;
620 8         43 for ($i=0; $i<$len; $i+=$chunkSize) {
621 42         72 my $n = $len - $i;
622 42 100       134 $n = $chunkSize if $n > $chunkSize;
623             # add uuencoded data to output (minus size byte, but including trailing newline)
624 42         182 $str .= substr(pack('u', substr($_[0], $i, $n)), 1);
625             }
626             # convert to base64 (remember that "\0" may be encoded as ' ' or '`')
627 8         29 $str =~ tr/` -_/AA-Za-z0-9+\//;
628             # convert pad characters at the end (remember to account for trailing newline)
629 8         25 my $pad = 3 - ($len % 3);
630 8 50       54 substr($str, -$pad-1, $pad) = ('=' x $pad) if $pad < 3;
631 8 50       31 $str =~ tr/\n//d if $_[1]; # remove newlines if specified
632 8         104 return $str;
633             }
634              
635             #------------------------------------------------------------------------------
636             # sort tagInfo hash references by tag name
637             sub ByTagName
638             {
639 136841     136841 0 236837 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 8092 100   8092 0 15280 if ($a =~ /rdf:type$/) {
    100          
647 22 100       65 return substr($a, 0, -8) cmp $b unless $b =~ /rdf:type$/;
648             } elsif ($b =~ /rdf:type$/) {
649 14         26 return $a cmp substr($b, 0, -8);
650             }
651 8057         10469 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 151 my ($et, $dataPt, $maxLen, $about, $startPt, $extStart) = @_;
663              
664             # return straight away if it isn't too big
665 38 50       186 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 1083     1083 0 2008 my ($curPropList, $long, $short, $resFlag) = @_;
714              
715 1083         1653 my $prop = pop @$curPropList;
716 1083         1997 $prop =~ s/ .*//; # remove list index if it exists
717 1083         2021 my $pad = $sp x (scalar(@$curPropList) + 1);
718 1083 100       2580 if ($$resFlag[@$curPropList]) {
    100          
719             # close this XMP structure with possible shorthand properties
720 1 50       6 if (length $$short[-1]) {
721 1 50       5 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         3 $$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       5 $$long[-1] .= "$pad$nl" if length $$long[-1];
736 1         4 $$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       18 if (length $$long[-1]) {
742 3         19 $$long[-2] .= $$short[-1] . ">$nl" . $$long[-1] . "$pad$nl";
743             } else {
744 3         8 $$long[-2] .= $$short[-1] . "/>$nl"; # empty element (ie. all shorthand)
745             }
746 6         15 $$short[-1] = $$long[-1] = '';
747             } else {
748             # close this property (no chance of shorthand)
749 1076         2038 $$long[-1] .= "$pad$nl";
750 1076 100       2009 unless (@$curPropList) {
751             # add properties now that this top-level Description is complete
752 309         1103 $$long[-2] .= ">$nl" . $$long[-1];
753 309         576 $$long[-1] = '';
754             }
755             }
756 1083         3601 $#$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 6626     6626 0 15040 my ($et, $dirInfo, $tagTablePtr) = @_;
773 6626 100       27405 $et or return 1; # allow dummy access to autoload this package
774 130         504 my $dataPt = $$dirInfo{DataPt};
775 130         321 my (%capture, %nsUsed, $xmpErr, $about);
776 130         314 my $changed = 0;
777 130         365 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     970 my $preferred = $xmpFile || ($$et{PreferredGroup} and $$et{PreferredGroup} eq 'XMP');
780 130         496 my $verbose = $$et{OPTIONS}{Verbose};
781 130         298 my %compact = ( %{$$et{OPTIONS}{Compact}} ); # (make a copy so we can change settings)
  130         668  
782 130         355 my $dirLen = $$dirInfo{DirLen};
783 130 100 100     743 $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         512 $$et{XMP_CAPTURE} = \%capture;
794 130         445 $$et{XMP_NS} = \%nsUsed;
795 130         367 delete $$et{XMP_NO_XMPMETA};
796 130         344 delete $$et{XMP_NO_XPACKET};
797 130         351 delete $$et{XMP_IS_XML};
798 130         318 delete $$et{XMP_IS_SVG};
799              
800             # set current padding characters
801 130 50       931 ($sp, $nl) = ($compact{NoIndent} ? '' : ' ', $compact{NoNewline} ? '' : "\n");
    50          
802              
803             # get value for new rdf:about
804 130         536 my $tagInfo = $Image::ExifTool::XMP::rdf{about};
805 130 100       649 if (defined $$et{NEW_VALUE}{$tagInfo}) {
806 1   50     5 $about = $et->GetNewValue($$et{NEW_VALUE}{$tagInfo}) || '';
807             }
808              
809 130 100 100     889 if ($xmpFile or $dirLen) {
    50          
810 76         193 delete $$et{XMP_ERROR};
811             # extract all existing XMP information (to the XMP_CAPTURE hash)
812 76         511 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     530 unless ($success and not $$et{XMP_ERROR}) {
815 17   50     98 my $err = $$et{XMP_ERROR} || 'Error parsing XMP';
816             # may ignore this error only if we were successful
817 17 50       168 if ($xmpFile) {
818 17         43 my $raf = $$dirInfo{RAF};
819             # allow empty XMP data so we can create something from nothing
820 17 50 33     106 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       312 if (defined $about) {
837 1 50       3 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         4 $about = EscapeXML($about); # must escape for XML
843 1         2 ++$changed;
844             } else {
845 75   100     609 $about = $$et{XmpAbout} || '';
846             }
847 76         158 delete $$et{XMP_ERROR};
848              
849             # call InitWriteDirs to initialize FORCE_WRITE flags if necessary
850 76 50 66     478 $et->InitWriteDirs({}, 'XMP') if $xmpFile and $et->GetNewValue('ForceWrite');
851             # set changed if we are ForceWrite tag was set to "XMP"
852 76 50       408 ++$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         185 $about = '';
860             }
861             #
862             # handle writing XMP as a block to XMP file
863             #
864 130 100       624 if ($xmpFile) {
865 36         150 $tagInfo = $Image::ExifTool::Extra{XMP};
866 36 50 33     245 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     263 if (%{$$et{DEL_GROUP}} and (grep /^XMP-.+$/, keys %{$$et{DEL_GROUP}} or
  130   66     604  
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         39 my $del = $$et{DEL_GROUP};
886 12         31 my $path;
887 12         69 foreach $path (keys %capture) {
888 147         231 my @propList = split('/',$path); # get property list
889 147         233 my ($tag, $ns) = GetXMPTagID(\@propList);
890             # translate namespace if necessary
891 147 50       261 $ns = $stdXlatNS{$ns} if $stdXlatNS{$ns};
892 147         155 my ($grp, @g);
893             # no "XMP-" added to most groups in exiftool RDF/XML output file
894 147 100 66     495 if ($nsUsed{$ns} and (@g = ($nsUsed{$ns} =~ m{^http://ns.exiftool.(?:ca|org)/(.*?)/(.*?)/}))) {
895 84 100       116 if ($g[1] =~ /^\d/) {
896 17         21 $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     74 next unless $$del{$ucg} or ($$del{'XML-*'} and not $$del{"-$ucg"});
      66        
900             } else {
901 67         67 $grp = $g[1];
902 67 100 66     181 next unless $$del{$grp} or ($$del{$g[0]} and not $$del{"-$grp"});
      66        
903             }
904             } else {
905 63         76 $grp = "XMP-$ns";
906 63         94 my $ucg = uc $grp;
907 63 100 100     204 next unless $$del{$ucg} or ($$del{'XMP-*'} and not $$del{"-$ucg"});
      100        
908             }
909 99         290 $et->VerboseValue("- $grp:$tag", $capture{$path}->[0]);
910 99         199 delete $capture{$path};
911 99         160 ++$changed;
912             }
913             }
914             # delete HasExtendedXMP tag (we create it as needed)
915 130         423 my $hasExtTag = 'xmpNote:HasExtendedXMP';
916 130 100       532 if ($capture{$hasExtTag}) {
917 1         8 $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       1020 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         372 my (@tagInfoList, @structList, $delLangPath, %delLangPaths, %delAllLang, $firstNewPath, @langTags);
929 130         388 my $writeGroup = $$dirInfo{WriteGroup};
930 130         865 foreach $tagInfo (sort ByTagName $et->GetNewTagInfoList()) {
931 15547 100       30895 next unless $et->GetGroup($tagInfo, 0) eq 'XMP';
932 4713 50       8730 next if $$tagInfo{Name} eq 'XMP'; # (ignore full XMP block if we didn't write it already)
933 4713 50 66     8199 next if $writeGroup and $writeGroup ne $$et{NEW_VALUE}{$tagInfo}{WriteGroup};
934 4713 100       8850 if ($$tagInfo{LangCode}) {
    100          
935 25         74 push @langTags, $tagInfo
936             } elsif ($$tagInfo{Struct}) {
937 217         434 push @structList, $tagInfo;
938             } else {
939 4471         7814 push @tagInfoList, $tagInfo;
940             }
941             }
942 130 100       3077 if (@langTags) {
943             # keep original order in which lang-alt entries were added
944 14         45 foreach $tagInfo (sort { $$et{NEW_VALUE}{$a}{Order} <=> $$et{NEW_VALUE}{$b}{Order} } @langTags) {
  14         50  
945 25 50       59 if ($$tagInfo{Struct}) {
946 0         0 push @structList, $tagInfo;
947             } else {
948 25         47 push @tagInfoList, $tagInfo;
949             }
950             }
951             }
952 130         369 foreach $tagInfo (@structList, @tagInfoList) {
953 4713         7172 my @delPaths; # list of deleted paths
954 4713         12952 my $tag = $$tagInfo{TagID};
955 4713         10929 my $path = GetPropertyPath($tagInfo);
956 4713 50       9315 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 4713 100 100     18705 if ($path eq 'rdf:about' or $path eq 'x:xmptk') {
962 2         3 ++$changed;
963 2         4 next;
964             }
965 4711         8433 my $isStruct = $$tagInfo{Struct};
966             # change our property path namespace prefixes to conform
967             # to the ones used in this file
968 4711         9886 $path = ConformPathToNamespace($et, $path);
969             # find existing property
970 4711         8871 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 4711         8964 until ($cap) {
975             # find and fix all incorrect property names if this is a structure or a flattened tag
976 4581         5991 my @fixInfo;
977 4581 100 100     14387 if ($isStruct or defined $$tagInfo{Flat}) {
978             # get tagInfo for all containing (possibly nested) structures
979 2496         6829 my @props = split '/', $path;
980 2496         4726 my $tbl = $$tagInfo{Table};
981 2496         5035 while (@props) {
982 14065         32074 my $info = $$tbl{GetXMPTagID(\@props)};
983             unshift @fixInfo, $info if ref $info eq 'HASH' and $$info{Struct} and
984 14065 100 100     78556 (not @fixInfo or $fixInfo[0] ne $info);
      100        
      100        
985 14065         29385 pop @props;
986             }
987 2496 50       6167 $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 4581 100 100     14467 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 4581         6073 my $err;
994 4581         9121 while (@fixInfo) {
995 4899         7794 my $fixInfo = shift @fixInfo;
996 4899         11942 my $fixPath = ConformPathToNamespace($et, GetPropertyPath($fixInfo));
997 4899         10743 my $regex = quotemeta($fixPath);
998 4899         22388 $regex =~ s/ \d+/ \\d\+/g; # match any list index
999 4899         7534 my $ok = $regex;
1000 4899         7585 my ($ok2, $match, $i, @fixed, %fixed, $fixed);
1001             # check for incorrect list types
1002 4899 100       17048 if ($regex =~ s{\\/rdf\\:(Bag|Seq|Alt)\\/}{/rdf:(Bag|Seq|Alt)/}g) {
    100          
1003             # also look for missing bottom-level list
1004 2225 100       7431 if ($regex =~ s{/rdf:\(Bag\|Seq\|Alt\)\/rdf\\:li\\ \\d\+$}{}) {
1005 2196 100       4917 $regex .= '(/.*)?' unless @fixInfo;
1006             }
1007             } elsif (not @fixInfo) {
1008 2091         3032 $ok2 = $regex;
1009             # check for properties in lists that shouldn't be (ref forum4325)
1010 2091         3947 $regex .= '(/rdf:(Bag|Seq|Alt)/rdf:li \d+)?';
1011             }
1012 4899 100       8929 if (@fixInfo) {
1013 2418         4228 $regex .= '(/.*)?';
1014 2418         3571 $ok .= '(/.*)?';
1015             }
1016 4899         365702 my @matches = sort grep m{^$regex$}i, keys %capture;
1017 4899 100       26061 last unless @matches;
1018 322 100       3001 if ($matches[0] =~ m{^$ok$}) {
1019 317 50       697 unless (@fixInfo) {
1020 0         0 $path = $matches[0];
1021 0         0 $cap = $capture{$path};
1022             }
1023 317         1204 next;
1024             }
1025             # needs fixing...
1026 5         17 my @fixProps = split '/', $fixPath;
1027 5         12 foreach $match (@matches) {
1028 7         18 my @matchProps = split '/', $match;
1029             # remove superfluous list properties if necessary
1030 7 100 66     25 $#matchProps = $#fixProps if $ok2 and $#matchProps > $#fixProps;
1031 7         20 for ($i=0; $i<@fixProps; ++$i) {
1032 19 50       29 defined $matchProps[$i] or $matchProps[$i] = $fixProps[$i], next;
1033 19 100 100     69 next if $matchProps[$i] =~ / \d+$/ or $matchProps[$i] eq $fixProps[$i];
1034 4         7 $matchProps[$i] = $fixProps[$i];
1035             }
1036 7         21 $fixed = join '/', @matchProps;
1037 7 50 66     33 $err = 1 if $fixed{$fixed} or ($capture{$fixed} and $match ne $fixed);
      33        
1038 7         24 push @fixed, $fixed;
1039 7         20 $fixed{$fixed} = 1;
1040             }
1041 5         24 my $tg = $et->GetGroup($fixInfo, 1) . ':' . $$fixInfo{Name};
1042 5 100       26 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         6 my $didFix;
1048 5         11 foreach $fixed (@fixed) {
1049 7         10 my $match = shift @matches;
1050 7 100       20 next if $fixed eq $match;
1051 5         7 $capture{$fixed} = $capture{$match};
1052 5         7 delete $capture{$match};
1053             # remove xml:lang attribute from incorrect lang-alt list if necessary
1054 5 100 66     28 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         14 $et->Warn("Fixed incorrect $wrn for $tg", 1);
1060 3         12 ++$changed;
1061             }
1062             }
1063             }
1064 4581         7995 last;
1065             }
1066 4711         21075 my $nvHash = $et->GetNewValueHash($tagInfo);
1067 4711         14014 my $overwrite = $et->IsOverwriting($nvHash);
1068 4711   100     16537 my $writable = $$tagInfo{Writable} || '';
1069 4711         9027 my (%attrs, $deleted, $added, $existed, $newLang);
1070             # set up variables to save/restore paths of deleted lang-alt tags
1071 4711 100       9739 if ($writable eq 'lang-alt') {
1072 169   100     805 $newLang = lc($$tagInfo{LangCode} || 'x-default');
1073 169 100 100     697 if ($delLangPath and $delLangPath eq $path) {
1074             # restore paths of deleted entries for this language
1075 7 100       21 @delPaths = @{$delLangPaths{$newLang}} if $delLangPaths{$newLang};
  2         5  
1076             } else {
1077 162         329 undef %delLangPaths;
1078 162         278 $delLangPath = $path; # base path for deleted lang-alt tags
1079 162         226 undef %delAllLang;
1080 162         268 undef $firstNewPath; # reset first path for new lang-alt tag
1081             }
1082 169 100       419 if (%delAllLang) {
1083             # add missing paths to delete list for entries where all languages were deleted
1084 2         3 my ($prefix, $reSort);
1085 2         4 foreach $prefix (keys %delAllLang) {
1086 6 100       63 next if grep /^$prefix/, @delPaths;
1087 1         4 push @delPaths, "${prefix}10";
1088 1         21 $reSort = 1;
1089             }
1090 2 100       9 @delPaths = sort @delPaths if $reSort;
1091             }
1092             }
1093             # delete existing entry if necessary
1094 4711 100       11281 if ($isStruct) {
    100          
1095             # delete all structure (or pseudo-structure) elements
1096 217         1433 require 'Image/ExifTool/XMPStruct.pl';
1097 217         962 ($deleted, $added, $existed) = DeleteStruct($et, \%capture, \$path, $nvHash, \$changed);
1098             # don't add if it didn't exist and not IsCreating and Avoid
1099 217 100 100     1621 undef $added if not $existed and not $$nvHash{IsCreating} and $$tagInfo{Avoid};
      100        
1100 217 50 100     1232 next unless $deleted or $added or $et->IsOverwriting($nvHash);
      66        
1101 217 100 100     653 next if $existed and $$nvHash{CreateOnly};
1102             } elsif ($cap) {
1103 132 100       370 next if $$nvHash{CreateOnly}; # (necessary for List-type tags)
1104             # take attributes from old values if they exist
1105 130         197 %attrs = %{$$cap[1]};
  130         463  
1106 130 100       324 if ($overwrite) {
1107 126         273 my ($oldLang, $delLang, $addLang, @matchingPaths, $langPathPat, %langsHere);
1108             # check to see if this is an indexed list item
1109 126 100       407 if ($path =~ / /) {
1110 44         80 my $pp;
1111 44         244 ($pp = $path) =~ s/ \d+/ \\d\+/g;
1112 44         2171 @matchingPaths = sort grep(/^$pp$/, keys %capture);
1113             } else {
1114 82         204 push @matchingPaths, $path;
1115             }
1116 126         330 my $oldOverwrite = $overwrite;
1117 126         250 foreach $path (@matchingPaths) {
1118 181         269 my ($val, $attrs) = @{$capture{$path}};
  181         517  
1119 181 100       558 if ($writable eq 'lang-alt') {
    100          
1120             # get original language code (lc for comparisons)
1121 60   50     168 $oldLang = lc($$attrs{'xml:lang'} || 'x-default');
1122             # revert to original overwrite flag if this is in a different structure
1123 60 100 100     531 if (not $langPathPat or $path !~ /^$langPathPat$/) {
1124 38         48 $overwrite = $oldOverwrite;
1125 38         227 ($langPathPat = $path) =~ s/\d+$/\\d+/;
1126             }
1127             # remember languages in this lang-alt list
1128 60         172 $langsHere{$langPathPat}{$oldLang} = 1;
1129 60 100       109 unless (defined $addLang) {
1130             # add to lang-alt list by default if creating this tag from scratch
1131 24 100       62 $addLang = $$nvHash{IsCreating} ? 1 : 0;
1132             }
1133 60 100       126 if ($overwrite < 0) {
1134 13 100       45 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         33 $addLang = $et->IsOverwriting($nvHash, UnescapeXML($val));
1138 8 100       34 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     231 if ($oldLang eq 'x-default' and not $$tagInfo{LangCode}) {
    100 66        
1143 13         21 $delLang = 1; # delete all languages
1144 13         19 $overwrite = 1; # force overwrite
1145             } elsif ($$tagInfo{LangCode} and not $delLang) {
1146             # only overwrite specified language
1147 31 100       84 next unless lc($$tagInfo{LangCode}) eq $oldLang;
1148             }
1149             } elsif ($overwrite < 0) {
1150             # only overwrite specific values
1151 7 100       25 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     15 my $fmt = $$tagInfo{Writable} || '';
1155 3 100       19 if ($fmt eq 'rational') {
    50          
1156 1         10 ConvertRational($val);
1157             } elsif ($fmt eq 'date') {
1158 2         15 $val = ConvertXMPDate($val);
1159             }
1160             }
1161             # (note: this won't match if original XML contains CDATA!)
1162 7 100       33 next unless $et->IsOverwriting($nvHash, UnescapeXML($val));
1163             }
1164 143 50       316 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       420 %attrs = %$attrs unless @delPaths;
1174 143 100       362 if ($writable eq 'lang-alt') {
1175 23         44 $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     475 if ($writable ne 'lang-alt' or $oldLang eq $newLang) {
1180 137         265 push @delPaths, $path;
1181             } else {
1182 6 100       18 $delLangPaths{$oldLang} or $delLangPaths{$oldLang} = [ ];
1183 6         6 push @{$delLangPaths{$oldLang}}, $path;
  6         10  
1184             }
1185             # keep track of paths where we deleted all languages of a lang-alt tag
1186 143 100       306 if ($delLang) {
1187 19         22 my $p;
1188 19         70 ($p = $path) =~ s/\d+$//;
1189 19         37 $delAllLang{$p} = 1;
1190             }
1191             # delete this tag
1192 143         353 delete $capture{$path};
1193 143         283 ++$changed;
1194             # delete rdf:type tag if it is the only thing left in this structure
1195 143 50 66     907 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     378 next unless @delPaths or $$tagInfo{List} or $addLang;
      100        
1202 125 100       272 if (@delPaths) {
1203 118         238 $path = shift @delPaths;
1204             # make sure new path is unique
1205 118         327 while ($capture{$path}) {
1206 0 0       0 last unless $path =~ s/ \d(\d+)$/' '.length($1+1).($1+1)/e;
  0         0  
1207             }
1208 118         345 $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     66 next unless $$tagInfo{List} or $oldLang;
1213             # avoid adding duplicate entry to lang-alt in a list
1214 7 50 33     34 if ($writable eq 'lang-alt' and %langsHere) {
1215 7         25 foreach (sort keys %langsHere) {
1216 9 50       149 next unless $path =~ /^$_$/;
1217 9 100       33 last unless $langsHere{$_}{$newLang};
1218 3 50       22 $path =~ /(.* )\d(\d+)(.*? \d+)$/ or $et->Error('Internal error writing lang-alt list'), last;
1219 3         10 my $nxt = $2 + 1;
1220 3         16 $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         41 $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         6 my $pat = '.* (\d+)';
1231 4 100       11 if ($writable eq 'lang-alt') {
1232 2 100       4 if ($firstNewPath) {
1233 1         2 $path = $firstNewPath;
1234 1         3 $overwrite = 1; # necessary to put x-default entry first below
1235             } else {
1236 1         2 $pat = '.* (\d+)(.*? \d+)';
1237             }
1238             }
1239 4 50       105 if ($path =~ m/$pat/g) {
1240 4         13 $added = $1;
1241             # set position to end of matching index number
1242 4 100       16 pos($path) = pos($path) - length($2) if $2;
1243             }
1244             }
1245 129 100       328 if (defined $added) {
1246 11         25 my $len = length $added;
1247 11         23 my $pos = pos($path) - $len;
1248 11         30 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     82 if ($overwrite and $writable eq 'lang-alt' and (not $$tagInfo{LangCode} or
      100        
      100        
1252             $$tagInfo{LangCode} eq 'x-default'))
1253             {
1254 2         9 my $saveCap = $capture{$path};
1255 2         8 while ($saveCap) {
1256 1         3 my $p = $path;
1257 1         5 substr($p, $pos, $len) = length($nxt) . $nxt;
1258             # increment index in the path of the existing item
1259 1         3 my $nextCap = $capture{$p};
1260 1         2 $capture{$p} = $saveCap;
1261 1 50       4 last unless $nextCap;
1262 0         0 $saveCap = $nextCap;
1263 0         0 ++$nxt;
1264             }
1265             } else {
1266             # add to end of list
1267 9         31 while ($capture{$path}) {
1268 16         27 my $try = length($nxt) . $nxt;
1269 16         44 substr($path, $pos, $len) = $try;
1270 16         18 $len = length $try;
1271 16         43 ++$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 4706   100     36634 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 4706 100 100     25931 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 2845 100       9943 my @newValues = $et->GetNewValue($nvHash) or next;
1290              
1291             # set language attribute for lang-alt lists
1292 921 100       2170 if ($writable eq 'lang-alt') {
1293 73   100     372 $attrs{'xml:lang'} = $$tagInfo{LangCode} || 'x-default';
1294 73 100       192 $firstNewPath = $path if defined $added; # save path of first lang-alt tag added
1295             }
1296             # add new value(s) to %capture hash
1297 921         1373 my $subIdx;
1298 921         1492 for (;;) {
1299 1045         2160 my $newValue = shift @newValues;
1300 1045 100       2104 if ($isStruct) {
1301             ++$changed if AddNewStruct($et, $tagInfo, \%capture,
1302 30 50       158 $path, $newValue, $$tagInfo{Struct});
1303             } else {
1304 1015         3231 $newValue = EscapeXML($newValue);
1305 1015         1571 for (;;) { # (a cheap 'goto')
1306 1015 100       2671 if ($$tagInfo{Resource}) {
1307             # only store as a resource if it doesn't contain any illegal characters
1308 3 50       11 if ($newValue !~ /[^a-z0-9\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\.\-\_\~]/i) {
1309 3         15 $capture{$path} = [ '', { %attrs, 'rdf:resource' => $newValue } ];
1310 3         7 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 1012         1641 delete $attrs{'rdf:value'};
1318 1012         1536 delete $attrs{'rdf:resource'};
1319 1012         3822 $capture{$path} = [ $newValue, \%attrs ];
1320 1012         1821 last;
1321             }
1322 1015 100       2231 if ($verbose > 1) {
1323 1         9 my $grp = $et->GetGroup($tagInfo, 1);
1324 1         10 $et->VerboseValue("+ $grp:$$tagInfo{Name}", $newValue);
1325             }
1326 1015         1632 ++$changed;
1327             # add rdf:type if necessary
1328 1015 50       2516 if ($$tagInfo{StructType}) {
1329 0         0 AddStructType($et, $$tagInfo{Table}, \%capture, $path);
1330             }
1331             }
1332 1045 100       2302 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       353 my $pat = $writable eq 'lang-alt' ? '.* (\d+)(.*? \d+)' : '.* (\d+)';
1336 124         376 pos($path) = 0;
1337 124 50       1236 $path =~ m/$pat/g or warn("Internal error: no list index for $tag ($path) ($pat)!\n"), next;
1338 124         399 my $idx = $1;
1339 124         233 my $len = length $1;
1340 124 100       356 my $pos = pos($path) - $len - ($2 ? length $2 : 0);
1341             # use sub-indices if necessary to store additional values in sequence
1342 124 100       363 if ($subIdx) {
    100          
1343 52         130 $idx = substr($idx, 0, -length($subIdx)); # remove old sub-index
1344 52         152 $subIdx = substr($subIdx, 1) + 1;
1345 52         154 $subIdx = length($subIdx) . $subIdx;
1346             } elsif (@delPaths) {
1347 19         32 $path = shift @delPaths;
1348             # make sure new path is unique
1349 19         49 while ($capture{$path}) {
1350 2 50       10 last unless $path =~ s/ \d(\d+)$/' '.length($1+1).($1+1)/e;
  2         14  
1351             }
1352 19         36 next;
1353             } else {
1354 53         123 $subIdx = '10';
1355             }
1356 105         536 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 921 100       3297 if (defined $$tagInfo{Flat}) {
1361 344         687 my $p = $path;
1362 344         2400 while ($p =~ s/\/[^\/]+$//) {
1363 490 50       2441 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         428 delete $$et{XMP_CAPTURE};
1372 130         451 delete $$et{XMP_NS};
1373              
1374 130         359 my $maxDataLen = $$dirInfo{MaxDataLen};
1375             # get DataPt again because it may have been set by ProcessXMP
1376 130         389 $dataPt = $$dirInfo{DataPt};
1377              
1378             # return now if we didn't change anything
1379 130 50 66     608 unless ($changed or ($maxDataLen and $dataPt and defined $$dataPt and
      66        
      33        
      66        
1380             length($$dataPt) > $maxDataLen))
1381             {
1382 19 50       318 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         311 my (@long, @short, @resFlag);
1391 111         429 $long[0] = $long[1] = $short[0] = '';
1392 111 100       395 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         377 $long[-2] .= $pktOpen;
1397             }
1398 111 100       428 $long[-2] .= $xmlOpen if $$et{XMP_IS_XML};
1399 111         396 $long[-2] .= $xmpOpen . $rdfOpen;
1400              
1401             # initialize current property path list
1402 111         409 my (@curPropList, @writeLast, @descStart, $extStart);
1403 111         0 my (%nsCur, $prop, $n, $path);
1404 111         1261 my @pathList = sort TypeFirst keys %capture;
1405             # order properties to write large values last if we have a MaxDataLen limit
1406 111 100 100     709 if ($maxDataLen and @pathList) {
1407 36         71 my @pathTmp;
1408 36         122 my ($lastProp, $lastNS, $propSize) = ('', '', 0);
1409 36         160 my @pathLoop = (@pathList, ''); # add empty path to end of list for loop
1410 36         113 undef @pathList;
1411 36         104 foreach $path (@pathLoop) {
1412 409         1082 $path =~ /^((\w*)[^\/]*)/; # get path element ($1) and ns ($2)
1413 409 100       907 if ($1 eq $lastProp) {
1414 108         201 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 301 100 66     1342 if ($extendedRes{$lastProp} or $extendedRes{$lastNS} or
      66        
1418             $propSize > $newDescThresh)
1419             {
1420 14         30 push @writeLast, @pathTmp;
1421             } else {
1422 287         534 push @pathList, @pathTmp;
1423             }
1424 301 100       623 last unless $path; # all done if we hit empty path
1425 265         519 @pathTmp = ( $path );
1426 265         699 ($lastProp, $lastNS, $propSize) = ($1, $2, 0);
1427             }
1428 373         845 $propSize += length $capture{$path}->[0];
1429             }
1430             }
1431              
1432             # write out all properties
1433 111         215 for (;;) {
1434 1991         2799 my (%nsNew, $newDesc);
1435 1991 100       3640 unless (@pathList) {
1436 115 100       419 last unless @writeLast;
1437 4         15 @pathList = @writeLast;
1438 4         13 undef @writeLast;
1439 4         15 $newDesc = 2; # start with a new description for the extended data
1440             }
1441 1880         2743 $path = shift @pathList;
1442 1880         3942 my @propList = split('/',$path); # get property list
1443             # must open/close rdf:Description too
1444 1880         3306 unshift @propList, $rdfDesc;
1445             # make sure we have defined all necessary namespaces
1446 1880         2748 foreach $prop (@propList) {
1447 5699 50       13938 $prop =~ /(.*):/ or next;
1448 5699 100       10962 $1 eq 'rdf' and next; # rdf namespace already defined
1449 2497         4078 my $uri = $nsUsed{$1};
1450 2497 100       3974 unless ($uri) {
1451 1479         2459 $uri = $nsURI{$1}; # we must have added a namespace
1452 1479 50       2398 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 2497         4204 $nsNew{$1} = $uri;
1465             # need a new description if any new namespaces
1466 2497 100       5254 $newDesc = 1 unless $nsCur{$1};
1467             }
1468 1880         2376 my $closeTo = 0;
1469 1880 100       2927 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 315         569 my ($path2, $ns2);
1475 315         626 foreach $path2 (@pathList) {
1476 1769         8212 my @ns2s = ($path2 =~ m{(?:^|/)([^/]+?):}g);
1477 1769 50       2984 my $opening = $compact{OneDesc} ? 1 : 0;
1478 1769         2421 foreach $ns2 (@ns2s) {
1479 3381 100       5215 next if $ns2 eq 'rdf';
1480 2321 100       4102 $nsNew{$ns2} and ++$opening, next;
1481 217 100       512 last unless $opening;
1482             # get URI for this existing or new namespace
1483 13 50 66     79 my $uri = $nsUsed{$ns2} || $nsURI{$ns2} or last;
1484 13         36 $nsNew{$ns2} = $uri; # also open this namespace
1485             }
1486 1769 100       3544 last unless $opening;
1487             }
1488             } else {
1489             # find first property where the current path differs from the new path
1490 1565         2857 for ($closeTo=0; $closeTo<@curPropList; ++$closeTo) {
1491 2971 50       4468 last unless $closeTo < @propList;
1492 2971 100       6354 last unless $propList[$closeTo] eq $curPropList[$closeTo];
1493             }
1494             }
1495             # close out properties down to the common base path
1496 1880         4274 CloseProperty(\@curPropList, \@long, \@short, \@resFlag) while @curPropList > $closeTo;
1497              
1498             # open new description if necessary
1499 1880 100       3176 if ($newDesc) {
1500 315 50       745 $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 315 100       737 push @descStart, length($long[-2]) if $maxDataLen;
1503             # open the new description
1504 315         563 $prop = $rdfDesc;
1505 315         1043 %nsCur = %nsNew; # save current namespaces
1506 315         1042 my @ns = sort keys %nsCur;
1507 315         1079 $long[-2] .= "$nl$sp<$prop rdf:about='${about}'";
1508             # generate et:toolkit attribute if this is an exiftool RDF/XML output file
1509 315 100 66     1075 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 315         1210 $long[-2] .= "\n$sp${sp}xmlns:$_='$nsCur{$_}'" foreach @ns;
1514 315         655 push @curPropList, $prop;
1515             # set resFlag to 0 to indicate base description when Shorthand enabled
1516 315 100       857 $resFlag[0] = 0 if $compact{Shorthand};
1517             }
1518 1880         2449 my ($val, $attrs) = @{$capture{$path}};
  1880         4636  
1519 1880 50       3194 $debug and print "$path = $val\n";
1520             # open new properties if necessary
1521 1880         2433 my ($attr, $dummy);
1522 1880         3789 for ($n=@curPropList; $n<$#propList; ++$n) {
1523 771         1224 $prop = $propList[$n];
1524 771         1208 push @curPropList, $prop;
1525 771         1429 $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       2109 $long[-1] .= ($compact{NoIndent} ? '' : ' ' x scalar(@curPropList)) . "<$prop";
1529 771 100 100     4062 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       391 if ($propList[$n+1] =~ /:~dummy~$/) {
1534 3         9 $long[-1] .= " rdf:parseType='Resource'/>$nl";
1535 3         9 pop @curPropList;
1536 3         5 $dummy = 1;
1537 3         5 last;
1538             }
1539 136 100       266 if ($compact{Shorthand}) {
1540 1         6 $resFlag[$#curPropList] = 1;
1541 1         4 push @long, '';
1542 1         4 push @short, '';
1543             } else {
1544             # use rdf:parseType='Resource' to avoid new 'rdf:Description'
1545 135         399 $long[-1] .= " rdf:parseType='Resource'>$nl";
1546             }
1547             } else {
1548 632         1516 $long[-1] .= ">$nl"; # (will be no shorthand properties)
1549             }
1550             }
1551 1880         2749 my $prop2 = pop @propList; # get new property name
1552             # add element unless it was a dummy structure field
1553 1880 50 66     5611 unless ($dummy or ($val eq '' and $prop2 =~ /:~dummy~$/)) {
      66        
1554 1877         3602 $prop2 =~ s/ .*//; # remove list index if it exists
1555 1877 50       4035 my $pad = $compact{NoIndent} ? '' : ' ' x (scalar(@curPropList) + 1);
1556             # (can't write as shortcut if it has attributes or CDATA)
1557 1877 100 66     4738 if (defined $resFlag[$#curPropList] and not %$attrs and $val !~ /
      66        
1558 19         66 $short[-1] .= "\n$pad$prop2='${val}'";
1559             } else {
1560 1858         2998 $long[-1] .= "$pad<$prop2";
1561             # write out attributes
1562 1858         4597 foreach $attr (sort keys %$attrs) {
1563 209         420 my $attrVal = $$attrs{$attr};
1564 209 50       543 my $quot = ($attrVal =~ /'/) ? '"' : "'";
1565 209         501 $long[-1] .= " $attr=$quot$attrVal$quot";
1566             }
1567 1858 100       6445 $long[-1] .= length $val ? ">$val$nl" : "/>$nl";
1568             }
1569             }
1570             }
1571             # close out all open properties
1572 111         685 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       344 if ($maxDataLen) {
1576             # adjust maxDataLen to allow room for closing elements
1577 38         138 $maxDataLen -= length($rdfClose) + length($xmpClose) + length($pktCloseW);
1578 38 50       191 $extStart or $extStart = length $long[-2];
1579 38         235 my @rtn = LimitXMPSize($et, \$long[-2], $maxDataLen, $about, \@descStart, $extStart);
1580             # return extended XMP information in $dirInfo
1581 38         235 $$dirInfo{ExtendedXMP} = $rtn[0];
1582 38         125 $$dirInfo{ExtendedGUID} = $rtn[1];
1583             # compact if necessary to fit
1584 38 50       166 $compact{NoPadding} = 1 if length($long[-2]) + 101 * $numPadLines > $maxDataLen;
1585             }
1586 111 50       467 $compact{NoPadding} = 1 if $$dirInfo{Compact};
1587             #
1588             # close out the XMP, clean up, and return our data
1589             #
1590 111         340 $long[-2] .= $rdfClose;
1591 111 100       481 $long[-2] .= $xmpClose unless $$et{XMP_NO_XMPMETA};
1592              
1593             # remove the ExifTool members we created
1594 111         290 delete $$et{XMP_CAPTURE};
1595 111         1182 delete $$et{XMP_NS};
1596 111         231 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       351 unless ($$et{XMP_NO_XPACKET}) {
1601 110         240 my $pad = (' ' x 100) . "\n";
1602             # get current XMP length without padding
1603 110         323 my $len = length($long[-2]) + length($pktCloseW);
1604 110 50 0     1249 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         1649 $long[-2] .= $pad x $numPadLines;
1620             }
1621 110 100       682 $long[-2] .= ($$dirInfo{ReadOnly} ? $pktCloseR : $pktCloseW);
1622             }
1623             # return empty data if no properties exist and this is allowed
1624 111 100 66     487 unless (%capture or $xmpFile or $$dirInfo{InPlace} or $$dirInfo{NoDelete}) {
      66        
      66        
1625 3         5 $long[-2] = '';
1626             }
1627 111 0       310 return($xmpFile ? -1 : undef) if $xmpErr;
    50          
1628 111         309 $$et{CHANGED} += $changed;
1629 111 0 33     364 $debug > 1 and $long[-2] and print $long[-2],"\n";
1630 111 100       2075 return $long[-2] unless $xmpFile;
1631 36 50       262 Write($$dirInfo{OutFile}, $long[-2]) or return -1;
1632 36         2418 return 1;
1633             }
1634              
1635              
1636             1; # end
1637              
1638             __END__