1:
2eval 'exec perl -wS $0 ${1+"$@"}'
3    if 0;
4#*************************************************************************
5#
6# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7#
8# Copyright 2000, 2010 Oracle and/or its affiliates.
9#
10# OpenOffice.org - a multi-platform office productivity suite
11#
12# This file is part of OpenOffice.org.
13#
14# OpenOffice.org is free software: you can redistribute it and/or modify
15# it under the terms of the GNU Lesser General Public License version 3
16# only, as published by the Free Software Foundation.
17#
18# OpenOffice.org is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21# GNU Lesser General Public License version 3 for more details
22# (a copy is included in the LICENSE file that accompanied this code).
23#
24# You should have received a copy of the GNU Lesser General Public License
25# version 3 along with OpenOffice.org.  If not, see
26# <http://www.openoffice.org/license.html>
27# for a copy of the LGPLv3 License.
28#
29#*************************************************************************
30
31#here the definition for d would be written into dependencies. The reason is that when the event handler
32#for the element is called, we can only find out the namespace but not the prefix. So we cannot
33#distinguish if the namespace is used because the element was prefixed or because it uses the default
34#namespace.
35use warnings;
36use strict;
37
38use XML::Parser;
39use Getopt::Long;
40use Carp;
41
42sub getUpdateInfoFileName($);
43sub writeUpdateInformationData($);
44sub findAttribute($$);
45sub getNotDefPrefs($$$);
46sub collectPrefixes($$$$);
47sub determineNsDefinitions($$$);
48sub determineNsDefinitionForItem($$$);
49
50my $inDescription = 0;
51my $inDependencies = 0;
52my $inIdentifier = 0;
53my $inVersion = 0;
54my $descNS = "http://openoffice.org/extensions/description/2006";
55                   my $indent;
56my $identifier;
57my $version;
58
59#contains prefixes and the corresponding namespaces which are used in the <dependencies>
60#element and all children of the description.xml
61my @usedNsInDependencies;
62
63#Maps  prefix to namespaces which are valid in <dependencies>. That is, they are
64#either defined in <dependencies> or in the hirarchy above <dependencies>
65my %validPrefsInDep;
66#Contains the prefixes which are defined in <dependencies>
67my @newPrefsInDep;
68#Contains the prefixes/namespaces which need to be defined in <dependencies> but which are currently
69#not. For example a prefix is defined in the parent and is used in a child of <dependencies>
70my %notDefInDep;
71
72#prefix used in start and end element
73my $prefix;
74
75#The default namespace valid in <dependencies>
76my $defNsInDep;
77#The prefix which we use for the default namespace used in <dependencies>
78my $generatedPrefix;
79
80my $helptext =
81"make_ext_update_info.pl produces an update information file for an extension. ".
82"It will use a dummy URL as URL for the extension update unless a URL has been ".
83"provided with the --update_url option. The name of the update ".
84"information file, which must be provided with the --out switch, should be formed ".
85"according to this scheme: \n\n".
86"extension_identifier.update.xml\n\n".
87"extension_identifier should correspond to the extension identifier. In some cases ".
88"this may not be possible because the identifier may contain characters which are not ".
89"allowd in file names.\n\n".
90"usage:\n".
91"perl make_ext_update_info.pl [--help][--update_url url] --out update_information_file description.xml \n\n".
92"Options: \n".
93"--help - prints the help message and exits \n".
94"--out file - the update information file to be written including the path \n".
95"--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 \"?\". ".
96"It can be used multiple times\n\n";
97
98#handling of arguments
99my $help = 0;
100my $out;
101my @update_urls;
102if (!GetOptions('help|?' => \$help,
103                'out=s' => \$out,
104                'update-url=s'=> \@update_urls))
105{
106    print $helptext;
107    exit -1;
108}
109my $cArgs = scalar @ARGV;
110die "You need to provide a description.xml\n\n$helptext" if $cArgs ==0;
111die "You need to provide the name of the update information file ".
112    "with the --out switch.\n" unless ($out);
113die "Too many arguments. \n\n$helptext" if $cArgs > 1;
114print $helptext if $help;
115
116
117#open the update information file for writing
118my $FH;
119open $FH, "> $out" or die $!;
120
121#write the xml header and root element
122print $FH '<?xml version="1.0" encoding="UTF-8"?>', "\n";
123print $FH '<description xmlns="http://openoffice.org/extensions/update/2006"', "\n";
124print $FH '    xmlns:xlink="http://www.w3.org/1999/xlink">', "\n";
125
126#obtain from description.xml the data for the update information
127writeUpdateInformationData($ARGV[0]);
128#We will die if there is no <version> or <identifier> in the description.xml
129die "Error: The description.xml does not contain a <identifier> element.\n" unless $identifier;
130die "Error: The description.xml does not contain a <version> element. \n" unless $version;
131
132#write the write the update-download element and the children.
133#the indention of <update-download> corresponds to that of <version>
134print $FH ' 'x$indent, '<update-download>', "\n";
135#check if update-urls have been provided through --update-url option
136if (scalar @update_urls)
137{
138    my $urlIndent = $indent > 8 ? 8 : 2 * $indent;
139    #use provided urls
140    for (@update_urls)
141    {
142        print $FH ' 'x$urlIndent, '<src xlink:href="'.$_.'" />', "\n";
143    }
144}
145else
146{
147    #use dummy update url
148    print $FH ' 'x8, '<src xlink:href="http://extensions.openoffice.org/testarea/dummy.oxt" />', "\n";
149}
150print $FH ' 'x$indent, '</update-download>', "\n";
151
152print $FH '</description>', "\n";
153close $FH;
154
155exit 0;
156
157
158
159sub start_handler
160{
161    my $parser = shift;
162    my $name = shift;
163
164    if ($name eq "description"
165        && $descNS eq $parser->namespace($name))
166    {
167        $inDescription = 1;
168    }
169    elsif ($inDescription
170           && $name eq "version"
171           && $descNS eq  $parser->namespace($name))
172    {
173        $inVersion = 1;
174        $version = 1;
175        $indent = $parser->current_column();
176        print $FH " "x$indent, $parser->original_string();
177    }
178    elsif ($inDescription
179           && $name eq "identifier"
180           && $descNS eq  $parser->namespace($name))
181    {
182        $inIdentifier = 1;
183        $identifier = 1;
184        print $FH " "x$parser->current_column(), $parser->original_string();
185    }
186    elsif ($inDescription
187           && $name eq "dependencies"
188           && $descNS eq  $parser->namespace($name))
189    {
190        $inDependencies = 1;
191        my $dep = $parser->original_string();
192        #add the additional namespace definitions, which we have discovered during the first
193        #parsing
194        #cut of the closing > or /> from the start element, so we can append the namespace definitions
195        $dep =~ /(\s*<.*) ((\s*\/>)|(\s*>))/x;
196        my $dep1 = $1;
197        $dep1.= " xmlns:".$_.'="'.$notDefInDep{$_}.'"' for (keys %notDefInDep);
198        $dep1.= $2;
199        print $FH " "x$parser->current_column(), $dep1;
200    }
201    elsif ($inDependencies)
202    {
203        #$prefix is global because we need to use it in the end element as well.
204        $prefix = "";
205        my $fullString;
206        my $orig = $parser->original_string();
207        #Split up the string so we can insert the prefix for the element.
208        # <OpenOffice.org-minimal-version>
209        # <d:OpenOffice.org-minimal-version>
210        $orig=~/(\s*<)(.*?)\s/x;
211        #in $2 is the element name, look for the prefix
212        if ($2 !~/(.*?):/ && $parser->namespace($name)) {
213            #no prefix, that is element uses default namespace.
214            #Now check if the default namespace in <dependencies> is the same as the one in this
215            #element. If not, then the default ns was defined "after" <dependencies>. Because all
216            #children of <dependencies> are copied into the update information, so will this default
217            #namespace definition. Hence this element will have the same default namespace in the
218            #update information.
219            my $defNsDep = $validPrefsInDep{"#default"};
220            #we must have #default, see the if statement above
221            my $defNsCur = $parser->expand_ns_prefix("#default");
222
223            if ($defNsDep eq $defNsCur) {
224                #Determine if there is in <dependency> a prefix defined (only valid there and need not
225                #directly defined in this element). If there is no prefix defined then we will
226                #add a new definition to <dependencies>.
227                for (keys %validPrefsInDep) {
228                    if (($validPrefsInDep{$_} eq $defNsDep) && $_ ne "#default") {
229                        $prefix = $_; last;
230                    }
231                }
232                if (! $prefix) {
233                    #If there was no prefix, we will add new prefix definition to <dependency>
234                    #Which prefix this is has been determined during the first parsing.
235                    for (keys %notDefInDep) {
236                        if (($notDefInDep{$_} eq $defNsCur) && $_ ne "#default") {
237                            $prefix = $_; last;
238                        }
239                    }
240                }
241                #die if we have no prefix
242                confess "No prefix defined for default namespace " unless $prefix;
243                #get the full part after <
244                $orig=~/(\s*<)(.*)/x;
245                $fullString= $1.$prefix.":".$2;
246            }
247
248        }
249        $fullString = $orig unless $fullString;
250
251        # We record anything within <dependencies> </dependencies>.
252        print $FH $fullString;
253    }
254}
255
256sub end_handler
257{
258    my $parser = shift;
259    my $name = shift;
260
261    if ($name eq "description"
262        && $descNS eq  $parser->namespace($name))
263    {
264        $inDescription = 0;
265    }
266    elsif ($inDescription
267           && $name eq "version"
268           && $descNS eq  $parser->namespace($name))
269    {
270        $inVersion = 0;
271        print $FH  $parser->original_string(), "\n";
272    }
273    elsif ($inDescription
274           && $name eq "identifier"
275           && $descNS eq  $parser->namespace($name))
276    {
277        $inIdentifier = 0;
278        print $FH $parser->original_string(), "\n";
279    }
280    elsif($inDescription
281          && $name eq "dependencies"
282          && $descNS eq $parser->namespace($name))
283    {
284        $inDependencies = 0;
285        print $FH $parser->original_string(), "\n";
286    }
287    elsif ($inDependencies)
288    {
289        my $orig = $parser->original_string();
290        #$orig is empty if we have tags like this: <name />
291        if ($orig && $prefix) {
292            $orig=~/(\s*<\/)(.*)/x;
293            $orig= $1.$prefix.":".$2;
294        }
295        print $FH $orig;
296    }
297}
298
299#We write the complete content between start and end tags of
300# <identifier>, <version>, <dependencies>
301sub default_handler
302{
303    my $parser = shift;
304    my $name = shift;
305    if ($inIdentifier || $inVersion) {
306        print $FH $parser->original_string();
307    } elsif ($inDependencies) {
308        print $FH  $parser->original_string();
309    }
310
311}  # End of default_handler
312
313#sax handler used for the first parsing to recognize the used prefixes in <dependencies > and its
314#children and to find out if we need to define a new prefix for the current default namespace.
315sub start_handler_infos
316{
317    my $parser = shift;
318    my $name = shift;
319    if ($name eq "description"
320        && $descNS eq $parser->namespace($name)) {
321        $inDescription = 1;
322    }
323    elsif ($inDescription
324           && $name eq "dependencies"
325           && $descNS eq  $parser->namespace($name)) {
326        $inDependencies = 1;
327        #build the map of prefix/namespace which are valid in <dependencies>
328        my @cur = $parser->current_ns_prefixes();
329        for (@cur) {
330            $validPrefsInDep{$_} = $parser->expand_ns_prefix($_);
331        }
332        #remember the prefixes defined in <dependencies>
333        @newPrefsInDep = $parser->new_ns_prefixes();
334
335        collectPrefixes($parser, $name, \@_, \@usedNsInDependencies);
336        return if  $generatedPrefix;
337
338        #determine if need to create a new prefix for the current element if it uses a default ns.
339        #Split up the string so we can see if there is a prefix used
340        # <OpenOffice.org-minimal-version>
341        # <d:OpenOffice.org-minimal-version>
342        my $orig = $parser->original_string();
343        $orig=~/(\s*<)(.*?)\s/x;
344        #in $2 is the element name, look for the prefix
345        if ($2 !~/(.*?):/ && $parser->namespace($name)) {
346            #no prefix, that is element uses default namespace.
347            #Now check if the default namespace in <dependencies> is the same as the one in this
348            #element. If not, then the default ns was defined "after" <dependencies>. Because all
349            #children of <dependencies> are copied into the update information, so will this default
350            #namespace definition. Hence this element will have the same default namespace in the
351            #update information.
352            my $defNsDep = $validPrefsInDep{"#default"};
353            #we must have #default, see the if statement above
354            my $defNsCur = $parser->expand_ns_prefix("#default");
355
356            if ($defNsDep eq $defNsCur) {
357                #Determine if there is in <dependency> a prefix defined (only valid there and need not
358                #directly defined in this element). If there is no prefix defined then we will
359                #add a new definition to <dependencies>.
360                for (keys %validPrefsInDep) {
361                    if (($validPrefsInDep{$_} eq $defNsDep) && $_ ne "#default") {
362                        $prefix = $_; last;
363                    }
364                }
365
366                if (! $prefix) {
367
368                    #define a new prefix
369                    #actually there can be only onle prefix, which is the case when the element
370                    #uses the same default namespace as <dependencies> otherwise, the default
371                    #namespace was redefined by the children of <dependencies>. These are completely
372                    #copied and still valid in the update information file
373                    $generatedPrefix = "a";
374                    $defNsInDep = $defNsDep;
375                }
376            }
377        }
378
379    }
380    elsif ($inDependencies) {
381        determineNsDefinitions($parser, $name, \@_);
382        collectPrefixes($parser, $name, \@_, \@usedNsInDependencies);
383    }
384}
385#sax handler used for the first parsing to recognize the used prefixes in <dependencies > and its
386#children
387sub end_handler_infos
388{
389    my $parser = shift;
390    my $name = shift;
391
392    if ($name eq "description"
393        && $descNS eq  $parser->namespace($name)) {
394        $inDescription = 0;
395    }
396    elsif($inDescription
397          && $name eq "dependencies"
398          && $descNS eq $parser->namespace($name)) {
399        $inDependencies = 0;
400    }
401}
402
403sub writeUpdateInformationData($)
404{
405    my $desc = shift;
406    {
407        #parse description xml to collect information about all used
408        #prefixes and names within <dependencies>
409
410        my $parser = new XML::Parser(ErrorContext => 2,
411                                     Namespaces => 1);
412        $parser->setHandlers(Start => \&start_handler_infos,
413                             End => \&end_handler_infos);
414
415        $parser->parsefile($desc);
416
417
418    }
419    #remove duplicates in the array containing the prefixes
420    if ($generatedPrefix) {
421        my %hashtmp;
422        @usedNsInDependencies = grep(!$hashtmp{$_}++, @usedNsInDependencies);
423
424        #check that the prefix for the default namespace in <dependencies> does not clash
425        #with any other prefixes
426        my $clash;
427        do {
428            $clash = 0;
429            for (@usedNsInDependencies) {
430                if ($_ eq $generatedPrefix) {
431                    $generatedPrefix++;
432                    $clash = 1; last;
433                }
434            }
435        } while ($clash);
436        $notDefInDep{$generatedPrefix} = $defNsInDep;
437    }
438    #if $notDefInDep contains the prefix #default then we need to add the generated prefix as well
439
440    #add the special prefix for the default namespace into the map of prefixes that will be
441    #added to the <dependencies> element in the update information file
442
443
444    ($inDependencies, $inDescription) = (0,0);
445    {
446        my $parser = new XML::Parser(ErrorContext => 2,
447                                     Namespaces => 1);
448        $parser->setHandlers(
449                             Start => \&start_handler,
450                             End => \&end_handler,
451                             Default => \&default_handler);
452        $parser->parsefile($desc);
453    }
454}
455
456# param 1: name of the attribute we look for
457# param 2: array of name value pairs, the first subscript is the attribute and the second
458# is the value.
459sub findAttribute($$)
460{
461    my ($name, $args_r) = @_;
462    my @args = @{$args_r};
463    my $value;
464    while (my $attr = shift(@args))
465    {
466        if ($attr eq $name) {
467            $value = shift(@args);
468            die "href attribut has no valid URL" unless $value;
469            last;
470        } else { # shift away the following value for the attribute
471            shift(@args);
472        }
473    }
474    return $value;
475}
476
477#collect the prefixes used in an xml element
478#param 1: parser,
479#param 2: element name,
480#param 3: array of name and values of attributes
481#param 4: out parameter, the array containing the prefixes
482sub collectPrefixes($$$$)
483{
484    my $parser = shift;
485    my $name = shift;
486    my $attr_r = shift;
487    my $out_r = shift;
488    #get the prefixes which are currently valid
489    my @cur = $parser->current_ns_prefixes();
490    my %map_ns;
491    #get the namespaces for the prefixes
492    for (@cur) {
493        if ($_ eq '#default') {
494            next;
495        }
496        my $ns = $parser->expand_ns_prefix($_);
497        $map_ns{$ns} = $_;
498    }
499    #investigat ns of element
500    my $pref = $map_ns{$parser->namespace($name)};
501    push(@{$out_r}, $pref) if $pref;
502    #now go over the attributes
503
504    while (my $attr = shift(@{$attr_r})) {
505        my $ns = $parser->namespace($attr);
506        if (! $ns) {
507            shift(@{$attr_r});
508            next;
509        }
510        $pref = $map_ns{$ns};
511        push( @{$out_r}, $pref) if $pref;
512        shift(@{$attr_r});
513    }
514    #also add newly defined prefixes
515    my @newNs = $parser->new_ns_prefixes();
516    for (@newNs) {
517        if ($_ eq '#default') {
518            next;
519        }
520        push (@{$out_r}, $_);
521    }
522}
523
524#The function is called for each child element of dependencies. It finds out the prefixes
525#which are used by the children and which are defined by the parents of <dependencies>. These
526#would be lost when copying the children of <dependencies> into the update information file.
527#Therefore these definitions are collected so that they then can be written in the <dependencies>
528#element of the update information file.
529#param 1: parser
530#param 2: namsepace
531#param 3: the @_ received in the start handler
532sub determineNsDefinitions($$$)
533{
534    my ($parser, $name, $attr_r) = @_;
535    my @attr = @{$attr_r};
536
537    determineNsDefinitionForItem($parser, $name, 1);
538
539    while (my $attr = shift(@attr)) {
540        determineNsDefinitionForItem($parser, $attr, 0);
541        shift @attr;
542    }
543}
544
545#do not call this function for the element that does not use a prefix
546#param 1: parser
547#param 2: name of the element or attribute
548#param 3: 1 if called for an elment name and 0 when called for attribue
549sub determineNsDefinitionForItem($$$)
550{
551    my ($parser, $name) = @_;
552    my $ns = $parser->namespace($name);
553    if (! $ns) {
554        return;
555    }
556    #If the namespace was not kwown in <dependencies> then it was defined in one of its children
557    #or in this element. Then we are done since this namespace definition is copied into the
558    #update information.
559    my $bNsKnownInDep;
560    for ( keys %validPrefsInDep) {
561        if ( $validPrefsInDep{$_} eq $ns) {
562            $bNsKnownInDep = 1;
563            last;
564        }
565    }
566    #If the namespace of the current element is known in <dependencies> then check if the same
567    #prefix is used. If not, then the prefix was defined in one of the children of <dependencies>
568    #and was assigned the same namespace. Because we copy of children into the update information,
569    #this definition is also copied.
570    if ($bNsKnownInDep) {
571        #create a map of currently valid prefix/namespace
572        my %curPrefToNs;
573        my @curNs = $parser->current_ns_prefixes();
574        for (@curNs) {
575            $curPrefToNs{$_} = $parser->expand_ns_prefix($_);
576        }
577        #find the prefix used in <dependencies> to define the namespace of the current element
578        my $validDepPref;
579        for (keys %validPrefsInDep) {
580            if ($validPrefsInDep{$_} eq $ns) {
581                #ignore #default
582                next if $_ eq "#default";
583                $validDepPref = $_;
584                last;
585            }
586        }
587        #find the prefix defined in the current element used for the namespace of the element
588        my $curPref;
589        for (keys %curPrefToNs) {
590            if ($curPrefToNs{$_} eq $ns) {
591                #ignore #default
592                next if $_ eq "#default";
593                $curPref = $_;
594                last;
595            }
596        }
597        if ($curPref && $validDepPref && ($curPref eq $validDepPref)) {
598            #If the prefixes and ns are the same, then the prefix definition of <dependencies> or its
599            #parent can be used. However, we need to find out which prefixed are NOT defined in
600            #<dependencies> so we can add them to it when we write the update information.
601            my $bDefined = 0;
602            for (@newPrefsInDep) {
603                if ($curPref eq $_) {
604                    $bDefined = 1;
605                    last;
606                }
607            }
608            if (! $bDefined) {
609                $notDefInDep{$curPref} = $ns;
610            }
611        }
612    }
613}
614