xref: /trunk/main/solenv/bin/modules/pre2par/work.pm (revision cdf0e10c)
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
28
29package pre2par::work;
30
31use pre2par::exiter;
32use pre2par::remover;
33use pre2par::pathanalyzer;
34
35############################################
36# pre2par working module
37############################################
38
39############################################
40# procedure to split a line, that contains
41# more than one par file lines
42############################################
43
44sub split_line
45{
46	my ($line, $parfile) = @_;
47
48	while ( $line =~ /^((?:[^"]|\"(?:[^"\\]|\\.)*\")*?\;\s+)\s*(.*)$/ )
49	{
50		my $oneline = $1;
51		$line = $2;
52		pre2par::remover::remove_leading_and_ending_whitespaces(\$oneline);
53		$oneline = $oneline . "\n";
54		push(@{$parfile}, $oneline);
55
56		if ( $line =~ /^\s*End\s+(\w+.*$)/i )
57		{
58			$line = $1;
59			push(@{$parfile}, "End\n\n");
60		}
61	}
62
63	# the last line
64
65	pre2par::remover::remove_leading_and_ending_whitespaces(\$line);
66	$line = $line . "\n";
67	push(@{$parfile}, $line);
68
69	if ( $line =~ /^\s*End\s*$/i ) { push(@{$parfile}, "\n"); }
70}
71
72###################################################################
73# Preprocessing the pre file to split all lines with semicolon
74###################################################################
75
76sub preprocess_macros
77{
78	my ($prefile) = @_;
79
80	my @newprefile = ();
81
82	for ( my $i = 0; $i <= $#{$prefile}; $i++ )
83	{
84		my $oneline = ${$prefile}[$i];
85		if ( $oneline =~ /\;\s*\w+/ )
86		{
87			split_line($oneline, \@newprefile);
88		}
89		else
90		{
91			push(@newprefile, $oneline);
92		}
93	}
94
95	return \@newprefile;
96}
97
98############################################
99# main working procedure
100############################################
101
102sub convert
103{
104	my ($prefile) = @_;
105
106	my @parfile = ();
107
108	my $iscodesection = 0;
109	my $ismultiliner = 0;
110	my $globalline = "";
111
112	# Preprocessing the pre file to split all lines with semicolon
113	$prefile = preprocess_macros($prefile);
114
115	for ( my $i = 0; $i <= $#{$prefile}; $i++ )
116	{
117		my $oneline = ${$prefile}[$i];
118
119		if ($iscodesection)
120		{
121			if ( $oneline =~ /^\s*\}\;\s*$/ )
122			{
123				$iscodesection = 0;
124			}
125			else	# nothing to do for code inside a code section
126			{
127				push(@parfile, $oneline);
128				next;
129			 }
130		}
131
132		if ( $oneline =~ /^\s*$/ ) { next; }
133
134		if ( $oneline =~ /^\s*Code\s+\=\s+\{/ )
135		{
136			$iscodesection = 1;
137		}
138
139		pre2par::remover::remove_leading_and_ending_whitespaces(\$oneline);
140
141		my $insertemptyline = 0;
142
143		if ( $oneline =~ /^\s*End\s*$/i ) { $insertemptyline = 1; }
144
145		# Sometimes the complete file is in one line, then the gid line has to be separated
146
147		if ( $oneline =~ /^\s*(\w+\s+\w+)\s+(\w+\s+\=.*$)/ )	# three words before the equal sign
148		{
149			my $gidline = $1;
150			$oneline = $2;
151			$gidline = $gidline . "\n";
152
153			push(@parfile, $gidline);
154		}
155
156		if ( $oneline =~ /\;\s*\w+/ )
157		{
158			split_line($oneline, \@parfile);
159			next;
160		}
161
162		# searching for lines with brackets, like Customs = { ..., which can be parted above several lines
163
164		if ( $oneline =~ /^\s*\w+\s+\=\s*\(.*\)\s*\;\s*$/ )		# only one line
165		{
166			if (( ! ( $oneline =~ /^\s*Assignment\d+\s*\=/ )) && ( ! ( $oneline =~ /^\s*PatchAssignment\d+\s*\=/ )))
167			{
168				$oneline =~ s/\s//g;		# removing whitespaces in lists
169				$oneline =~ s/\=/\ \=\ /;	# adding whitespace around equals sign
170			}
171		}
172
173		if ( $oneline =~ /^\s*\w+\s+\=\s*$/ )
174		{
175			$oneline =~ s/\s*$//;
176			pre2par::exiter::exit_program("Error: Illegal syntax, no line break after eqals sign allowed. Line: \"$oneline\"", "convert");
177		}
178
179		if (( $oneline =~ /^\s*\w+\s+\=\s*\(/ ) && (!( $oneline =~ /\)\s*\;\s*$/ )))	 # several lines
180		{
181			$ismultiliner = 1;
182			$oneline =~ s/\s//g;
183			$globalline .= $oneline;
184			next;						# not including yet
185		}
186
187		if ( $ismultiliner )
188		{
189			$oneline =~ s/\s//g;
190			$globalline .= $oneline;
191
192			if ( $oneline =~ /\)\s*\;\s*$/ ) {	$ismultiliner = 0; }
193
194			if (! ( $ismultiliner ))
195			{
196				$globalline =~ s/\=/\ \=\ /;	# adding whitespace around equals sign
197				$globalline .= "\n";
198				push(@parfile, $globalline);
199				$globalline = "";
200			}
201
202			next;
203		}
204
205		$oneline = $oneline . "\n";
206
207		$oneline =~ s/\s*\=\s*/ \= /;	# nice, to have only one whitespace around equal signs
208
209        # Concatenate adjacent string literals:
210        while ($oneline =~
211               s/^((?:[^"]*
212                      \"(?:[^\\"]|\\.)*\"
213                      (?:[^"]*[^[:blank:]"][^"]*\"(?:[^\\"]|\\.)*\")*)*
214                   [^"]*
215                   \"(?:[^\\"]|\\.)*)
216                 \"[[:blank:]]*\"
217                 ((?:[^\\"]|\\.)*\")
218                /\1\2/x)
219        {}
220
221		push(@parfile, $oneline);
222
223		if ($insertemptyline) { push(@parfile, "\n"); }
224
225	}
226
227	return \@parfile;
228}
229
230############################################
231# formatting the par file
232############################################
233
234sub formatter
235{
236	my ($parfile) = @_;
237
238	my $iscodesection = 0;
239
240	my $tabcounter = 0;
241	my $isinsideitem = 0;
242	my $currentitem;
243
244	for ( my $i = 0; $i <= $#{$parfile}; $i++ )
245	{
246		my $oneline = ${$parfile}[$i];
247		my $isitemline = 0;
248
249		if (! $isinsideitem )
250		{
251			for ( my $j = 0; $j <= $#pre2par::globals::allitems; $j++ )
252			{
253				if ( $oneline =~ /^\s*$pre2par::globals::allitems[$j]\s+\w+\s*$/ )
254				{
255					$currentitem = $pre2par::globals::allitems[$j];
256					$isitemline = 1;
257					$isinsideitem = 1;
258					$tabcounter = 0;
259					last;
260				}
261			}
262		}
263
264		if ( $isitemline )
265		{
266			next;	# nothing to do
267		}
268
269		if ( $oneline =~ /^\s*end\s*$/i )
270		{
271			$isinsideitem = 0;
272			$tabcounter--;
273		}
274
275		if ( $isinsideitem )
276		{
277			$oneline = "\t" . $oneline;
278			${$parfile}[$i] = $oneline;
279		}
280	}
281}
282
283###################################################
284# Returning the language file name
285###################################################
286
287sub getlangfilename
288{
289	return $pre2par::globals::langfilename;
290}
291
292###################################################
293# Creating the ulf file name from the
294# corresponding pre file name
295###################################################
296
297sub getulffilename
298{
299	my ($prefilename) = @_;
300
301	my $ulffilename = $prefilename;
302	$ulffilename =~ s/\.pre\s*$/\.ulf/;
303	pre2par::pathanalyzer::make_absolute_filename_to_relative_filename(\$ulffilename);
304
305	return $ulffilename;
306}
307
308############################################
309# Checking if a file exists
310############################################
311
312sub fileexists
313{
314	my ($langfilename) = @_;
315
316	my $fileexists = 0;
317
318	if( -f $langfilename ) { $fileexists = 1; }
319
320	return $fileexists;
321}
322
323############################################
324# Checking the existence of ulf and
325# jlf/mlf files
326############################################
327
328sub check_existence_of_langfiles
329{
330	my ($langfilename, $ulffilename) = @_;
331
332	my $do_localize = 0;
333
334	if (( fileexists($ulffilename) ) && ( ! fileexists($langfilename) )) { pre2par::exiter::exit_program("Error: Did not find language file $langfilename", "check_existence_of_langfiles"); }
335	if (( fileexists($ulffilename) ) && ( fileexists($langfilename) )) { $do_localize = 1; }
336
337	return $do_localize;
338}
339
340############################################
341# Checking that the pre file has content
342############################################
343
344sub check_content
345{
346	my ($filecontent, $filename) = @_;
347
348	if ( $#{$filecontent} < 0 ) { pre2par::exiter::exit_program("Error: $filename has no content!", "check_content"); }
349}
350
351############################################
352# Checking content of par files.
353# Currently only size.
354############################################
355
356sub diff_content
357{
358	my ($content1, $content2, $filename) = @_;
359
360	if ( $#{$content1} != $#{$content2} ) { pre2par::exiter::exit_program("Error: $filename was not saved correctly!", "diff_content"); }
361}
362
3631;
364