Update ooo320-m1
[ooovba.git] / solenv / bin / modules / par2script / work.pm
blob9f68edfe55df16664967aa56e272cd3a2710e884
1 #*************************************************************************
3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 #
5 # Copyright 2008 by Sun Microsystems, Inc.
7 # OpenOffice.org - a multi-platform office productivity suite
9 # $RCSfile: work.pm,v $
11 # $Revision: 1.8 $
13 # This file is part of OpenOffice.org.
15 # OpenOffice.org is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU Lesser General Public License version 3
17 # only, as published by the Free Software Foundation.
19 # OpenOffice.org is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU Lesser General Public License version 3 for more details
23 # (a copy is included in the LICENSE file that accompanied this code).
25 # You should have received a copy of the GNU Lesser General Public License
26 # version 3 along with OpenOffice.org. If not, see
27 # <http://www.openoffice.org/license.html>
28 # for a copy of the LGPLv3 License.
30 #*************************************************************************
33 package par2script::work;
35 use par2script::existence;
36 use par2script::globals;
37 use par2script::remover;
39 ############################################
40 # par2script working module
41 ############################################
43 sub analyze_comma_separated_list
45 my ($list, $listref) = @_; # second parameter is optional
47 my @list = ();
48 my $locallistref;
50 if (!( $listref )) { $locallistref = \@list; }
51 else { $locallistref = $listref; }
53 par2script::remover::remove_leading_and_ending_comma(\$list);
54 par2script::remover::remove_leading_and_ending_whitespaces(\$list);
56 while ( $list =~ /^\s*(.*?)\s*\,\s*(.*)\s*$/ )
58 my $oneentry = $1;
59 $list = $2;
60 par2script::remover::remove_leading_and_ending_whitespaces(\$oneentry);
61 push(@{$locallistref}, $oneentry);
64 # the last entry
66 par2script::remover::remove_leading_and_ending_whitespaces(\$list);
67 push(@{$locallistref}, $list);
69 return $locallistref;
72 ############################################
73 # setting list of include pathes
74 ############################################
76 sub setincludes
78 my ($list) = @_;
80 # input is the comma separated list of include pathes
82 my $includes = analyze_comma_separated_list($list);
84 return $includes;
87 ############################################
88 # setting list of all par files
89 ############################################
91 sub setparfiles
93 my ($filename) = @_;
95 # input is the name of the list file
96 $filename =~ s/\@//; # removing the leading \@
98 my $filecontent = par2script::files::read_file($filename);
100 my @parfiles = ();
101 my $parfilesref = \@parfiles;
103 foreach ( @{$filecontent} ) { $parfilesref = analyze_comma_separated_list($_, $parfilesref); }
105 return $parfilesref;
108 ############################################
109 # finding the correct include path
110 # for the par files
111 ############################################
113 sub make_complete_pathes_for_parfiles
115 my ($parfiles, $includes) = @_;
117 my $oneparfile;
119 foreach $oneparfile ( @{$parfiles} )
121 my $foundparfile = 0;
122 my $includepath;
124 foreach $includepath ( @{$includes} )
126 my $parfile = "$includepath/$oneparfile";
128 if ( -f $parfile )
130 $foundparfile = 1;
131 $oneparfile = $parfile;
132 last;
136 if ( ! $foundparfile )
138 die "ERROR: Could not find parfile ${$parfiles}[$i] in includes pathes: $par2script::globals::includepathlist !\n";
143 ######################################################
144 # collecting one special item in the par files and
145 # including it into the "definitions" hash
146 ######################################################
148 sub collect_definitions
150 my ($parfilecontent) = @_;
152 my $multidefinitionerror = 0;
153 my @multidefinitiongids = ();
156 foreach $oneitem ( @par2script::globals::allitems )
158 my $docollect = 0;
159 my $gid = "";
160 my %allitemhash = ();
162 for ( my $i = 0; $i <= $#{$parfilecontent}; $i++ )
164 my $line = ${$parfilecontent}[$i];
166 if ( $line =~ /^\s*$oneitem\s+(\w+)\s*$/ )
168 $gid = $1;
169 $docollect = 1;
171 else
173 $docollect = 0;
176 if ( $docollect )
178 my $currentline = $i;
179 my %oneitemhash;
181 while (! ( ${$parfilecontent}[$currentline] =~ /^\s*End\s*$/i ) )
183 if ( ${$parfilecontent}[$currentline] =~ /^\s*(.+?)\s*\=\s*(.+?)\s*\;\s*$/ ) # only oneliner!
185 $itemkey = $1;
186 $itemvalue = $2;
188 if ( $oneitem eq "Directory" ) { if ( $itemkey =~ "DosName" ) { $itemkey =~ s/DosName/HostName/; } }
189 if (( $oneitem eq "Directory" ) || ( $oneitem eq "File" ) || ( $oneitem eq "Unixlink" )) { if ( $itemvalue eq "PD_PROGDIR" ) { $itemvalue = "PREDEFINED_PROGDIR"; }}
190 if (( $itemkey eq "Styles" ) && ( $itemvalue =~ /^\s*(\w+)(\s*\;\s*)$/ )) { $itemvalue = "($1)$2"; }
192 $oneitemhash{$itemkey} = $itemvalue;
195 $currentline++;
198 # no hyphen allowed in gids -> cannot happen here because (\w+) is required for gids
199 if ( $gid =~ /-/ ) { par2script::exiter::exit_program("ERROR: No hyphen allowed in global id: $gid", "test_of_hyphen"); }
201 # test of uniqueness
202 if ( exists($allitemhash{$gid}) )
204 $multidefinitionerror = 1;
205 push(@multidefinitiongids, $gid);
208 $allitemhash{$gid} = \%oneitemhash;
212 $par2script::globals::definitions{$oneitem} = \%allitemhash;
215 if ( $multidefinitionerror ) { par2script::exiter::multidefinitionerror(\@multidefinitiongids); }
217 # foreach $key (keys %par2script::globals::definitions)
219 # print "Key: $key \n";
221 # foreach $key (keys %{$par2script::globals::definitions{$key}})
223 # print "\t$key \n";
228 ######################################################
229 # Filling content into the script
230 ######################################################
232 sub put_oneitem_into_script
234 my ( $script, $item, $itemhash, $itemkey ) = @_;
236 push(@{$script}, "$item $itemkey\n" );
237 my $content = "";
238 foreach $content (sort keys %{$itemhash->{$itemkey}}) { push(@{$script}, "\t$content = $itemhash->{$itemkey}->{$content};\n" ); }
239 push(@{$script}, "End\n" );
240 push(@{$script}, "\n" );
243 ######################################################
244 # Creating the script
245 ######################################################
247 sub create_script
249 my @script = ();
250 my $oneitem;
252 foreach $oneitem ( @par2script::globals::allitems )
254 if ( exists($par2script::globals::definitions{$oneitem}) )
256 if ( $oneitem eq "Shortcut" ) { next; } # "Shortcuts" after "Files"
258 if (( $oneitem eq "Module" ) || ( $oneitem eq "Directory" )) { write_sorted_items(\@script, $oneitem); }
259 else { write_unsorted_items(\@script, $oneitem); }
263 return \@script;
266 ######################################################
267 # Adding script content for the unsorted items
268 ######################################################
270 sub write_unsorted_items
272 my ( $script, $oneitem ) = @_;
274 my $itemhash = $par2script::globals::definitions{$oneitem};
276 my $itemkey = "";
277 foreach $itemkey (sort keys %{$itemhash})
279 put_oneitem_into_script($script, $oneitem, $itemhash, $itemkey);
281 # special handling for Shortcuts after Files
282 if (( $oneitem eq "File" ) && ( exists($par2script::globals::definitions{"Shortcut"}) ))
284 my $shortcutkey;
285 foreach $shortcutkey ( keys %{$par2script::globals::definitions{"Shortcut"}} )
287 if ( $par2script::globals::definitions{"Shortcut"}->{$shortcutkey}->{'FileID'} eq $itemkey )
289 put_oneitem_into_script($script, "Shortcut", $par2script::globals::definitions{"Shortcut"}, $shortcutkey);
291 # and Shortcut to Shortcut also
292 my $internshortcutkey;
293 foreach $internshortcutkey ( keys %{$par2script::globals::definitions{"Shortcut"}} )
295 if ( $par2script::globals::definitions{"Shortcut"}->{$internshortcutkey}->{'ShortcutID'} eq $shortcutkey )
297 put_oneitem_into_script($script, "Shortcut", $par2script::globals::definitions{"Shortcut"}, $internshortcutkey);
306 ######################################################
307 # Collecting all children of a specified parent
308 ######################################################
310 sub collect_children
312 my ( $itemhash, $parent, $order ) = @_;
314 my $item;
315 foreach $item ( keys %{$itemhash} )
317 if ( $itemhash->{$item}->{'ParentID'} eq $parent )
319 push(@{$order}, $item);
320 my $newparent = $item;
321 collect_children($itemhash, $newparent, $order);
326 ######################################################
327 # Adding script content for the sorted items
328 ######################################################
330 sub write_sorted_items
332 my ( $script, $oneitem ) = @_;
334 my $itemhash = $par2script::globals::definitions{$oneitem};
336 my @itemorder = ();
337 my @startparents = ();
339 if ( $oneitem eq "Module" ) { push(@startparents, ""); }
340 elsif ( $oneitem eq "Directory" ) { push(@startparents, "PREDEFINED_PROGDIR"); }
341 else { die "ERROR: No root parent defined for item type $oneitem !\n"; }
343 # supporting more than one toplevel item
344 my $parent;
345 foreach $parent ( @startparents ) { collect_children($itemhash, $parent, \@itemorder); }
347 my $itemkey;
348 foreach $itemkey ( @itemorder ) { put_oneitem_into_script($script, $oneitem, $itemhash, $itemkey); }
351 #######################################################################
352 # Collecting all assigned gids of the type "item" from the modules
353 # in the par files. Using a hash!
354 #######################################################################
356 sub collect_assigned_gids
358 my $allmodules = $par2script::globals::definitions{'Module'};
360 my $item;
361 foreach $item ( @par2script::globals::items_assigned_at_modules )
363 if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "collect_assigned_gids"); }
365 my $searchkey = $par2script::globals::searchkeys{$item};
367 my %assignitems = ();
368 my $modulegid = "";
370 foreach $modulegid (keys %{$allmodules} )
372 # print "Module $modulegid\n";
373 # my $content = "";
374 # foreach $content (sort keys %{$allmodules->{$modulegid}}) { print "\t$content = $allmodules->{$modulegid}->{$content};\n"; }
375 # print "End\n";
376 # print "\n";
378 if ( exists($allmodules->{$modulegid}->{$searchkey}) )
380 my $list = $allmodules->{$modulegid}->{$searchkey};
381 if ( $list =~ /^\s*\((.*?)\)\s*(.*?)\s*$/ ) { $list = $1; }
382 else { par2script::exiter::exit_program("ERROR: Invalid module list: $list", "collect_assigned_gids"); }
383 my $allassigneditems = par2script::converter::convert_stringlist_into_array_2($list, ",");
385 my $gid;
386 foreach $gid ( @{$allassigneditems} )
388 if ( exists($assignitems{$gid}) ) { $assignitems{$gid} = $assignitems{$gid} + 1; }
389 else { $assignitems{$gid} = 1; }
394 $par2script::globals::assignedgids{$item} = \%assignitems;
398 ##################################################
399 # Collecting the content of all par files.
400 # Then the files do not need to be opened twice.
401 ##################################################
403 sub read_all_parfiles
405 my ($parfiles) = @_;
407 my @parfilecontent = ();
408 my $parfilename;
410 foreach $parfilename ( @{$parfiles} )
412 my $parfile = par2script::files::read_file($parfilename);
413 foreach ( @{$parfile} ) { push(@parfilecontent, $_); }
414 push(@parfilecontent, "\n");
417 return \@parfilecontent;