Ignore:
Timestamp:
2008-10-15T12:27:10+13:00 (13 years ago)
Author:
oranfry
Message:

protect against a particular error message poluting XML output

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gsdl/trunk/perllib/gsprintf.pm

    r16635 r17533  
    7777    # Try the default resource bundle next
    7878    if (!defined($utf8string)) {
    79     # Load the default resource bundle if it is not already loaded
    80     &load_default_resource_bundle() if (!%defaultresourcebundle);
    81 
    82     $utf8string = $defaultresourcebundle{$stringkey};
    83     $outputencoding = $defaultoutputencoding;
     79    # Load the default resource bundle if it is not already loaded
     80    &load_default_resource_bundle() if (!%defaultresourcebundle);
     81
     82    $utf8string = $defaultresourcebundle{$stringkey};
     83    $outputencoding = $defaultoutputencoding;
    8484    }
    8585
    8686    # Try the English resource bundle last
    8787    if (!defined($utf8string)) {
    88     # Load the English resource bundle if it is not already loaded
    89     &load_english_resource_bundle() if (!%englishresourcebundle);
    90 
    91     $utf8string = $englishresourcebundle{$stringkey};
    92     $outputencoding = $englishoutputencoding;
     88    # Load the English resource bundle if it is not already loaded
     89    &load_english_resource_bundle() if (!%englishresourcebundle);
     90
     91    $utf8string = $englishresourcebundle{$stringkey};
     92    $outputencoding = $englishoutputencoding;
    9393    }
    9494
    9595    # No matching string was found, so just return the key
    9696    if (!defined($utf8string)) {
    97     return $stringkey;
     97    return $stringkey;
    9898    }
    9999
    100100    # Return the string matching the key
    101101    return $utf8string if (!defined($outputencoding) || $outputstringsinUTF8
    102                || $outputencoding eq "utf8");
     102               || $outputencoding eq "utf8");
    103103
    104104    # If an 8-bit output encoding has been defined, encode the string appropriately
     
    128128    # Read the output encoding to use from the resource bundle
    129129    if ($ENV{'GSDLOS'} =~ /windows/) {
    130     $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.windows}"};
     130    $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.windows}"};
    131131    }
    132132    else {
    133     # see if there is an encoding set in the appropriate locale env var
    134 
    135     foreach my $envvar ('LC_ALL', 'LANG') {
    136         if (!exists $ENV{$envvar}) { next }
    137         my $locale=$ENV{$envvar};
    138         if ($locale !~ /^\w+\.(.+)$/) { next }
    139         my $enc=lc($1);
    140         $enc =~ s/-/_/g;
    141         if ($enc eq 'utf_8') { $enc='utf8' } # normalise to this name
    142         $specialoutputencoding = $enc;
    143         return;
    144     }
    145     $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.unix}"};
     133    # see if there is an encoding set in the appropriate locale env var
     134
     135    foreach my $envvar ('LC_ALL', 'LANG') {
     136        if (!exists $ENV{$envvar}) { next }
     137        my $locale=$ENV{$envvar};
     138        if ($locale !~ /^\w+\.(.+)$/) { next }
     139        my $enc=lc($1);
     140        $enc =~ s/-/_/g;
     141        if ($enc eq 'utf_8') { $enc='utf8' } # normalise to this name
     142        $specialoutputencoding = $enc;
     143        return;
     144    }
     145    $specialoutputencoding = $specialresourcebundle{"{OutputEncoding.unix}"};
    146146    }
    147147}
     
    159159        # $! will still have the error value for the last failed syscall
    160160        print STDERR "$! $resourcebundlefile\n";
    161     # set something so we don't bother trying to load it again
    162     $defaultresourcebundle{0}=undef;
     161    # set something so we don't bother trying to load it again
     162    $defaultresourcebundle{0}=undef;
    163163        return;
    164164    }
     
    166166    # Read the output encoding to use from the resource bundle
    167167    if ($ENV{'GSDLOS'} =~ /windows/) {
    168     $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.windows}"};
     168    $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.windows}"};
    169169    }
    170170    else {
    171     $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.unix}"};
     171    $defaultoutputencoding = $defaultresourcebundle{"{OutputEncoding.unix}"};
    172172    }
    173173}
     
    178178    # Ensure the English resource bundle hasn't already been loaded
    179179    if (%specialresourcebundle && $specialresourcebundle{"{Language.code}"} eq "en") {
    180     %englishresourcebundle = %specialresourcebundle;
    181     $englishoutputencoding = $specialoutputencoding;
     180    %englishresourcebundle = %specialresourcebundle;
     181    $englishoutputencoding = $specialoutputencoding;
    182182    }
    183183   
    184184    if ($defaultresourcebundle{"{Language.code}"} &&
    185         $defaultresourcebundle{"{Language.code}"} eq "en") {
    186     %englishresourcebundle = %defaultresourcebundle;
    187     $englishoutputencoding = $defaultoutputencoding;
     185        $defaultresourcebundle{"{Language.code}"} eq "en") {
     186    %englishresourcebundle = %defaultresourcebundle;
     187    $englishoutputencoding = $defaultoutputencoding;
    188188    }
    189189
     
    198198    # Read the output encoding to use from the resource bundle
    199199    if ($ENV{'GSDLOS'} =~ /windows/) {
    200     $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.windows}"};
     200    $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.windows}"};
    201201    }
    202202    else {
    203     $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.unix}"};
     203    $englishoutputencoding = $englishresourcebundle{"{OutputEncoding.unix}"};
    204204    }
    205205}
     
    220220    my %resourcebundle = ();
    221221    foreach my $line (@resourcebundlelines) {
    222     # Remove any trailing whitespace
    223     $line =~ s/(\s*)$//;
    224 
    225     # Ignore comments and empty lines
    226     if ($line !~ /^\#/ && $line ne "") {
    227         # Parse key (everything up to the first colon)
    228         $line =~ /^([^:]+):(.+)$/;
    229         my $linekey = "{" . $1 . "}";
    230         my $linetext = $2;
    231         $linetext =~ s/(\s*)\#\s+Updated\s+(\d?\d-\D\D\D-\d\d\d\d).*$//i;
    232 
    233         # Map key to text
    234         $resourcebundle{$linekey} = $linetext;
    235     }
     222        # Remove any trailing whitespace
     223        $line =~ s/(\s*)$//;
     224
     225        # Ignore comments and empty lines
     226        if ($line !~ /^\#/ && $line ne "") {
     227            # Parse key (everything up to the first colon)
     228            if ($line =~ m/^([^:]+):(.+)$/) {
     229                my $linekey = "{" . $1 . "}";
     230                my $linetext = $2;
     231                $linetext =~ s/(\s*)\#\s+Updated\s+(\d?\d-\D\D\D-\d\d\d\d).*$//i;
     232
     233                # Map key to text
     234                $resourcebundle{$linekey} = $linetext;
     235            }
     236        }
    236237    }
    237238
     
    249250{
    250251    join("",
    251     map { $_ > 255 ?                      # if wide character...
    252            sprintf("\\x{%04X}", $_) :  # \x{...}
    253            chr($_)         
    254            } unpack("U*", $_[0]));         # unpack Unicode characters
     252    map { $_ > 255 ?                      # if wide character...
     253           sprintf("\\x{%04X}", $_) :  # \x{...}
     254           chr($_)         
     255           } unpack("U*", $_[0]));         # unpack Unicode characters
    255256}
    256257
Note: See TracChangeset for help on using the changeset viewer.