1: 2eval 'exec perl -wS $0 ${1+"$@"}' 3 if 0; 4#************************************************************** 5# 6# Licensed to the Apache Software Foundation (ASF) under one 7# or more contributor license agreements. See the NOTICE file 8# distributed with this work for additional information 9# regarding copyright ownership. The ASF licenses this file 10# to you under the Apache License, Version 2.0 (the 11# "License"); you may not use this file except in compliance 12# with the License. You may obtain a copy of the License at 13# 14# http://www.apache.org/licenses/LICENSE-2.0 15# 16# Unless required by applicable law or agreed to in writing, 17# software distributed under the License is distributed on an 18# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19# KIND, either express or implied. See the License for the 20# specific language governing permissions and limitations 21# under the License. 22# 23#************************************************************** 24 25 26 27use Cwd 'abs_path'; 28use File::Find; 29use File::Copy qw/cp mv/; 30use File::Basename; 31use Benchmark; 32 33$t0 = new Benchmark; 34# update the tree files in <platform>/misc/* 35 36$| = 1; 37 38my $prj = $ENV{ENVPRJ}; 39 40my $inpath = $ENV{INPATH}; 41terminate() if ( ! defined $inpath ); 42 43my $destpath = $inpath; 44my $with_lang = $ENV{WITH_LANG}; 45 46 47# Always use / directory separators 48$prj =~ s/\\/\//g if defined($prj); 49$inpath =~ s/\\/\//g; 50$destpath =~ s/\\/\//g; 51 52 53if ( ! defined $prj ) { 54# do someting that works for manual call 55 ($scriptname = `pwd`) =~ s/\n/\/$0/; 56 ($tree_src = $scriptname) =~ s/\/update_tree.pl/\/..\/source\/auxiliary/; 57 ($tree_dest = $scriptname) =~ s/\/update_tree.pl/\/..\/$destpath\/misc/; 58 ($source_dir = $scriptname) =~ s/\/update_tree.pl/\/..\/source/; 59 ($source_dir_xhp = $scriptname) =~ s/\/update_tree.pl/\/..\/source/; 60 61 if ( defined $ENV{TRYSDF} || defined $ENV{LOCALIZESDF} ) 62 { 63 if( defined $ENV{LOCALIZATION_FOUND} && $ENV{LOCALIZATION_FOUND} eq "YES" ) 64 { 65 $source_dir = $ENV{TRYSDF}; 66 } 67 else 68 { 69 $source_dir = $ENV{LOCALIZESDF}; 70 } 71 $source_dir =~ s/\/auxiliary\/localize.sdf$// ; 72 } 73 #else {die "ERROR: The env variables TRYSDF LOCALIZATION_FOUND LOCALIZESDF not found ... something is wrong!\n";} 74 75 76 $treestrings = "$source_dir/text/shared/tree_strings.xhp"; 77} else { 78 $tree_src = "$prj\/source\/auxiliary"; 79 $tree_dest = "$prj\/$destpath\/misc"; 80 $source_dir = "$prj\/source"; 81 $source_dir_xhp = "$prj\/source"; 82 $treestrings = "$source_dir/text/shared/tree_strings.xhp"; 83 84 if( defined $ENV{LOCALIZATION_FOUND} && $ENV{LOCALIZATION_FOUND} eq "YES" ) 85 { 86 $source_dir = $ENV{TRYSDF}; 87 } 88 else 89 { 90 $source_dir = $ENV{LOCALIZESDF}; 91 } 92 $source_dir =~ s/\/auxiliary\/localize.sdf$// ; 93 #else {die "ERROR: The env variables TRYSDF LOCALIZATION_FOUND LOCALIZESDF not found ... something is wrong!\n";} 94 95} 96 97# Get the English tree files as master 98#------------------------------- 99# Update English from xhp 100#------------------------------- 101&do_english; 102#------------------------------- 103# Update localizations from sdf 104#------------------------------- 105 106@langs = split /\s+/, $with_lang; 107&read_loc; 108print "################\nUpdating the treefiles for @langs \n"; 109for $l(@langs) { 110 if ($l ne "en-US") { 111 &do_lang($l); 112 } 113} 114 115#------------------------------- 116# 117$t1 = new Benchmark; 118$td = timediff($t1, $t0); 119print timestr($td),"\n"; 120 121#################### 122# SUBS 123#################### 124sub terminate { 125 $err = shift; 126 print "$err\n\n"; 127 $msg = <<"MSG"; 128 129update_tree.pl 130 all languages in WITH_LANG are processed. WITH_LANG=ALL is 131 not supported in manual calls. 132 133 Updates the *.tree files. 134 At first, the English file is updated based on the English 135 help topic titles as read from the help files. Then, the 136 localized tree files are written based on the English tree 137 file and the localized help topic titles. 138 139 Requires a valid SO/OOo environment. 140MSG 141 print "$msg\n"; 142 exit( -1 ); 143 # die "$msg\n"; 144} 145 146#--------------------------------------------------- 147 148sub do_english { 149 print "Processing en-US\n"; 150 undef %helpsection; undef %node; 151 &readtreestrings; 152 &gettreefiles; 153 &processtreefiles('en-US'); 154} 155 156#--------------------------------------------------- 157sub do_lang { 158 $lng = shift; 159 print "\n---------------------------------------------------\nProcessing $lng\n"; 160 &processtreefiles($lng); 161 print "\n"; 162} 163 164#--------------------------------------------------- 165sub readtreestrings { 166 print "Reading tree strings for en-US..."; 167 if (open TREE, $treestrings) { 168 while (<TREE>) { 169 chomp; 170 s/<\/*help:productname>//gis; 171 if (/help_section/) { 172 s/^\s*<.*help_section//; 173 s/<\/.*$//; 174 ($id = $_) =~ s/^.*id="(\d+)".*$/$1/; 175 ($title = $_) =~ s/^.*title="(.*)".*$/$1/; 176 $helpsection{$id} = $title; 177 } 178 179 if (/node id=/) { 180 s/^\s*<.*node //; 181 s/<\/.*$//; 182 ($id = $_) =~ s/^.*id="(\d+)".*$/$1/; 183 ($title = $_) =~ s/^.*title="(.*)".*$/$1/; 184 $node{$id} = $title; 185 } 186 } 187 close TREE; 188 } else { 189 &terminate("Error opening $treestrings"); 190 } 191 print "done\n"; 192} 193 194#------------------------------------ 195sub gettreefiles { 196 # Read the tree files from the directory 197 # this list is also used for all foreign languages 198 print "Reading tree files..."; 199 if (opendir ENUS, "$tree_src") { 200 @treeviews = grep /\.tree/, readdir ENUS; 201 closedir ENUS; 202 } else { 203 &terminate("Cannot open directory $tree_src"); 204 } 205 print "done\n"; 206} 207 208#------------------------------------ 209sub processtreefiles { 210 $lng = shift; 211 use File::Temp qw/ tempfile /; 212 use File::Spec; 213 214 for $tv(@treeviews) { 215 print "\nProcessing $tv\n"; 216 @lines = &readtv("$tree_src/$tv"); 217 print "Read ".scalar @lines." lines\n"; 218 for $l(@lines) { 219 if ($l =~ /topic/) { 220 ($id = $l) =~ s/^.*id="([^"]*)".*$/$1/gis; 221 ($module = $id) =~ s/^([^\/]*).*$/$1/; 222 $id =~ s/^.*?\///; 223 $file = "$source_dir_xhp/$id"; 224 225 if ($lng eq 'en-US') { # english comes from the file 226 if (open F,$file) { 227 print "."; 228 undef $/; $cnt = <F>; close F; 229 $cnt =~ s/^.*<title[^>]+id="tit"[^>]*>([^<]*)<\/title>.*$/$1/gis; 230 $cnt =~ s/'/\'/gis; $cnt =~ s/&/+/gis; 231 $cnt =~ s/"/\'/gis; $cnt =~ s/&/+/gis; 232 $l = "<topic id=\"$module/$id\">$cnt</topic>\n"; 233 } else { 234 print "!"; 235 $l = "<!-- removed $module/$id -->\n"; 236 } 237 } else { # localized comes from the localize sdf 238 #print "\nid: $id"; 239 if (defined($loc_title{$lng}->{$id})) { 240 print "."; 241 $l = "<topic id=\"$module/$id\">$loc_title{$lng}->{$id}</topic>\n"; 242 } else { 243 print "!"; 244 } 245 } 246 } 247 248 if ($l =~/<node/) { 249 ($id = $l) =~ s/^.*id="(\d+)".*$/$1/gis; 250 if ($lng eq 'en-US') { 251 if (defined($node{$id})) { 252 $l =~ s/title="(.*)"/title="$node{$id}"/; 253 } else { 254 $l =~ s/title="(.*)"/title="NOTFOUND:$id"/; 255 } 256 } else { 257 if (defined($node{$lng}->{$id})) { 258 $l =~ s/title="(.*)"/title="$node{$lng}->{$id}"/; 259 } 260 } 261 } 262 263 if ($l =~/<help_section/) { 264 ($id = $l) =~ s/^.*id="(\d+)".*$/$1/gis; 265 if ($lng eq 'en-US') { 266 if (defined($helpsection{$id})) { 267 $l =~ s/title="(.*)"/title="$helpsection{$id}"/; 268 } else { 269 print "#"; 270 $l =~ s/title="(.*)"/title="NOTFOUND:$id"/; 271 } 272 } else { 273 if (defined($helpsection{$lng}->{$id})) { 274 $l =~ s/title="(.*)"/title="$helpsection{$lng}->{$id}"/; 275 } 276 } 277 } 278 } 279 if ( ! -d "$tree_dest/$lng" ) { 280 mkdir "$tree_dest/$lng" or die "\nCouldn't create directory \"$tree_dest/$lng\""; 281 } 282 my $treeoutdir = "$tree_dest/$lng"; 283 my $tmpname_template=$tv."_XXXXX"; 284 my ( $treetmpfilehandle, $treetmpfile ) = tempfile($tmpname_template , DIR => File::Spec->tmpdir() ); 285 close $treetmpfilehandle ; 286 if (open TV, ">$treetmpfile") { 287 for $line(@lines) { 288 $line =~ s/\$\[officename\]/%PRODUCTNAME/g; 289 $line =~ s/\$\[officeversion\]/%PRODUCTVERSION/g; 290 print TV $line; 291 } 292 close TV; 293 chmod 0664, $treetmpfile or &terminate("Cannot change rights on $treetmpfile"); 294 if( $^O eq 'MSWin32' ) 295 { 296 $tree_dest =~ s/\//\\/g ; 297 unlink "$tree_dest\\$lng\\$tv" ; 298 mv $treetmpfile , "$tree_dest\\$lng\\$tv" or &terminate("Cannot mv $treetmpfile to $tree_dest\\$lng\\$tv" ); 299 } 300 else 301 { 302 unlink "$tree_dest/$lng/$tv" ; 303 my $ret=mv $treetmpfile , "$tree_dest/$lng/$tv$inpath" or &terminate("Cannot write to $tree_dest/$lng/$tv$inpath - Error $!"); 304 my $ret=mv "$tree_dest/$lng/$tv$inpath" , "$tree_dest/$lng/$tv" or &terminate("Cannot write to $tree_dest/$lng/$tv - Error $!"); 305 } 306 } else { 307 &terminate("Cannot write to $tvout"); 308 } 309 } 310} 311 312#------------------------------------ 313sub readtv { 314 my $f = shift; 315 if (open TV, $f) { 316 $/ = "\n"; 317 my @l = <TV>; 318 close TV; 319 return @l; 320 } else { 321 &terminate("Error opening $f"); 322 } 323} 324 325#------------------------------------ 326# read entries form localize.sdf files 327#------------------------------------ 328sub read_loc { 329 print "\n\nReading localized titles..."; 330 $/ = "\n"; 331 my $path = "$source_dir/text"; 332 print " in $source_dir/text\n"; 333 @files = `find $source_dir/text -name localize.sdf`; 334 for my $fname (@files) { 335 $FS = '\t'; 336 print "."; 337 open(LOCALIZE_SDF, $fname) || die 'Cannot open "localize.sdf".'."$fname"; 338 while (<LOCALIZE_SDF>) { 339 my $sdf_line = $_; 340 my ($Fld1,$file,$Fld3,$Fld4,$id,$Fld6,$Fld7,$Fld8,$Fld9,$lang,$text) = split($FS, $sdf_line , 12); 341 next if ( $Fld1 =~ /^#/); 342 if ($id eq 'tit') { 343 #strip filename 344 $file =~ s/.*text\\/text\\/g; 345 #convert \ to / in filename 346 $file =~ s/\\/\//g; 347 #fpe: i46823 - need to encode &s, added encoding 348 $text =~ s/&(?!amp;)/&/g; 349 # add entry to the hash 350 $loc_title{$lang}->{$file} = $text; 351 } 352 if ($file =~ /tree_strings.xhp/) { 353 #strip filename 354 $file =~ s/.*text/text/g; 355 #convert \ to / in filename 356 $file =~ s/\\/\//g; 357 if ($text =~ /^<help_section/) { 358 #example: <help_section application="scalc" id="08" title="表計算ドキュメント"> 359 my ($fld1,$app,$fld3,$id,$fld5,$sec_title) = split('"', $text, 7); 360 #fpe: i46823 - need to encode &s, added encoding 361 if( defined $sec_title ) 362 { 363 $sec_title =~ s/&(?!amp;)/&/g; 364 #unquot \<item ... /\> 365 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 =~ /[\<\>]/ ); 366 $helpsection{$lang}->{$id} = $sec_title; 367 } 368 } elsif ($text =~/<node id=/) { 369 # example: <node id="0205" title="Tabelas em documentos de texto"> 370 # BEWARE: title may contain escaped '"' so only match " not preceded by \ 371 # using a zero‐width negative look‐behind assertion. 372 my ($fld1,$id,$fld3,$node_title,$Fld5) = split(/(?<!\\)"/, $text, 5); 373 #fpe: i46823 - need to encode &s, added encoding 374 if( defined $node_title ) 375 { 376 $node_title =~ s/&(?!amp;)/&/g; 377 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 =~ /[\<\>]/ ); 378 } 379 $node{$lang}->{$id} = $node_title; 380 } 381 } 382 } 383 close LOCALIZE_SDF; 384 } 385 # statistics 386 $total_elements=0; 387 foreach $lang (keys %loc_title) { 388 $no_elements = scalar(keys(%{$loc_title{$lang}})); 389 push(@langstat, "$lang:\t ".$no_elements." matches\n"); 390 $total_elements += $no_elements; 391 } 392 print "\ndone reading a total of ".$total_elements." localized titles for ".scalar(keys(%loc_title))." languages from ".scalar @files ." files\n"; 393 print sort(@langstat); 394} 395 396