1*cdf0e10cSrcweir#*************************************************************************
2*cdf0e10cSrcweir#
3*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir#
5*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir#
7*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir#
9*cdf0e10cSrcweir# This file is part of OpenOffice.org.
10*cdf0e10cSrcweir#
11*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir# only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir#
15*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir#
21*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir# version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir# <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir# for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir#
26*cdf0e10cSrcweir#*************************************************************************
27*cdf0e10cSrcweir
28*cdf0e10cSrcweirpackage par2script::module;
29*cdf0e10cSrcweir
30*cdf0e10cSrcweiruse par2script::converter;
31*cdf0e10cSrcweiruse par2script::exiter;
32*cdf0e10cSrcweir
33*cdf0e10cSrcweir###########################################
34*cdf0e10cSrcweir# Removing undefined gids
35*cdf0e10cSrcweir# from modules
36*cdf0e10cSrcweir###########################################
37*cdf0e10cSrcweir
38*cdf0e10cSrcweirsub remove_from_modules
39*cdf0e10cSrcweir{
40*cdf0e10cSrcweir	my ($gid, $item) = @_;
41*cdf0e10cSrcweir
42*cdf0e10cSrcweir	my $counter = 0;
43*cdf0e10cSrcweir
44*cdf0e10cSrcweir	if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "remove_from_modules"); }
45*cdf0e10cSrcweir	my $searchkey = $par2script::globals::searchkeys{$item};
46*cdf0e10cSrcweir
47*cdf0e10cSrcweir	my $allmodules = $par2script::globals::definitions{'Module'};
48*cdf0e10cSrcweir
49*cdf0e10cSrcweir	my $onemodule;
50*cdf0e10cSrcweir	foreach $onemodule (keys %{$allmodules})
51*cdf0e10cSrcweir	{
52*cdf0e10cSrcweir		if (( exists($allmodules->{$onemodule}->{$searchkey}) ) && ( $allmodules->{$onemodule}->{$searchkey} =~ /\b$gid\b/ ))
53*cdf0e10cSrcweir		{
54*cdf0e10cSrcweir			my $infoline = "WARNING: Removing $gid because of missing definition\n";
55*cdf0e10cSrcweir			# print $infoline;
56*cdf0e10cSrcweir			push(@par2script::globals::logfileinfo, $infoline);
57*cdf0e10cSrcweir
58*cdf0e10cSrcweir			$allmodules->{$onemodule}->{$searchkey} =~ s/\b$gid\b//;
59*cdf0e10cSrcweir			$allmodules->{$onemodule}->{$searchkey} =~ s/\,\s*\,/\,/;
60*cdf0e10cSrcweir			$allmodules->{$onemodule}->{$searchkey} =~ s/\(\s*\,\s*/\(/;
61*cdf0e10cSrcweir			$allmodules->{$onemodule}->{$searchkey} =~ s/\s*\,\s*\)/\)/;
62*cdf0e10cSrcweir
63*cdf0e10cSrcweir			if (( $allmodules->{$onemodule}->{$searchkey} =~ /\(\s*\,\s*\)/ ) ||
64*cdf0e10cSrcweir				( $allmodules->{$onemodule}->{$searchkey} =~ /\(\s*\)/ ))
65*cdf0e10cSrcweir			{
66*cdf0e10cSrcweir				delete($allmodules->{$onemodule}->{$searchkey});
67*cdf0e10cSrcweir			}
68*cdf0e10cSrcweir
69*cdf0e10cSrcweir			$counter++;
70*cdf0e10cSrcweir		}
71*cdf0e10cSrcweir	}
72*cdf0e10cSrcweir
73*cdf0e10cSrcweir	return $counter;
74*cdf0e10cSrcweir}
75*cdf0e10cSrcweir
76*cdf0e10cSrcweir###########################################
77*cdf0e10cSrcweir# Removing undefined gids automatically
78*cdf0e10cSrcweir# from modules
79*cdf0e10cSrcweir###########################################
80*cdf0e10cSrcweir
81*cdf0e10cSrcweirsub remove_undefined_gids_from_modules
82*cdf0e10cSrcweir{
83*cdf0e10cSrcweir	# If assigned gids for "File", "Directory" or "Unixlink" are not defined,
84*cdf0e10cSrcweir	# they are automatically removed from the module
85*cdf0e10cSrcweir
86*cdf0e10cSrcweir	foreach $item ( @par2script::globals::items_assigned_at_modules )
87*cdf0e10cSrcweir	{
88*cdf0e10cSrcweir		my $assignedgids = $par2script::globals::assignedgids{$item};
89*cdf0e10cSrcweir		my $definedgids = $par2script::globals::definitions{$item};
90*cdf0e10cSrcweir
91*cdf0e10cSrcweir		my $gid;
92*cdf0e10cSrcweir		foreach $gid ( keys %{$assignedgids} )
93*cdf0e10cSrcweir		{
94*cdf0e10cSrcweir			if ( ! exists( $definedgids->{$gid} ))
95*cdf0e10cSrcweir			{
96*cdf0e10cSrcweir				# deleting entry in module definition
97*cdf0e10cSrcweir				my $number_of_removals = remove_from_modules($gid, $item);
98*cdf0e10cSrcweir				# decreasing counter in assignments
99*cdf0e10cSrcweir				if ( $assignedgids->{$gid} > $number_of_removals ) { $assignedgids->{$gid} = $assignedgids->{$gid} - $number_of_removals; }
100*cdf0e10cSrcweir				else { delete($assignedgids->{$gid}); }
101*cdf0e10cSrcweir			}
102*cdf0e10cSrcweir		}
103*cdf0e10cSrcweir	}
104*cdf0e10cSrcweir}
105*cdf0e10cSrcweir
106*cdf0e10cSrcweir############################################
107*cdf0e10cSrcweir# Getting the gid of the root module. The
108*cdf0e10cSrcweir# root module has no ParentID or an empty
109*cdf0e10cSrcweir# ParentID.
110*cdf0e10cSrcweir############################################
111*cdf0e10cSrcweir
112*cdf0e10cSrcweirsub get_rootmodule_gid
113*cdf0e10cSrcweir{
114*cdf0e10cSrcweir	my $rootgid = "";
115*cdf0e10cSrcweir	my $foundroot = 0;
116*cdf0e10cSrcweir
117*cdf0e10cSrcweir	my $allmodules = $par2script::globals::definitions{'Module'};
118*cdf0e10cSrcweir
119*cdf0e10cSrcweir	my $modulegid = "";
120*cdf0e10cSrcweir	foreach $modulegid (keys %{$allmodules} )
121*cdf0e10cSrcweir	{
122*cdf0e10cSrcweir		# print "Module $modulegid\n";
123*cdf0e10cSrcweir		# my $content = "";
124*cdf0e10cSrcweir		# foreach $content (sort keys %{$allmodules->{$modulegid}}) { print "\t$content = $allmodules->{$modulegid}->{$content};\n"; }
125*cdf0e10cSrcweir		# print "End\n";
126*cdf0e10cSrcweir		# print "\n";
127*cdf0e10cSrcweir
128*cdf0e10cSrcweir		if (( ! exists($allmodules->{$modulegid}->{'ParentID'})) || ( $allmodules->{$modulegid}->{'ParentID'} eq "" ))
129*cdf0e10cSrcweir		{
130*cdf0e10cSrcweir			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"); }
131*cdf0e10cSrcweir			$rootgid = $modulegid;
132*cdf0e10cSrcweir			$foundroot = 1;
133*cdf0e10cSrcweir		}
134*cdf0e10cSrcweir	}
135*cdf0e10cSrcweir
136*cdf0e10cSrcweir	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"); }
137*cdf0e10cSrcweir
138*cdf0e10cSrcweir	return $rootgid;
139*cdf0e10cSrcweir}
140*cdf0e10cSrcweir
141*cdf0e10cSrcweir####################################
142*cdf0e10cSrcweir# Adding defined items without
143*cdf0e10cSrcweir# assignment to the root module.
144*cdf0e10cSrcweir####################################
145*cdf0e10cSrcweir
146*cdf0e10cSrcweirsub add_to_root_module
147*cdf0e10cSrcweir{
148*cdf0e10cSrcweir	# If defined gids for "File", "Directory" or "Unixlink" are not assigned,
149*cdf0e10cSrcweir	# they are automatically assigned to the root module
150*cdf0e10cSrcweir
151*cdf0e10cSrcweir	my $rootmodulegid = get_rootmodule_gid();
152*cdf0e10cSrcweir
153*cdf0e10cSrcweir	my $item;
154*cdf0e10cSrcweir	foreach $item ( @par2script::globals::items_assigned_at_modules )
155*cdf0e10cSrcweir	{
156*cdf0e10cSrcweir		my $assignedgids = $par2script::globals::assignedgids{$item};
157*cdf0e10cSrcweir		my $definedgids = $par2script::globals::definitions{$item};
158*cdf0e10cSrcweir
159*cdf0e10cSrcweir		my $gidstring = "";
160*cdf0e10cSrcweir
161*cdf0e10cSrcweir		# Perhaps there are already items assigned to the root
162*cdf0e10cSrcweir		if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "remove_from_modules"); }
163*cdf0e10cSrcweir		my $modulekey = $par2script::globals::searchkeys{$item};
164*cdf0e10cSrcweir		if ( exists($par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey}) )
165*cdf0e10cSrcweir		{
166*cdf0e10cSrcweir			$gidstring = $par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey};
167*cdf0e10cSrcweir			$gidstring =~ s/\(//;
168*cdf0e10cSrcweir			$gidstring =~ s/\)//;
169*cdf0e10cSrcweir		}
170*cdf0e10cSrcweir
171*cdf0e10cSrcweir		my $gid;
172*cdf0e10cSrcweir		foreach $gid ( keys %{$definedgids} )
173*cdf0e10cSrcweir		{
174*cdf0e10cSrcweir			if ( ! exists( $assignedgids->{$gid} ))
175*cdf0e10cSrcweir			{
176*cdf0e10cSrcweir				if ( $gidstring eq "" )
177*cdf0e10cSrcweir				{
178*cdf0e10cSrcweir					$gidstring = $gid;
179*cdf0e10cSrcweir				}
180*cdf0e10cSrcweir				else
181*cdf0e10cSrcweir				{
182*cdf0e10cSrcweir					$gidstring = "$gidstring,$gid";
183*cdf0e10cSrcweir				}
184*cdf0e10cSrcweir
185*cdf0e10cSrcweir				$assignedgids->{$gid} = 1;
186*cdf0e10cSrcweir			}
187*cdf0e10cSrcweir		}
188*cdf0e10cSrcweir
189*cdf0e10cSrcweir		if ( $gidstring ne "" )
190*cdf0e10cSrcweir		{
191*cdf0e10cSrcweir			$gidstring = "\($gidstring\)";
192*cdf0e10cSrcweir			$par2script::globals::definitions{'Module'}->{$rootmodulegid}->{$modulekey} = $gidstring;
193*cdf0e10cSrcweir		}
194*cdf0e10cSrcweir	}
195*cdf0e10cSrcweir}
196*cdf0e10cSrcweir
197*cdf0e10cSrcweir###################################################
198*cdf0e10cSrcweir# Including \n in a very long string
199*cdf0e10cSrcweir###################################################
200*cdf0e10cSrcweir
201*cdf0e10cSrcweirsub include_linebreaks
202*cdf0e10cSrcweir{
203*cdf0e10cSrcweir	my ($allgidstring) = @_;
204*cdf0e10cSrcweir
205*cdf0e10cSrcweir	my $newline = "";
206*cdf0e10cSrcweir	my $newlength = 0;
207*cdf0e10cSrcweir
208*cdf0e10cSrcweir	$allgidstring =~ s/\(//;
209*cdf0e10cSrcweir	$allgidstring =~ s/\)//;
210*cdf0e10cSrcweir
211*cdf0e10cSrcweir	my $allgids = par2script::converter::convert_stringlist_into_array_2($allgidstring, ",");
212*cdf0e10cSrcweir
213*cdf0e10cSrcweir	if ( $#{$allgids} > -1 )
214*cdf0e10cSrcweir	{
215*cdf0e10cSrcweir		my $onegid;
216*cdf0e10cSrcweir		foreach $onegid ( @{$allgids} )
217*cdf0e10cSrcweir		{
218*cdf0e10cSrcweir			$newline = "$newline$onegid,";
219*cdf0e10cSrcweir			$newlength = $newlength + length($onegid) + 1; # +1 for the comma
220*cdf0e10cSrcweir
221*cdf0e10cSrcweir			if ( $newlength > 80 )
222*cdf0e10cSrcweir			{
223*cdf0e10cSrcweir				$newline = $newline . "\n\t\t\t\t";
224*cdf0e10cSrcweir				$newlength = 0;
225*cdf0e10cSrcweir			}
226*cdf0e10cSrcweir		}
227*cdf0e10cSrcweir	}
228*cdf0e10cSrcweir
229*cdf0e10cSrcweir	$newline =~ s/,\s*$//;
230*cdf0e10cSrcweir	$newline = "($newline)";
231*cdf0e10cSrcweir
232*cdf0e10cSrcweir	return $newline;
233*cdf0e10cSrcweir}
234*cdf0e10cSrcweir
235*cdf0e10cSrcweir###################################################
236*cdf0e10cSrcweir# Shorten the lines that belong to modules, if
237*cdf0e10cSrcweir# the length of the line is greater 100
238*cdf0e10cSrcweir###################################################
239*cdf0e10cSrcweir
240*cdf0e10cSrcweirsub shorten_lines_at_modules
241*cdf0e10cSrcweir{
242*cdf0e10cSrcweir	my $item;
243*cdf0e10cSrcweir	foreach $item ( @par2script::globals::items_assigned_at_modules )
244*cdf0e10cSrcweir	{
245*cdf0e10cSrcweir		if ( ! exists($par2script::globals::searchkeys{$item}) ) { par2script::exiter::exit_program("ERROR: Unknown type \"$item\" at modules.", "shorten_lines_at_modules"); }
246*cdf0e10cSrcweir		my $searchkey = $par2script::globals::searchkeys{$item};
247*cdf0e10cSrcweir
248*cdf0e10cSrcweir		my $allmodules = $par2script::globals::definitions{'Module'};
249*cdf0e10cSrcweir
250*cdf0e10cSrcweir		my $onemodule;
251*cdf0e10cSrcweir		foreach $onemodule (keys %{$allmodules})
252*cdf0e10cSrcweir		{
253*cdf0e10cSrcweir			if (( exists($allmodules->{$onemodule}->{$searchkey}) ) &&
254*cdf0e10cSrcweir				( length($allmodules->{$onemodule}->{$searchkey}) > 100 ))
255*cdf0e10cSrcweir			{
256*cdf0e10cSrcweir				# including "\n\t\t\t\t"
257*cdf0e10cSrcweir				my $newstring = include_linebreaks($allmodules->{$onemodule}->{$searchkey});
258*cdf0e10cSrcweir				$allmodules->{$onemodule}->{$searchkey} = $newstring;
259*cdf0e10cSrcweir			}
260*cdf0e10cSrcweir		}
261*cdf0e10cSrcweir	}
262*cdf0e10cSrcweir}
263*cdf0e10cSrcweir
264*cdf0e10cSrcweir1;
265