• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/perl
2
3####################################################################################
4# txt2ldml.pl:
5# This tool invokes genldml and genrb to produce res files from xml files
6# Author: Ram Viswanadha
7#
8####################################################################################
9use File::Find;
10use File::Basename;
11use IO::File;
12use Cwd;
13use File::Copy;
14use Getopt::Long;
15use File::Path;
16use File::Copy;
17
18GetOptions(
19           "--lib=s" => \$envVar,
20           "--sourcedir=s" => \$sourceDir,
21           "--destdir=s" => \$destDir,
22           "--icuroot=s" => \$icuRoot,
23           "--genldml=s" => \$genldml,
24           "--genrb=s" => \$genrb,
25	         "--ldml-only=s" => \$ldmlOnly,
26           "--ignore-collation" => \$ignoreCollation,
27		       "--base=s" => \$baseFile,
28           "--ignore-specials" => \$ignoreSpecials,
29           "--ignore-layout" => \$ignoreLayout,
30           "--draft"  => \$draft,
31           "--only-specials" => \$onlySpecials,
32          );
33
34
35usage() unless defined $sourceDir;
36usage() unless defined $destDir;
37usage() unless defined $icuRoot;
38usage() unless defined $genldml;
39
40getPathToGenrb() unless defined $genrb;
41
42# create a temp directory and copy all the txt files there
43my $tempDir = $destDir."/temp";
44mkpath($tempDir);
45my $prefix;
46my $tempPackage="LDMLTestPackage";
47# set up environment
48if($$^O =~ /win/){
49    $prefix ="";
50    cmd("set PATH=%PATH%;$icuRoot/bin;");
51}else{
52    $prefix ="$ldVar=$ICU_ROOT/source/common:$ICU_ROOT/source/i18n:$ICU_ROOT/source/tools/toolutil:$ICU_ROOT/source/data/out:$ICU_ROOT/source/data: "
53}
54
55# create list of xml files
56my @list;
57if (@ARGV) {
58    @list = @ARGV;
59    foreach (@list) { $_ .= ".txt" unless (/\.txt$/i); }
60} else {
61    opendir(DIR,$sourceDir);
62    @list = grep{/\.txt$/} readdir(DIR);
63    closedir(DIR);
64}
65
66# creating res files and LDML files
67# cannot be combined in one step
68# since there may be dependencies while building
69# the res files which may not be resolved until
70# all files are built
71
72# Step 1: create *.res files
73foreach $item (@list){
74    next if($item eq "." || $item eq "..");
75    resify($item) unless defined $ldmlOnly;
76}
77
78# Step 2:  create *.xml files
79foreach $item (@list){
80    next if($item eq "." || $item eq ".." );
81    $txt = $item;;
82    $txt =~ s/\.txt$//i;
83    ldmlify($txt);
84}
85
86# run the xmlify converter
87sub ldmlify{
88    my $infile = shift;
89    my $genldmlExec = $genldml."/genldml";
90    my $ic ="";
91    my $is ="";
92    my $il ="";
93    my $base = "";
94    my $baseTyp = "";
95    my $id = "";
96    my $os = "";
97    if (defined $ignoreCollation){
98        $ic = "--ignore-collation";
99    }
100    if (defined $ignoreSpecials){
101        $is = "--ignore-specials";
102    }
103    if(defined $ignoreLayout){
104        $il = "--ignore-layout";
105    }
106    if(defined $draft){
107        $id = "--draft";
108    }
109    if(defined $onlySpecials){
110        $os = "--only-specials";
111    }
112    if(defined $baseFile){
113        $baseLoc = getBaseLocale($baseFile, $infile);
114        $base = "--base $baseLoc";
115
116        # Commented out because currently we don't need to
117        # pass in the type argument to genldml
118        # can be uncommented if needed
119        #$baseLoc = getBaseType ($baseFile, $infile);
120        #if(baseLoc ne ""){
121        #    $baseTyp ="--base-type $baseLoc";
122        #}
123
124    }
125
126    cmd("$prefix $genldmlExec --sourcedir $tempDir --destdir $destDir --package $tempPackage $base $baseTyp $ic $il $is $id $os $infile");
127}
128
129sub getBaseLocale(){
130    my $basefile = shift;
131    my $locale = shift;
132    $baseFH = IO::File->new($basefile,"r")
133            or die  "could not open the file $basefile for reading: $! \n";
134    while(defined ( $line = <$baseFH>)){
135        if( $line =~ /^\<$locale\>/){
136            ($loc,$bse) = split (/\>/, $line);
137             $bse =~ s/^\s+\<//;
138             return $bse;
139        }
140    }
141}
142
143#------------------------
144# This method is commented out for now
145# can be uncommented if we need to pass baseType argument to
146# genldml. Currently we don't need this feature
147# P.S: Not tested.
148#--------------------------
149#sub getBaseType(){
150#    my $basefile = shift;
151#    my $locale = shift;
152#    $baseFH = IO::File->new($basefile,"r")
153#            or die  "could not open the file $basefile for reading: $! \n";
154#    while(defined ( $line = <$baseFH>)){
155#        if( $line =~ /\<$locale\>/){
156#            ($loc,$bse) = split (/\>/, $line);
157#             $bse =~ s/^\s+\<//;
158#             return $bse;
159#        }
160#    }
161#}
162
163# run genrb
164sub resify{
165    my $infile = shift;
166    my $genrbExec = $genrb."/genrb";
167
168    cmd("$prefix $genrbExec --sourcedir $sourceDir --package-name $tempPackage --destdir $tempDir --encoding UTF8 $infile");
169}
170
171#
172sub getPathToGenrb{
173    $genrb = $icuRoot."/bin";
174}
175
176#-----------------------------------------------------------------------
177# Execute a command
178# Param: Command
179# Param: Display line, or '' to display command
180sub cmd {
181    my $cmd = shift;
182    my $prompt = shift;
183    $prompt = "Command: $cmd.." unless ($prompt);
184    print $prompt."\n";
185    system($cmd);
186    my $exit_value  = $? >> 8;
187    #my $signal_num  = $? & 127;
188    #my $dumped_core = $? & 128;
189    if ($exit_value == 0) {
190        print "ok\n";
191    } else {
192        ++$errCount;
193        print "ERROR ($exit_value)\n";
194        exit(1);
195    }
196}
197
198#-----------------------------------------------------------------------
199sub usage {
200    print << "END";
201Usage:
202txt2ldml.pl
203Options:
204        --lib=<environment variable for lib path>
205        --sourcedir=<directory>
206        --destdir=<directory>
207        --icuroot=<path to ICU's root directory>
208        --genldml=<path to genldml executatble>
209        --genrb=<path to genrb executatble>
210        --ignore-collation
211        --base=<the text file that contains the base to locale mapping including the path>
212        --ignore-layout
213        --ignore-specials
214        --only-specials
215        --draft
216
217txt2ldml creates *.xml file from *.txt files by invoking the respective tools
218Optionally, one or more locales may be specified on the command line.
219If this is done, only those locales will be processed.  If no locales
220are listed, all locales are processed.
221
222END
223  exit(0);
224}
225