Branch libreoffice-5-0-4
[LibreOffice.git] / solenv / bin / modules / par2script / module.pm
blobef916650b42fe318a7107ec6abc998f5677bb7ce
2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package par2script::module;
21 use par2script::converter;
22 use par2script::exiter;
24 ###########################################
25 # Removing undefined gids
26 # from modules
27 ###########################################
29 sub remove_from_modules
31 my ($gid, $item) = @_;
33 my $counter = 0;
35 if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "remove_from_modules"); }
36 my $searchkey = $par2script::globals::searchkeys{$item};
38 my $allmodules = $par2script::globals::definitions{'Module'};
40 my $onemodule;
41 foreach $onemodule (keys %{$allmodules})
43 if (( exists($allmodules->{$onemodule}->{$searchkey}) ) && ( $allmodules->{$onemodule}->{$searchkey} =~ /\b$gid\b/ ))
45 my $infoline = "WARNING: Removing $gid because of missing definition\n";
46 # print $infoline;
47 push(@par2script::globals::logfileinfo, $infoline);
49 $allmodules->{$onemodule}->{$searchkey} =~ s/\b$gid\b//;
50 $allmodules->{$onemodule}->{$searchkey} =~ s/\,\s*\,/\,/;
51 $allmodules->{$onemodule}->{$searchkey} =~ s/\(\s*\,\s*/\(/;
52 $allmodules->{$onemodule}->{$searchkey} =~ s/\s*\,\s*\)/\)/;
54 if (( $allmodules->{$onemodule}->{$searchkey} =~ /\(\s*\,\s*\)/ ) ||
55 ( $allmodules->{$onemodule}->{$searchkey} =~ /\(\s*\)/ ))
57 delete($allmodules->{$onemodule}->{$searchkey});
60 $counter++;
64 return $counter;
67 ###########################################
68 # Removing undefined gids automatically
69 # from modules
70 ###########################################
72 sub remove_undefined_gids_from_modules
74 # If assigned gids for "File", "Directory" or "Unixlink" are not defined,
75 # they are automatically removed from the module
77 foreach $item ( @par2script::globals::items_assigned_at_modules )
79 my $assignedgids = $par2script::globals::assignedgids{$item};
80 my $definedgids = $par2script::globals::definitions{$item};
82 my $gid;
83 foreach $gid ( keys %{$assignedgids} )
85 if ( ! exists( $definedgids->{$gid} ))
87 # deleting entry in module definition
88 my $number_of_removals = remove_from_modules($gid, $item);
89 # decreasing counter in assignments
90 if ( $assignedgids->{$gid} > $number_of_removals ) { $assignedgids->{$gid} = $assignedgids->{$gid} - $number_of_removals; }
91 else { delete($assignedgids->{$gid}); }
97 ############################################
98 # Getting the gid of the root module. The
99 # root module has no ParentID or an empty
100 # ParentID.
101 ############################################
103 sub get_rootmodule_gid
105 my $rootgid = "";
106 my $foundroot = 0;
108 my $allmodules = $par2script::globals::definitions{'Module'};
110 my $modulegid = "";
111 foreach $modulegid (keys %{$allmodules} )
113 # print "Module $modulegid\n";
114 # my $content = "";
115 # foreach $content (sort keys %{$allmodules->{$modulegid}}) { print "\t$content = $allmodules->{$modulegid}->{$content};\n"; }
116 # print "End\n";
117 # print "\n";
119 if (( ! exists($allmodules->{$modulegid}->{'ParentID'})) || ( $allmodules->{$modulegid}->{'ParentID'} eq "" ))
121 if ( $foundroot ) { par2script::exiter::exit_program("ERROR: More than one Root module. Only one module without ParentID or with empty ParentID allowed ($rootgid and $modulegid).", "get_rootmodule_gid"); }
122 $rootgid = $modulegid;
123 $foundroot = 1;
127 if ( ! $foundroot ) { par2script::exiter::exit_program("ERROR: Could not find Root module. Did not find module without ParentID or with empty ParentID.", "get_rootmodule_gid"); }
129 return $rootgid;
132 ####################################
133 # Adding defined items without
134 # assignment to the root module.
135 ####################################
137 sub add_to_root_module
139 # If defined gids for "File", "Directory" or "Unixlink" are not assigned,
140 # they are automatically assigned to the root module
142 my $rootmodulegid = get_rootmodule_gid();
144 my $item;
145 foreach $item ( @par2script::globals::items_assigned_at_modules )
147 my $assignedgids = $par2script::globals::assignedgids{$item};
148 my $definedgids = $par2script::globals::definitions{$item};
150 my $gidstring = "";
152 # Perhaps there are already items assigned to the root
153 if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "remove_from_modules"); }
154 my $modulekey = $par2script::globals::searchkeys{$item};
155 if ( exists($par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey}) )
157 $gidstring = $par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey};
158 $gidstring =~ s/\(//;
159 $gidstring =~ s/\)//;
162 my $gid;
163 foreach $gid ( keys %{$definedgids} )
165 if ( ! exists( $assignedgids->{$gid} ))
167 if ( $gidstring eq "" )
169 $gidstring = $gid;
171 else
173 $gidstring = "$gidstring,$gid";
176 $assignedgids->{$gid} = 1;
180 if ( $gidstring ne "" )
182 $gidstring = "\($gidstring\)";
183 $par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey} = $gidstring;
188 ###################################################
189 # Including \n in a very long string
190 ###################################################
192 sub include_linebreaks
194 my ($allgidstring) = @_;
196 my $newline = "";
197 my $newlength = 0;
199 $allgidstring =~ s/\(//;
200 $allgidstring =~ s/\)//;
202 my $allgids = par2script::converter::convert_stringlist_into_array_2($allgidstring, ",");
204 if ( $#{$allgids} > -1 )
206 my $onegid;
207 foreach $onegid ( @{$allgids} )
209 $newline = "$newline$onegid,";
210 $newlength = $newlength + length($onegid) + 1; # +1 for the comma
212 if ( $newlength > 80 )
214 $newline = $newline . "\n\t\t\t\t";
215 $newlength = 0;
220 $newline =~ s/,\s*$//;
221 $newline = "($newline)";
223 return $newline;
226 ###################################################
227 # Shorten the lines that belong to modules, if
228 # the length of the line is greater 100
229 ###################################################
231 sub shorten_lines_at_modules
233 my $item;
234 foreach $item ( @par2script::globals::items_assigned_at_modules )
236 if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "shorten_lines_at_modules"); }
237 my $searchkey = $par2script::globals::searchkeys{$item};
239 my $allmodules = $par2script::globals::definitions{'Module'};
241 my $onemodule;
242 foreach $onemodule (keys %{$allmodules})
244 if (( exists($allmodules->{$onemodule}->{$searchkey}) ) &&
245 ( length($allmodules->{$onemodule}->{$searchkey}) > 100 ))
247 # including "\n\t\t\t\t"
248 my $newstring = include_linebreaks($allmodules->{$onemodule}->{$searchkey});
249 $allmodules->{$onemodule}->{$searchkey} = $newstring;