/[suikacvs]/messaging/manakai/bin/daf.pl
Suika

Diff of /messaging/manakai/bin/daf.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.12 by wakaba, Sun Apr 9 14:29:41 2006 UTC revision 1.15 by wakaba, Sat May 20 05:11:37 2006 UTC
# Line 4  use Message::Util::QName::Filter { Line 4  use Message::Util::QName::Filter {
4    c => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/dom-core#>,    c => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/dom-core#>,
5    DIS => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#>,    DIS => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#>,
6    dis => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#dis-->,    dis => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#dis-->,
   DOMLS => q<http://suika.fam.cx/~wakaba/archive/2004/dom/ls#>,  
7    dp => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#Perl/>,    dp => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#Perl/>,
8    fe => q<http://suika.fam.cx/www/2006/feature/>,    fe => q<http://suika.fam.cx/www/2006/feature/>,
9    ManakaiDOM => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/manakai-dom#>,    ManakaiDOM => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/manakai-dom#>,
10    pc => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/PerlCode#>,    pc => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/PerlCode#>,
11    swcfg21 => q<http://suika.fam.cx/~wakaba/archive/2005/swcfg21#>,    swcfg21 => q<http://suika.fam.cx/~wakaba/archive/2005/swcfg21#>,
   test => q<http://suika.fam.cx/~wakaba/archive/2004/dis/Test#>,  
12    Util => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/>,    Util => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/>,
   xp => q<http://suika.fam.cx/~wakaba/archive/2004/dom/xml-parser#>,  
