2 eval 'exec perl -wS $0 ${1+"$@"}'
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
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;
45 my $descNS = "http://openoffice.org/extensions/description/2006";
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>
57 #Contains the prefixes which are defined in <dependencies>
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>
63 #prefix used in start and end element
66 #The default namespace valid in <dependencies>
68 #The prefix which we use for the default namespace used in <dependencies>
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".
82 "perl make_ext_update_info.pl [--help][--update_url url] --out update_information_file description.xml \n\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
93 if (!GetOptions
('help|?' => \
$help,
95 'update-url=s'=> \
@update_urls))
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
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;
133 print $FH ' 'x
$urlIndent, '<src xlink:href="'.$_.'" />', "\n";
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";
155 if ($name eq "description"
156 && $descNS eq $parser->namespace($name))
160 elsif ($inDescription
161 && $name eq "version"
162 && $descNS eq $parser->namespace($name))
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))
175 print $FH " "x
$parser->current_column(), $parser->original_string();
177 elsif ($inDescription
178 && $name eq "dependencies"
179 && $descNS eq $parser->namespace($name))
182 my $dep = $parser->original_string();
183 #add the additional namespace definitions, which we have discovered during the first
185 #cut of the closing > or /> from the start element, so we can append the namespace definitions
186 $dep =~ /(\s*<.*) ((\s*\/>)|(\s
*>))/x
;
188 $dep1.= " xmlns:".$_.'="'.$notDefInDep{$_}.'"' for (keys %notDefInDep);
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.
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
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") {
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") {
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;
252 if ($name eq "description"
253 && $descNS eq $parser->namespace($name))
257 elsif ($inDescription
258 && $name eq "version"
259 && $descNS eq $parser->namespace($name))
262 print $FH $parser->original_string(), "\n";
264 elsif ($inDescription
265 && $name eq "identifier"
266 && $descNS eq $parser->namespace($name))
269 print $FH $parser->original_string(), "\n";
272 && $name eq "dependencies"
273 && $descNS eq $parser->namespace($name))
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;
290 #We write the complete content between start and end tags of
291 # <identifier>, <version>, <dependencies>
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
310 if ($name eq "description"
311 && $descNS eq $parser->namespace($name)) {
314 elsif ($inDescription
315 && $name eq "dependencies"
316 && $descNS eq $parser->namespace($name)) {
318 #build the map of prefix/namespace which are valid in <dependencies>
319 my @cur = $parser->current_ns_prefixes();
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
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") {
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
378 sub end_handler_infos
383 if ($name eq "description"
384 && $descNS eq $parser->namespace($name)) {
388 && $name eq "dependencies"
389 && $descNS eq $parser->namespace($name)) {
394 sub writeUpdateInformationData
($)
398 #parse description xml to collect information about all used
399 #prefixes and names within <dependencies>
401 my $parser = new XML
::Parser
(ErrorContext
=> 2,
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) {
413 @usedNsInDependencies = grep(!$hashtmp{$_}++, @usedNsInDependencies);
415 #check that the prefix for the default namespace in <dependencies> does not clash
416 #with any other prefixes
420 for (@usedNsInDependencies) {
421 if ($_ eq $generatedPrefix) {
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,
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
450 sub findAttribute
($$)
452 my ($name, $args_r) = @_;
453 my @args = @
{$args_r};
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;
461 } else { # shift away the following value for the attribute
468 #collect the prefixes used in an xml element
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
($$$$)
479 #get the prefixes which are currently valid
480 my @cur = $parser->current_ns_prefixes();
482 #get the namespaces for the prefixes
484 if ($_ eq '#default') {
487 my $ns = $parser->expand_ns_prefix($_);
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);
501 $pref = $map_ns{$ns};
502 push( @
{$out_r}, $pref) if $pref;
505 #also add newly defined prefixes
506 my @newNs = $parser->new_ns_prefixes();
508 if ($_ eq '#default') {
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.
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);
536 #do not call this function for the element that does not use a prefix
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);
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
551 for ( keys %validPrefsInDep) {
552 if ( $validPrefsInDep{$_} eq $ns) {
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
564 my @curNs = $parser->current_ns_prefixes();
566 $curPrefToNs{$_} = $parser->expand_ns_prefix($_);
568 #find the prefix used in <dependencies> to define the namespace of the current element
570 for (keys %validPrefsInDep) {
571 if ($validPrefsInDep{$_} eq $ns) {
573 next if $_ eq "#default";
578 #find the prefix defined in the current element used for the namespace of the element
580 for (keys %curPrefToNs) {
581 if ($curPrefToNs{$_} eq $ns) {
583 next if $_ eq "#default";
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.
593 for (@newPrefsInDep) {
594 if ($curPref eq $_) {
600 $notDefInDep{$curPref} = $ns;