Mozdev
third-party/test-locale-files.pl
author babel-sync
Sat Jul 04 03:02:24 2009 +0200 (4 hours ago)
changeset 1993 db0ce50c3f53
permissions -rw-r--r--
Uploading pt-PT update from Babelzilla
     1 #!/usr/bin/perl
     2 
     3 # ***** BEGIN LICENSE BLOCK *****
     4 # Version: MPL 1.1
     5 #
     6 # The contents of this file are subject to the Mozilla Public License Version
     7 # 1.1 (the "License"); you may not use this file except in compliance with
     8 # the License. You may obtain a copy of the License at
     9 # http://www.mozilla.org/MPL/
    10 #
    11 # Software distributed under the License is distributed on an "AS IS" basis,
    12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13 # for the specific language governing rights and limitations under the
    14 # License.
    15 #
    16 # The Original Code is TomTom HOME build automation.
    17 #
    18 # The Initial Developer of the Original Code is
    19 # Wladimir Palant <trev@adblockplus.org>.
    20 # Portions created by the Initial Developer are Copyright (C) 2008
    21 # the Initial Developer. All Rights Reserved.
    22 #
    23 # Contributor(s):
    24 #
    25 # ***** END LICENSE BLOCK *****
    26 
    27 use strict;
    28 use warnings;
    29 
    30 die "Usage: $^X $0 directory\n" unless @ARGV;
    31 
    32 my $failure = 0;
    33 processDir($ARGV[0]);
    34 exit $failure;
    35 
    36 sub processDir
    37 {
    38     my $dir = shift;
    39     opendir(local *DIR, $dir);
    40     foreach my $file (readdir(DIR))
    41     {
    42         next if $file =~ /^\./;
    43         my $path = "$dir/$file";
    44         if (-d $path)
    45         {
    46             processDir($path);
    47         }
    48         elsif ($file =~ /\.dtd$/)
    49         {
    50             processDTDFile($path);
    51         }
    52         elsif ($file =~ /\.properties$/)
    53         {
    54             processPropertiesFile($path);
    55         }
    56     }
    57     closedir(DIR);
    58 }
    59 
    60 sub processDTDFile
    61 {
    62     my $file = shift;
    63     my $data = validateUTF8($file);
    64     return unless defined $data;
    65 
    66     my $S = qr/[\x20\x09\x0D\x0A]/;
    67     my $Name = qr/[A-Za-z_:][\w.\-:]*/;
    68     my $Reference = qr/&$Name;|&#\d+;|&#x[\da-fA-F]+;/;
    69     my $PEReference = qr/%$Name;/;
    70     my $EntityValue = qr/"([^%&"]|$PEReference|$Reference)*"|'([^%&']|$PEReference|$Reference)*'/;
    71     my $SystemLiteral = qr/"[^"]*"|'[^']*'/;
    72     my $ExternalID = qr/SYSTEM$S+$SystemLiteral/;
    73     my $EntityDef = qr/$EntityValue|$ExternalID/;
    74     my $PEDef = qr/$EntityValue|$ExternalID/;
    75 
    76     # Remove comments
    77     $data =~ s/<!--([^\-]|-[^\-])*-->//gs;
    78 
    79     # Remove entities
    80     $data =~ s/<!ENTITY$S+$Name$S+$EntityDef$S*>//gs;
    81 
    82     # Remove external entities
    83     $data =~ s/<!ENTITY$S+%$S+$Name$S+$PEDef$S*>//gs;
    84 
    85     # Remove external references
    86     $data =~ s/$PEReference//gs;
    87 
    88     # Remove spaces
    89     $data =~ s/^\s+//gs;
    90     $data =~ s/\s+$//gs;
    91     $data =~ s/\s+/ /gs;
    92 
    93     if ($data ne "")
    94     {
    95         $failure = 1;
    96         warn "$0: Unrecognized data in file $file: $data\n";
    97         return;
    98     }
    99 }
   100 
   101 sub processPropertiesFile
   102 {
   103     my $file = shift;
   104     my $data = validateUTF8($file);
   105     return unless defined $data;
   106 
   107     foreach my $line (split(/[\r\n]+/, $data))
   108     {
   109         if ($line !~ /^\s*[#!]/ && $line !~ /=/ && $line =~ /\S/)
   110         {
   111             # Not a comment, not a property, trash?
   112             $failure = 1;
   113             warn "$0: Unrecognized data in file $file: $line\n";
   114         }
   115     }
   116 }
   117 
   118 sub validateUTF8
   119 {
   120     my $file = shift;
   121 
   122     open(local *FILE, "<", $file);
   123     local $/;
   124     my $data = <FILE>;
   125     close(FILE);
   126 
   127     if ($data =~ /\xEF\xBB\xBF/)
   128     {
   129         $failure = 1;
   130         warn "$0: Byte Order Mark found in file $file\n";
   131         return;
   132     }
   133 
   134     unless (utf8::decode($data))
   135     {
   136         $failure = 1;
   137         warn "$0: File $file is not valid UTF-8\n";
   138         return;
   139     }
   140 
   141     return $data;
   142 }