#!/usr/bin/perl -w use strict; use Message::Util::QName::Filter { d => q, dis2pm => q, DOMCore => q, DOMMain => q, lang => q, Perl => q, license => q, ManakaiDOM => q, owl => q, rdf => q, rdfs => q, }; use Getopt::Long; use Pod::Usage; use Storable; my %Opt; GetOptions ( 'for=s' => \$Opt{For}, 'help' => \$Opt{help}, 'module-name=s' => \$Opt{module_name}, 'module-uri=s' => \$Opt{module_uri}, 'verbose!' => $Opt{verbose}, ) or pod2usage (2); pod2usage ({-exitval => 0, -verbose => 1}) if $Opt{help}; $Opt{file_name} = shift; pod2usage ({-exitval => 2, -verbose => 0}) unless $Opt{file_name}; pod2usage (2) if not $Opt{module_uri} and not $Opt{module_name}; BEGIN { require 'manakai/genlib.pl'; require 'manakai/dis.pl'; } our $State = retrieve ($Opt{file_name}) or die "$0: $Opt{file_name}: Cannot load"; our $result = ''; eval q{ sub impl_msg ($;%) { warn shift () . "\n"; } } unless $Opt{verbose}; sub perl_change_package (%) { my %opt = @_; my $fn = $opt{full_name}; impl_err (qq<$fn: Bad package name>) unless $fn; unless ($fn eq $State->{ExpandedURI q}) { $State->{ExpandedURI q} = $fn; return perl_statement qq; } else { return ''; } } # perl_change_package ## Outputed module and "For" my $mf = dis_get_module_uri (module_name => $Opt{module_name}, module_uri => $Opt{module_uri}, For => $Opt{For}); $State->{DefaultFor} = $mf->{For}; $State->{module} = $mf->{module}; $State->{ExpandedURI q} = 'main'; $result .= "#!/usr/bin/perl \n"; $result .= perl_comment q . "\n" . q<"> . $Opt{file_name} . q<" at > . rfc3339_date (time) . qq<.\n> . q; $result .= perl_comment qq{Module <$State->{module}>}; $result .= perl_comment qq{For <$State->{DefaultFor}>}; $result .= perl_statement q; $result .= perl_change_package (full_name => $State->{Module}->{$State->{module}} ->{ExpandedURI q}); $result .= perl_statement perl_assign perl_var (type => '$', local_name => 'VERSION', scope => 'our') => perl_literal version_date time; for my $pack (values %{$State->{Module}->{$State->{module}} ->{ExpandedURI q}||{}}) { next unless defined $pack->{Name}; if ({ ExpandedURI q => 1, ExpandedURI q => 1, ExpandedURI q => 1, ExpandedURI q => 1, ExpandedURI q => 1, }->{$pack->{ExpandedURI q}}) { ## Package name and version $result .= perl_change_package (full_name => $pack->{ExpandedURI q}); $result .= perl_statement perl_assign perl_var (type => '$', local_name => 'VERSION', scope => 'our') => perl_literal version_date time; ## Inheritance my $isa = []; for my $uri (@{$pack->{ISA}||[]}, @{$pack->{Implement}||[]}) { my $pack = $State->{Type}->{$uri}; if (defined $pack->{ExpandedURI q}) { push @$isa, $pack->{ExpandedURI q}; } else { impl_msg ("Inheriting package name for <$uri> not defined", node => $pack->{src}) if $Opt{verbose}; } } $isa = array_uniq $isa; $result .= perl_inherit $isa; $result .= '$' . $_ . "::;\n" for @$isa; ## Members if ({ ExpandedURI q => 1, ExpandedURI q => 1, ExpandedURI q => 1, }->{$pack->{ExpandedURI q}}) { for my $method (values %{$pack->{ExpandedURI q}}) { next unless defined $method->{Name}; if ($method->{ExpandedURI q} eq ExpandedURI q) { $result .= perl_sub (name => $method->{ExpandedURI q}, code => ''); } elsif ($method->{ExpandedURI q} eq ExpandedURI q) { $result .= perl_sub (name => $method->{ExpandedURI q}, 'prototype' => (defined $method->{ExpandedURI q} ->{Name} ? '$;$' : '$'), code => ''); } } # package method ## TODO: Const } ## TODO: Const } # root object } ## Export if (keys %{$State->{perl_primary_module}->{perl_export_ok}||{}}) { $result .= perl_change_package full_name => $State->{perl_primary_module}->{perl_package_name}; $result .= perl_statement 'require Exporter'; $result .= perl_inherit ['Exporter']; $result .= perl_statement perl_assign perl_var (type => '@', scope => 'our', local_name => 'EXPORT_OK') => '(' . perl_list (keys %{$State->{perl_primary_module} ->{perl_export_ok}}) . ')'; if (keys %{$State->{perl_primary_module}->{perl_export_tags}||{}}) { $result .= perl_statement perl_assign perl_var (type => '%', scope => 'our', local_name => 'EXPORT_TAGS') => '(' . perl_list (map { $_ => [keys %{$State->{perl_primary_module} ->{perl_export_tags}->{$_}}] } keys %{$State->{perl_primary_module} ->{perl_export_tags}}) . ')'; } } $result .= perl_statement 1; output_result $result; 1;