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