1 |
#!/usr/bin/perl -w |
2 |
use strict; |
3 |
use Message::Util::QName::Filter { |
4 |
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#>, |
6 |
dis => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#dis-->, |
7 |
DOMLS => q<http://suika.fam.cx/~wakaba/archive/2004/dom/ls#>, |
8 |
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 |
pc => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/PerlCode#>, |
12 |
swcfg21 => q<http://suika.fam.cx/~wakaba/archive/2005/swcfg21#>, |
13 |
test => q<http://suika.fam.cx/~wakaba/archive/2004/dis/Test#>, |
14 |
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 |
$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 |
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 |
## ---- The MAIN Program |
113 |
|
114 |
my $start_time; |
115 |
BEGIN { $start_time = time } |
116 |
|
117 |
use Message::Util::DIS::DNLite; |
118 |
use Message::Util::PerlCode; |
119 |
use Message::Util::DIS::Test; |
120 |
use Message::DOM::GenericLS; |
121 |
|
122 |
my $limpl = $Message::DOM::ImplementationRegistry->get_implementation |
123 |
({ExpandedURI q<fe:Min> => '3.0', |
124 |
'+' . ExpandedURI q<DIS:DNLite> => '1.0', |
125 |
'+' . ExpandedURI q<DIS:Core> => '1.0', |
126 |
'+' . ExpandedURI q<Util:PerlCode> => '1.0', |
127 |
'+' . ExpandedURI q<DIS:TDT> => '1.0', |
128 |
}); |
129 |
my $impl = $limpl->get_feature (ExpandedURI q<DIS:Core> => '1.0'); |
130 |
my $pc = $impl->get_feature (ExpandedURI q<Util:PerlCode> => '1.0'); |
131 |
my $di = $impl->get_feature (ExpandedURI q<DIS:Core> => '1.0'); |
132 |
my $tdt_parser; |
133 |
|
134 |
## --- Loading and Updating the Database |
135 |
|
136 |
my $HasError; |
137 |
my $db = $impl->create_dis_database; |
138 |
$db->pl_database_module_resolver (\&daf_db_module_resolver); |
139 |
$db->dom_config->set_parameter ('error-handler' => \&daf_on_error); |
140 |
|
141 |
my $parser = $impl->create_dis_parser; |
142 |
my $DNi = $impl->get_feature (ExpandedURI q<DIS:DNLite> => '1.0'); |
143 |
my %ModuleSourceDISDocument; |
144 |
my %ModuleSourceDNLDocument; |
145 |
my %ModuleNameNamespaceBinding = ( |
146 |
DISCore => q<http://suika.fam.cx/~wakaba/archive/2004/dis/Core#>, |
147 |
## This builtin binding is required since |
148 |
## some module has |DISCore:author| property before |dis:Require| |
149 |
## property. |
150 |
); |
151 |
|
152 |
my @target_modules; |
153 |
for (@{$Opt{create_module}}) { |
154 |
my ($mod_uri, $out_path, $mod_for, $out_type) = @$_; |
155 |
push @target_modules, [$mod_uri, $mod_for]; |
156 |
} |
157 |
|
158 |
my $ResourceCount = 0; |
159 |
$db->pl_update_module (\@target_modules, |
160 |
get_module_index_file_name => sub { |
161 |
shift; # $db |
162 |
daf_get_module_index_file_name (@_); |
163 |
}, |
164 |
get_module_source_document_from_uri => sub { |
165 |
my ($db, $module_uri, $module_for) = @_; |
166 |
status_msg ''; |
167 |
status_msg qq<Loading module <$module_uri> for <$module_for>...>; |
168 |
$ResourceCount = 0; |
169 |
|
170 |
unless (defined $ModuleSourceDNLDocument{$module_uri}) { |
171 |
unless (defined $ModuleSourceDISDocument{$module_uri}) { |
172 |
daf_open_source_dis_document ($module_uri); |
173 |
} |
174 |
daf_convert_dis_document_to_dnl_document (); |
175 |
} |
176 |
return $ModuleSourceDNLDocument{$module_uri}; |
177 |
}, |
178 |
get_module_source_document_from_resource => sub ($$$$$$) { |
179 |
my ($self, $db, $uri, $ns, $ln, $for) = @_; |
180 |
status_msg ''; |
181 |
status_msg qq<Loading module "$ln" for <$for>...>; |
182 |
$ResourceCount = 0; |
183 |
|
184 |
my $module_uri = $ns.$ln; |
185 |
unless (defined $ModuleSourceDNLDocument{$module_uri}) { |
186 |
unless (defined $ModuleSourceDISDocument{$module_uri}) { |
187 |
daf_open_source_dis_document ($module_uri); |
188 |
} |
189 |
daf_convert_dis_document_to_dnl_document (); |
190 |
} |
191 |
return $ModuleSourceDNLDocument{$module_uri}; |
192 |
}, |
193 |
get_module_source_revision => sub { |
194 |
my ($db, $module_uri) = @_; |
195 |
my $ns = $module_uri; |
196 |
$ns =~ s/(\w+)\z//; |
197 |
my $ln = $1; |
198 |
|
199 |
my $name = dac_search_file_path_stem ($ns, $ln, $Opt{dis_suffix}); |
200 |
if (defined $name) { |
201 |
return [stat $name.$Opt{dis_suffix}]->[9]; |
202 |
} else { |
203 |
return 0; |
204 |
} |
205 |
}, |
206 |
get_referring_module_uri_list => sub { |
207 |
my ($db, $module_uri) = @_; |
208 |
unless (defined $ModuleSourceDNLDocument{$module_uri}) { |
209 |
unless (defined $ModuleSourceDISDocument{$module_uri}) { |
210 |
daf_open_source_dis_document ($module_uri); |
211 |
} |
212 |
daf_convert_dis_document_to_dnl_document (); |
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 |
undef $DNi; |
277 |
undef %ModuleSourceDNLDocument; |
278 |
exit $HasError if $HasError; |
279 |
|
280 |
## --- Creating Files |
281 |
|
282 |
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 |
if ($out_type eq 'perl-pm') { |
292 |
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 |
} 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 |
my $output; |
314 |
defined $out_file_path |
315 |
? (open $output, '>', $out_file_path or die "$0: $out_file_path: $!") |
316 |
: ($output = \*STDOUT); |
317 |
|
318 |
status_msg_ sprintf qq<Writing Perl test %s...>, |
319 |
defined $out_file_path |
320 |
? q<">.$out_file_path.q<"> |
321 |
: 'to stdout'; |
322 |
print $output $pl->stringify; |
323 |
close $output; |
324 |
status_msg q<done>; |
325 |
} |
326 |
} |
327 |
|
328 |
daf_check_undefined (); |
329 |
|
330 |
## --- The END |
331 |
|
332 |
status_msg_ "Closing the database..."; |
333 |
$db->free; |
334 |
undef $db; |
335 |
status_msg "done"; |
336 |
|
337 |
{ |
338 |
use integer; |
339 |
my $time = time - $start_time; |
340 |
status_msg sprintf qq<%d'%02d''>, $time / 60, $time % 60; |
341 |
} |
342 |
exit $HasError; |
343 |
|
344 |
END { |
345 |
$db->free if $db; |
346 |
} |
347 |
|
348 |
## ---- Subroutines |
349 |
|
350 |
sub daf_open_source_dis_document ($) { |
351 |
my ($module_uri) = @_; |
352 |
|
353 |
## -- Finds |dis| source file |
354 |
my $ns = $module_uri; |
355 |
$ns =~ s/(\w+)\z//; |
356 |
my $ln = $1; |
357 |
my $file_name = dac_search_file_path_stem ($ns, $ln, $Opt{dis_suffix}); |
358 |
unless (defined $file_name) { |
359 |
die "$0: Source file for <$ns$ln> is not found"; |
360 |
} |
361 |
$file_name .= $Opt{dis_suffix}; |
362 |
|
363 |
status_msg_ qq<Opening dis source file "$file_name"...>; |
364 |
|
365 |
## -- Opens |dis| file and construct |DISDocument| tree |
366 |
open my $file, '<', $file_name or die "$0: $file_name: $!"; |
367 |
my $dis = $parser->parse ({character_stream => $file}); |
368 |
require File::Spec; |
369 |
$dis->flag (ExpandedURI q<swcfg21:fileName> => |
370 |
File::Spec->abs2rel ($file_name)); |
371 |
$dis->dis_namespace_resolver (\&daf_module_name_namespace_resolver); |
372 |
close $file; |
373 |
|
374 |
## -- Registers namespace URI |
375 |
my $mod = $dis->module_element; |
376 |
if ($mod) { |
377 |
my $qn = $mod->get_attribute_ns (ExpandedURI q<dis:>, 'QName'); |
378 |
if ($qn) { |
379 |
my $prefix = $qn->value; |
380 |
$prefix =~ s/^[^:|]*[:|]\s*//; |
381 |
$prefix =~ s/\s+$//; |
382 |
unless (defined $ModuleNameNamespaceBinding{$prefix}) { |
383 |
$ModuleNameNamespaceBinding{$prefix} = $mod->defining_namespace_uri; |
384 |
} |
385 |
} |
386 |
} |
387 |
|
388 |
$ModuleSourceDISDocument{$module_uri} = $dis; |
389 |
status_msg q<done>; |
390 |
|
391 |
R: for (@{daf_get_referring_module_uri_list ($module_uri)}) { |
392 |
next R if defined $db->{modDef}->{$_}; |
393 |
next R if defined $ModuleSourceDNLDocument{$_}; |
394 |
next R if defined $ModuleSourceDISDocument{$_}; |
395 |
my $idx_file_name = daf_get_module_index_file_name ($_); |
396 |
if (-f $idx_file_name) { |
397 |
daf_open_current_module_index ($_, $idx_file_name); |
398 |
} else { |
399 |
daf_open_source_dis_document ($_); |
400 |
} |
401 |
} |
402 |
} # daf_open_source_dis_document |
403 |
|
404 |
sub daf_open_current_module_index ($$) { |
405 |
my ($module_uri, $file_name) = @_; |
406 |
$db->pl_load_dis_database_index ($file_name); |
407 |
|
408 |
R: for (@{$db->get_module ($module_uri) |
409 |
->get_referring_module_uri_list}) { |
410 |
next R if defined $db->{modDef}->{$_}; |
411 |
next R if defined $ModuleSourceDNLDocument{$_}; |
412 |
next R if defined $ModuleSourceDISDocument{$_}; |
413 |
my $idx_file_name = daf_get_module_index_file_name ($_); |
414 |
if (-f $idx_file_name) { |
415 |
daf_open_current_module_index ($_, $idx_file_name); |
416 |
} else { |
417 |
daf_open_source_dis_document ($_); |
418 |
} |
419 |
} |
420 |
} # daf_open_current_module_index |
421 |
|
422 |
sub daf_convert_dis_document_to_dnl_document () { |
423 |
M: for my $module_uri (keys %ModuleSourceDISDocument) { |
424 |
my $dis_doc = $ModuleSourceDISDocument{$module_uri}; |
425 |
next M unless $dis_doc; |
426 |
verbose_msg_ qq<Converting <$module_uri>...>; |
427 |
my $dnl_doc = $DNi->convert_dis_document_to_dnl_document |
428 |
($dis_doc, database_arg => $db, |
429 |
base_namespace_binding => |
430 |
{(map {$_->local_name => $_->target_namespace_uri} |
431 |
grep {$_} values %{$db->{modDef}}), |
432 |
%ModuleNameNamespaceBinding}); |
433 |
push @Document, $dnl_doc; |
434 |
$ModuleSourceDNLDocument{$module_uri} = $dnl_doc; |
435 |
$dis_doc->free; |
436 |
delete $ModuleSourceDISDocument{$module_uri}; |
437 |
verbose_msg q<done>; |
438 |
} |
439 |
} # daf_convert_dis_document_to_dnl_document |
440 |
|
441 |
sub daf_get_referring_module_uri_list ($) { |
442 |
my $module_uri = shift; |
443 |
my $ns = $module_uri; |
444 |
$ns =~ s/\w+\z//; |
445 |
my $src = $ModuleSourceDNLDocument{$module_uri}; |
446 |
$src = $ModuleSourceDISDocument{$module_uri} unless defined $src; |
447 |
my $mod_el = $src->module_element; |
448 |
my $r = []; |
449 |
if ($mod_el) { |
450 |
my $req_el = $mod_el->require_element; |
451 |
if ($req_el) { |
452 |
M: for my $m_el (@{$req_el->child_nodes}) { |
453 |
next M unless $m_el->node_type eq '#element'; |
454 |
next M unless $m_el->expanded_uri eq ExpandedURI q<dis:Module>; |
455 |
my $qn_el = $m_el->get_attribute_ns (ExpandedURI q<dis:>, 'QName'); |
456 |
if ($qn_el) { |
457 |
push @$r, $qn_el->qname_value_uri; |
458 |
} else { |
459 |
my $n_el = $m_el->get_attribute_ns (ExpandedURI q<dis:>, 'Name'); |
460 |
if ($n_el) { |
461 |
push @$r, $ns.$n_el->value; |
462 |
} else { |
463 |
# The module itself |
464 |
} |
465 |
} |
466 |
} |
467 |
} |
468 |
} |
469 |
return $r; |
470 |
} # daf_get_referring_module_uri_list |
471 |
|
472 |
sub dac_search_file_path_stem ($$$) { |
473 |
my ($ns, $ln, $suffix) = @_; |
474 |
require File::Spec; |
475 |
for my $dir ('.', @{$Opt{input_search_path}->{$ns}||[]}) { |
476 |
my $name = Cwd::abs_path |
477 |
(File::Spec->canonpath |
478 |
(File::Spec->catfile ($dir, $ln))); |
479 |
if (-f $name.$suffix) { |
480 |
return $name; |
481 |
} |
482 |
} |
483 |
return undef; |
484 |
} # dac_search_file_path_stem; |
485 |
|
486 |
sub daf_get_module_index_file_name ($$) { |
487 |
my ($module_uri) = @_; |
488 |
my $ns = $module_uri; |
489 |
$ns =~ s/(\w+)\z//; |
490 |
my $ln = $1; |
491 |
|
492 |
verbose_msg qq<Database module index <$module_uri> is requested>; |
493 |
my $suffix = $Opt{dafx_suffix}; |
494 |
my $name = dac_search_file_path_stem ($ns, $ln, $suffix); |
495 |
if (defined $name) { |
496 |
$name .= $suffix; |
497 |
} elsif (defined ($name = dac_search_file_path_stem |
498 |
($ns, $ln, $Opt{dis_suffix}))) { |
499 |
$name .= $suffix; |
500 |
} else { |
501 |
$name = Cwd::abs_path |
502 |
(File::Spec->canonpath |
503 |
(File::Spec->catfile |
504 |
(defined $Opt{input_search_path}->{$ns}->[0] |
505 |
? $Opt{input_search_path}->{$ns}->[0] : '.', |
506 |
$ln.$suffix))); |
507 |
} |
508 |
return $name; |
509 |
} # daf_get_module_index_file_name |
510 |
|
511 |
sub daf_module_name_namespace_resolver ($) { |
512 |
my $prefix = shift; |
513 |
|
514 |
## -- From modules in database |
515 |
M: for (values %{$db->{modDef}}) { |
516 |
my $mod = $_; |
517 |
next M unless defined $mod; |
518 |
if ($mod->local_name eq $prefix) { |
519 |
return $mod->target_namespace_uri; |
520 |
} |
521 |
} |
522 |
|
523 |
## -- From not-in-database-yet module list |
524 |
if (defined $ModuleNameNamespaceBinding{$prefix}) { |
525 |
return $ModuleNameNamespaceBinding{$prefix}; |
526 |
} |
527 |
return undef; |
528 |
} # daf_module_name_namespace_resolver |
529 |
|
530 |
sub daf_db_module_resolver ($$$) { |
531 |
my ($db, $mod, $type) = @_; |
532 |
my $ns = $mod->namespace_uri; |
533 |
my $ln = $mod->local_name; |
534 |
my $suffix = $type eq ExpandedURI q<dp:ModuleIndexFile> |
535 |
? $Opt{dafx_suffix} : $Opt{daem_suffix}; |
536 |
verbose_msg qq<Database module <$ns$ln> is requested>; |
537 |
my $name = dac_search_file_path_stem ($ns, $ln, $suffix); |
538 |
if (defined $name) { |
539 |
return $name.$suffix; |
540 |
} else { |
541 |
return undef; |
542 |
} |
543 |
} # daf_db_module_resolver |
544 |
|
545 |
sub daf_on_error ($$) { |
546 |
my ($self, $err) = @_; |
547 |
if ($err->severity == $err->SEVERITY_WARNING) { |
548 |
my $info = ExpandedURI q<dp:info>; |
549 |
if ($err->type =~ /\Q$info\E/) { |
550 |
my $msg = $err->text; |
551 |
if ($msg =~ /\.\.\.\z/) { |
552 |
verbose_msg_ $msg; |
553 |
} else { |
554 |
verbose_msg $msg; |
555 |
} |
556 |
} else { |
557 |
my $msg = $err->text; |
558 |
if ($msg =~ /\.\.\.\z/) { |
559 |
status_msg_ $msg; |
560 |
} else { |
561 |
status_msg $msg; |
562 |
} |
563 |
} |
564 |
} else { |
565 |
warn $err; |
566 |
$HasError = 1; |
567 |
} |
568 |
} # daf_on_error |
569 |
|
570 |
sub daf_check_undefined () { |
571 |
unless ($Opt{no_undef_check}) { |
572 |
status_msg_ "Checking undefined resources..."; |
573 |
$db->check_undefined_resource; |
574 |
print STDERR "done\n"; |
575 |
} |
576 |
} # daf_check_undefined |
577 |
|
578 |
sub daf_generate_perl_test_file ($) { |
579 |
my $mod = shift; |
580 |
my $pl = $pc->create_perl_file; |
581 |
my $pack = $pl->get_last_package ("Manakai::Test", make_new_package => 1); |
582 |
$pack->add_use_perl_module_name ("Message::Util::DIS::Test"); |
583 |
$pack->add_use_perl_module_name ("Message::Util::Error"); |
584 |
$pack->add_require_perl_module_name ($mod->pl_fully_qualified_name); |
585 |
|
586 |
$pl->source_file ($mod->get_property_text (ExpandedURI q<DIS:sourceFile>, "")); |
587 |
$pl->source_module ($mod->name_uri); |
588 |
$pl->source_for ($mod->for_uri); |
589 |
$pl->license_uri ($mod->get_property_resource (ExpandedURI q<dis:License>) |
590 |
->uri); |
591 |
|
592 |
$pack->append_code |
593 |
($pc->create_perl_statement |
594 |
('my $impl = $Message::DOM::ImplementationRegistry->get_implementation ({ |
595 |
"http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#Test" |
596 |
=> "1.0", |
597 |
})')); |
598 |
|
599 |
$pack->append_code |
600 |
(my $num_statement = $pc->create_perl_statement |
601 |
('my $test = $impl->create_test_manager')); |
602 |
|
603 |
my $total_tests = 0; |
604 |
my %processed; |
605 |
for my $res (@{$mod->get_resource_list}) { |
606 |
next if $res->owner_module ne $mod or $processed{$res->uri}; |
607 |
$processed{$res->uri} = 1; |
608 |
|
609 |
if ($res->is_type_uri (ExpandedURI q<test:Test>)) { |
610 |
if ($res->is_type_uri (ExpandedURI q<test:StandaloneTest>)) { |
611 |
$total_tests++; |
612 |
$pack->append_code ('$test->start_new_test ('); |
613 |
$pack->append_new_pc_literal ($res->name_uri || $res->uri); |
614 |
$pack->append_code (');'); |
615 |
|
616 |
$pack->append_code ('try {'); |
617 |
|
618 |
my $test_pc = $res->pl_code_fragment; |
619 |
if (not defined $test_pc) { |
620 |
die "Perl test code not defined for <".$res->uri.">"; |
621 |
} |
622 |
|
623 |
$pack->append_code_fragment ($test_pc); |
624 |
|
625 |
$pack->append_code ('$test->ok;'); |
626 |
|
627 |
$pack->append_code ('} catch Message::Util::IF::DTException with { |
628 |
## |
629 |
} otherwise { |
630 |
my $err = shift; |
631 |
warn $err; |
632 |
$test->not_ok; |
633 |
};'); |
634 |
|
635 |
} elsif ($res->is_type_uri (ExpandedURI q<test:ParserTestSet>)) { |
636 |
my $block = $pack->append_new_pc_block; |
637 |
my @test; |
638 |
|
639 |
$tdt_parser ||= $impl->create_gls_parser |
640 |
({ |
641 |
ExpandedURI q<DIS:TDT> => '1.0', |
642 |
}); |
643 |
for my $tres (@{$res->get_child_resource_list_by_type |
644 |
(ExpandedURI q<test:ParserTest>)}) { |
645 |
$total_tests++; |
646 |
push @test, my $ttest = {entity => {}}; |
647 |
$ttest->{uri} = $tres->uri; |
648 |
for my $eres (@{$tres->get_child_resource_list_by_type |
649 |
(ExpandedURI q<test:Entity>)}) { |
650 |
my $tent = $ttest->{entity}->{$eres->uri} = {}; |
651 |
for (ExpandedURI q<test:uri>, ExpandedURI q<test:baseURI>, |
652 |
ExpandedURI q<test:value>) { |
653 |
my $v = $eres->get_property_text ($_); |
654 |
$tent->{$_} = $v if defined $v; |
655 |
} |
656 |
$ttest->{root_uri} = $eres->uri |
657 |
if $eres->is_type_uri (ExpandedURI q<test:RootEntity>) or |
658 |
not defined $ttest->{root_uri}; |
659 |
} |
660 |
|
661 |
## Result DOM tree |
662 |
my $tree_t = $tres->get_property_text (ExpandedURI q<test:domTree>); |
663 |
if (defined $tree_t) { |
664 |
$ttest->{dom_tree} = $tdt_parser->parse_string ($tree_t); |
665 |
} |
666 |
|
667 |
## Expected |DOMError|s |
668 |
for (@{$tres->get_property_value_list (ExpandedURI q<c:erred>)}) { |
669 |
my $err = $tdt_parser->parse_tdt_error_string |
670 |
($_->string_value, $db, $_, |
671 |
undef, $tres->for_uri); |
672 |
push @{$ttest->{dom_error}->{$err->{type}->{value}} ||= []}, $err; |
673 |
} |
674 |
} |
675 |
|
676 |
for ($block->append_statement |
677 |
->append_new_pc_expression ('=')) { |
678 |
$_->append_new_pc_variable ('$', undef, 'TestData') |
679 |
->variable_scope ('my'); |
680 |
$_->append_new_pc_literal (\@test); |
681 |
} |
682 |
|
683 |
my $plc = $res->pl_code_fragment; |
684 |
unless ($plc) { |
685 |
die "Resource <".$res->uri."> does not have Perl test code"; |
686 |
} |
687 |
|
688 |
$block->append_code_fragment ($plc); |
689 |
|
690 |
} # test resource type |
691 |
} # test:Test |
692 |
} |
693 |
|
694 |
$num_statement->append_code (' (' . $total_tests . ')'); |
695 |
|
696 |
return $pl; |
697 |
} # daf_generate_perl_test_file |
698 |
|
699 |
__END__ |
700 |
|
701 |
=head1 NAME |
702 |
|
703 |
dac.pl - Creating "dac" Database File from "dis" Source Files |
704 |
|
705 |
=head1 SYNOPSIS |
706 |
|
707 |
perl path/to/dac.pl [--input-db-file-name=input.dac] \ |
708 |
--output-file-name=out.dac [options...] \ |
709 |
input.dis |
710 |
perl path/to/dac.pl --help |
711 |
|
712 |
=head1 DESCRIPTION |
713 |
|
714 |
This script, C<dac.pl>, compiles "dis" source files into "dac" |
715 |
database file. The generated database file can be used |
716 |
in turn to generate Perl module file, for example, by another |
717 |
script C<dac2pm.pl> or can be used to create larger database |
718 |
by specifying its file name as the C<--input-db-file-name> |
719 |
argument of another C<dac.pl> execution. |
720 |
|
721 |
This script is part of manakai. |
722 |
|
723 |
=head1 OPTIONS |
724 |
|
725 |
=over 4 |
726 |
|
727 |
=item I<input.dis> (Required) |
728 |
|
729 |
The unnamed option specifies a file name path of the source "dis" file |
730 |
from which a database is created. This option is required. |
731 |
|
732 |
=item C<--input-db-file-name=I<file-name>> (Default: none) |
733 |
|
734 |
A file path of the base database. This option is optional; if this |
735 |
option is specified, the database file is loaded first |
736 |
and then I<input.dis> file is loaded in the context of it. |
737 |
Otherwise, a new database is created. |
738 |
|
739 |
=item C<--output-file-name=I<file-name>> (Required) |
740 |
|
741 |
The |
742 |
|
743 |
=back |
744 |
|
745 |
=head1 SEE ALSO |
746 |
|
747 |
L<bin/dac2pm.pl> - Generating Perl module from "dac" file. |
748 |
|
749 |
L<lib/Message/Util/DIS.dis> - The actual implementation |
750 |
of the "dis" interpretation. |
751 |
|
752 |
=head1 LICENSE |
753 |
|
754 |
Copyright 2004-2005 Wakaba <w@suika.fam.cx>. All rights reserved. |
755 |
|
756 |
This program is free software; you can redistribute it and/or |
757 |
modify it under the same terms as Perl itself. |
758 |
|
759 |
=cut |