13  };  };
14    
15    our$VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
16  use Cwd;  use Cwd;
17  use Getopt::Long;  use Getopt::Long;
18  use Pod::Usage;  use Pod::Usage;
19  my %Opt = (create_module => []);  our %Opt = (create_module => []);
20    my @target_modules;
21  GetOptions (  GetOptions (
22      'create-dtd-modules=s' => sub {
23        shift;
24        my $i = [split /\s+/, shift, 3];
25        $i->[3] = 'dtd-modules';
26        push @{$Opt{create_module}}, $i;
27      },
28    'create-perl-module=s' => sub {    'create-perl-module=s' => sub {
29      shift;      shift;
30      my $i = [split /\s+/, shift, 3];      my $i = [split /\s+/, shift, 3];
31      $i->[3] = 'perl-pm';      $i->[3] = 'perl-pm';
32      push @{$Opt{create_module}}, $i;      push @{$Opt{create_module}}, $i;
33        push @target_modules, [$i->[0], $i->[2]];
34    },    },
35    'create-perl-test=s' => sub {    'create-perl-test=s' => sub {
36      shift;      shift;
37      my $i = [split /\s+/, shift, 3];      my $i = [split /\s+/, shift, 3];
38      $i->[3] = 'perl-t';      $i->[3] = 'perl-t';
39      push @{$Opt{create_module}}, $i;      push @{$Opt{create_module}}, $i;
40        push @target_modules, [$i->[0], $i->[2]];
41    },    },
42    'debug' => \$Opt{debug},    'debug' => \$Opt{debug},
43    'dis-file-suffix=s' => \$Opt{dis_suffix},    'dis-file-suffix=s' => \$Opt{dis_suffix},
# Line 38  GetOptions ( Line 45  GetOptions (
45    'dafs-file-suffix=s' => \$Opt{dafs_suffix},    'dafs-file-suffix=s' => \$Opt{dafs_suffix},
46    'dafx-file-suffix=s' => \$Opt{dafx_suffix},    'dafx-file-suffix=s' => \$Opt{dafx_suffix},
47    'help' => \$Opt{help},    'help' => \$Opt{help},
48      'load-module=s' => sub {
49        shift;
50        my $i = [split /\s+/, shift, 2];
51        push @target_modules, [$i->[0], $i->[1]];
52      },
53      'mod-file-suffix=s' => \$Opt{mod_suffix},
54    'search-path|I=s' => sub {    'search-path|I=s' => sub {
55      shift;      shift;
56      my @value = split /\s+/, shift;      my @value = split /\s+/, shift;
# Line 85  $Opt{dis_suffix} = '.dis' unless defined Line 98  $Opt{dis_suffix} = '.dis' unless defined
98  $Opt{daem_suffix} = '.dafm' unless defined $Opt{daem_suffix};  $Opt{daem_suffix} = '.dafm' unless defined $Opt{daem_suffix};
99  $Opt{dafx_suffix} = '.dafx' unless defined $Opt{dafx_suffix};  $Opt{dafx_suffix} = '.dafx' unless defined $Opt{dafx_suffix};
100  $Opt{dafs_suffix} = '.dafs' unless defined $Opt{dafs_suffix};  $Opt{dafs_suffix} = '.dafs' unless defined $Opt{dafs_suffix};
101    $Opt{mod_suffix} = '.mod' unless defined $Opt{mod_suffix};
102  $Message::DOM::DOMFeature::DEBUG = 1 if $Opt{debug};  $Message::DOM::DOMFeature::DEBUG = 1 if $Opt{debug};
103  require Error;  require Error;
104  $Error::Debug = 1 if $Opt{debug};  $Error::Debug = 1 if $Opt{debug};
# Line 118  my $start_time; Line 132  my $start_time;
132  BEGIN { $start_time = time }  BEGIN { $start_time = time }
133    
134  use Message::Util::DIS::DNLite;  use Message::Util::DIS::DNLite;
 use Message::Util::PerlCode;  
135    
136  my %feature;  my %feature;
 eval q{  
   use Message::Util::DIS::Test;  
   use Message::DOM::GenericLS;  
   $feature{ExpandedURI q<fe:GenericLS>} = '3.0';  
   $feature{'+' . ExpandedURI q<DIS:TDT>} = '1.0';  
 };  
137    
138  my $limpl = $Message::DOM::ImplementationRegistry->get_implementation  for (@{$Opt{create_module}}) {
139      my (undef, undef, undef, $out_type) = @$_;
140    
141      if ($out_type eq 'perl-pm') {
142        require 'manakai/daf-perl-pm.pl';
143        $feature{'+' . ExpandedURI q<Util:PerlCode>} = '1.0';
144      } elsif ($out_type eq 'perl-t') {
145        require 'manakai/daf-perl-t.pl';
146        $feature{ExpandedURI q<fe:GenericLS>} = '3.0';
147        $feature{'+' . ExpandedURI q<DIS:TDT>} = '1.0';
148        $feature{'+' . ExpandedURI q<Util:PerlCode>} = '1.0';
149      } elsif ($out_type eq 'dtd-modules') {
150        require 'manakai/daf-dtd-modules.pl';
151        $feature{ExpandedURI q<fe:GenericLS>} = '3.0';
152        $feature{'+' . ExpandedURI q<fe:XDP>} = '3.0';
153      }
154    }
155    
156    our $limpl = $Message::DOM::ImplementationRegistry->get_implementation
157                             ({ExpandedURI q<fe:Min> => '3.0',                             ({ExpandedURI q<fe:Min> => '3.0',
158                               '+' . ExpandedURI q<DIS:DNLite> => '1.0',                               '+' . ExpandedURI q<DIS:DNLite> => '1.0',
159                               '+' . ExpandedURI q<DIS:Core> => '1.0',                               '+' . ExpandedURI q<DIS:Core> => '1.0',
                              '+' . ExpandedURI q<Util:PerlCode> => '1.0',  
160                               %feature,                               %feature,
161                             });                             });
162  my $impl = $limpl->get_feature (ExpandedURI q<DIS:Core> => '1.0');  our $impl = $limpl->get_feature (ExpandedURI q<DIS:Core> => '1.0');
 my $tdt_parser;  
163    
164  ## --- Loading and Updating the Database  ## --- Loading and Updating the Database
165    
166  my $HasError;  my $HasError;
167  my $db = $impl->create_dis_database;  our $db = $impl->create_dis_database;
168  $db->pl_database_module_resolver (\&daf_db_module_resolver);  $db->pl_database_module_resolver (\&daf_db_module_resolver);
169  $db->dom_config->set_parameter ('error-handler' => \&daf_on_error);  $db->dom_config->set_parameter ('error-handler' => \&daf_on_error);
170    
# Line 156  my %ModuleNameNamespaceBinding = ( Line 179  my %ModuleNameNamespaceBinding = (
179      ## property.      ## property.
180  );  );
181    
 my @target_modules;  
 for (@{$Opt{create_module}}) {  
   my ($mod_uri, $out_path, $mod_for, $out_type) = @$_;  
   push @target_modules, [$mod_uri, $mod_for];  
 }  
   
182  my $ResourceCount = 0;  my $ResourceCount = 0;
183  $db->pl_update_module (\@target_modules,  $db->pl_update_module (\@target_modules,
184  get_module_index_file_name => sub {  get_module_index_file_name => sub {
# Line 287  exit $HasError if $HasError; Line 304  exit $HasError if $HasError;
304    
305  for (@{$Opt{create_module}}) {  for (@{$Opt{create_module}}) {
306    my ($mod_uri, $out_file_path, $mod_for, $out_type) = @$_;    my ($mod_uri, $out_file_path, $mod_for, $out_type) = @$_;
   unless (defined $mod_for) {  
     $mod_for = $db->get_module ($mod_uri)  
                   ->get_property_text (ExpandedURI q<dis:DefaultFor>,  
                                        ExpandedURI q<ManakaiDOM:all>);  
   }  
   my $mod = $db->get_module ($mod_uri, for_arg => $mod_for);  
307    
308    if ($out_type eq 'perl-pm') {    if ($out_type eq 'perl-pm') {
309      status_msg_ qq<Generating Perl module from <$mod_uri> for <$mod_for>...>;      daf_perl_pm ($mod_uri, $out_file_path, $mod_for);
     local $Message::Util::DIS::Perl::Implementation  
         = $impl->get_feature (ExpandedURI q<Util:PerlCode> => '1.0');  
     my $pl = $mod->pl_generate_perl_module_file  
                     ($impl->get_feature (ExpandedURI q<Util:PerlCode> => '1.0'));  
     status_msg qq<done>;  
   
     my $output;  
     defined $out_file_path  
         ? (open $output, '>', $out_file_path or die "$0: $out_file_path: $!")  
         : ($output = \*STDOUT);  
   
     status_msg_ sprintf qq<Writing Perl module %s...>,  
                           defined $out_file_path  
                             ? q<">.$out_file_path.q<">  
                             : 'to stdout';  
     print $output $pl->stringify;  
     close $output;  
     status_msg q<done>;  
310    } elsif ($out_type eq 'perl-t') {    } elsif ($out_type eq 'perl-t') {
311      status_msg_ qq<Generating Perl test from <$mod_uri> for <$mod_for>...>;      daf_perl_t ($mod_uri, $out_file_path, $mod_for);
312      my $pl = daf_generate_perl_test_file ($mod);    } elsif ($out_type eq 'dtd-modules') {
313      status_msg qq<done>;      daf_dtd_modules ($mod_uri, $out_file_path, $mod_for);
   
     my $cfg = $pl->owner_document->dom_config;  
     $cfg->set_parameter (ExpandedURI q<pc:preserve-line-break> => 1);  
   
     my $output;  
     defined $out_file_path  
         ? (open $output, '>', $out_file_path or die "$0: $out_file_path: $!")  
           : ($output = \*STDOUT);  
   
     status_msg_ sprintf qq<Writing Perl test %s...>,  
                           defined $out_file_path  
                             ? q<">.$out_file_path.q<">  
                             : 'to stdout';  
     print $output $pl->stringify;  
     close $output;  
     status_msg q<done>;  
314    }    }
315  }  }
316    
# Line 593  sub daf_check_undefined () { Line 570  sub daf_check_undefined () {
570    }    }
571  } # daf_check_undefined  } # daf_check_undefined
572    
 sub daf_generate_perl_test_file ($) {  
   my $mod = shift;  
   my $pc = $impl->get_feature (ExpandedURI q<Util:PerlCode> => '1.0');  
   local $Message::Util::DIS::Perl::Implementation = $pc;  
   my $pl = $pc->create_pc_file;  
   my $factory = $pl->owner_document;  
   my $pack = $pl->get_last_package ("Manakai::Test", make_new_package => 1);  
   $pack->add_use_perl_module_name ("Message::Util::DIS::Test");  
   $pack->add_use_perl_module_name ("Message::Util::Error");  
   $pack->add_require_perl_module_name ($mod->pl_fully_qualified_name);  
   
   $pl->source_file ($mod->get_property_text (ExpandedURI q<DIS:sourceFile>, ""));  
   $pl->source_module ($mod->name_uri);  
   $pl->source_for ($mod->for_uri);  
   $pl->license_uri ($mod->get_property_resource (ExpandedURI q<dis:License>)  
                         ->uri);  
   
   $pack->append_code ('  
     use Getopt::Long;  
     my %Skip;  
     GetOptions (  
       "Skip=s" => sub {  
         shift;  
         for (split /\s+/, shift) {  
           if (/^(\d+)-(\d+)$/) {  
             $Skip{$_} = 1 for $1..$2;  
           } else {  
             $Skip{$_} = 1;  
           }  
         }  
       },  
     );  
   ');  
   
   $pack->append_child ($factory->create_pc_statement)  
        ->append_code  
            ('my $impl = $Message::DOM::ImplementationRegistry  
                             ->get_implementation ({  
                 "http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#Test"  
                     => "1.0",  
             })');  
   
   my $num_statement = $pack->append_child ($factory->create_pc_statement);  
   $num_statement->append_code ('my $test = $impl->create_test_manager');  
   
   my $total_tests = 0;  
   my %processed;  
   for my $res (@{$mod->get_resource_list}) {  
     next if $res->owner_module ne $mod or $processed{$res->uri};  
     $processed{$res->uri} = 1;  
   
     if ($res->is_type_uri (ExpandedURI q<test:Test>)) {  
       if ($res->is_type_uri (ExpandedURI q<test:StandaloneTest>)) {  
         my $test_num = ++$total_tests;  
         my $test_uri = $res->name_uri || $res->uri;  
   
         $pack->append_code ('$test->start_new_test (');  
         $pack->append_new_pc_literal ($test_uri);  
         $pack->append_code (');');  
   
         $pack->append_code ('if (not $Skip{'.$test_num.'} and not $Skip{');  
         $pack->append_new_pc_literal ($test_uri);  
         $pack->append_code ('}) {');  
           
         $pack->append_code ('try {');  
           
         my $test_pc = $res->pl_code_fragment ($factory);  
         if (not defined $test_pc) {  
           die "Perl test code not defined for <".$res->uri.">";  
         }  
           
         $pack->append_child ($test_pc);  
           
         $pack->append_code ('$test->ok;');  
           
         $pack->append_code ('} catch Message::Util::IF::DTException with {  
           ##  
         } otherwise {  
           my $err = shift;  
           warn $err;  
           $test->not_ok;  
         };');  
   
         $pack->append_code ('} else { warn "'.$test_num.' skipped\n" }');  
   
       } elsif ($res->is_type_uri (ExpandedURI q<test:ParserTestSet>)) {  
         my $block = $pack->append_new_pc_block;  
         my @test;  
           
         $tdt_parser ||= $limpl->create_gls_parser  
                                  ({  
                                    ExpandedURI q<DIS:TDT> => '1.0',  
                                   });  
         for my $tres (@{$res->get_child_resource_list_by_type  
                                 (ExpandedURI q<test:ParserTest>)}) {  
           $total_tests++;  
           push @test, my $ttest = {entity => {}};  
           $ttest->{uri} = $tres->uri;  
           for my $eres (@{$tres->get_child_resource_list_by_type  
                                    (ExpandedURI q<test:Entity>)}) {  
             my $tent = $ttest->{entity}->{$eres->uri} = {};  
             for (ExpandedURI q<test:uri>, ExpandedURI q<test:baseURI>,  
                  ExpandedURI q<test:value>, ExpandedURI q<xp:encoding>) {  
               my $v = $eres->get_property_text ($_);  
               $tent->{$_} = $v if defined $v;  
             }  
             $ttest->{root_uri} = $eres->uri  
               if $eres->is_type_uri (ExpandedURI q<test:RootEntity>) or  
                  not defined $ttest->{root_uri};  
           }  
   
           ## DOM configuration parameters  
           for my $v (@{$tres->get_property_value_list  
                               (ExpandedURI q<c:anyDOMConfigurationParameter>)}) {  
             my $cpuri = $v->name;  
             my $cp = $db->get_resource ($cpuri, for_arg => $tres->for_uri);  
             $ttest->{dom_config}->{$cp->get_dom_configuration_parameter_name}  
               = $v->get_perl_code ($block->owner_document, $tres);  
           }  
   
           ## Result DOM tree  
           my $tree_t = $tres->get_property_text (ExpandedURI q<test:domTree>);  
           if (defined $tree_t) {  
             $ttest->{dom_tree} = $tdt_parser->parse_string ($tree_t);  
           }  
   
           ## Expected |DOMError|s  
           for (@{$tres->get_property_value_list (ExpandedURI q<c:erred>)}) {  
             my $err = $tdt_parser->parse_tdt_error_string  
                                      ($_->string_value, $db, $_,  
                                       undef, $tres->for_uri);  
             push @{$ttest->{dom_error}->{$err->{type}->{value}} ||= []}, $err;  
           }  
         }  
   
         for ($block->append_statement  
                    ->append_new_pc_expression ('=')) {  
           $_->append_new_pc_variable ('$', undef, 'TestData')  
             ->variable_scope ('my');  
           $_->append_new_pc_literal (\@test);  
         }  
           
         my $plc = $res->pl_code_fragment ($factory);  
         unless ($plc) {  
           die "Resource <".$res->uri."> does not have Perl test code";  
         }  
   
         $block->append_child ($plc);  
           
       } # test resource type  
     } # test:Test  
   }  
     
   $num_statement->append_code (' (' . $total_tests . ')');  
   
   return $pl;  
 } # daf_generate_perl_test_file  
   
573  __END__  __END__
574    
575  =head1 NAME  =head1 NAME
# Line 791  option is specified, the database file i Line 610  option is specified, the database file i
610  and then I<input.dis> file is loaded in the context of it.  and then I<input.dis> file is loaded in the context of it.
611  Otherwise, a new database is created.  Otherwise, a new database is created.
612    
 =item C<--output-file-name=I<file-name>> (Required)  
   
 The  
   
613  =back  =back
614    
615  =head1 SEE ALSO  =head1 SEE ALSO
616    
 L<bin/dac2pm.pl> - Generating Perl module from "dac" file.  
   
617  L<lib/Message/Util/DIS.dis> - The actual implementation  L<lib/Message/Util/DIS.dis> - The actual implementation
618  of the "dis" interpretation.  of the "dis" interpretation.
619    
620  =head1 LICENSE  =head1 LICENSE
621    
622  Copyright 2004-2005 Wakaba <w@suika.fam.cx>.  All rights reserved.  Copyright 2004-2006 Wakaba <w@suika.fam.cx>.  All rights reserved.
623    
624  This program is free software; you can redistribute it and/or  This program is free software; you can redistribute it and/or
625  modify it under the same terms as Perl itself.  modify it under the same terms as Perl itself.

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.15

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24