bump product version to 4.1.6.2
[LibreOffice.git] / solenv / bin / make_ext_update_info.pl
blobf8a93255f06af2dd4c1635fc92398fe6e76f2f2d
2 eval 'exec perl -wS $0 ${1+"$@"}'
3 if 0;
5 # This file is part of the LibreOffice project.
7 # This Source Code Form is subject to the terms of the Mozilla Public
8 # License, v. 2.0. If a copy of the MPL was not distributed with this
9 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 # This file incorporates work covered by the following license notice:
13 # Licensed to the Apache Software Foundation (ASF) under one or more
14 # contributor license agreements. See the NOTICE file distributed
15 # with this work for additional information regarding copyright
16 # ownership. The ASF licenses this file to you under the Apache
17 # License, Version 2.0 (the "License"); you may not use this file
18 # except in compliance with the License. You may obtain a copy of
19 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #here the definition for d would be written into dependencies. The reason is that when the event handler
23 #for the element is called, we can only find out the namespace but not the prefix. So we cannot
24 #distinguish if the namespace is used because the element was prefixed or because it uses the default
25 #namespace.
26 use warnings;
27 use strict;
29 use XML::Parser;
30 use Getopt::Long;
31 use Carp;
33 sub getUpdateInfoFileName($);
34 sub writeUpdateInformationData($);
35 sub findAttribute($$);
36 sub getNotDefPrefs($$$);
37 sub collectPrefixes($$$$);
38 sub determineNsDefinitions($$$);
39 sub determineNsDefinitionForItem($$$);
41 my $inDescription = 0;
42 my $inDependencies = 0;
43 my $inIdentifier = 0;
44 my $inVersion = 0;
45 my $descNS = "http://openoffice.org/extensions/description/2006";
46 my $indent;
47 my $identifier;
48 my $version;
50 #contains prefixes and the corresponding namespaces which are used in the <dependencies>
51 #element and all children of the description.xml
52 my @usedNsInDependencies;
54 #Maps prefix to namespaces which are valid in <dependencies>. That is, they are
55 #either defined in <dependencies> or in the hirarchy above <dependencies>
56 my %validPrefsInDep;
57 #Contains the prefixes which are defined in <dependencies>
58 my @newPrefsInDep;
59 #Contains the prefixes/namespaces which need to be defined in <dependencies> but which are currently
60 #not. For example a prefix is defined in the parent and is used in a child of <dependencies>
61 my %notDefInDep;
63 #prefix used in start and end element
64 my $prefix;
66 #The default namespace valid in <dependencies>
67 my $defNsInDep;
68 #The prefix which we use for the default namespace used in <dependencies>
69 my $generatedPrefix;
71 my $helptext =
72 "make_ext_update_info.pl produces an update information file for an extension. ".
73 "It will use a dummy URL as URL for the extension update unless a URL has been ".
74 "provided with the --update_url option. The name of the update ".
75 "information file, which must be provided with the --out switch, should be formed ".
76 "according to this scheme: \n\n".
77 "extension_identifier.update.xml\n\n".
78 "extension_identifier should correspond to the extension identifier. In some cases ".
79 "this may not be possible because the identifier may contain characters which are not ".
80 "allowd in file names.\n\n".
81 "usage:\n".
82 "perl make_ext_update_info.pl [--help][--update_url url] --out update_information_file description.xml \n\n".
83 "Options: \n".
84 "--help - prints the help message and exits \n".
85 "--out file - the update information file to be written including the path \n".
86 "--update-url url - inserts the url under the <update-download> element. It may be necessary to enclose the urls in quotes in case they contain characters such as \"?\". ".
87 "It can be used multiple times\n\n";
89 #handling of arguments
90 my $help = 0;
91 my $out;
92 my @update_urls;
93 if (!GetOptions('help|?' => \$help,
94 'out=s' => \$out,
95 'update-url=s'=> \@update_urls))
97 print $helptext;
98 exit -1;
100 my $cArgs = scalar @ARGV;
101 die "You need to provide a description.xml\n\n$helptext" if $cArgs ==0;
102 die "You need to provide the name of the update information file ".
103 "with the --out switch.\n" unless ($out);
104 die "Too many arguments. \n\n$helptext" if $cArgs > 1;
105 print $helptext if $help;
108 #open the update information file for writing
109 my $FH;
110 open $FH, "> $out" or die $!;
112 #write the xml header and root element
113 print $FH '<?xml version="1.0" encoding="UTF-8"?>', "\n";
114 print $FH '<description xmlns="http://openoffice.org/extensions/update/2006"', "\n";
115 print $FH ' xmlns:xlink="http://www.w3.org/1999/xlink">', "\n";
117 #obtain from description.xml the data for the update information
118 writeUpdateInformationData($ARGV[0]);
119 #We will die if there is no <version> or <identifier> in the description.xml
120 die "Error: The description.xml does not contain a <identifier> element.\n" unless $identifier;
121 die "Error: The description.xml does not contain a <version> element. \n" unless $version;
123 #write the write the update-download element and the children.
124 #the indention of <update-download> corresponds to that of <version>
125 print $FH ' 'x$indent, '<update-download>', "\n";
126 #check if update-urls have been provided through --update-url option
127 if (scalar @update_urls)
129 my $urlIndent = $indent > 8 ? 8 : 2 * $indent;
130 #use provided urls
131 for (@update_urls)
133 print $FH ' 'x$urlIndent, '<src xlink:href="'.$_.'" />', "\n";
136 else
138 #use dummy update url
139 print $FH ' 'x8, '<src xlink:href="http://extensions.openoffice.org/testarea/dummy.oxt" />', "\n";
141 print $FH ' 'x$indent, '</update-download>', "\n";
143 print $FH '</description>', "\n";
144 close $FH;
146 exit 0;
150 sub start_handler
152 my $parser = shift;
153 my $name = shift;
155 if ($name eq "description"
156 && $descNS eq $parser->namespace($name))
158 $inDescription = 1;
160 elsif ($inDescription
161 && $name eq "version"
162 && $descNS eq $parser->namespace($name))
164 $inVersion = 1;
165 $version = 1;
166 $indent = $parser->current_column();
167 print $FH " "x$indent, $parser->original_string();
169 elsif ($inDescription
170 && $name eq "identifier"
171 && $descNS eq $parser->namespace($name))
173 $inIdentifier = 1;
174 $identifier = 1;
175 print $FH " "x$parser->current_column(), $parser->original_string();
177 elsif ($inDescription
178 && $name eq "dependencies"
179 && $descNS eq $parser->namespace($name))
181 $inDependencies = 1;
182 my $dep = $parser->original_string();
183 #add the additional namespace definitions, which we have discovered during the first
184 #parsing
185 #cut of the closing > or /> from the start element, so we can append the namespace definitions
186 $dep =~ /(\s*<.*) ((\s*\/>)|(\s*>))/x;
187 my $dep1 = $1;
188 $dep1.= " xmlns:".$_.'="'.$notDefInDep{$_}.'"' for (keys %notDefInDep);
189 $dep1.= $2;
190 print $FH " "x$parser->current_column(), $dep1;
192 elsif ($inDependencies)
194 #$prefix is global because we need to use it in the end element as well.
195 $prefix = "";
196 my $fullString;
197 my $orig = $parser->original_string();
198 #Split up the string so we can insert the prefix for the element.
199 # <OpenOffice.org-minimal-version>
200 # <d:OpenOffice.org-minimal-version>
201 $orig=~/(\s*<)(.*?)\s/x;
202 #in $2 is the element name, look for the prefix
203 if ($2 !~/(.*?):/ && $parser->namespace($name)) {
204 #no prefix, that is element uses default namespace.
205 #Now check if the default namespace in <dependencies> is the same as the one in this
206 #element. If not, then the default ns was defined "after" <dependencies>. Because all
207 #children of <dependencies> are copied into the update information, so will this default
208 #namespace definition. Hence this element will have the same default namespace in the
209 #update information.
210 my $defNsDep = $validPrefsInDep{"#default"};
211 #we must have #default, see the if statement above
212 my $defNsCur = $parser->expand_ns_prefix("#default");
214 if ($defNsDep eq $defNsCur) {
215 #Determine if there is in <dependency> a prefix defined (only valid there and need not
216 #directly defined in this element). If there is no prefix defined then we will
217 #add a new definition to <dependencies>.
218 for (keys %validPrefsInDep) {
219 if (($validPrefsInDep{$_} eq $defNsDep) && $_ ne "#default") {
220 $prefix = $_; last;
223 if (! $prefix) {
224 #If there was no prefix, we will add new prefix definition to <dependency>
225 #Which prefix this is has been determined during the first parsing.
226 for (keys %notDefInDep) {
227 if (($notDefInDep{$_} eq $defNsCur) && $_ ne "#default") {
228 $prefix = $_; last;
232 #die if we have no prefix
233 confess "No prefix defined for default namespace " unless $prefix;
234 #get the full part after <
235 $orig=~/(\s*<)(.*)/x;
236 $fullString= $1.$prefix.":".$2;
240 $fullString = $orig unless $fullString;
242 # We record anything within <dependencies> </dependencies>.
243 print $FH $fullString;
247 sub end_handler
249 my $parser = shift;
250 my $name = shift;
252 if ($name eq "description"
253 && $descNS eq $parser->namespace($name))
255 $inDescription = 0;
257 elsif ($inDescription
258 && $name eq "version"
259 && $descNS eq $parser->namespace($name))
261 $inVersion = 0;
262 print $FH $parser->original_string(), "\n";
264 elsif ($inDescription
265 && $name eq "identifier"
266 && $descNS eq $parser->namespace($name))
268 $inIdentifier = 0;
269 print $FH $parser->original_string(), "\n";
271 elsif($inDescription
272 && $name eq "dependencies"
273 && $descNS eq $parser->namespace($name))
275 $inDependencies = 0;
276 print $FH $parser->original_string(), "\n";
278 elsif ($inDependencies)
280 my $orig = $parser->original_string();
281 #$orig is empty if we have tags like this: <name />
282 if ($orig && $prefix) {
283 $orig=~/(\s*<\/)(.*)/x;
284 $orig= $1.$prefix.":".$2;
286 print $FH $orig;
290 #We write the complete content between start and end tags of
291 # <identifier>, <version>, <dependencies>
292 sub default_handler
294 my $parser = shift;
295 my $name = shift;
296 if ($inIdentifier || $inVersion) {
297 print $FH $parser->original_string();
298 } elsif ($inDependencies) {
299 print $FH $parser->original_string();
302 } # End of default_handler
304 #sax handler used for the first parsing to recognize the used prefixes in <dependencies > and its
305 #children and to find out if we need to define a new prefix for the current default namespace.
306 sub start_handler_infos
308 my $parser = shift;
309 my $name = shift;
310 if ($name eq "description"
311 && $descNS eq $parser->namespace($name)) {
312 $inDescription = 1;
314 elsif ($inDescription
315 && $name eq "dependencies"
316 && $descNS eq $parser->namespace($name)) {
317 $inDependencies = 1;
318 #build the map of prefix/namespace which are valid in <dependencies>
319 my @cur = $parser->current_ns_prefixes();
320 for (@cur) {
321 $validPrefsInDep{$_} = $parser->expand_ns_prefix($_);
323 #remember the prefixes defined in <dependencies>
324 @newPrefsInDep = $parser->new_ns_prefixes();
326 collectPrefixes($parser, $name, \@_, \@usedNsInDependencies);
327 return if $generatedPrefix;
329 #determine if need to create a new prefix for the current element if it uses a default ns.
330 #Split up the string so we can see if there is a prefix used
331 # <OpenOffice.org-minimal-version>
332 # <d:OpenOffice.org-minimal-version>
333 my $orig = $parser->original_string();
334 $orig=~/(\s*<)(.*?)\s/x;
335 #in $2 is the element name, look for the prefix
336 if ($2 !~/(.*?):/ && $parser->namespace($name)) {
337 #no prefix, that is element uses default namespace.
338 #Now check if the default namespace in <dependencies> is the same as the one in this
339 #element. If not, then the default ns was defined "after" <dependencies>. Because all
340 #children of <dependencies> are copied into the update information, so will this default
341 #namespace definition. Hence this element will have the same default namespace in the
342 #update information.
343 my $defNsDep = $validPrefsInDep{"#default"};
344 #we must have #default, see the if statement above
345 my $defNsCur = $parser->expand_ns_prefix("#default");
347 if ($defNsDep eq $defNsCur) {
348 #Determine if there is in <dependency> a prefix defined (only valid there and need not
349 #directly defined in this element). If there is no prefix defined then we will
350 #add a new definition to <dependencies>.
351 for (keys %validPrefsInDep) {
352 if (($validPrefsInDep{$_} eq $defNsDep) && $_ ne "#default") {
353 $prefix = $_; last;
357 if (! $prefix) {
359 #define a new prefix
360 #actually there can be only onle prefix, which is the case when the element
361 #uses the same default namespace as <dependencies> otherwise, the default
362 #namespace was redefined by the children of <dependencies>. These are completely
363 #copied and still valid in the update information file
364 $generatedPrefix = "a";
365 $defNsInDep = $defNsDep;
371 elsif ($inDependencies) {
372 determineNsDefinitions($parser, $name, \@_);
373 collectPrefixes($parser, $name, \@_, \@usedNsInDependencies);
376 #sax handler used for the first parsing to recognize the used prefixes in <dependencies > and its
377 #children
378 sub end_handler_infos
380 my $parser = shift;
381 my $name = shift;
383 if ($name eq "description"
384 && $descNS eq $parser->namespace($name)) {
385 $inDescription = 0;
387 elsif($inDescription
388 && $name eq "dependencies"
389 && $descNS eq $parser->namespace($name)) {
390 $inDependencies = 0;
394 sub writeUpdateInformationData($)
396 my $desc = shift;
398 #parse description xml to collect information about all used
399 #prefixes and names within <dependencies>
401 my $parser = new XML::Parser(ErrorContext => 2,
402 Namespaces => 1);
403 $parser->setHandlers(Start => \&start_handler_infos,
404 End => \&end_handler_infos);
406 $parser->parsefile($desc);
410 #remove duplicates in the array containing the prefixes
411 if ($generatedPrefix) {
412 my %hashtmp;
413 @usedNsInDependencies = grep(!$hashtmp{$_}++, @usedNsInDependencies);
415 #check that the prefix for the default namespace in <dependencies> does not clash
416 #with any other prefixes
417 my $clash;
418 do {
419 $clash = 0;
420 for (@usedNsInDependencies) {
421 if ($_ eq $generatedPrefix) {
422 $generatedPrefix++;
423 $clash = 1; last;
426 } while ($clash);
427 $notDefInDep{$generatedPrefix} = $defNsInDep;
429 #if $notDefInDep contains the prefix #default then we need to add the generated prefix as well
431 #add the special prefix for the default namespace into the map of prefixes that will be
432 #added to the <dependencies> element in the update information file
435 ($inDependencies, $inDescription) = (0,0);
437 my $parser = new XML::Parser(ErrorContext => 2,
438 Namespaces => 1);
439 $parser->setHandlers(
440 Start => \&start_handler,
441 End => \&end_handler,
442 Default => \&default_handler);
443 $parser->parsefile($desc);
447 # param 1: name of the attribute we look for
448 # param 2: array of name value pairs, the first subscript is the attribute and the second
449 # is the value.
450 sub findAttribute($$)
452 my ($name, $args_r) = @_;
453 my @args = @{$args_r};
454 my $value;
455 while (my $attr = shift(@args))
457 if ($attr eq $name) {
458 $value = shift(@args);
459 die "href attribut has no valid URL" unless $value;
460 last;
461 } else { # shift away the following value for the attribute
462 shift(@args);
465 return $value;
468 #collect the prefixes used in an xml element
469 #param 1: parser,
470 #param 2: element name,
471 #param 3: array of name and values of attributes
472 #param 4: out parameter, the array containing the prefixes
473 sub collectPrefixes($$$$)
475 my $parser = shift;
476 my $name = shift;
477 my $attr_r = shift;
478 my $out_r = shift;
479 #get the prefixes which are currently valid
480 my @cur = $parser->current_ns_prefixes();
481 my %map_ns;
482 #get the namespaces for the prefixes
483 for (@cur) {
484 if ($_ eq '#default') {
485 next;
487 my $ns = $parser->expand_ns_prefix($_);
488 $map_ns{$ns} = $_;
490 #investigat ns of element
491 my $pref = $map_ns{$parser->namespace($name)};
492 push(@{$out_r}, $pref) if $pref;
493 #now go over the attributes
495 while (my $attr = shift(@{$attr_r})) {
496 my $ns = $parser->namespace($attr);
497 if (! $ns) {
498 shift(@{$attr_r});
499 next;
501 $pref = $map_ns{$ns};
502 push( @{$out_r}, $pref) if $pref;
503 shift(@{$attr_r});
505 #also add newly defined prefixes
506 my @newNs = $parser->new_ns_prefixes();
507 for (@newNs) {
508 if ($_ eq '#default') {
509 next;
511 push (@{$out_r}, $_);
515 #The function is called for each child element of dependencies. It finds out the prefixes
516 #which are used by the children and which are defined by the parents of <dependencies>. These
517 #would be lost when copying the children of <dependencies> into the update information file.
518 #Therefore these definitions are collected so that they then can be written in the <dependencies>
519 #element of the update information file.
520 #param 1: parser
521 #param 2: namsepace
522 #param 3: the @_ received in the start handler
523 sub determineNsDefinitions($$$)
525 my ($parser, $name, $attr_r) = @_;
526 my @attr = @{$attr_r};
528 determineNsDefinitionForItem($parser, $name, 1);
530 while (my $attr = shift(@attr)) {
531 determineNsDefinitionForItem($parser, $attr, 0);
532 shift @attr;
536 #do not call this function for the element that does not use a prefix
537 #param 1: parser
538 #param 2: name of the element or attribute
539 #param 3: 1 if called for an elment name and 0 when called for attribue
540 sub determineNsDefinitionForItem($$$)
542 my ($parser, $name) = @_;
543 my $ns = $parser->namespace($name);
544 if (! $ns) {
545 return;
547 #If the namespace was not kwown in <dependencies> then it was defined in one of its children
548 #or in this element. Then we are done since this namespace definition is copied into the
549 #update information.
550 my $bNsKnownInDep;
551 for ( keys %validPrefsInDep) {
552 if ( $validPrefsInDep{$_} eq $ns) {
553 $bNsKnownInDep = 1;
554 last;
557 #If the namespace of the current element is known in <dependencies> then check if the same
558 #prefix is used. If not, then the prefix was defined in one of the children of <dependencies>
559 #and was assigned the same namespace. Because we copy of children into the update information,
560 #this definition is also copied.
561 if ($bNsKnownInDep) {
562 #create a map of currently valid prefix/namespace
563 my %curPrefToNs;
564 my @curNs = $parser->current_ns_prefixes();
565 for (@curNs) {
566 $curPrefToNs{$_} = $parser->expand_ns_prefix($_);
568 #find the prefix used in <dependencies> to define the namespace of the current element
569 my $validDepPref;
570 for (keys %validPrefsInDep) {
571 if ($validPrefsInDep{$_} eq $ns) {
572 #ignore #default
573 next if $_ eq "#default";
574 $validDepPref = $_;
575 last;
578 #find the prefix defined in the current element used for the namespace of the element
579 my $curPref;
580 for (keys %curPrefToNs) {
581 if ($curPrefToNs{$_} eq $ns) {
582 #ignore #default
583 next if $_ eq "#default";
584 $curPref = $_;
585 last;
588 if ($curPref && $validDepPref && ($curPref eq $validDepPref)) {
589 #If the prefixes and ns are the same, then the prefix definition of <dependencies> or its
590 #parent can be used. However, we need to find out which prefixed are NOT defined in
591 #<dependencies> so we can add them to it when we write the update information.
592 my $bDefined = 0;
593 for (@newPrefsInDep) {
594 if ($curPref eq $_) {
595 $bDefined = 1;
596 last;
599 if (! $bDefined) {
600 $notDefInDep{$curPref} = $ns;