Update ooo320-m1
[ooovba.git] / solenv / bin / modules / pre2par / work.pm
blob64ea48b72dda06ef9d8a8ac3eda07227dec5f5f4
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.13 $
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 pre2par::work;
35 use pre2par::exiter;
36 use pre2par::remover;
37 use pre2par::pathanalyzer;
39 ############################################
40 # pre2par working module
41 ############################################
43 ############################################
44 # procedure to split a line, that contains
45 # more than one par file lines
46 ############################################
48 sub split_line
50 my ($line, $parfile) = @_;
52 while ( $line =~ /^((?:[^"]|\"(?:[^"\\]|\\.)*\")*?\;\s+)\s*(.*)$/ )
54 my $oneline = $1;
55 $line = $2;
56 pre2par::remover::remove_leading_and_ending_whitespaces(\$oneline);
57 $oneline = $oneline . "\n";
58 push(@{$parfile}, $oneline);
60 if ( $line =~ /^\s*End\s+(\w+.*$)/i )
62 $line = $1;
63 push(@{$parfile}, "End\n\n");
67 # the last line
69 pre2par::remover::remove_leading_and_ending_whitespaces(\$line);
70 $line = $line . "\n";
71 push(@{$parfile}, $line);
73 if ( $line =~ /^\s*End\s*$/i ) { push(@{$parfile}, "\n"); }
76 ###################################################################
77 # Preprocessing the pre file to split all lines with semicolon
78 ###################################################################
80 sub preprocess_macros
82 my ($prefile) = @_;
84 my @newprefile = ();
86 for ( my $i = 0; $i <= $#{$prefile}; $i++ )
88 my $oneline = ${$prefile}[$i];
89 if ( $oneline =~ /\;\s*\w+/ )
91 split_line($oneline, \@newprefile);
93 else
95 push(@newprefile, $oneline);
99 return \@newprefile;
102 ############################################
103 # main working procedure
104 ############################################
106 sub convert
108 my ($prefile) = @_;
110 my @parfile = ();
112 my $iscodesection = 0;
113 my $ismultiliner = 0;
114 my $globalline = "";
116 # Preprocessing the pre file to split all lines with semicolon
117 $prefile = preprocess_macros($prefile);
119 for ( my $i = 0; $i <= $#{$prefile}; $i++ )
121 my $oneline = ${$prefile}[$i];
123 if ($iscodesection)
125 if ( $oneline =~ /^\s*\}\;\s*$/ )
127 $iscodesection = 0;
129 else # nothing to do for code inside a code section
131 push(@parfile, $oneline);
132 next;
136 if ( $oneline =~ /^\s*$/ ) { next; }
138 if ( $oneline =~ /^\s*Code\s+\=\s+\{/ )
140 $iscodesection = 1;
143 pre2par::remover::remove_leading_and_ending_whitespaces(\$oneline);
145 my $insertemptyline = 0;
147 if ( $oneline =~ /^\s*End\s*$/i ) { $insertemptyline = 1; }
149 # Sometimes the complete file is in one line, then the gid line has to be separated
151 if ( $oneline =~ /^\s*(\w+\s+\w+)\s+(\w+\s+\=.*$)/ ) # three words before the equal sign
153 my $gidline = $1;
154 $oneline = $2;
155 $gidline = $gidline . "\n";
157 push(@parfile, $gidline);
160 if ( $oneline =~ /\;\s*\w+/ )
162 split_line($oneline, \@parfile);
163 next;
166 # searching for lines with brackets, like Customs = { ..., which can be parted above several lines
168 if ( $oneline =~ /^\s*\w+\s+\=\s*\(.*\)\s*\;\s*$/ ) # only one line
170 if (( ! ( $oneline =~ /^\s*Assignment\d+\s*\=/ )) && ( ! ( $oneline =~ /^\s*PatchAssignment\d+\s*\=/ )))
172 $oneline =~ s/\s//g; # removing whitespaces in lists
173 $oneline =~ s/\=/\ \=\ /; # adding whitespace around equals sign
177 if ( $oneline =~ /^\s*\w+\s+\=\s*$/ )
179 $oneline =~ s/\s*$//;
180 pre2par::exiter::exit_program("Error: Illegal syntax, no line break after eqals sign allowed. Line: \"$oneline\"", "convert");
183 if (( $oneline =~ /^\s*\w+\s+\=\s*\(/ ) && (!( $oneline =~ /\)\s*\;\s*$/ ))) # several lines
185 $ismultiliner = 1;
186 $oneline =~ s/\s//g;
187 $globalline .= $oneline;
188 next; # not including yet
191 if ( $ismultiliner )
193 $oneline =~ s/\s//g;
194 $globalline .= $oneline;
196 if ( $oneline =~ /\)\s*\;\s*$/ ) { $ismultiliner = 0; }
198 if (! ( $ismultiliner ))
200 $globalline =~ s/\=/\ \=\ /; # adding whitespace around equals sign
201 $globalline .= "\n";
202 push(@parfile, $globalline);
203 $globalline = "";
206 next;
209 $oneline = $oneline . "\n";
211 $oneline =~ s/\s*\=\s*/ \= /; # nice, to have only one whitespace around equal signs
213 # Concatenate adjacent string literals:
214 while ($oneline =~
215 s/^((?:[^"]*
216 \"(?:[^\\"]|\\.)*\"
217 (?:[^"]*[^[:blank:]"][^"]*\"(?:[^\\"]|\\.)*\")*)*
218 [^"]*
219 \"(?:[^\\"]|\\.)*)
220 \"[[:blank:]]*\"
221 ((?:[^\\"]|\\.)*\")
222 /\1\2/x)
225 push(@parfile, $oneline);
227 if ($insertemptyline) { push(@parfile, "\n"); }
231 return \@parfile;
234 ############################################
235 # formatting the par file
236 ############################################
238 sub formatter
240 my ($parfile) = @_;
242 my $iscodesection = 0;
244 my $tabcounter = 0;
245 my $isinsideitem = 0;
246 my $currentitem;
248 for ( my $i = 0; $i <= $#{$parfile}; $i++ )
250 my $oneline = ${$parfile}[$i];
251 my $isitemline = 0;
253 if (! $isinsideitem )
255 for ( my $j = 0; $j <= $#pre2par::globals::allitems; $j++ )
257 if ( $oneline =~ /^\s*$pre2par::globals::allitems[$j]\s+\w+\s*$/ )
259 $currentitem = $pre2par::globals::allitems[$j];
260 $isitemline = 1;
261 $isinsideitem = 1;
262 $tabcounter = 0;
263 last;
268 if ( $isitemline )
270 next; # nothing to do
273 if ( $oneline =~ /^\s*end\s*$/i )
275 $isinsideitem = 0;
276 $tabcounter--;
279 if ( $isinsideitem )
281 $oneline = "\t" . $oneline;
282 ${$parfile}[$i] = $oneline;
287 ###################################################
288 # Returning the language file name
289 ###################################################
291 sub getlangfilename
293 return $pre2par::globals::langfilename;
296 ###################################################
297 # Creating the ulf file name from the
298 # corresponding pre file name
299 ###################################################
301 sub getulffilename
303 my ($prefilename) = @_;
305 my $ulffilename = $prefilename;
306 $ulffilename =~ s/\.pre\s*$/\.ulf/;
307 pre2par::pathanalyzer::make_absolute_filename_to_relative_filename(\$ulffilename);
309 return $ulffilename;
312 ############################################
313 # Checking if a file exists
314 ############################################
316 sub fileexists
318 my ($langfilename) = @_;
320 my $fileexists = 0;
322 if( -f $langfilename ) { $fileexists = 1; }
324 return $fileexists;
327 ############################################
328 # Checking the existence of ulf and
329 # jlf/mlf files
330 ############################################
332 sub check_existence_of_langfiles
334 my ($langfilename, $ulffilename) = @_;
336 my $do_localize = 0;
338 if (( fileexists($ulffilename) ) && ( ! fileexists($langfilename) )) { pre2par::exiter::exit_program("Error: Did not find language file $langfilename", "check_existence_of_langfiles"); }
339 if (( fileexists($ulffilename) ) && ( fileexists($langfilename) )) { $do_localize = 1; }
341 return $do_localize;
344 ############################################
345 # Checking that the pre file has content
346 ############################################
348 sub check_content
350 my ($filecontent, $filename) = @_;
352 if ( $#{$filecontent} < 0 ) { pre2par::exiter::exit_program("Error: $filename has no content!", "check_content"); }
355 ############################################
356 # Checking content of par files.
357 # Currently only size.
358 ############################################
360 sub diff_content
362 my ($content1, $content2, $filename) = @_;
364 if ( $#{$content1} != $#{$content2} ) { pre2par::exiter::exit_program("Error: $filename was not saved correctly!", "diff_content"); }