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