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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations) (download)
Mon Mar 6 07:32:51 2006 UTC (18 years, 8 months ago) by wakaba
Branch: MAIN
Changes since 1.3: +0 -1 lines
File MIME type: text/plain
++ manakai/bin/ChangeLog	6 Mar 2006 07:30:27 -0000
2006-03-06  Wakaba  <wakaba@suika.fam.cx>

	* daf.pl (get_referring_module_uri_list): Don't
	invoke |daf_convert_dis_document_to_dnl_document| to prevent
	prefix-not-defined error because the referred module is not
	yet loaded.

++ manakai/lib/Message/DOM/ChangeLog	6 Mar 2006 07:32:43 -0000
2006-03-06  Wakaba  <wakaba@suika.fam.cx>

	* DOMXML.dis (ManakaiDOMXMLIdAttr): New class.

	* Tree.dis (createAttribute, createAttributeNS,
	setAttribute, setAttributeNS): They now supports
	the |cfg:xml-id| configuration parameter.
	(cfg:xml-id): New configuration parameter.

	* XDoctype.dis (createAttributeDefinition): It now
	supports the |cfg:xml-id| configuration parameter.

	* XMLParser.dis (parse): It now supports
	the |cfg:xml-id| configuration parameter.

1 wakaba 1.1 #!/usr/bin/perl -w
2     use strict;
3     use Message::Util::QName::Filter {
4 wakaba 1.2 c => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/dom-core#>,
5 wakaba 1.1 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-->,
7 wakaba 1.2 DOMLS => q<http://suika.fam.cx/~wakaba/archive/2004/dom/ls#>,
8 wakaba 1.1 dp => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#Perl/>,
9     fe => q<http://suika.fam.cx/www/2006/feature/>,
10     ManakaiDOM => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/manakai-dom#>,
11 wakaba 1.2 pc => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/PerlCode#>,
12 wakaba 1.1 swcfg21 => q<http://suika.fam.cx/~wakaba/archive/2005/swcfg21#>,
13 wakaba 1.2 test => q<http://suika.fam.cx/~wakaba/archive/2004/dis/Test#>,
14 wakaba 1.1 Util => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/>,
15     };
16    
17     use Cwd;
18     use Getopt::Long;
19     use Pod::Usage;
20     my %Opt = (create_module => []);
21     GetOptions (
22     'create-perl-module=s' => sub {
23     shift;
24     my $i = [split /\s+/, shift, 3];
25 wakaba 1.2 $i->[3] = 'perl-pm';
26     push @{$Opt{create_module}}, $i;
27     },
28     'create-perl-test=s' => sub {
29     shift;
30     my $i = [split /\s+/, shift, 3];
31     $i->[3] = 'perl-t';
32 wakaba 1.1 push @{$Opt{create_module}}, $i;
33     },
34     'debug' => \$Opt{debug},
35     'dis-file-suffix=s' => \$Opt{dis_suffix},
36     'daem-file-suffix=s' => \$Opt{daem_suffix},
37     'dafx-file-suffix=s' => \$Opt{dafx_suffix},
38     'help' => \$Opt{help},
39     'search-path|I=s' => sub {
40     shift;
41     my @value = split /\s+/, shift;
42     while (my ($ns, $path) = splice @value, 0, 2, ()) {
43     unless (defined $path) {
44     die qq[$0: Search-path parameter without path: "$ns"];
45     }
46     push @{$Opt{input_search_path}->{$ns} ||= []}, $path;
47     }
48     },
49     'search-path-catalog-file-name=s' => sub {
50     shift;
51     require File::Spec;
52     my $path = my $path_base = shift;
53     $path_base =~ s#[^/]+$##;
54     $Opt{search_path_base} = $path_base;
55     open my $file, '<', $path or die "$0: $path: $!";
56     while (<$file>) {
57     if (s/^\s*\@//) { ## Processing instruction
58     my ($target, $data) = split /\s+/;
59     if ($target eq 'base') {
60     $Opt{search_path_base} = File::Spec->rel2abs ($data, $path_base);
61     } else {
62     die "$0: $target: Unknown target";
63     }
64     } elsif (/^\s*\#/) { ## Comment
65     #
66     } elsif (/\S/) { ## Catalog entry
67     s/^\s+//;
68     my ($ns, $path) = split /\s+/;
69     push @{$Opt{input_search_path}->{$ns} ||= []},
70     File::Spec->rel2abs ($path, $Opt{search_path_base});
71     }
72     }
73     ## NOTE: File paths with SPACEs are not supported
74     ## NOTE: Future version might use file: URI instead of file path.
75     },
76     'undef-check!' => \$Opt{no_undef_check},
77     'verbose!' => \$Opt{verbose},
78     ) or pod2usage (2);
79     pod2usage ({-exitval => 0, -verbose => 1}) if $Opt{help};
80     $Opt{no_undef_check} = defined $Opt{no_undef_check}
81     ? $Opt{no_undef_check} ? 0 : 1 : 0;
82     $Opt{dis_suffix} = '.dis' unless defined $Opt{dis_suffix};
83     $Opt{daem_suffix} = '.dafm' unless defined $Opt{daem_suffix};
84     $Opt{dafx_suffix} = '.dafx' unless defined $Opt{dafx_suffix};
85     $Message::DOM::DOMFeature::DEBUG = 1 if $Opt{debug};
86     require Error;
87     $Error::Debug = 1 if $Opt{debug};
88     $Message::Util::Error::VERBOSE = 1 if $Opt{verbose};
89    
90     sub status_msg ($) {
91     my $s = shift;
92     $s .= "\n" unless $s =~ /\n$/;
93     print STDERR $s;
94     }
95    
96     sub status_msg_ ($) {
97     my $s = shift;
98     print STDERR $s;
99     }
100    
101     sub verbose_msg ($) {
102     my $s = shift;
103     $s .= "\n" unless $s =~ /\n$/;
104     print STDERR $s if $Opt{verbose};
105     }
106    
107     sub verbose_msg_ ($) {
108     my $s = shift;
109     print STDERR $s if $Opt{verbose};
110     }
111    
112 wakaba 1.2 ## ---- The MAIN Program
113    
114 wakaba 1.1 my $start_time;
115     BEGIN { $start_time = time }
116    
117     use Message::Util::DIS::DNLite;
118     use Message::Util::PerlCode;
119 wakaba 1.2 use Message::Util::DIS::Test;
120     use Message::DOM::GenericLS;
121 wakaba 1.1
122     my $limpl = $Message::DOM::ImplementationRegistry->get_implementation
123     ({ExpandedURI q<fe:Min> => '3.0',
124 wakaba 1.3 ExpandedURI q<fe:GenericLS> => '3.0',
125 wakaba 1.1 '+' . ExpandedURI q<DIS:DNLite> => '1.0',
126     '+' . ExpandedURI q<DIS:Core> => '1.0',
127     '+' . ExpandedURI q<Util:PerlCode> => '1.0',
128 wakaba 1.2 '+' . ExpandedURI q<DIS:TDT> => '1.0',
129 wakaba 1.1 });
130     my $impl = $limpl->get_feature (ExpandedURI q<DIS:Core> => '1.0');
131 wakaba 1.2 my $pc = $impl->get_feature (ExpandedURI q<Util:PerlCode> => '1.0');
132     my $di = $impl->get_feature (ExpandedURI q<DIS:Core> => '1.0');
133     my $tdt_parser;
134    
135     ## --- Loading and Updating the Database
136 wakaba 1.1
137     my $HasError;
138     my $db = $impl->create_dis_database;
139     $db->pl_database_module_resolver (\&daf_db_module_resolver);
140     $db->dom_config->set_parameter ('error-handler' => \&daf_on_error);
141    
142 wakaba 1.2 my $parser = $impl->create_dis_parser;
143     my $DNi = $impl->get_feature (ExpandedURI q<DIS:DNLite> => '1.0');
144 wakaba 1.1 my %ModuleSourceDISDocument;
145     my %ModuleSourceDNLDocument;
146     my %ModuleNameNamespaceBinding = (
147     DISCore => q<http://suika.fam.cx/~wakaba/archive/2004/dis/Core#>,
148     ## This builtin binding is required since
149     ## some module has |DISCore:author| property before |dis:Require|
150     ## property.
151     );
152    
153     my @target_modules;
154     for (@{$Opt{create_module}}) {
155     my ($mod_uri, $out_path, $mod_for, $out_type) = @$_;
156     push @target_modules, [$mod_uri, $mod_for];
157     }
158    
159     my $ResourceCount = 0;
160     $db->pl_update_module (\@target_modules,
161     get_module_index_file_name => sub {
162     shift; # $db
163     daf_get_module_index_file_name (@_);
164     },
165     get_module_source_document_from_uri => sub {
166     my ($db, $module_uri, $module_for) = @_;
167     status_msg '';
168     status_msg qq<Loading module <$module_uri> for <$module_for>...>;
169     $ResourceCount = 0;
170    
171     unless (defined $ModuleSourceDNLDocument{$module_uri}) {
172     unless (defined $ModuleSourceDISDocument{$module_uri}) {
173     daf_open_source_dis_document ($module_uri);
174     }
175     daf_convert_dis_document_to_dnl_document ();
176     }
177     return $ModuleSourceDNLDocument{$module_uri};
178     },
179     get_module_source_document_from_resource => sub ($$$$$$) {
180     my ($self, $db, $uri, $ns, $ln, $for) = @_;
181     status_msg '';
182     status_msg qq<Loading module "$ln" for <$for>...>;
183     $ResourceCount = 0;
184    
185     my $module_uri = $ns.$ln;
186     unless (defined $ModuleSourceDNLDocument{$module_uri}) {
187     unless (defined $ModuleSourceDISDocument{$module_uri}) {
188     daf_open_source_dis_document ($module_uri);
189     }
190     daf_convert_dis_document_to_dnl_document ();
191     }
192     return $ModuleSourceDNLDocument{$module_uri};
193     },
194     get_module_source_revision => sub {
195     my ($db, $module_uri) = @_;
196     my $ns = $module_uri;
197     $ns =~ s/(\w+)\z//;
198     my $ln = $1;
199    
200     my $name = dac_search_file_path_stem ($ns, $ln, $Opt{dis_suffix});
201     if (defined $name) {
202     return [stat $name.$Opt{dis_suffix}]->[9];
203     } else {
204     return 0;
205     }
206     },
207     get_referring_module_uri_list => sub {
208     my ($db, $module_uri) = @_;
209     unless (defined $ModuleSourceDNLDocument{$module_uri}) {
210     unless (defined $ModuleSourceDISDocument{$module_uri}) {
211     daf_open_source_dis_document ($module_uri);
212     }
213     }
214     return daf_get_referring_module_uri_list ($module_uri);
215     },
216     on_resource_read => sub ($$) {
217     if ((++$ResourceCount % 10) == 0) {
218     status_msg_ "*";
219     status_msg_ " " if ($ResourceCount % (10 * 10)) == 0;
220     status_msg '' if ($ResourceCount % (10 * 50)) == 0;
221     }
222     });
223    
224    
225     ## Removes reference from document to database
226     our @Document;
227     for my $dis (@Document) {
228     $dis->unlink_from_document;
229     $dis->dis_database (undef);
230     }
231    
232     status_msg '';
233    
234     status_msg qq<Reading properties...>;
235     $ResourceCount = 0;
236     $db->read_properties (on_resource_read => sub ($$) {
237     if ((++$ResourceCount % 10) == 0) {
238     status_msg_ "*";
239     status_msg_ " " if ($ResourceCount % (10 * 10)) == 0;
240     status_msg '' if ($ResourceCount % (10 * 50)) == 0;
241     }
242     });
243     status_msg '';
244     status_msg "done";
245    
246     status_msg_ qq<Writing database files...>;
247     $db->pl_store ('dummy', sub ($$) {
248     my ($db, $mod, $type) = @_;
249     my $ns = $mod->namespace_uri;
250     my $ln = $mod->local_name;
251     my $suffix = $type eq ExpandedURI q<dp:ModuleIndexFile>
252     ? $Opt{dafx_suffix} : $Opt{daem_suffix};
253     my $name = dac_search_file_path_stem ($ns, $ln, $suffix);
254     if (defined $name) {
255     $name .= $suffix;
256     } elsif (defined ($name = dac_search_file_path_stem
257     ($ns, $ln, $Opt{dis_suffix}))) {
258     $name .= $suffix;
259     } else {
260     $name = Cwd::abs_path
261     (File::Spec->canonpath
262     (File::Spec->catfile
263     (defined $Opt{input_search_path}->{$ns}->[0]
264     ? $Opt{input_search_path}->{$ns}->[0] : '.',
265     $ln.$suffix)));
266     }
267     verbose_msg qq<Database >.
268     ($type eq <Q::dp|ModuleIndexFile> ? 'index' : 'module').
269     qq< <$ns$ln> is written to "$name">;
270     return $name;
271     }, no_main_database => 1);
272     status_msg "done";
273    
274     daf_check_undefined ();
275    
276 wakaba 1.2 undef $DNi;
277     undef %ModuleSourceDNLDocument;
278     exit $HasError if $HasError;
279    
280     ## --- Creating Files
281    
282 wakaba 1.1 for (@{$Opt{create_module}}) {
283     my ($mod_uri, $out_file_path, $mod_for, $out_type) = @$_;
284     unless (defined $mod_for) {
285     $mod_for = $db->get_module ($mod_uri)
286     ->get_property_text (ExpandedURI q<dis:DefaultFor>,
287     ExpandedURI q<ManakaiDOM:all>);
288     }
289     my $mod = $db->get_module ($mod_uri, for_arg => $mod_for);
290    
291 wakaba 1.2 if ($out_type eq 'perl-pm') {
292 wakaba 1.1 status_msg_ qq<Generating Perl module from <$mod_uri> for <$mod_for>...>;
293     my $pl = $mod->pl_generate_perl_module_file;
294     status_msg qq<done>;
295    
296     my $output;
297     defined $out_file_path
298     ? (open $output, '>', $out_file_path or die "$0: $out_file_path: $!")
299     : ($output = \*STDOUT);
300    
301     status_msg_ sprintf qq<Writing Perl module %s...>,
302     defined $out_file_path
303     ? q<">.$out_file_path.q<">
304     : 'to stdout';
305     print $output $pl->stringify;
306     close $output;
307     status_msg q<done>;
308 wakaba 1.2 } elsif ($out_type eq 'perl-t') {
309     status_msg_ qq<Generating Perl test from <$mod_uri> for <$mod_for>...>;
310     my $pl = daf_generate_perl_test_file ($mod);
311     status_msg qq<done>;
312    
313 wakaba 1.3 my $cfg = $pl->owner_document->dom_config;
314     $cfg->set_parameter (ExpandedURI q<pc:preserve-line-break> => 1);
315    
316 wakaba 1.2 my $output;
317     defined $out_file_path
318     ? (open $output, '>', $out_file_path or die "$0: $out_file_path: $!")
319     : ($output = \*STDOUT);
320    
321     status_msg_ sprintf qq<Writing Perl test %s...>,
322     defined $out_file_path
323     ? q<">.$out_file_path.q<">
324     : 'to stdout';
325     print $output $pl->stringify;
326     close $output;
327     status_msg q<done>;
328 wakaba 1.1 }
329     }
330    
331     daf_check_undefined ();
332    
333 wakaba 1.2 ## --- The END
334    
335 wakaba 1.1 status_msg_ "Closing the database...";
336     $db->free;
337     undef $db;
338     status_msg "done";
339    
340     {
341     use integer;
342     my $time = time - $start_time;
343     status_msg sprintf qq<%d'%02d''>, $time / 60, $time % 60;
344     }
345     exit $HasError;
346    
347     END {
348     $db->free if $db;
349     }
350    
351 wakaba 1.2 ## ---- Subroutines
352    
353 wakaba 1.1 sub daf_open_source_dis_document ($) {
354     my ($module_uri) = @_;
355    
356     ## -- Finds |dis| source file
357     my $ns = $module_uri;
358     $ns =~ s/(\w+)\z//;
359     my $ln = $1;
360     my $file_name = dac_search_file_path_stem ($ns, $ln, $Opt{dis_suffix});
361     unless (defined $file_name) {
362     die "$0: Source file for <$ns$ln> is not found";
363     }
364     $file_name .= $Opt{dis_suffix};
365    
366     status_msg_ qq<Opening dis source file "$file_name"...>;
367    
368     ## -- Opens |dis| file and construct |DISDocument| tree
369     open my $file, '<', $file_name or die "$0: $file_name: $!";
370     my $dis = $parser->parse ({character_stream => $file});
371     require File::Spec;
372     $dis->flag (ExpandedURI q<swcfg21:fileName> =>
373     File::Spec->abs2rel ($file_name));
374     $dis->dis_namespace_resolver (\&daf_module_name_namespace_resolver);
375     close $file;
376    
377     ## -- Registers namespace URI
378     my $mod = $dis->module_element;
379     if ($mod) {
380     my $qn = $mod->get_attribute_ns (ExpandedURI q<dis:>, 'QName');
381     if ($qn) {
382     my $prefix = $qn->value;
383     $prefix =~ s/^[^:|]*[:|]\s*//;
384     $prefix =~ s/\s+$//;
385     unless (defined $ModuleNameNamespaceBinding{$prefix}) {
386     $ModuleNameNamespaceBinding{$prefix} = $mod->defining_namespace_uri;
387     }
388     }
389     }
390    
391     $ModuleSourceDISDocument{$module_uri} = $dis;
392     status_msg q<done>;
393    
394     R: for (@{daf_get_referring_module_uri_list ($module_uri)}) {
395     next R if defined $db->{modDef}->{$_};
396     next R if defined $ModuleSourceDNLDocument{$_};
397     next R if defined $ModuleSourceDISDocument{$_};
398     my $idx_file_name = daf_get_module_index_file_name ($_);
399     if (-f $idx_file_name) {
400     daf_open_current_module_index ($_, $idx_file_name);
401     } else {
402     daf_open_source_dis_document ($_);
403     }
404     }
405     } # daf_open_source_dis_document
406    
407     sub daf_open_current_module_index ($$) {
408     my ($module_uri, $file_name) = @_;
409     $db->pl_load_dis_database_index ($file_name);
410    
411     R: for (@{$db->get_module ($module_uri)
412     ->get_referring_module_uri_list}) {
413     next R if defined $db->{modDef}->{$_};
414     next R if defined $ModuleSourceDNLDocument{$_};
415     next R if defined $ModuleSourceDISDocument{$_};
416     my $idx_file_name = daf_get_module_index_file_name ($_);
417     if (-f $idx_file_name) {
418     daf_open_current_module_index ($_, $idx_file_name);
419     } else {
420     daf_open_source_dis_document ($_);
421     }
422     }
423     } # daf_open_current_module_index
424    
425     sub daf_convert_dis_document_to_dnl_document () {
426     M: for my $module_uri (keys %ModuleSourceDISDocument) {
427     my $dis_doc = $ModuleSourceDISDocument{$module_uri};
428     next M unless $dis_doc;
429     verbose_msg_ qq<Converting <$module_uri>...>;
430     my $dnl_doc = $DNi->convert_dis_document_to_dnl_document
431     ($dis_doc, database_arg => $db,
432     base_namespace_binding =>
433     {(map {$_->local_name => $_->target_namespace_uri}
434     grep {$_} values %{$db->{modDef}}),
435     %ModuleNameNamespaceBinding});
436     push @Document, $dnl_doc;
437     $ModuleSourceDNLDocument{$module_uri} = $dnl_doc;
438     $dis_doc->free;
439     delete $ModuleSourceDISDocument{$module_uri};
440     verbose_msg q<done>;
441     }
442     } # daf_convert_dis_document_to_dnl_document
443    
444     sub daf_get_referring_module_uri_list ($) {
445     my $module_uri = shift;
446     my $ns = $module_uri;
447     $ns =~ s/\w+\z//;
448     my $src = $ModuleSourceDNLDocument{$module_uri};
449     $src = $ModuleSourceDISDocument{$module_uri} unless defined $src;
450     my $mod_el = $src->module_element;
451     my $r = [];
452     if ($mod_el) {
453     my $req_el = $mod_el->require_element;
454     if ($req_el) {
455     M: for my $m_el (@{$req_el->child_nodes}) {
456     next M unless $m_el->node_type eq '#element';
457     next M unless $m_el->expanded_uri eq ExpandedURI q<dis:Module>;
458     my $qn_el = $m_el->get_attribute_ns (ExpandedURI q<dis:>, 'QName');
459     if ($qn_el) {
460     push @$r, $qn_el->qname_value_uri;
461     } else {
462     my $n_el = $m_el->get_attribute_ns (ExpandedURI q<dis:>, 'Name');
463     if ($n_el) {
464     push @$r, $ns.$n_el->value;
465     } else {
466     # The module itself
467     }
468     }
469     }
470     }
471     }
472     return $r;
473     } # daf_get_referring_module_uri_list
474    
475     sub dac_search_file_path_stem ($$$) {
476     my ($ns, $ln, $suffix) = @_;
477     require File::Spec;
478     for my $dir ('.', @{$Opt{input_search_path}->{$ns}||[]}) {
479     my $name = Cwd::abs_path
480     (File::Spec->canonpath
481     (File::Spec->catfile ($dir, $ln)));
482     if (-f $name.$suffix) {
483     return $name;
484     }
485     }
486     return undef;
487     } # dac_search_file_path_stem;
488    
489     sub daf_get_module_index_file_name ($$) {
490     my ($module_uri) = @_;
491     my $ns = $module_uri;
492     $ns =~ s/(\w+)\z//;
493     my $ln = $1;
494    
495     verbose_msg qq<Database module index <$module_uri> is requested>;
496     my $suffix = $Opt{dafx_suffix};
497     my $name = dac_search_file_path_stem ($ns, $ln, $suffix);
498     if (defined $name) {
499     $name .= $suffix;
500     } elsif (defined ($name = dac_search_file_path_stem
501     ($ns, $ln, $Opt{dis_suffix}))) {
502     $name .= $suffix;
503     } else {
504     $name = Cwd::abs_path
505     (File::Spec->canonpath
506     (File::Spec->catfile
507     (defined $Opt{input_search_path}->{$ns}->[0]
508     ? $Opt{input_search_path}->{$ns}->[0] : '.',
509     $ln.$suffix)));
510     }
511     return $name;
512     } # daf_get_module_index_file_name
513    
514     sub daf_module_name_namespace_resolver ($) {
515     my $prefix = shift;
516    
517     ## -- From modules in database
518     M: for (values %{$db->{modDef}}) {
519     my $mod = $_;
520     next M unless defined $mod;
521     if ($mod->local_name eq $prefix) {
522     return $mod->target_namespace_uri;
523     }
524     }
525    
526     ## -- From not-in-database-yet module list
527     if (defined $ModuleNameNamespaceBinding{$prefix}) {
528     return $ModuleNameNamespaceBinding{$prefix};
529     }
530     return undef;
531     } # daf_module_name_namespace_resolver
532    
533     sub daf_db_module_resolver ($$$) {
534     my ($db, $mod, $type) = @_;
535     my $ns = $mod->namespace_uri;
536     my $ln = $mod->local_name;
537     my $suffix = $type eq ExpandedURI q<dp:ModuleIndexFile>
538     ? $Opt{dafx_suffix} : $Opt{daem_suffix};
539     verbose_msg qq<Database module <$ns$ln> is requested>;
540     my $name = dac_search_file_path_stem ($ns, $ln, $suffix);
541     if (defined $name) {
542     return $name.$suffix;
543     } else {
544     return undef;
545     }
546     } # daf_db_module_resolver
547    
548     sub daf_on_error ($$) {
549     my ($self, $err) = @_;
550     if ($err->severity == $err->SEVERITY_WARNING) {
551     my $info = ExpandedURI q<dp:info>;
552     if ($err->type =~ /\Q$info\E/) {
553     my $msg = $err->text;
554     if ($msg =~ /\.\.\.\z/) {
555     verbose_msg_ $msg;
556     } else {
557     verbose_msg $msg;
558     }
559     } else {
560     my $msg = $err->text;
561     if ($msg =~ /\.\.\.\z/) {
562     status_msg_ $msg;
563     } else {
564     status_msg $msg;
565     }
566     }
567     } else {
568     warn $err;
569     $HasError = 1;
570     }
571     } # daf_on_error
572    
573     sub daf_check_undefined () {
574     unless ($Opt{no_undef_check}) {
575     status_msg_ "Checking undefined resources...";
576     $db->check_undefined_resource;
577     print STDERR "done\n";
578     }
579     } # daf_check_undefined
580 wakaba 1.2
581     sub daf_generate_perl_test_file ($) {
582     my $mod = shift;
583     my $pl = $pc->create_perl_file;
584     my $pack = $pl->get_last_package ("Manakai::Test", make_new_package => 1);
585     $pack->add_use_perl_module_name ("Message::Util::DIS::Test");
586     $pack->add_use_perl_module_name ("Message::Util::Error");
587     $pack->add_require_perl_module_name ($mod->pl_fully_qualified_name);
588    
589     $pl->source_file ($mod->get_property_text (ExpandedURI q<DIS:sourceFile>, ""));
590     $pl->source_module ($mod->name_uri);
591     $pl->source_for ($mod->for_uri);
592     $pl->license_uri ($mod->get_property_resource (ExpandedURI q<dis:License>)
593     ->uri);
594    
595     $pack->append_code
596     ($pc->create_perl_statement
597     ('my $impl = $Message::DOM::ImplementationRegistry->get_implementation ({
598     "http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#Test"
599     => "1.0",
600     })'));
601    
602     $pack->append_code
603     (my $num_statement = $pc->create_perl_statement
604     ('my $test = $impl->create_test_manager'));
605    
606     my $total_tests = 0;
607     my %processed;
608     for my $res (@{$mod->get_resource_list}) {
609     next if $res->owner_module ne $mod or $processed{$res->uri};
610     $processed{$res->uri} = 1;
611    
612     if ($res->is_type_uri (ExpandedURI q<test:Test>)) {
613     if ($res->is_type_uri (ExpandedURI q<test:StandaloneTest>)) {
614     $total_tests++;
615     $pack->append_code ('$test->start_new_test (');
616     $pack->append_new_pc_literal ($res->name_uri || $res->uri);
617     $pack->append_code (');');
618    
619     $pack->append_code ('try {');
620    
621     my $test_pc = $res->pl_code_fragment;
622     if (not defined $test_pc) {
623     die "Perl test code not defined for <".$res->uri.">";
624     }
625    
626     $pack->append_code_fragment ($test_pc);
627    
628     $pack->append_code ('$test->ok;');
629    
630     $pack->append_code ('} catch Message::Util::IF::DTException with {
631     ##
632     } otherwise {
633     my $err = shift;
634     warn $err;
635     $test->not_ok;
636     };');
637    
638     } elsif ($res->is_type_uri (ExpandedURI q<test:ParserTestSet>)) {
639     my $block = $pack->append_new_pc_block;
640     my @test;
641    
642 wakaba 1.3 $tdt_parser ||= $limpl->create_gls_parser
643 wakaba 1.2 ({
644     ExpandedURI q<DIS:TDT> => '1.0',
645     });
646     for my $tres (@{$res->get_child_resource_list_by_type
647     (ExpandedURI q<test:ParserTest>)}) {
648     $total_tests++;
649     push @test, my $ttest = {entity => {}};
650     $ttest->{uri} = $tres->uri;
651     for my $eres (@{$tres->get_child_resource_list_by_type
652     (ExpandedURI q<test:Entity>)}) {
653     my $tent = $ttest->{entity}->{$eres->uri} = {};
654     for (ExpandedURI q<test:uri>, ExpandedURI q<test:baseURI>,
655     ExpandedURI q<test:value>) {
656     my $v = $eres->get_property_text ($_);
657     $tent->{$_} = $v if defined $v;
658     }
659     $ttest->{root_uri} = $eres->uri
660     if $eres->is_type_uri (ExpandedURI q<test:RootEntity>) or
661     not defined $ttest->{root_uri};
662     }
663    
664     ## Result DOM tree
665     my $tree_t = $tres->get_property_text (ExpandedURI q<test:domTree>);
666     if (defined $tree_t) {
667     $ttest->{dom_tree} = $tdt_parser->parse_string ($tree_t);
668     }
669    
670     ## Expected |DOMError|s
671     for (@{$tres->get_property_value_list (ExpandedURI q<c:erred>)}) {
672     my $err = $tdt_parser->parse_tdt_error_string
673     ($_->string_value, $db, $_,
674     undef, $tres->for_uri);
675     push @{$ttest->{dom_error}->{$err->{type}->{value}} ||= []}, $err;
676     }
677     }
678    
679     for ($block->append_statement
680     ->append_new_pc_expression ('=')) {
681     $_->append_new_pc_variable ('$', undef, 'TestData')
682     ->variable_scope ('my');
683     $_->append_new_pc_literal (\@test);
684     }
685    
686     my $plc = $res->pl_code_fragment;
687     unless ($plc) {
688     die "Resource <".$res->uri."> does not have Perl test code";
689     }
690    
691     $block->append_code_fragment ($plc);
692    
693     } # test resource type
694     } # test:Test
695     }
696    
697     $num_statement->append_code (' (' . $total_tests . ')');
698    
699     return $pl;
700     } # daf_generate_perl_test_file
701 wakaba 1.1
702     __END__
703    
704     =head1 NAME
705    
706     dac.pl - Creating "dac" Database File from "dis" Source Files
707    
708     =head1 SYNOPSIS
709    
710     perl path/to/dac.pl [--input-db-file-name=input.dac] \
711     --output-file-name=out.dac [options...] \
712     input.dis
713     perl path/to/dac.pl --help
714    
715     =head1 DESCRIPTION
716    
717     This script, C<dac.pl>, compiles "dis" source files into "dac"
718     database file. The generated database file can be used
719     in turn to generate Perl module file, for example, by another
720     script C<dac2pm.pl> or can be used to create larger database
721     by specifying its file name as the C<--input-db-file-name>
722     argument of another C<dac.pl> execution.
723    
724     This script is part of manakai.
725    
726     =head1 OPTIONS
727    
728     =over 4
729    
730     =item I<input.dis> (Required)
731    
732     The unnamed option specifies a file name path of the source "dis" file
733     from which a database is created. This option is required.
734    
735     =item C<--input-db-file-name=I<file-name>> (Default: none)
736    
737     A file path of the base database. This option is optional; if this
738     option is specified, the database file is loaded first
739     and then I<input.dis> file is loaded in the context of it.
740     Otherwise, a new database is created.
741    
742     =item C<--output-file-name=I<file-name>> (Required)
743    
744     The
745    
746     =back
747    
748     =head1 SEE ALSO
749    
750     L<bin/dac2pm.pl> - Generating Perl module from "dac" file.
751    
752     L<lib/Message/Util/DIS.dis> - The actual implementation
753     of the "dis" interpretation.
754    
755     =head1 LICENSE
756    
757     Copyright 2004-2005 Wakaba <w@suika.fam.cx>. All rights reserved.
758    
759     This program is free software; you can redistribute it and/or
760     modify it under the same terms as Perl itself.
761    
762     =cut

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24