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