1 |
#!/usr/bin/perl -w |
2 |
use strict; |
3 |
use Message::Util::QName::Filter { |
4 |
DIS => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#>, |
5 |
dis => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#dis-->, |
6 |
dp => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#Perl/>, |
7 |
ManakaiDOM => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/manakai-dom#>, |
8 |
swcfg21 => q<http://suika.fam.cx/~wakaba/archive/2005/swcfg21#>, |
9 |
}; |
10 |
|
11 |
use Getopt::Long; |
12 |
use Pod::Usage; |
13 |
my %Opt = (); |
14 |
GetOptions ( |
15 |
'db-base-directory-path=s' => \$Opt{db_base_path}, |
16 |
'debug' => \$Opt{debug}, |
17 |
'dis-file-suffix=s' => \$Opt{dis_suffix}, |
18 |
'daem-file-suffix=s' => \$Opt{daem_suffix}, |
19 |
'dafx-file-suffix=s' => \$Opt{dafx_suffix}, |
20 |
'for=s' => \$Opt{For}, |
21 |
'help' => \$Opt{help}, |
22 |
'input-db-file-name=s' => \$Opt{input_file_name}, |
23 |
'output-file-name=s' => \$Opt{output_file_name}, |
24 |
'search-path|I=s' => sub { |
25 |
shift; |
26 |
my @value = split /\s+/, shift; |
27 |
while (my ($ns, $path) = splice @value, 0, 2, ()) { |
28 |
unless (defined $path) { |
29 |
die qq[$0: Search-path parameter without path: "$ns"]; |
30 |
} |
31 |
push @{$Opt{input_search_path}->{$ns} ||= []}, $path; |
32 |
} |
33 |
}, |
34 |
'search-path-catalog-file-name=s' => sub { |
35 |
shift; |
36 |
require File::Spec; |
37 |
my $path = my $path_base = shift; |
38 |
$path_base =~ s#[^/]+$##; |
39 |
$Opt{search_path_base} = $path_base; |
40 |
open my $file, '<', $path or die "$0: $path: $!"; |
41 |
while (<$file>) { |
42 |
if (s/^\s*\@//) { ## Processing instruction |
43 |
my ($target, $data) = split /\s+/; |
44 |
if ($target eq 'base') { |
45 |
$Opt{search_path_base} = File::Spec->rel2abs ($data, $path_base); |
46 |
} else { |
47 |
die "$0: $target: Unknown target"; |
48 |
} |
49 |
} elsif (/^\s*\#/) { ## Comment |
50 |
# |
51 |
} elsif (/\S/) { ## Catalog entry |
52 |
s/^\s+//; |
53 |
my ($ns, $path) = split /\s+/; |
54 |
push @{$Opt{input_search_path}->{$ns} ||= []}, |
55 |
File::Spec->rel2abs ($path, $Opt{search_path_base}); |
56 |
} |
57 |
} |
58 |
## NOTE: File paths with SPACEs are not supported |
59 |
## NOTE: Future version might use file: URI instead of file path. |
60 |
}, |
61 |
'undef-check!' => \$Opt{no_undef_check}, |
62 |
'update!' => \$Opt{update}, |
63 |
'verbose!' => \$Opt{verbose}, |
64 |
) or pod2usage (2); |
65 |
pod2usage ({-exitval => 0, -verbose => 1}) if $Opt{help}; |
66 |
$Opt{file_name} = shift; |
67 |
pod2usage ({-exitval => 2, -verbose => 0}) unless $Opt{file_name}; |
68 |
pod2usage ({-exitval => 2, -verbose => 0}) unless $Opt{output_file_name}; |
69 |
$Opt{no_undef_check} = defined $Opt{no_undef_check} |
70 |
? $Opt{no_undef_check} ? 0 : 1 : 0; |
71 |
$Opt{dis_suffix} = '.dis' unless defined $Opt{dis_suffix}; |
72 |
$Opt{daem_suffix} = '.dafm' unless defined $Opt{daem_suffix}; |
73 |
$Opt{dafx_suffix} = '.dafx' unless defined $Opt{dafx_suffix}; |
74 |
$Message::DOM::DOMFeature::DEBUG = 1 if $Opt{debug}; |
75 |
require Error; |
76 |
$Error::Debug = 1 if $Opt{debug}; |
77 |
$Message::Util::Error::VERBOSE = 1 if $Opt{verbose}; |
78 |
|
79 |
sub status_msg ($) { |
80 |
my $s = shift; |
81 |
$s .= "\n" unless $s =~ /\n$/; |
82 |
print STDERR $s; |
83 |
} |
84 |
|
85 |
sub status_msg_ ($) { |
86 |
my $s = shift; |
87 |
print STDERR $s; |
88 |
} |
89 |
|
90 |
sub verbose_msg ($) { |
91 |
my $s = shift; |
92 |
$s .= "\n" unless $s =~ /\n$/; |
93 |
print STDERR $s if $Opt{verbose}; |
94 |
} |
95 |
|
96 |
sub verbose_msg_ ($) { |
97 |
my $s = shift; |
98 |
print STDERR $s if $Opt{verbose}; |
99 |
} |
100 |
|
101 |
my $start_time; |
102 |
BEGIN { $start_time = time } |
103 |
|
104 |
use Message::Util::DIS::DNLite; |
105 |
|
106 |
my $limpl = $Message::DOM::ImplementationRegistry->get_implementation |
107 |
({ExpandedURI q<ManakaiDOM:Minimum> => '3.0', |
108 |
'+' . ExpandedURI q<DIS:DNLite> => '1.0'}); |
109 |
my $impl = $limpl->get_feature (ExpandedURI q<DIS:Core> => '1.0'); |
110 |
my $parser = $impl->create_dis_parser; |
111 |
our $DNi = $impl->get_feature (ExpandedURI q<DIS:DNLite> => '1.0'); |
112 |
|
113 |
my $db; |
114 |
|
115 |
if (defined $Opt{input_file_name}) { |
116 |
status_msg_ qq<Loading database "$Opt{input_file_name}"...>; |
117 |
$db = $impl->pl_load_dis_database ($Opt{input_file_name}, sub ($$) { |
118 |
my ($db, $mod, $type) = @_; |
119 |
my $ns = $mod->namespace_uri; |
120 |
my $ln = $mod->local_name; |
121 |
my $suffix = $type eq ExpandedURI q<dp:ModuleIndexFile> |
122 |
? $Opt{dafx_suffix} : $Opt{daem_suffix}; |
123 |
verbose_msg qq<Database module <$ns$ln> is requested>; |
124 |
my $name = dac_search_file_path_stem ($ns, $ln, $suffix); |
125 |
if (defined $name) { |
126 |
return $name.$suffix; |
127 |
} else { |
128 |
return undef; |
129 |
} |
130 |
}); |
131 |
status_msg qq<done>; |
132 |
} else { ## New database |
133 |
$db = $impl->create_dis_database; |
134 |
} |
135 |
|
136 |
require Cwd; |
137 |
my $file_name = Cwd::abs_path ($Opt{file_name}); |
138 |
$Opt{db_base_path} = Cwd::abs_path ($Opt{db_base_path}) |
139 |
if length $Opt{db_base_path}; |
140 |
my $doc = dac_load_module_file ($db, $parser, $file_name, $Opt{db_base_path}); |
141 |
|
142 |
my $for = $Opt{For}; |
143 |
$for = $doc->module_element->default_for_uri unless length $for; |
144 |
$db->get_for ($for)->is_referred ($doc); |
145 |
status_msg qq<Loading module in file "$file_name" for <$for>...>; |
146 |
|
147 |
my $srinfo = {}; |
148 |
if ($Opt{update}) { |
149 |
my $mod_uri = $doc->module_element |
150 |
->get_attribute_ns (ExpandedURI q<dis:>, 'QName') |
151 |
->qname_value_uri; |
152 |
for my $mod (@{$db->get_module_resource_list}) { |
153 |
if ($mod_uri eq $mod->name_uri) { |
154 |
status_msg_ qq[Removing module <$mod_uri> for <@{[$mod->for_uri]}>...]; |
155 |
$srinfo = $db->unload_module ($mod, srinfo => $srinfo); |
156 |
status_msg q<done>; |
157 |
} |
158 |
} |
159 |
## NOTE: Remove all |for|-variants of the module at the same time |
160 |
## - otherwise, |DISCore:TFPQNames| links will break. |
161 |
} |
162 |
|
163 |
my $ResourceCount = 0; |
164 |
$db->load_module ($doc, sub ($$$$$$) { |
165 |
my ($self, $db, $uri, $ns, $ln, $for) = @_; |
166 |
status_msg ''; |
167 |
status_msg qq<Loading module "$ln" for <$for>...>; |
168 |
$ResourceCount = 0; |
169 |
|
170 |
## -- Already in database |
171 |
my $doc = $db->get_source_file ($ns.$ln); |
172 |
return $doc if $doc; |
173 |
|
174 |
## -- Finds the source file |
175 |
my $name = dac_search_file_path_stem ($ns, $ln, $Opt{dis_suffix}); |
176 |
if (defined $name) { |
177 |
return dac_load_module_file |
178 |
($db, $parser, $name.$Opt{dis_suffix}, $Opt{db_base_path}); |
179 |
} |
180 |
|
181 |
## -- Not found |
182 |
return undef; |
183 |
}, for_arg => $for, on_resource_read => sub ($$) { |
184 |
if ((++$ResourceCount % 10) == 0) { |
185 |
status_msg_ "*"; |
186 |
status_msg_ " " if ($ResourceCount % (10 * 10)) == 0; |
187 |
status_msg '' if ($ResourceCount % (10 * 50)) == 0; |
188 |
} |
189 |
}, srinfo => $srinfo); |
190 |
|
191 |
|
192 |
## Removes reference from document to database |
193 |
our @Document; |
194 |
for my $dis (@Document) { |
195 |
$dis->unlink_from_document; |
196 |
$dis->dis_database (undef); |
197 |
} |
198 |
|
199 |
status_msg ''; |
200 |
|
201 |
status_msg qq<Reading properties...>; |
202 |
$ResourceCount = 0; |
203 |
$db->read_properties (on_resource_read => sub ($$) { |
204 |
if ((++$ResourceCount % 10) == 0) { |
205 |
status_msg_ "*"; |
206 |
status_msg_ " " if ($ResourceCount % (10 * 10)) == 0; |
207 |
status_msg '' if ($ResourceCount % (10 * 50)) == 0; |
208 |
} |
209 |
}); |
210 |
status_msg ''; |
211 |
status_msg "done"; |
212 |
|
213 |
status_msg_ qq<Writing file "$Opt{output_file_name}"...>; |
214 |
$db->pl_store ($Opt{output_file_name}, sub ($$) { |
215 |
my ($db, $mod, $type) = @_; |
216 |
my $ns = $mod->namespace_uri; |
217 |
my $ln = $mod->local_name; |
218 |
my $suffix = $type eq ExpandedURI q<dp:ModuleIndexFile> |
219 |
? $Opt{dafx_suffix} : $Opt{daem_suffix}; |
220 |
my $name = dac_search_file_path_stem ($ns, $ln, $suffix); |
221 |
if (defined $name) { |
222 |
$name .= $suffix; |
223 |
} elsif (defined ($name = dac_search_file_path_stem |
224 |
($ns, $ln, $Opt{dis_suffix}))) { |
225 |
$name .= $suffix; |
226 |
} else { |
227 |
$name = Cwd::abs_path |
228 |
(File::Spec->canonpath |
229 |
(File::Spec->catfile |
230 |
(defined $Opt{input_search_path}->{$ns}->[0] |
231 |
? $Opt{input_search_path}->{$ns}->[0] : '.', |
232 |
$ln.$suffix))); |
233 |
} |
234 |
verbose_msg qq<Database >. |
235 |
($type eq <Q::dp|ModuleIndexFile> ? 'index' : 'module'). |
236 |
qq< <$ns$ln> is written to "$name">; |
237 |
return $name; |
238 |
}); |
239 |
status_msg "done"; |
240 |
|
241 |
unless ($Opt{no_undef_check}) { |
242 |
status_msg_ "Checking undefined resources..."; |
243 |
$db->check_undefined_resource; |
244 |
print STDERR "done\n"; |
245 |
} |
246 |
|
247 |
status_msg_ "Closing the database..."; |
248 |
$db->free; |
249 |
undef $db; |
250 |
status_msg "done"; |
251 |
|
252 |
undef $DNi; |
253 |
|
254 |
{ |
255 |
use integer; |
256 |
my $time = time - $start_time; |
257 |
status_msg sprintf qq<%d'%02d''>, $time / 60, $time % 60; |
258 |
} |
259 |
exit; |
260 |
|
261 |
END { |
262 |
$db->free if $db; |
263 |
} |
264 |
|
265 |
## (db, parser, abs file path, abs base path) -> dis doc obj |
266 |
sub dac_load_module_file ($$$;$) { |
267 |
my ($db, $parser, $file_name, $base_path) = @_; |
268 |
require URI::file; |
269 |
my $base_uri = length $base_path ? URI::file->new ($base_path.'/') |
270 |
: 'http://dummy.invalid/'; |
271 |
my $file_uri = URI::file->new ($file_name)->rel ($base_uri); |
272 |
my $dis = $db->get_source_file ($file_uri); |
273 |
unless ($dis) { |
274 |
status_msg_ qq<Opening source file <$file_uri>...>; |
275 |
open my $file, '<', $file_name or die "$0: $file_name: $!"; |
276 |
$dis = $parser->parse ({character_stream => $file}); |
277 |
$dis->flag (ExpandedURI q<swcfg21:fileName> => $file_uri); |
278 |
$dis->dis_database ($db); |
279 |
|
280 |
my $mod = $dis->module_element; |
281 |
if ($mod) { |
282 |
my $qn = $mod->get_attribute_ns (ExpandedURI q<dis:>, 'QName'); |
283 |
if ($qn) { |
284 |
my $prefix = $qn->value; |
285 |
$prefix =~ s/^[^:|]*[:|]\s*//; |
286 |
$prefix =~ s/\s+$//; |
287 |
unless (defined $dis->lookup_namespace_uri ($prefix)) { |
288 |
$dis->add_namespace_binding ($prefix => $mod->defining_namespace_uri); |
289 |
} |
290 |
} |
291 |
} |
292 |
|
293 |
my $old_dis = $dis; |
294 |
status_msg_ qq<...>; |
295 |
$dis = $DNi->convert_dis_document_to_dnl_document |
296 |
($old_dis, database_arg => $db); |
297 |
push @Document, $dis; |
298 |
$old_dis->free; |
299 |
|
300 |
$db->set_source_file ($file_uri => $dis); |
301 |
status_msg qq<done>; |
302 |
} |
303 |
$dis; |
304 |
} |
305 |
|
306 |
sub dac_search_file_path_stem ($$$) { |
307 |
my ($ns, $ln, $suffix) = @_; |
308 |
require File::Spec; |
309 |
for my $dir ('.', @{$Opt{input_search_path}->{$ns}||[]}) { |
310 |
my $name = Cwd::abs_path |
311 |
(File::Spec->canonpath |
312 |
(File::Spec->catfile ($dir, $ln))); |
313 |
if (-f $name.$suffix) { |
314 |
return $name; |
315 |
} |
316 |
} |
317 |
return undef; |
318 |
} # dac_search_file_path_stem; |
319 |
|
320 |
__END__ |
321 |
|
322 |
=head1 NAME |
323 |
|
324 |
dac.pl - Creating "dac" Database File from "dis" Source Files |
325 |
|
326 |
=head1 SYNOPSIS |
327 |
|
328 |
perl path/to/dac.pl [--input-db-file-name=input.dac] \ |
329 |
--output-file-name=out.dac [options...] \ |
330 |
input.dis |
331 |
perl path/to/dac.pl --help |
332 |
|
333 |
=head1 DESCRIPTION |
334 |
|
335 |
This script, C<dac.pl>, compiles "dis" source files into "dac" |
336 |
database file. The generated database file can be used |
337 |
in turn to generate Perl module file, for example, by another |
338 |
script C<dac2pm.pl> or can be used to create larger database |
339 |
by specifying its file name as the C<--input-db-file-name> |
340 |
argument of another C<dac.pl> execution. |
341 |
|
342 |
This script is part of manakai. |
343 |
|
344 |
=head1 OPTIONS |
345 |
|
346 |
=over 4 |
347 |
|
348 |
=item I<input.dis> (Required) |
349 |
|
350 |
The unnamed option specifies a file name path of the source "dis" file |
351 |
from which a database is created. This option is required. |
352 |
|
353 |
=item C<--input-db-file-name=I<file-name>> (Default: none) |
354 |
|
355 |
A file path of the base database. This option is optional; if this |
356 |
option is specified, the database file is loaded first |
357 |
and then I<input.dis> file is loaded in the context of it. |
358 |
Otherwise, a new database is created. |
359 |
|
360 |
=item C<--output-file-name=I<file-name>> (Required) |
361 |
|
362 |
The |
363 |
|
364 |
=back |
365 |
|
366 |
=head1 SEE ALSO |
367 |
|
368 |
L<bin/dac2pm.pl> - Generating Perl module from "dac" file. |
369 |
|
370 |
L<lib/Message/Util/DIS.dis> - The actual implementation |
371 |
of the "dis" interpretation. |
372 |
|
373 |
=head1 LICENSE |
374 |
|
375 |
Copyright 2004-2006 Wakaba <w@suika.fam.cx>. All rights reserved. |
376 |
|
377 |
This program is free software; you can redistribute it and/or |
378 |
modify it under the same terms as Perl itself. |
379 |
|
380 |
=cut |