[plug] Save a soul from scripting insanity...

ryan at is.as.geeky.as ryan at is.as.geeky.as
Tue Aug 13 21:12:42 WST 2002


Okay, if you value Perl code please don't keep reading..

Don't bother telling me how badly hacked it is or how much shorter you could
make it or how many cases it doesn't accounted for - it is just to show it
*could* be done :)

Perl is not the answer for this prob, a java parser is, maybe there is a
perl module for that already, who knows, it's quicker to write one that to
learn about an existing one :)

For the examples given and those examples ONLY (when placed in complete
code), the following Perl does as requested as far as I can see, other
random java code will probably break it.

It does not follow the typical rules of a parser, it handles the rules as
based on the examples given, and not even on that hardly :)

It is VERY VERY VERY badly hacked together  .. bad luck :P

Examples - scattered anywhere in files a directory structure:
===============================
Translator.translate("foobar is just the start" +
                     " of a very long string");
dostuff(Translator.translate("Foo bar funct"),1,2,3);
dostuff(Translator.translate("Foo bar func multi
1"),Translator.translate("Foo bar func multi 2"),2,3);
Translator.translate("Foo multi 1"); Translator.translate("Bar mulit 2");
Translator.translate("foo: (Bar or \"whiz\") stupid chars");
Translator.translate("Foobar more params", 3);
===============================

Output:
===============================
ryan at slowest:~/perl/java$ ./trans.pl /home/ryan/
foobar is just the start of a very long string
Foo bar funct
Foo bar func multi 1
Foo bar func multi 2
Foo multi 1
Bar mulit 2
foo: (Bar or \"whiz\") stupid chars
Foobar more params
===============================

The code:
===============================
#!/usr/bin/perl

use strict;
use File::Find;

if ($ARGV[0] eq '') {
    print "Must specify directory to search as arguement\n";
    exit;
}

my @files;

sub eachFile {
  if (-e $_) { push @files, $File::Find::name; }
}

find (\&eachFile, $ARGV[0]);

my @lines;
my @found;
my $line_open = 0;
my $split_line;

foreach my $file (@files) {

    open(INFILE,"<$file") or die "Can't open input file $file - $!";
    @lines = <INFILE>;
    for (0..scalar(@lines)) {

        # catch normal and broken lines
        if ($lines[$_] =~ /\s{0,}Translator\.translate\(/) {

                # split line?
                if ($lines[$_] =~ /.+\"\s{0,}\+\s{0,}\n$/ && !$line_open) {
                $line_open = 1;
                $split_line = $lines[$_];
                } else {
                    # not a split line, is it a multiple call line ?
                    if ($lines[$_] =~
/(Translator\.translate\(\".+\)[\;|\,]\s{0,}\s{0,}Translator\.translate\(){1
,}/) {
                        $lines[$_] =~ s/\n//g;
                        $lines[$_] =~ s/^\s{0,}//;
                        $lines[$_] =~
s/^.+(Translator\.translate\(\".+\)[\;|\,]\s{0,}\s{0,}Translator\.translate\
(){1,}/$1/;
                        my @temp = split(/Translator\.translate/,
$lines[$_]);
                        splice(@found,scalar(@found),scalar(@temp), at temp);
                    } else {
                    # not a multiple line call
                    $lines[$_] =~ s/\n//g;
                    $lines[$_] =~ s/^\s{0,}.{0,}Translator\.translate//;
                    splice(@found,scalar(@found),0,$lines[$_]);
                    }
                }
        }

        if ($line_open && $lines[$_] !~ /\s{0,}Translator\.translate\(/) {
                $split_line .= $lines[$_];
                if ($lines[$_] =~ /\)\;/) {
                    $line_open = 0;
                    $split_line =~ s/\"\s{0,}\+\s{0,}\n\s+\"//g;
                    $split_line =~ s/\n//g;
                    $split_line =~ s/^\s{0,}Translator\.translate//;
                    splice(@found,scalar(@found),1,$split_line);
                };
        }
    }
}

foreach my $output (@found) {
    $output =~ s/\("((?:\\["\\]|[^"])*)".{0,}\)+([;|,])/$1/;
    $output =~ s/(\,\d{0,}){0,}$//g;
    if ($output =~ /\w/) {
        print "$output\n";
    }
}
===============================

Redir it to a file and your automation is done.

Told you it was badly hacked together :)

Ryan


----- Original Message -----
From: "Russell Keith-Magee" <Russell.Magee at calytrix.com>
To: "Plug at Plug. Linux. Org. Au" <plug at plug.linux.org.au>
Sent: Tuesday, August 13, 2002 11:38 AM
Subject: [plug] Save a soul from scripting insanity...


>
> Ok - I'm about to go mad (more so than normal).
>
> I have a large number of Java source files, which contain a large number
of
> function calls to a translation engine:
>
...

> This will leave me with one big file, containing one translation string
per
> line. I can then sort and uniq this file to get a list of strings that
need
> to be translated.




More information about the plug mailing list