2 eval 'exec perl -wS $0 ${1+"$@"}'
4 #*************************************************************************
6 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 # Copyright 2008 by Sun Microsystems, Inc.
10 # OpenOffice.org - a multi-platform office productivity suite
12 # $RCSfile: update_tree.pl,v $
16 # This file is part of OpenOffice.org.
18 # OpenOffice.org is free software: you can redistribute it and/or modify
19 # it under the terms of the GNU Lesser General Public License version 3
20 # only, as published by the Free Software Foundation.
22 # OpenOffice.org is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU Lesser General Public License version 3 for more details
26 # (a copy is included in the LICENSE file that accompanied this code).
28 # You should have received a copy of the GNU Lesser General Public License
29 # version 3 along with OpenOffice.org. If not, see
30 # <http://www.openoffice.org/license.html>
31 # for a copy of the LGPLv3 License.
33 #*************************************************************************
37 use File
::Copy qw
/cp mv/;
43 $is_win = "true" , if ( defined $ENV{USE_SHELL
} && $ENV{USE_SHELL
} eq "4nt" );
46 # update the tree files in <platform>/misc/*
50 my $prj = $ENV{ENVPRJ
};
52 my $inpath = $ENV{INPATH
};
53 terminate
() if ( ! defined $inpath );
55 my $destpath = $inpath;
56 my $with_lang = $ENV{WITH_LANG
};
58 if ( defined $ENV{COMMON_OUTDIR
} && $ENV{COMMON_OUTDIR
} ne "" ) {
59 $destpath =~ s/$ENV{OUTPATH}/$ENV{COMMON_OUTDIR}/;
62 # Always use / directory separators
63 $prj =~ s/\\/\//g
if defined($prj);
65 $destpath =~ s/\\/\//g
;
68 if ( ! defined $prj ) {
69 # do someting that works for manual call
70 ($scriptname = `pwd`) =~ s/\n/\/$0/;
71 ($tree_src = $scriptname) =~ s/\/update_tree.pl/\
/..\/source\
/auxiliary/;
72 ($tree_dest = $scriptname) =~ s/\/update_tree.pl/\
/..\/$destpath\
/misc/;
73 ($source_dir = $scriptname) =~ s/\/update_tree.pl/\
/..\/source
/;
74 ($source_dir_xhp = $scriptname) =~ s/\/update_tree.pl/\
/..\/source
/;
76 if ( defined $ENV{TRYSDF
} && defined $ENV{LOCALIZESDF
} )
78 if( defined $ENV{LOCALIZATION_FOUND
} && $ENV{LOCALIZATION_FOUND
} eq "YES" )
80 $source_dir = $ENV{TRYSDF
};
84 $source_dir = $ENV{LOCALIZESDF
};
86 if( $is_win eq "false" ) { $source_dir =~ s/\/auxiliary\/localize
.sdf
$// ; }
87 else { $source_dir =~ s/\\auxiliary\\localize.sdf$// ; }
90 #else {die "ERROR: The env variables TRYSDF LOCALIZATION_FOUND LOCALIZESDF not found ... something is wrong!\n";}
93 $treestrings = "$source_dir/text/shared/tree_strings.xhp";
95 $tree_src = "$prj\/source\/auxiliary";
96 $tree_dest = "$prj\/$destpath\/misc";
97 $source_dir = "$prj\/source";
98 $source_dir_xhp = "$prj\/source";
99 $treestrings = "$source_dir/text/shared/tree_strings.xhp";
101 if ( defined $ENV{TRYSDF
} && defined $ENV{LOCALIZESDF
} )
103 if( defined $ENV{LOCALIZATION_FOUND
} && $ENV{LOCALIZATION_FOUND
} eq "YES" )
105 $source_dir = $ENV{TRYSDF
};
109 $source_dir = $ENV{LOCALIZESDF
};
111 if( $is_win eq "false" ) { $source_dir =~ s/\/auxiliary\/localize
.sdf
$// ; }
112 else { $source_dir =~ s/\\auxiliary\\localize.sdf$// ; }
114 #else {die "ERROR: The env variables TRYSDF LOCALIZATION_FOUND LOCALIZESDF not found ... something is wrong!\n";}
118 # Get the English tree files as master
119 #-------------------------------
120 # Update English from xhp
121 #-------------------------------
123 #-------------------------------
124 # Update localizations from sdf
125 #-------------------------------
127 @langs = split /\s+/, $with_lang;
129 print "################\nUpdating the treefiles for @langs \n";
136 #-------------------------------
139 $td = timediff
($t1, $t0);
140 print timestr
($td),"\n";
151 all languages in WITH_LANG are processed. WITH_LANG=ALL is
152 not supported in manual calls.
154 Updates the *.tree files.
155 At first, the English file is updated based on the English
156 help topic titles as read from the help files. Then, the
157 localized tree files are written based on the English tree
158 file and the localized help topic titles.
160 Requires a valid SO/OOo environment.
167 #---------------------------------------------------
170 print "Processing en-US\n";
171 undef %helpsection; undef %node;
174 &processtreefiles
('en-US');
177 #---------------------------------------------------
180 print "\n---------------------------------------------------\nProcessing $lng\n";
181 &processtreefiles
($lng);
185 #---------------------------------------------------
186 sub readtreestrings
{
187 print "Reading tree strings for en-US...";
188 if (open TREE
, $treestrings) {
191 s/<\/*help:productname>//gis
;
192 if (/help_section/) {
193 s/^\s*<.*help_section//;
195 ($id = $_) =~ s/^.*id="(\d+)".*$/$1/;
196 ($title = $_) =~ s/^.*title="(.*)".*$/$1/;
197 $helpsection{$id} = $title;
203 ($id = $_) =~ s/^.*id="(\d+)".*$/$1/;
204 ($title = $_) =~ s/^.*title="(.*)".*$/$1/;
210 &terminate
("Error opening $treestrings");
215 #------------------------------------
217 # Read the tree files from the directory
218 # this list is also used for all foreign languages
219 print "Reading tree files...";
220 if (opendir ENUS
, "$tree_src") {
221 @treeviews = grep /\.tree/, readdir ENUS
;
224 &terminate
("Cannot open directory $tree_src");
229 #------------------------------------
230 sub processtreefiles
{
232 use File
::Temp qw
/ tempfile /;
235 for $tv(@treeviews) {
236 print "\nProcessing $tv\n";
237 @lines = &readtv
("$tree_src/$tv");
238 print "Read ".scalar @lines." lines\n";
241 ($id = $l) =~ s/^.*id="([^"]*)".*$/$1/gis;
242 ($module = $id) =~ s/^([^\/]*).*$/$1/;
244 $file = "$source_dir_xhp/$id";
246 if ($lng eq 'en-US') { # english comes from the file
249 undef $/; $cnt = <F
>; close F
;
250 $cnt =~ s/^.*<title[^>]+id="tit"[^>]*>([^<]*)<\/title>.*$/$1/gis
;
251 $cnt =~ s/'/\'/gis; $cnt =~ s/&/+/gis;
252 $cnt =~ s/"/\'/gis; $cnt =~ s/&/+/gis;
253 $l = "<topic id=\"$module/$id\">$cnt</topic>\n";
256 $l = "<!-- removed $module/$id -->\n";
258 } else { # localized comes from the localize sdf
260 if (defined($loc_title{$lng}->{$id})) {
262 $l = "<topic id=\"$module/$id\">$loc_title{$lng}->{$id}</topic>\n";
270 ($id = $l) =~ s/^.*id="(\d+)".*$/$1/gis;
271 if ($lng eq 'en-US') {
272 if (defined($node{$id})) {
273 $l =~ s/title="(.*)"/title="$node{$id}"/;
275 $l =~ s/title="(.*)"/title="NOTFOUND:$id"/;
278 if (defined($node{$lng}->{$id})) {
279 $l =~ s/title="(.*)"/title="$node{$lng}->{$id}"/;
284 if ($l =~/<help_section/) {
285 ($id = $l) =~ s/^.*id="(\d+)".*$/$1/gis;
286 if ($lng eq 'en-US') {
287 if (defined($helpsection{$id})) {
288 $l =~ s/title="(.*)"/title="$helpsection{$id}"/;
291 $l =~ s/title="(.*)"/title="NOTFOUND:$id"/;
294 if (defined($helpsection{$lng}->{$id})) {
295 $l =~ s/title="(.*)"/title="$helpsection{$lng}->{$id}"/;
300 if ( ! -d
"$tree_dest/$lng" ) {
301 mkdir "$tree_dest/$lng" or die "\nCouldn't create directory \"$tree_dest/$lng\"";
303 my $treeoutdir = "$tree_dest/$lng";
304 my $tmpname_template=$tv."_XXXXX";
305 my ( $treetmpfilehandle, $treetmpfile ) = tempfile
($tmpname_template , DIR
=> File
::Spec
->tmpdir() );
306 close $treetmpfilehandle ;
307 if (open TV
, ">$treetmpfile") {
309 $line =~ s/\$\[officename\]/%PRODUCTNAME/g;
310 $line =~ s/\$\[officeversion\]/%PRODUCTVERSION/g;
314 chmod 0664, $treetmpfile or &terminate
("Cannot change rights on $treetmpfile");
315 if( $^O
eq 'MSWin32' )
317 $tree_dest =~ s/\//\\/g
;
318 unlink "$tree_dest\\$lng\\$tv" ;
319 mv
$treetmpfile , "$tree_dest\\$lng\\$tv" or &terminate
("Cannot mv $treetmpfile to $tree_dest\\$lng\\$tv" );
323 unlink "$tree_dest/$lng/$tv" ;
324 my $ret=mv
$treetmpfile , "$tree_dest/$lng/$tv$inpath" or &terminate
("Cannot write to $tree_dest/$lng/$tv$inpath - Error $!");
325 my $ret=mv
"$tree_dest/$lng/$tv$inpath" , "$tree_dest/$lng/$tv" or &terminate
("Cannot write to $tree_dest/$lng/$tv - Error $!");
328 &terminate
("Cannot write to $tvout");
333 #------------------------------------
342 &terminate
("Error opening $f");
346 #------------------------------------
347 # read entries form localize.sdf files
348 #------------------------------------
350 print "\n\nReading localized titles...";
352 my $path = "$source_dir/text";
353 $path =~ s/\//\\/g
, if $is_win eq "true";
354 print " in $source_dir/text\n";
355 @files = `find $source_dir/text -name localize.sdf`;
356 for my $fname (@files) {
359 open(LOCALIZE_SDF
, $fname) || die 'Cannot open "localize.sdf".'."$fname";
360 while (<LOCALIZE_SDF
>) {
362 my ($Fld1,$file,$Fld3,$Fld4,$id,$Fld6,$Fld7,$Fld8,$Fld9,$lang,$text) = split($FS, $sdf_line , 12);
363 next if ( $Fld1 =~ /^#/);
366 $file =~ s/.*text\\/text\\/g;
367 #convert \ to / in filename
369 #fpe: i46823 - need to encode &s, added encoding
370 $text =~ s/&(?!amp;)/&/g;
371 # add entry to the hash
372 $loc_title{$lang}->{$file} = $text;
374 if ($file =~ /tree_strings.xhp/) {
376 $file =~ s/.*text/text/g;
377 #convert \ to / in filename
379 if ($text =~ /^<help_section/) {
380 #example: <help_section application="scalc" id="08" title="表計算ドキュメント">
381 my ($fld1,$app,$fld3,$id,$fld5,$sec_title) = split('"', $text, 7);
382 #fpe: i46823 - need to encode &s, added encoding
383 if( defined $sec_title )
385 $sec_title =~ s/&(?!amp;)/&/g;
386 #unquot \<item ... /\>
387 terminate
( "\n\nERROR: Bad string in file '$fname' will cause invalid xml tree file \n---\n'$sdf_line'\n---\nPlease remove or replace < = '<' and > = '>' within the title attribute '$sec_title'\n") , if( $sec_title =~ /[\<\>]/ );
388 $helpsection{$lang}->{$id} = $sec_title;
390 } elsif ($text =~/<node id=/) {
391 # example: <node id="0205" title="Tabelas em documentos de texto">
392 # BEWARE: title may contain escaped '"' so only match " not preceded by \
393 # using a zero‐width negative look‐behind assertion.
394 my ($fld1,$id,$fld3,$node_title,$Fld5) = split(/(?<!\\)"/, $text, 5);
395 #fpe: i46823 - need to encode &s, added encoding
396 if( defined $node_title )
398 $node_title =~ s/&(?!amp;)/&/g;
399 terminate
( "\n\nERROR: Bad string in '$fname' will cause invalid xml tree file \n---\n'$sdf_line'\n---\nPlease remove or replace < = '<' and > = '>' within the title attribute '$node_title'\n") , if( $node_title =~ /[\<\>]/ );
401 $node{$lang}->{$id} = $node_title;
409 foreach $lang (keys %loc_title) {
410 $no_elements = scalar(keys(%{$loc_title{$lang}}));
411 push(@langstat, "$lang:\t ".$no_elements." matches\n");
412 $total_elements += $no_elements;
414 print "\ndone reading a total of ".$total_elements." localized titles for ".scalar(keys(%loc_title))." languages from ".scalar @files ." files\n";
415 print sort(@langstat);