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