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

Contents of /messaging/manakai/bin/dac2pm.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (hide annotations) (download)
Sun Oct 16 06:08:22 2005 UTC (19 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.12: +12 -1 lines
File MIME type: text/plain
++ manakai/bin/ChangeLog	16 Oct 2005 06:05:54 -0000
2005-10-16  Wakaba  <wakaba@suika.fam.cx>

	* dac2pm.pl (--output-line): New option.

++ manakai/lib/Message/Util/ChangeLog	16 Oct 2005 06:07:43 -0000
2005-10-16  Wakaba  <wakaba@suika.fam.cx>

	* PerlCode.dis (pc:line): New configuration parameter.
	(stringify): Don't output "#line" directive
	unless "pc:line" parameter is set to "true".
	(pc:split-resolver): New configuration parameter.

++ manakai/lib/Message/DOM/ChangeLog	16 Oct 2005 06:06:20 -0000
2005-10-16  Wakaba  <wakaba@suika.fam.cx>

	* DOMCore.dis (DOMConfiguration): Extends "ManakaiDOM:ManakaiDOMObject".

1 wakaba 1.1 #!/usr/bin/perl -w
2     use strict;
3    
4     =head1 NAME
5    
6 wakaba 1.5 dac2pm - Generating Perl Module from "dac" File
7 wakaba 1.1
8     =head1 SYNOPSIS
9    
10 wakaba 1.5 perl path/to/dac2pm.pl input.dac \
11     --module-uri=module-uri [--for=for-uri] [options] > ModuleName.pm
12     perl path/to/dac2pm.pl input.dac \
13     --module-uri=module-uri [--for=for-uri] [options] \
14     --output-file-path=ModuleName.pm
15 wakaba 1.8 perl path/to/dac2pm.pl input.dac \
16     --create-perl-module="module-uri ModuleName.pm [for-uri]" \
17     [--create-perl-module="..." ...]
18 wakaba 1.5 perl path/to/dac2pm.pl --help
19 wakaba 1.1
20     =head1 DESCRIPTION
21    
22 wakaba 1.8 The C<dac2pm.pl> script generates Perl modules from a "dac" database file
23     created by C<dac.pl>.
24 wakaba 1.1
25     This script is part of manakai.
26    
27     =cut
28    
29     use strict;
30     use Message::Util::QName::Filter {
31     DIS => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#>,
32     dis => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#dis-->,
33     ManakaiDOM => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/manakai-dom#>,
34     Markup => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Markup#>,
35 wakaba 1.13 pc => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/PerlCode#>,
36 wakaba 1.1 Util => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/>,
37     };
38    
39     =head1 OPTIONS
40    
41     =over 4
42    
43     =item --enable-assertion / --noenable-assertion (default)
44    
45     Whether assertion codes should be outputed or not.
46    
47 wakaba 1.8 =item --create-perl-module="I<module-uri> I<ModuleName.pm> [I<for-uri>]" (Zero or more)
48    
49     The C<--create-perl-module> option can be used to specify
50     I<--module-uri>, I<--output-file-path>, and I<--for> options
51     once. Its value is a space-separated triplet of "dis" module name URI,
52     Perl module file path (environment dependent), and optional
53     "dis" module "for" URI.
54    
55     This option can be specified more than once; it would
56     make multiple Perl module files to be created. If
57     both I<--module-uri> and this options are specified,
58     I<--module-uri>, I<--output-file-path>, and I<--for>
59     options are treated as if there is another I<--create-perl-module>
60     option specified.
61    
62 wakaba 1.1 =item --for=I<for-uri> (Optional)
63    
64     Specifies the "For" URI reference for which the outputed module is.
65     If this parameter is ommitted, the default "For" URI reference
66 wakaba 1.5 for the module specified by the C<dis:DefaultFor> attribute
67     of the C<dis:Module> element, if any, or C<ManakaiDOM:all> is assumed.
68 wakaba 1.1
69     =item --help
70    
71     Shows the help message.
72    
73     =item --module-uri=I<module-uri>
74    
75 wakaba 1.5 A URI reference that identifies a module from which a Perl
76     module file is generated. This argument is I<required>.
77 wakaba 1.4
78 wakaba 1.5 =item --output-file-path=I<perl-module-file-path> (default: the standard output)
79 wakaba 1.1
80 wakaba 1.5 A platform-dependent file path to which the Perl module
81     is written down.
82 wakaba 1.1
83 wakaba 1.13 =item C<--output-line> / C<--nooutput-line> (default: C<--nooutput-line>)
84    
85     Whether C<#line> directives should be included to the generated
86     Perl module files.
87    
88 wakaba 1.1 =item --verbose / --noverbose (default)
89    
90     Whether a verbose message mode should be selected or not.
91    
92     =back
93    
94     =cut
95    
96     use Getopt::Long;
97     use Pod::Usage;
98 wakaba 1.8 my %Opt = (
99     create_module => [],
100     );
101 wakaba 1.1 GetOptions (
102 wakaba 1.8 'create-perl-module=s' => sub {
103     shift;
104     push @{$Opt{create_module}}, [split /\s+/, shift, 3];
105     },
106 wakaba 1.10 'dis-file-suffix=s' => \$Opt{dis_suffix},
107     'daem-file-suffix=s' => \$Opt{daem_suffix},
108 wakaba 1.9 'debug' => \$Opt{debug},
109 wakaba 1.1 'enable-assertion!' => \$Opt{outputAssertion},
110     'for=s' => \$Opt{For},
111     'help' => \$Opt{help},
112     'module-uri=s' => \$Opt{module_uri},
113 wakaba 1.4 'output-file-path=s' => \$Opt{output_file_name},
114 wakaba 1.13 'output-line' => \$Opt{output_line},
115 wakaba 1.10 'search-path|I=s' => sub {
116     shift;
117     my @value = split /\s+/, shift;
118     while (my ($ns, $path) = splice @value, 0, 2, ()) {
119     unless (defined $path) {
120     die qq[$0: Search-path parameter without path: "$ns"];
121     }
122     push @{$Opt{input_search_path}->{$ns} ||= []}, $path;
123     }
124     },
125     'search-path-catalog-file-name=s' => sub {
126     shift;
127     require File::Spec;
128     my $path = my $path_base = shift;
129     $path_base =~ s#[^/]+$##;
130     $Opt{search_path_base} = $path_base;
131     open my $file, '<', $path or die "$0: $path: $!";
132     while (<$file>) {
133     if (s/^\s*\@//) { ## Processing instruction
134     my ($target, $data) = split /\s+/;
135     if ($target eq 'base') {
136     $Opt{search_path_base} = File::Spec->rel2abs ($data, $path_base);
137     } else {
138     die "$0: $target: Unknown target";
139     }
140     } elsif (/^\s*\#/) { ## Comment
141     #
142     } elsif (/\S/) { ## Catalog entry
143     s/^\s+//;
144     my ($ns, $path) = split /\s+/;
145     push @{$Opt{input_search_path}->{$ns} ||= []},
146     File::Spec->rel2abs ($path, $Opt{search_path_base});
147     }
148     }
149     ## NOTE: File paths with SPACEs are not supported
150     ## NOTE: Future version might use file: URI instead of file path.
151     },
152     'verbose!' => \$Opt{verbose},
153 wakaba 1.1 ) or pod2usage (2);
154     pod2usage ({-exitval => 0, -verbose => 1}) if $Opt{help};
155     $Opt{file_name} = shift;
156     pod2usage ({-exitval => 2, -verbose => 0}) unless $Opt{file_name};
157 wakaba 1.9 $Message::DOM::DOMFeature::DEBUG = 1 if $Opt{debug};
158 wakaba 1.10 $Opt{daem_suffix} = '.daem' unless defined $Opt{daem_suffix};
159 wakaba 1.11 $Opt{dis_suffix} = '.dis' unless defined $Opt{dis_suffix};
160 wakaba 1.8
161     if ($Opt{module_uri}) {
162     push @{$Opt{create_module}},
163     [$Opt{module_uri}, $Opt{output_file_name}, $Opt{For}];
164     }
165    
166     pod2usage (2) unless @{$Opt{create_module}};
167    
168     sub status_msg ($) {
169     my $s = shift;
170     $s .= "\n" unless $s =~ /\n$/;
171     print STDERR $s;
172     }
173    
174     sub status_msg_ ($) {
175     my $s = shift;
176     print STDERR $s;
177     }
178    
179     sub verbose_msg ($) {
180     my $s = shift;
181     $s .= "\n" unless $s =~ /\n$/;
182     print STDERR $s if $Opt{verbose};
183     }
184    
185     sub verbose_msg_ ($) {
186     my $s = shift;
187     print STDERR $s if $Opt{verbose};
188     }
189 wakaba 1.1
190     ## TODO: Assertion control
191    
192 wakaba 1.8 use Message::Util::DIS::DNLite;
193 wakaba 1.1
194 wakaba 1.12 my $start_time;
195     BEGIN { $start_time = time }
196    
197 wakaba 1.5 my $impl = $Message::DOM::ImplementationRegistry->get_implementation
198 wakaba 1.1 ({
199     ExpandedURI q<ManakaiDOM:Minimum> => '3.0',
200     '+' . ExpandedURI q<DIS:Core> => '1.0',
201     '+' . ExpandedURI q<Util:PerlCode> => '1.0',
202     });
203     my $pc = $impl->get_feature (ExpandedURI q<Util:PerlCode> => '1.0');
204     my $di = $impl->get_feature (ExpandedURI q<DIS:Core> => '1.0');
205    
206 wakaba 1.10 status_msg_ qq<Loading the database "$Opt{file_name}"...>;
207     my $db = $di->pl_load_dis_database ($Opt{file_name}, sub ($$) {
208     my ($db, $mod) = @_;
209     my $ns = $mod->namespace_uri;
210     my $ln = $mod->local_name;
211     verbose_msg qq<Database module <$ns$ln> is requested>;
212     my $name = dac_search_file_path_stem ($ns, $ln, $Opt{daem_suffix});
213     if (defined $name) {
214     return $name.$Opt{daem_suffix};
215     } else {
216     return $ln.$Opt{daem_suffix};
217     }
218     });
219     status_msg q<done>;
220 wakaba 1.1
221 wakaba 1.8 for (@{$Opt{create_module}}) {
222     my ($mod_uri, $out_file_path, $mod_for) = @$_;
223    
224     my $mod = $db->get_module ($mod_uri, for_arg => $mod_for);
225     unless ($mod_for) {
226     $mod_for = $mod->get_property_text (ExpandedURI q<dis:DefaultFor>, undef);
227     if (defined $mod_for) {
228     $mod = $db->get_module ($mod_uri, for_arg => $mod_for);
229 wakaba 1.6 }
230 wakaba 1.1 }
231 wakaba 1.8 unless ($mod->is_defined) {
232     die qq<$0: Module <$mod_uri> for <$mod_for> is not defined>;
233     }
234 wakaba 1.1
235 wakaba 1.8 status_msg_ qq<Generating Perl module from <$mod_uri> for <$mod_for>...>;
236     my $pl = $mod->pl_generate_perl_module_file;
237     status_msg qq<done>;
238    
239     my $output;
240     defined $out_file_path
241     ? (open $output, '>', $out_file_path or die "$0: $out_file_path: $!")
242 wakaba 1.4 : ($output = \*STDOUT);
243 wakaba 1.13
244     if ($Opt{output_line}) {
245     $pl->owner_document->dom_config->set_parameter (ExpandedURI q<pc:line> => 1);
246     }
247 wakaba 1.8
248     status_msg_ sprintf qq<Writing Perl module %s...>,
249     defined $out_file_path
250     ? q<">.$out_file_path.q<">
251     : 'to stdout';
252     print $output $pl->stringify;
253     close $output;
254     status_msg q<done>;
255     } # create_module
256 wakaba 1.4
257 wakaba 1.8 status_msg_ "Checking undefined resources...";
258 wakaba 1.1 $db->check_undefined_resource;
259 wakaba 1.8 status_msg q<done>;
260 wakaba 1.5
261 wakaba 1.8 status_msg_ "Closing the database...";
262 wakaba 1.5 $db->free;
263 wakaba 1.7 undef $db;
264 wakaba 1.8 status_msg q<done>;
265 wakaba 1.12
266     END {
267     use integer;
268     my $time = time - $start_time;
269     status_msg sprintf qq<%d'%02d''>, $time / 60, $time % 60;
270     }
271 wakaba 1.10 exit;
272    
273     sub dac_search_file_path_stem ($$$) {
274     my ($ns, $ln, $suffix) = @_;
275     require Cwd;
276     require File::Spec;
277     for my $dir ('.', @{$Opt{input_search_path}->{$ns}||[]}) {
278     my $name = Cwd::abs_path
279     (File::Spec->canonpath
280     (File::Spec->catfile ($dir, $ln)));
281     if (-f $name.$suffix) {
282     return $name;
283     }
284     }
285     return undef;
286     } # dac_search_file_path_stem;
287 wakaba 1.1
288     =head1 SEE ALSO
289    
290     L<lib/Message/Util/DIS.dis> - The <QUOTE::dis> object implementation.
291    
292 wakaba 1.8 L<lib/Message/Util/DIS/Perl.dis> - The <QUOTE::dis> object implementation,
293     submodule for Perl modules.
294    
295 wakaba 1.1 L<lib/Message/Util/PerlCode.dis> - The Perl code generator.
296    
297     L<lib/manakai/DISCore.dis> - The definition for the "dis" format.
298    
299     L<lib/manakai/DISPerl.dis> - The definition for the "dis" Perl-specific
300     vocabulary.
301    
302 wakaba 1.5 L<bin/dac.pl> - The "dac" database generator.
303    
304 wakaba 1.1 =head1 LICENSE
305    
306     Copyright 2004-2005 Wakaba <w@suika.fam.cx>. All rights reserved.
307    
308     This program is free software; you can redistribute it and/or
309     modify it under the same terms as Perl itself.
310    
311     =cut
312    
313 wakaba 1.13 1; # $Date: 2005/10/12 00:30:54 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24