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 |
|
|
use Storable qw/nstore retrieve/; |
13 |
wakaba |
1.4 |
my %Opt = (); |
14 |
wakaba |
1.1 |
GetOptions ( |
15 |
|
|
'db-base-directory-path=s' => \$Opt{db_base_path}, |
16 |
|
|
'for=s' => \$Opt{For}, |
17 |
|
|
'help' => \$Opt{help}, |
18 |
wakaba |
1.4 |
'input-db-file-name=s' => \$Opt{input_file_name}, |
19 |
wakaba |
1.1 |
'output-file-name=s' => \$Opt{output_file_name}, |
20 |
wakaba |
1.2 |
'search-path|I=s' => sub { |
21 |
|
|
shift; |
22 |
|
|
my @value = split /\s+/, shift; |
23 |
|
|
while (my ($ns, $path) = splice @value, 0, 2, ()) { |
24 |
wakaba |
1.4 |
unless (defined $path) { |
25 |
|
|
die qq[$0: Search-path parameter without path: "$ns"]; |
26 |
|
|
} |
27 |
wakaba |
1.2 |
push @{$Opt{input_search_path}->{$ns} ||= []}, $path; |
28 |
|
|
} |
29 |
|
|
}, |
30 |
wakaba |
1.4 |
'search-path-catalog-file-name=s' => sub { |
31 |
|
|
shift; |
32 |
|
|
require File::Spec; |
33 |
|
|
my $path = my $path_base = shift; |
34 |
|
|
$path_base =~ s#[^/]+$##; |
35 |
|
|
$Opt{search_path_base} = $path_base; |
36 |
|
|
open my $file, '<', $path or die "$0: $path: $!"; |
37 |
|
|
while (<$file>) { |
38 |
|
|
if (s/^\s*\@//) { ## Processing instruction |
39 |
|
|
my ($target, $data) = split /\s+/; |
40 |
|
|
if ($target eq 'base') { |
41 |
|
|
$Opt{search_path_base} = File::Spec->rel2abs ($data, $path_base); |
42 |
|
|
} else { |
43 |
|
|
die "$0: $target: Unknown target"; |
44 |
|
|
} |
45 |
|
|
} elsif (/^\s*\#/) { ## Comment |
46 |
|
|
# |
47 |
|
|
} elsif (/\S/) { ## Catalog entry |
48 |
|
|
s/^\s+//; |
49 |
|
|
my ($ns, $path) = split /\s+/; |
50 |
|
|
push @{$Opt{input_search_path}->{$ns} ||= []}, |
51 |
|
|
File::Spec->rel2abs ($path, $Opt{search_path_base}); |
52 |
|
|
} |
53 |
|
|
} |
54 |
|
|
## NOTE: File paths with SPACEs are not supported |
55 |
|
|
## NOTE: Future version might use file: URI instead of file path. |
56 |
|
|
}, |
57 |
wakaba |
1.1 |
'undef-check!' => \$Opt{no_undef_check}, |
58 |
|
|
'verbose!' => $Opt{verbose}, |
59 |
|
|
) or pod2usage (2); |
60 |
|
|
pod2usage ({-exitval => 0, -verbose => 1}) if $Opt{help}; |
61 |
|
|
$Opt{file_name} = shift; |
62 |
|
|
pod2usage ({-exitval => 2, -verbose => 0}) unless $Opt{file_name}; |
63 |
|
|
pod2usage ({-exitval => 2, -verbose => 0}) unless $Opt{output_file_name}; |
64 |
|
|
$Opt{no_undef_check} = defined $Opt{no_undef_check} |
65 |
|
|
? $Opt{no_undef_check} ? 0 : 1 : 0; |
66 |
|
|
|
67 |
|
|
use Message::DOM::DOMMetaImpl; |
68 |
|
|
use Message::Util::DIS; |
69 |
|
|
my $impl = $Message::DOM::DOMImplementationRegistry |
70 |
|
|
->get_dom_implementation |
71 |
|
|
({ExpandedURI q<ManakaiDOM:Minimum> => '3.0', |
72 |
|
|
'+' . ExpandedURI q<DIS:Core> => '1.0'}) |
73 |
|
|
->get_feature (ExpandedURI q<DIS:Core> => '1.0'); |
74 |
|
|
my $parser = $impl->create_dis_parser; |
75 |
|
|
|
76 |
|
|
my $db; |
77 |
|
|
|
78 |
|
|
if (defined $Opt{input_file_name}) { |
79 |
wakaba |
1.3 |
$db = $impl->pl_load_dis_database ($Opt{input_file_name}); |
80 |
wakaba |
1.1 |
} else { ## New database |
81 |
|
|
$db = $impl->create_dis_database; |
82 |
|
|
} |
83 |
|
|
|
84 |
|
|
require Cwd; |
85 |
|
|
my $file_name = Cwd::abs_path ($Opt{file_name}); |
86 |
|
|
my $base_path = Cwd::abs_path ($Opt{db_base_path}) if length $Opt{db_base_path}; |
87 |
|
|
my $doc = dac_load_module_file ($db, $parser, $file_name, $base_path); |
88 |
|
|
$doc->dis_database ($db); |
89 |
|
|
|
90 |
|
|
my $for = $Opt{for}; |
91 |
|
|
$for = $doc->module_element->default_for_uri unless length $for; |
92 |
wakaba |
1.3 |
$db->get_for ($for)->is_referred ($doc); |
93 |
wakaba |
1.2 |
print STDERR qq<Loading definition of "$file_name" for <$for>...\n>; |
94 |
wakaba |
1.1 |
|
95 |
|
|
$db->load_module ($doc, sub ($$$$$$) { |
96 |
|
|
my ($self, $db, $uri, $ns, $ln, $for) = @_; |
97 |
wakaba |
1.2 |
print STDERR qq<Loading definition of "$ln" for <$for>...\n>; |
98 |
|
|
|
99 |
|
|
## -- Already in database |
100 |
wakaba |
1.1 |
my $doc = $db->get_source_file ($ns.$ln); |
101 |
wakaba |
1.2 |
return $doc if $doc; |
102 |
wakaba |
1.1 |
|
103 |
wakaba |
1.2 |
## -- Finds the source file |
104 |
|
|
require File::Spec; |
105 |
|
|
for my $dir ('.', @{$Opt{input_search_path}->{$ns}||[]}) { |
106 |
|
|
my $name = Cwd::abs_path |
107 |
|
|
(File::Spec->canonpath |
108 |
|
|
(File::Spec->catfile ($dir, $ln.'.dis'))); |
109 |
|
|
if (-f $name) { |
110 |
|
|
my $doc = dac_load_module_file ($db, $parser, $name, $base_path); |
111 |
|
|
$doc->dis_database ($db); |
112 |
|
|
return $doc; |
113 |
|
|
} |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
## -- Not found |
117 |
|
|
return undef; |
118 |
wakaba |
1.1 |
}, for_arg => $for); |
119 |
|
|
|
120 |
wakaba |
1.3 |
|
121 |
|
|
$db->check_undefined_resource unless $Opt{no_undef_check}; |
122 |
|
|
|
123 |
|
|
$db->pl_store ($Opt{output_file_name}); |
124 |
wakaba |
1.1 |
exit; |
125 |
|
|
|
126 |
|
|
## (db, parser, abs file path, abs base path) -> dis doc obj |
127 |
|
|
sub dac_load_module_file ($$$;$) { |
128 |
|
|
my ($db, $parser, $file_name, $base_path) = @_; |
129 |
|
|
require URI::file; |
130 |
|
|
my $base_uri = length $base_path ? URI::file->new ($base_path.'/') |
131 |
|
|
: 'http://dummy.invalid/'; |
132 |
|
|
my $file_uri = URI::file->new ($file_name)->rel ($base_uri); |
133 |
|
|
my $dis = $db->get_source_file ($file_uri); |
134 |
|
|
unless ($dis) { |
135 |
wakaba |
1.4 |
print STDERR qq<Opening file <$file_uri>...>; |
136 |
wakaba |
1.1 |
open my $file, '<', $file_name or die "$0: $file_name: $!"; |
137 |
|
|
$dis = $parser->parse ({character_stream => $file}); |
138 |
|
|
$db->set_source_file ($file_uri => $dis); |
139 |
wakaba |
1.3 |
$dis->flag (ExpandedURI q<swcfg21:fileName> => $file_uri); |
140 |
wakaba |
1.2 |
print STDERR qq<done\n>; |
141 |
wakaba |
1.1 |
} |
142 |
|
|
$dis; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
__END__ |
146 |
|
|
|
147 |
|
|
=head1 NAME |
148 |
|
|
|
149 |
|
|
... |
150 |
|
|
|
151 |
|
|
=head1 OPTIONS |
152 |
|
|
|
153 |
|
|
... |
154 |
wakaba |
1.2 |
|
155 |
|
|
=cut |