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 |
|
|
dp => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#Perl/>, |
8 |
|
|
fe => q<http://suika.fam.cx/www/2006/feature/>, |
9 |
|
|
ManakaiDOM => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/manakai-dom#>, |
10 |
wakaba |
1.2 |
pc => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/PerlCode#>, |
11 |
wakaba |
1.1 |
swcfg21 => q<http://suika.fam.cx/~wakaba/archive/2005/swcfg21#>, |
12 |
|
|
Util => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/>, |
13 |
|
|
}; |
14 |
|
|
|
15 |
wakaba |
1.17 |
our$VERSION=do{my @r=(q$Revision: 1.16 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
16 |
wakaba |
1.1 |
use Cwd; |
17 |
|
|
use Getopt::Long; |
18 |
|
|
use Pod::Usage; |
19 |
wakaba |
1.15 |
our %Opt = (create_module => []); |
20 |
wakaba |
1.14 |
my @target_modules; |
21 |
wakaba |
1.1 |
GetOptions ( |
22 |
wakaba |
1.17 |
'create-dtd-driver=s' => sub { |
23 |
|
|
shift; |
24 |
|
|
my $i = [split /\s+/, shift, 3]; |
25 |
|
|
$i->[3] = 'dtd-driver'; |
26 |
|
|
push @{$Opt{create_module}}, $i; |
27 |
|
|
}, |
28 |
wakaba |
1.14 |
'create-dtd-modules=s' => sub { |
29 |
|
|
shift; |
30 |
|
|
my $i = [split /\s+/, shift, 3]; |
31 |
|
|
$i->[3] = 'dtd-modules'; |
32 |
|
|
push @{$Opt{create_module}}, $i; |
33 |
|
|
}, |
34 |
wakaba |
1.1 |
'create-perl-module=s' => sub { |
35 |
|
|
shift; |
36 |
|
|
my $i = [split /\s+/, shift, 3]; |
37 |
wakaba |
1.2 |
$i->[3] = 'perl-pm'; |
38 |
|
|
push @{$Opt{create_module}}, $i; |
39 |
wakaba |
1.14 |
push @target_modules, [$i->[0], $i->[2]]; |
40 |
wakaba |
1.2 |
}, |
41 |
|
|
'create-perl-test=s' => sub { |
42 |
|
|
shift; |
43 |
|
|
my $i = [split /\s+/, shift, 3]; |
44 |
|
|
$i->[3] = 'perl-t'; |
45 |
wakaba |
1.1 |
push @{$Opt{create_module}}, $i; |
46 |
wakaba |
1.14 |
push @target_modules, [$i->[0], $i->[2]]; |
47 |
wakaba |
1.1 |
}, |
48 |
|
|
'debug' => \$Opt{debug}, |
49 |
|
|
'dis-file-suffix=s' => \$Opt{dis_suffix}, |
50 |
|
|
'daem-file-suffix=s' => \$Opt{daem_suffix}, |
51 |
wakaba |
1.11 |
'dafs-file-suffix=s' => \$Opt{dafs_suffix}, |
52 |
wakaba |
1.1 |
'dafx-file-suffix=s' => \$Opt{dafx_suffix}, |
53 |
wakaba |
1.17 |
'dtd-file-suffix=s' => \$Opt{dtd_suffix}, |
54 |
wakaba |
1.1 |
'help' => \$Opt{help}, |
55 |
wakaba |
1.14 |
'load-module=s' => sub { |
56 |
|
|
shift; |
57 |
|
|
my $i = [split /\s+/, shift, 2]; |
58 |
|
|
push @target_modules, [$i->[0], $i->[1]]; |
59 |
|
|
}, |
60 |
wakaba |
1.15 |
'mod-file-suffix=s' => \$Opt{mod_suffix}, |
61 |
wakaba |
1.1 |
'search-path|I=s' => sub { |
62 |
|
|
shift; |
63 |
|
|
my @value = split /\s+/, shift; |
64 |
|
|
while (my ($ns, $path) = splice @value, 0, 2, ()) { |
65 |
|
|
unless (defined $path) { |
66 |
|
|
die qq[$0: Search-path parameter without path: "$ns"]; |
67 |
|
|
} |
68 |
|
|
push @{$Opt{input_search_path}->{$ns} ||= []}, $path; |
69 |
|
|
} |
70 |
|
|
}, |
71 |
|
|
'search-path-catalog-file-name=s' => sub { |
72 |
|
|
shift; |
73 |
|
|
require File::Spec; |
74 |
|
|
my $path = my $path_base = shift; |
75 |
|
|
$path_base =~ s#[^/]+$##; |
76 |
|
|
$Opt{search_path_base} = $path_base; |
77 |
|
|
open my $file, '<', $path or die "$0: $path: $!"; |
78 |
|
|
while (<$file>) { |
79 |
|
|
if (s/^\s*\@//) { ## Processing instruction |
80 |
|
|
my ($target, $data) = split /\s+/; |
81 |
|
|
if ($target eq 'base') { |
82 |
|
|
$Opt{search_path_base} = File::Spec->rel2abs ($data, $path_base); |
83 |
|
|
} else { |
84 |
|
|
die "$0: $target: Unknown target"; |
85 |
|
|
} |
86 |
|
|
} elsif (/^\s*\#/) { ## Comment |
87 |
|
|
# |
88 |
|
|
} elsif (/\S/) { ## Catalog entry |
89 |
|
|
s/^\s+//; |
90 |
|
|
my ($ns, $path) = split /\s+/; |
91 |
|
|
push @{$Opt{input_search_path}->{$ns} ||= []}, |
92 |
|
|
File::Spec->rel2abs ($path, $Opt{search_path_base}); |
93 |
|
|
} |
94 |
|
|
} |
95 |
|
|
## NOTE: File paths with SPACEs are not supported |
96 |
|
|
## NOTE: Future version might use file: URI instead of file path. |
97 |
|
|
}, |
98 |
|
|
'undef-check!' => \$Opt{no_undef_check}, |
99 |
|
|
'verbose!' => \$Opt{verbose}, |
100 |
|
|
) or pod2usage (2); |
101 |
|
|
pod2usage ({-exitval => 0, -verbose => 1}) if $Opt{help}; |
102 |
|
|
$Opt{no_undef_check} = defined $Opt{no_undef_check} |
103 |
|
|
? $Opt{no_undef_check} ? 0 : 1 : 0; |
104 |
|
|
$Opt{dis_suffix} = '.dis' unless defined $Opt{dis_suffix}; |
105 |
|
|
$Opt{daem_suffix} = '.dafm' unless defined $Opt{daem_suffix}; |
106 |
|
|
$Opt{dafx_suffix} = '.dafx' unless defined $Opt{dafx_suffix}; |
107 |
wakaba |
1.11 |
$Opt{dafs_suffix} = '.dafs' unless defined $Opt{dafs_suffix}; |
108 |
wakaba |
1.17 |
$Opt{dtd_suffix} = '.dtd' unless defined $Opt{dtd_suffix}; |
109 |
wakaba |
1.15 |
$Opt{mod_suffix} = '.mod' unless defined $Opt{mod_suffix}; |
110 |
wakaba |
1.1 |
$Message::DOM::DOMFeature::DEBUG = 1 if $Opt{debug}; |
111 |
|
|
require Error; |
112 |
|
|
$Error::Debug = 1 if $Opt{debug}; |
113 |
|
|
$Message::Util::Error::VERBOSE = 1 if $Opt{verbose}; |
114 |
|
|
|
115 |
|
|
sub status_msg ($) { |
116 |
|
|
my $s = shift; |
117 |
|
|
$s .= "\n" unless $s =~ /\n$/; |
118 |
|
|
print STDERR $s; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
sub status_msg_ ($) { |
122 |
|
|
my $s = shift; |
123 |
|
|
print STDERR $s; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
sub verbose_msg ($) { |
127 |
|
|
my $s = shift; |
128 |
|
|
$s .= "\n" unless $s =~ /\n$/; |
129 |
|
|
print STDERR $s if $Opt{verbose}; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
sub verbose_msg_ ($) { |
133 |
|
|
my $s = shift; |
134 |
|
|
print STDERR $s if $Opt{verbose}; |
135 |
|
|
} |
136 |
|
|
|
137 |
wakaba |
1.2 |
## ---- The MAIN Program |
138 |
|
|
|
139 |
wakaba |
1.1 |
my $start_time; |
140 |
|
|
BEGIN { $start_time = time } |
141 |
|
|
|
142 |
|
|
use Message::Util::DIS::DNLite; |
143 |
wakaba |
1.5 |
|
144 |
|
|
my %feature; |
145 |
wakaba |
1.1 |
|
146 |
wakaba |
1.13 |
for (@{$Opt{create_module}}) { |
147 |
|
|
my (undef, undef, undef, $out_type) = @$_; |
148 |
|
|
|
149 |
|
|
if ($out_type eq 'perl-pm') { |
150 |
|
|
require 'manakai/daf-perl-pm.pl'; |
151 |
|
|
$feature{'+' . ExpandedURI q<Util:PerlCode>} = '1.0'; |
152 |
|
|
} elsif ($out_type eq 'perl-t') { |
153 |
|
|
require 'manakai/daf-perl-t.pl'; |
154 |
|
|
$feature{ExpandedURI q<fe:GenericLS>} = '3.0'; |
155 |
|
|
$feature{'+' . ExpandedURI q<DIS:TDT>} = '1.0'; |
156 |
|
|
$feature{'+' . ExpandedURI q<Util:PerlCode>} = '1.0'; |
157 |
wakaba |
1.14 |
} elsif ($out_type eq 'dtd-modules') { |
158 |
|
|
require 'manakai/daf-dtd-modules.pl'; |
159 |
|
|
$feature{ExpandedURI q<fe:GenericLS>} = '3.0'; |
160 |
|
|
$feature{'+' . ExpandedURI q<fe:XDP>} = '3.0'; |
161 |
wakaba |
1.17 |
} elsif ($out_type eq 'dtd-driver') { |
162 |
|
|
require 'manakai/daf-dtd-modules.pl'; |
163 |
|
|
$feature{ExpandedURI q<fe:GenericLS>} = '3.0'; |
164 |
|
|
$feature{'+' . ExpandedURI q<fe:XDP>} = '3.0'; |
165 |
wakaba |
1.13 |
} |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
our $limpl = $Message::DOM::ImplementationRegistry->get_implementation |
169 |
wakaba |
1.1 |
({ExpandedURI q<fe:Min> => '3.0', |
170 |
|
|
'+' . ExpandedURI q<DIS:DNLite> => '1.0', |
171 |
|
|
'+' . ExpandedURI q<DIS:Core> => '1.0', |
172 |
wakaba |
1.5 |
%feature, |
173 |
wakaba |
1.1 |
}); |
174 |
wakaba |
1.13 |
our $impl = $limpl->get_feature (ExpandedURI q<DIS:Core> => '1.0'); |
175 |
wakaba |
1.2 |
|
176 |
|
|
## --- Loading and Updating the Database |
177 |
wakaba |
1.1 |
|
178 |
|
|
my $HasError; |
179 |
wakaba |
1.13 |
our $db = $impl->create_dis_database; |
180 |
wakaba |
1.1 |
$db->pl_database_module_resolver (\&daf_db_module_resolver); |
181 |
|
|
$db->dom_config->set_parameter ('error-handler' => \&daf_on_error); |
182 |
|
|
|
183 |
wakaba |
1.2 |
my $parser = $impl->create_dis_parser; |
184 |
|
|
my $DNi = $impl->get_feature (ExpandedURI q<DIS:DNLite> => '1.0'); |
185 |
wakaba |
1.1 |
my %ModuleSourceDISDocument; |
186 |
|
|
my %ModuleSourceDNLDocument; |
187 |
|
|
my %ModuleNameNamespaceBinding = ( |
188 |
|
|
DISCore => q<http://suika.fam.cx/~wakaba/archive/2004/dis/Core#>, |
189 |
|
|
## This builtin binding is required since |
190 |
|
|
## some module has |DISCore:author| property before |dis:Require| |
191 |
|
|
## property. |
192 |
|
|
); |
193 |
|
|
|
194 |
|
|
my $ResourceCount = 0; |
195 |
|
|
$db->pl_update_module (\@target_modules, |
196 |
|
|
get_module_index_file_name => sub { |
197 |
|
|
shift; # $db |
198 |
|
|
daf_get_module_index_file_name (@_); |
199 |
|
|
}, |
200 |
|
|
get_module_source_document_from_uri => sub { |
201 |
|
|
my ($db, $module_uri, $module_for) = @_; |
202 |
|
|
status_msg ''; |
203 |
|
|
status_msg qq<Loading module <$module_uri> for <$module_for>...>; |
204 |
|
|
$ResourceCount = 0; |
205 |
|
|
|
206 |
|
|
unless (defined $ModuleSourceDNLDocument{$module_uri}) { |
207 |
|
|
unless (defined $ModuleSourceDISDocument{$module_uri}) { |
208 |
|
|
daf_open_source_dis_document ($module_uri); |
209 |
|
|
} |
210 |
|
|
daf_convert_dis_document_to_dnl_document (); |
211 |
|
|
} |
212 |
|
|
return $ModuleSourceDNLDocument{$module_uri}; |
213 |
|
|
}, |
214 |
|
|
get_module_source_document_from_resource => sub ($$$$$$) { |
215 |
|
|
my ($self, $db, $uri, $ns, $ln, $for) = @_; |
216 |
|
|
status_msg ''; |
217 |
|
|
status_msg qq<Loading module "$ln" for <$for>...>; |
218 |
|
|
$ResourceCount = 0; |
219 |
|
|
|
220 |
|
|
my $module_uri = $ns.$ln; |
221 |
|
|
unless (defined $ModuleSourceDNLDocument{$module_uri}) { |
222 |
|
|
unless (defined $ModuleSourceDISDocument{$module_uri}) { |
223 |
|
|
daf_open_source_dis_document ($module_uri); |
224 |
|
|
} |
225 |
|
|
daf_convert_dis_document_to_dnl_document (); |
226 |
|
|
} |
227 |
|
|
return $ModuleSourceDNLDocument{$module_uri}; |
228 |
|
|
}, |
229 |
|
|
get_module_source_revision => sub { |
230 |
|
|
my ($db, $module_uri) = @_; |
231 |
|
|
my $ns = $module_uri; |
232 |
|
|
$ns =~ s/(\w+)\z//; |
233 |
|
|
my $ln = $1; |
234 |
|
|
|
235 |
|
|
my $name = dac_search_file_path_stem ($ns, $ln, $Opt{dis_suffix}); |
236 |
|
|
if (defined $name) { |
237 |
|
|
return [stat $name.$Opt{dis_suffix}]->[9]; |
238 |
|
|
} else { |
239 |
|
|
return 0; |
240 |
|
|
} |
241 |
|
|
}, |
242 |
|
|
get_referring_module_uri_list => sub { |
243 |
|
|
my ($db, $module_uri) = @_; |
244 |
|
|
unless (defined $ModuleSourceDNLDocument{$module_uri}) { |
245 |
|
|
unless (defined $ModuleSourceDISDocument{$module_uri}) { |
246 |
|
|
daf_open_source_dis_document ($module_uri); |
247 |
|
|
} |
248 |
|
|
} |
249 |
|
|
return daf_get_referring_module_uri_list ($module_uri); |
250 |
|
|
}, |
251 |
|
|
on_resource_read => sub ($$) { |
252 |
|
|
if ((++$ResourceCount % 10) == 0) { |
253 |
|
|
status_msg_ "*"; |
254 |
|
|
status_msg_ " " if ($ResourceCount % (10 * 10)) == 0; |
255 |
|
|
status_msg '' if ($ResourceCount % (10 * 50)) == 0; |
256 |
|
|
} |
257 |
|
|
}); |
258 |
|
|
|
259 |
|
|
|
260 |
|
|
## Removes reference from document to database |
261 |
|
|
our @Document; |
262 |
|
|
for my $dis (@Document) { |
263 |
|
|
$dis->unlink_from_document; |
264 |
|
|
$dis->dis_database (undef); |
265 |
|
|
} |
266 |
|
|
|
267 |
|
|
status_msg ''; |
268 |
|
|
|
269 |
|
|
status_msg qq<Reading properties...>; |
270 |
|
|
$ResourceCount = 0; |
271 |
|
|
$db->read_properties (on_resource_read => sub ($$) { |
272 |
|
|
if ((++$ResourceCount % 10) == 0) { |
273 |
|
|
status_msg_ "*"; |
274 |
|
|
status_msg_ " " if ($ResourceCount % (10 * 10)) == 0; |
275 |
|
|
status_msg '' if ($ResourceCount % (10 * 50)) == 0; |
276 |
|
|
} |
277 |
wakaba |
1.16 |
}, implementation => $impl); |
278 |
wakaba |
1.1 |
status_msg ''; |
279 |
|
|
status_msg "done"; |
280 |
|
|
|
281 |
|
|
status_msg_ qq<Writing database files...>; |
282 |
|
|
$db->pl_store ('dummy', sub ($$) { |
283 |
|
|
my ($db, $mod, $type) = @_; |
284 |
|
|
my $ns = $mod->namespace_uri; |
285 |
|
|
my $ln = $mod->local_name; |
286 |
|
|
my $suffix = $type eq ExpandedURI q<dp:ModuleIndexFile> |
287 |
|
|
? $Opt{dafx_suffix} : $Opt{daem_suffix}; |
288 |
|
|
my $name = dac_search_file_path_stem ($ns, $ln, $suffix); |
289 |
|
|
if (defined $name) { |
290 |
|
|
$name .= $suffix; |
291 |
|
|
} elsif (defined ($name = dac_search_file_path_stem |
292 |
|
|
($ns, $ln, $Opt{dis_suffix}))) { |
293 |
|
|
$name .= $suffix; |
294 |
|
|
} else { |
295 |
|
|
$name = Cwd::abs_path |
296 |
|
|
(File::Spec->canonpath |
297 |
|
|
(File::Spec->catfile |
298 |
|
|
(defined $Opt{input_search_path}->{$ns}->[0] |
299 |
|
|
? $Opt{input_search_path}->{$ns}->[0] : '.', |
300 |
|
|
$ln.$suffix))); |
301 |
|
|
} |
302 |
|
|
verbose_msg qq<Database >. |
303 |
|
|
($type eq <Q::dp|ModuleIndexFile> ? 'index' : 'module'). |
304 |
|
|
qq< <$ns$ln> is written to "$name">; |
305 |
|
|
return $name; |
306 |
|
|
}, no_main_database => 1); |
307 |
|
|
status_msg "done"; |
308 |
|
|
|
309 |
|
|
daf_check_undefined (); |
310 |
|
|
|
311 |
wakaba |
1.2 |
undef $DNi; |
312 |
|
|
undef %ModuleSourceDNLDocument; |
313 |
|
|
exit $HasError if $HasError; |
314 |
|
|
|
315 |
|
|
## --- Creating Files |
316 |
|
|
|
317 |
wakaba |
1.1 |
for (@{$Opt{create_module}}) { |
318 |
|
|
my ($mod_uri, $out_file_path, $mod_for, $out_type) = @$_; |
319 |
|
|
|
320 |
wakaba |
1.2 |
if ($out_type eq 'perl-pm') { |
321 |
wakaba |
1.13 |
daf_perl_pm ($mod_uri, $out_file_path, $mod_for); |
322 |
wakaba |
1.2 |
} elsif ($out_type eq 'perl-t') { |
323 |
wakaba |
1.13 |
daf_perl_t ($mod_uri, $out_file_path, $mod_for); |
324 |
wakaba |
1.14 |
} elsif ($out_type eq 'dtd-modules') { |
325 |
|
|
daf_dtd_modules ($mod_uri, $out_file_path, $mod_for); |
326 |
wakaba |
1.17 |
} elsif ($out_type eq 'dtd-driver') { |
327 |
|
|
daf_dtd_driver ($mod_uri, $out_file_path, $mod_for); |
328 |
wakaba |
1.1 |
} |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
daf_check_undefined (); |
332 |
|
|
|
333 |
wakaba |
1.2 |
## --- The END |
334 |
|
|
|
335 |
wakaba |
1.1 |
status_msg_ "Closing the database..."; |
336 |
|
|
$db->free; |
337 |
|
|
undef $db; |
338 |
|
|
status_msg "done"; |
339 |
|
|
|
340 |
wakaba |
1.7 |
undef $limpl; |
341 |
|
|
undef $impl; |
342 |
|
|
|
343 |
wakaba |
1.1 |
{ |
344 |
|
|
use integer; |
345 |
|
|
my $time = time - $start_time; |
346 |
|
|
status_msg sprintf qq<%d'%02d''>, $time / 60, $time % 60; |
347 |
|
|
} |
348 |
|
|
exit $HasError; |
349 |
|
|
|
350 |
|
|
END { |
351 |
|
|
$db->free if $db; |
352 |
|
|
} |
353 |
|
|
|
354 |
wakaba |
1.2 |
## ---- Subroutines |
355 |
|
|
|
356 |
wakaba |
1.1 |
sub daf_open_source_dis_document ($) { |
357 |
|
|
my ($module_uri) = @_; |
358 |
|
|
|
359 |
|
|
## -- Finds |dis| source file |
360 |
|
|
my $ns = $module_uri; |
361 |
|
|
$ns =~ s/(\w+)\z//; |
362 |
|
|
my $ln = $1; |
363 |
|
|
my $file_name = dac_search_file_path_stem ($ns, $ln, $Opt{dis_suffix}); |
364 |
|
|
unless (defined $file_name) { |
365 |
|
|
die "$0: Source file for <$ns$ln> is not found"; |
366 |
|
|
} |
367 |
|
|
$file_name .= $Opt{dis_suffix}; |
368 |
|
|
|
369 |
|
|
status_msg_ qq<Opening dis source file "$file_name"...>; |
370 |
|
|
|
371 |
|
|
## -- Opens |dis| file and construct |DISDocument| tree |
372 |
|
|
open my $file, '<', $file_name or die "$0: $file_name: $!"; |
373 |
|
|
my $dis = $parser->parse ({character_stream => $file}); |
374 |
|
|
require File::Spec; |
375 |
|
|
$dis->flag (ExpandedURI q<swcfg21:fileName> => |
376 |
|
|
File::Spec->abs2rel ($file_name)); |
377 |
|
|
$dis->dis_namespace_resolver (\&daf_module_name_namespace_resolver); |
378 |
|
|
close $file; |
379 |
|
|
|
380 |
|
|
## -- Registers namespace URI |
381 |
|
|
my $mod = $dis->module_element; |
382 |
|
|
if ($mod) { |
383 |
|
|
my $qn = $mod->get_attribute_ns (ExpandedURI q<dis:>, 'QName'); |
384 |
|
|
if ($qn) { |
385 |
|
|
my $prefix = $qn->value; |
386 |
|
|
$prefix =~ s/^[^:|]*[:|]\s*//; |
387 |
|
|
$prefix =~ s/\s+$//; |
388 |
|
|
unless (defined $ModuleNameNamespaceBinding{$prefix}) { |
389 |
|
|
$ModuleNameNamespaceBinding{$prefix} = $mod->defining_namespace_uri; |
390 |
|
|
} |
391 |
|
|
} |
392 |
|
|
} |
393 |
|
|
|
394 |
|
|
$ModuleSourceDISDocument{$module_uri} = $dis; |
395 |
|
|
status_msg q<done>; |
396 |
|
|
|
397 |
|
|
R: for (@{daf_get_referring_module_uri_list ($module_uri)}) { |
398 |
|
|
next R if defined $db->{modDef}->{$_}; |
399 |
|
|
next R if defined $ModuleSourceDNLDocument{$_}; |
400 |
|
|
next R if defined $ModuleSourceDISDocument{$_}; |
401 |
|
|
my $idx_file_name = daf_get_module_index_file_name ($_); |
402 |
|
|
if (-f $idx_file_name) { |
403 |
|
|
daf_open_current_module_index ($_, $idx_file_name); |
404 |
|
|
} else { |
405 |
|
|
daf_open_source_dis_document ($_); |
406 |
|
|
} |
407 |
|
|
} |
408 |
|
|
} # daf_open_source_dis_document |
409 |
|
|
|
410 |
|
|
sub daf_open_current_module_index ($$) { |
411 |
|
|
my ($module_uri, $file_name) = @_; |
412 |
|
|
$db->pl_load_dis_database_index ($file_name); |
413 |
|
|
|
414 |
|
|
R: for (@{$db->get_module ($module_uri) |
415 |
|
|
->get_referring_module_uri_list}) { |
416 |
|
|
next R if defined $db->{modDef}->{$_}; |
417 |
|
|
next R if defined $ModuleSourceDNLDocument{$_}; |
418 |
|
|
next R if defined $ModuleSourceDISDocument{$_}; |
419 |
|
|
my $idx_file_name = daf_get_module_index_file_name ($_); |
420 |
|
|
if (-f $idx_file_name) { |
421 |
|
|
daf_open_current_module_index ($_, $idx_file_name); |
422 |
|
|
} else { |
423 |
|
|
daf_open_source_dis_document ($_); |
424 |
|
|
} |
425 |
|
|
} |
426 |
|
|
} # daf_open_current_module_index |
427 |
|
|
|
428 |
|
|
sub daf_convert_dis_document_to_dnl_document () { |
429 |
|
|
M: for my $module_uri (keys %ModuleSourceDISDocument) { |
430 |
|
|
my $dis_doc = $ModuleSourceDISDocument{$module_uri}; |
431 |
|
|
next M unless $dis_doc; |
432 |
|
|
verbose_msg_ qq<Converting <$module_uri>...>; |
433 |
|
|
my $dnl_doc = $DNi->convert_dis_document_to_dnl_document |
434 |
|
|
($dis_doc, database_arg => $db, |
435 |
|
|
base_namespace_binding => |
436 |
|
|
{(map {$_->local_name => $_->target_namespace_uri} |
437 |
|
|
grep {$_} values %{$db->{modDef}}), |
438 |
|
|
%ModuleNameNamespaceBinding}); |
439 |
|
|
push @Document, $dnl_doc; |
440 |
|
|
$ModuleSourceDNLDocument{$module_uri} = $dnl_doc; |
441 |
|
|
$dis_doc->free; |
442 |
|
|
delete $ModuleSourceDISDocument{$module_uri}; |
443 |
|
|
verbose_msg q<done>; |
444 |
|
|
} |
445 |
|
|
} # daf_convert_dis_document_to_dnl_document |
446 |
|
|
|
447 |
|
|
sub daf_get_referring_module_uri_list ($) { |
448 |
|
|
my $module_uri = shift; |
449 |
|
|
my $ns = $module_uri; |
450 |
|
|
$ns =~ s/\w+\z//; |
451 |
|
|
my $src = $ModuleSourceDNLDocument{$module_uri}; |
452 |
|
|
$src = $ModuleSourceDISDocument{$module_uri} unless defined $src; |
453 |
|
|
my $mod_el = $src->module_element; |
454 |
|
|
my $r = []; |
455 |
|
|
if ($mod_el) { |
456 |
|
|
my $req_el = $mod_el->require_element; |
457 |
|
|
if ($req_el) { |
458 |
|
|
M: for my $m_el (@{$req_el->child_nodes}) { |
459 |
|
|
next M unless $m_el->node_type eq '#element'; |
460 |
|
|
next M unless $m_el->expanded_uri eq ExpandedURI q<dis:Module>; |
461 |
|
|
my $qn_el = $m_el->get_attribute_ns (ExpandedURI q<dis:>, 'QName'); |
462 |
|
|
if ($qn_el) { |
463 |
|
|
push @$r, $qn_el->qname_value_uri; |
464 |
|
|
} else { |
465 |
|
|
my $n_el = $m_el->get_attribute_ns (ExpandedURI q<dis:>, 'Name'); |
466 |
|
|
if ($n_el) { |
467 |
|
|
push @$r, $ns.$n_el->value; |
468 |
|
|
} else { |
469 |
|
|
# The module itself |
470 |
|
|
} |
471 |
|
|
} |
472 |
|
|
} |
473 |
|
|
} |
474 |
|
|
} |
475 |
|
|
return $r; |
476 |
|
|
} # daf_get_referring_module_uri_list |
477 |
|
|
|
478 |
|
|
sub dac_search_file_path_stem ($$$) { |
479 |
|
|
my ($ns, $ln, $suffix) = @_; |
480 |
|
|
require File::Spec; |
481 |
wakaba |
1.8 |
for my $dir (@{$Opt{input_search_path}->{$ns}||[]}) { |
482 |
wakaba |
1.1 |
my $name = Cwd::abs_path |
483 |
|
|
(File::Spec->canonpath |
484 |
|
|
(File::Spec->catfile ($dir, $ln))); |
485 |
|
|
if (-f $name.$suffix) { |
486 |
|
|
return $name; |
487 |
|
|
} |
488 |
|
|
} |
489 |
|
|
return undef; |
490 |
|
|
} # dac_search_file_path_stem; |
491 |
|
|
|
492 |
|
|
sub daf_get_module_index_file_name ($$) { |
493 |
|
|
my ($module_uri) = @_; |
494 |
|
|
my $ns = $module_uri; |
495 |
|
|
$ns =~ s/(\w+)\z//; |
496 |
|
|
my $ln = $1; |
497 |
|
|
|
498 |
|
|
verbose_msg qq<Database module index <$module_uri> is requested>; |
499 |
|
|
my $suffix = $Opt{dafx_suffix}; |
500 |
|
|
my $name = dac_search_file_path_stem ($ns, $ln, $suffix); |
501 |
|
|
if (defined $name) { |
502 |
|
|
$name .= $suffix; |
503 |
|
|
} elsif (defined ($name = dac_search_file_path_stem |
504 |
|
|
($ns, $ln, $Opt{dis_suffix}))) { |
505 |
|
|
$name .= $suffix; |
506 |
|
|
} else { |
507 |
|
|
$name = Cwd::abs_path |
508 |
|
|
(File::Spec->canonpath |
509 |
|
|
(File::Spec->catfile |
510 |
|
|
(defined $Opt{input_search_path}->{$ns}->[0] |
511 |
|
|
? $Opt{input_search_path}->{$ns}->[0] : '.', |
512 |
|
|
$ln.$suffix))); |
513 |
|
|
} |
514 |
|
|
return $name; |
515 |
|
|
} # daf_get_module_index_file_name |
516 |
|
|
|
517 |
|
|
sub daf_module_name_namespace_resolver ($) { |
518 |
|
|
my $prefix = shift; |
519 |
|
|
|
520 |
|
|
## -- From modules in database |
521 |
|
|
M: for (values %{$db->{modDef}}) { |
522 |
|
|
my $mod = $_; |
523 |
|
|
next M unless defined $mod; |
524 |
|
|
if ($mod->local_name eq $prefix) { |
525 |
|
|
return $mod->target_namespace_uri; |
526 |
|
|
} |
527 |
|
|
} |
528 |
|
|
|
529 |
|
|
## -- From not-in-database-yet module list |
530 |
|
|
if (defined $ModuleNameNamespaceBinding{$prefix}) { |
531 |
|
|
return $ModuleNameNamespaceBinding{$prefix}; |
532 |
|
|
} |
533 |
|
|
return undef; |
534 |
|
|
} # daf_module_name_namespace_resolver |
535 |
|
|
|
536 |
|
|
sub daf_db_module_resolver ($$$) { |
537 |
|
|
my ($db, $mod, $type) = @_; |
538 |
|
|
my $ns = $mod->namespace_uri; |
539 |
|
|
my $ln = $mod->local_name; |
540 |
wakaba |
1.11 |
my $suffix = { |
541 |
|
|
ExpandedURI q<dp:ModuleIndexFile> => $Opt{dafx_suffix}, |
542 |
|
|
ExpandedURI q<dp:ModuleResourceFile> => $Opt{daem_suffix}, |
543 |
|
|
ExpandedURI q<dp:ModuleNodeStorageFile> => $Opt{dafs_suffix}, |
544 |
|
|
}->{$type} or die "Unsupported type: <$type>"; |
545 |
wakaba |
1.1 |
verbose_msg qq<Database module <$ns$ln> is requested>; |
546 |
|
|
my $name = dac_search_file_path_stem ($ns, $ln, $suffix); |
547 |
|
|
if (defined $name) { |
548 |
|
|
return $name.$suffix; |
549 |
|
|
} else { |
550 |
|
|
return undef; |
551 |
|
|
} |
552 |
|
|
} # daf_db_module_resolver |
553 |
|
|
|
554 |
|
|
sub daf_on_error ($$) { |
555 |
|
|
my ($self, $err) = @_; |
556 |
|
|
if ($err->severity == $err->SEVERITY_WARNING) { |
557 |
|
|
my $info = ExpandedURI q<dp:info>; |
558 |
|
|
if ($err->type =~ /\Q$info\E/) { |
559 |
|
|
my $msg = $err->text; |
560 |
|
|
if ($msg =~ /\.\.\.\z/) { |
561 |
|
|
verbose_msg_ $msg; |
562 |
|
|
} else { |
563 |
|
|
verbose_msg $msg; |
564 |
|
|
} |
565 |
|
|
} else { |
566 |
|
|
my $msg = $err->text; |
567 |
|
|
if ($msg =~ /\.\.\.\z/) { |
568 |
|
|
status_msg_ $msg; |
569 |
|
|
} else { |
570 |
|
|
status_msg $msg; |
571 |
|
|
} |
572 |
|
|
} |
573 |
|
|
} else { |
574 |
|
|
warn $err; |
575 |
|
|
$HasError = 1; |
576 |
|
|
} |
577 |
|
|
} # daf_on_error |
578 |
|
|
|
579 |
|
|
sub daf_check_undefined () { |
580 |
|
|
unless ($Opt{no_undef_check}) { |
581 |
|
|
status_msg_ "Checking undefined resources..."; |
582 |
|
|
$db->check_undefined_resource; |
583 |
|
|
print STDERR "done\n"; |
584 |
|
|
} |
585 |
|
|
} # daf_check_undefined |
586 |
wakaba |
1.2 |
|
587 |
wakaba |
1.1 |
__END__ |
588 |
|
|
|
589 |
|
|
=head1 NAME |
590 |
|
|
|
591 |
|
|
dac.pl - Creating "dac" Database File from "dis" Source Files |
592 |
|
|
|
593 |
|
|
=head1 SYNOPSIS |
594 |
|
|
|
595 |
|
|
perl path/to/dac.pl [--input-db-file-name=input.dac] \ |
596 |
|
|
--output-file-name=out.dac [options...] \ |
597 |
|
|
input.dis |
598 |
|
|
perl path/to/dac.pl --help |
599 |
|
|
|
600 |
|
|
=head1 DESCRIPTION |
601 |
|
|
|
602 |
|
|
This script, C<dac.pl>, compiles "dis" source files into "dac" |
603 |
|
|
database file. The generated database file can be used |
604 |
|
|
in turn to generate Perl module file, for example, by another |
605 |
|
|
script C<dac2pm.pl> or can be used to create larger database |
606 |
|
|
by specifying its file name as the C<--input-db-file-name> |
607 |
|
|
argument of another C<dac.pl> execution. |
608 |
|
|
|
609 |
|
|
This script is part of manakai. |
610 |
|
|
|
611 |
|
|
=head1 OPTIONS |
612 |
|
|
|
613 |
|
|
=over 4 |
614 |
|
|
|
615 |
|
|
=item I<input.dis> (Required) |
616 |
|
|
|
617 |
|
|
The unnamed option specifies a file name path of the source "dis" file |
618 |
|
|
from which a database is created. This option is required. |
619 |
|
|
|
620 |
|
|
=item C<--input-db-file-name=I<file-name>> (Default: none) |
621 |
|
|
|
622 |
|
|
A file path of the base database. This option is optional; if this |
623 |
|
|
option is specified, the database file is loaded first |
624 |
|
|
and then I<input.dis> file is loaded in the context of it. |
625 |
|
|
Otherwise, a new database is created. |
626 |
|
|
|
627 |
|
|
=back |
628 |
|
|
|
629 |
|
|
=head1 SEE ALSO |
630 |
|
|
|
631 |
|
|
L<lib/Message/Util/DIS.dis> - The actual implementation |
632 |
|
|
of the "dis" interpretation. |
633 |
|
|
|
634 |
|
|
=head1 LICENSE |
635 |
|
|
|
636 |
wakaba |
1.13 |
Copyright 2004-2006 Wakaba <w@suika.fam.cx>. All rights reserved. |
637 |
wakaba |
1.1 |
|
638 |
|
|
This program is free software; you can redistribute it and/or |
639 |
|
|
modify it under the same terms as Perl itself. |
640 |
|
|
|
641 |
|
|
=cut |