1 |
#!/usr/bin/perl -w |
2 |
use strict; |
3 |
use Message::Util::QName::Filter { |
4 |
d => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#dis-->, |
5 |
dis2pm => q<http://suika.fam.cx/~wakaba/archive/2004/11/8/dis2pm#>, |
6 |
DOMCore => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/dom-core#>, |
7 |
DOMMain => q<http://suika.fam.cx/~wakaba/archive/2004/dom/main#>, |
8 |
infoset => q<http://www.w3.org/2001/04/infoset#>, |
9 |
lang => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#>, |
10 |
Perl => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#Perl-->, |
11 |
license => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/license#>, |
12 |
ManakaiDOM => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/manakai-dom#>, |
13 |
MDOM_EXCEPTION => q<http://suika.fam.cx/~wakaba/archive/2004/8/4/manakai-dom-exception#>, |
14 |
owl => q<http://www.w3.org/2002/07/owl#>, |
15 |
rdf => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#>, |
16 |
rdfs => q<http://www.w3.org/2000/01/rdf-schema#>, |
17 |
xml => q<http://www.w3.org/XML/1998/namespace>, |
18 |
xmlns => q<http://www.w3.org/2000/xmlns/>, |
19 |
xsd => q<http://www.w3.org/2001/XMLSchema#>, |
20 |
}; |
21 |
|
22 |
use Getopt::Long; |
23 |
use Pod::Usage; |
24 |
my %Opt; |
25 |
GetOptions ( |
26 |
'for=s' => \$Opt{For}, |
27 |
'help' => \$Opt{help}, |
28 |
'undef-check!' => \$Opt{no_undef_check}, |
29 |
'output-anon-resource!' => \$Opt{output_anon_resource}, |
30 |
'output-as-n3' => \$Opt{output_as_n3}, |
31 |
'output-as-xml' => \$Opt{output_as_xml}, |
32 |
'output-for!' => \$Opt{output_for}, |
33 |
'output-local-resource!' => \$Opt{output_local_resource}, |
34 |
'output-module!' => \$Opt{output_module}, |
35 |
'output-only-in-module=s' => \$Opt{output_resource_pattern}, |
36 |
'output-prop-perl!' => \$Opt{output_prop_perl}, |
37 |
'output-resource!' => \$Opt{output_resource}, |
38 |
'output-resource-uri-pattern=s' => \$Opt{output_resource_uri_pattern}, |
39 |
) or pod2usage (2); |
40 |
if ($Opt{help}) { |
41 |
pod2usage (0); |
42 |
exit; |
43 |
} |
44 |
if ($Opt{output_as_n3} and $Opt{output_as_xml}) { |
45 |
pod2usage (2); |
46 |
exit; |
47 |
} |
48 |
$Opt{output_as_xml} = 1 unless $Opt{output_as_n3}; |
49 |
$Opt{output_anon_resource} = 1 unless defined $Opt{output_anon_resource}; |
50 |
$Opt{output_local_resource} = 1 unless defined $Opt{output_local_resource}; |
51 |
$Opt{no_undef_check} = $Opt{no_undef_check} ? 0 : 1; |
52 |
|
53 |
BEGIN { |
54 |
require 'manakai/genlib.pl'; |
55 |
require 'manakai/dis.pl'; |
56 |
} |
57 |
sub n3_literal ($) { |
58 |
my $s = shift; |
59 |
impl_err ("Literal value not defined") unless defined $s; |
60 |
qq<"$s">; |
61 |
} |
62 |
our $State; |
63 |
our $result = new manakai::n3; |
64 |
|
65 |
$Opt{file_name} = shift; |
66 |
$Opt{output_resource_pattern} ||= qr/.+/; |
67 |
$Opt{output_resource_uri_pattern} ||= qr/.+/; |
68 |
|
69 |
$State->{DefaultFor} = $Opt{For}; |
70 |
|
71 |
my $source = dis_load_module_file (module_file_name => $Opt{file_name}, |
72 |
For => $Opt{For}, |
73 |
use_default_for => 1); |
74 |
$State->{for_def_required}->{$State->{DefaultFor}} ||= 1; |
75 |
|
76 |
dis_check_undef_type_and_for () |
77 |
unless $Opt{no_undef_check}; |
78 |
|
79 |
if (dis_uri_for_match (ExpandedURI q<ManakaiDOM:Perl>, $State->{DefaultFor})) { |
80 |
dis_perl_init ($source, For => $State->{DefaultFor}); |
81 |
} |
82 |
|
83 |
my $primary = $result->get_new_anon_id (Name => 'boot'); |
84 |
$result->add_triple ($primary =>ExpandedURI q<d:module>=> $State->{module}) |
85 |
if $Opt{output_module}; |
86 |
$result->add_triple ($primary =>ExpandedURI q<d:DefaultFor> => $State->{DefaultFor}) |
87 |
if $Opt{output_for}; |
88 |
|
89 |
if ($Opt{output_module}) { |
90 |
for (keys %{$State->{Module}}) { |
91 |
my $mod = $State->{Module}->{$_}; |
92 |
if ($_ eq $mod->{URI}) { |
93 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<rdf:type>=> |
94 |
ExpandedURI q<d:Module>); |
95 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<d:Name>=> |
96 |
n3_literal $mod->{Name}); |
97 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<d:NameURI>=> |
98 |
$mod->{NameURI}); |
99 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<d:ModuleGroup>=> |
100 |
$mod->{ModuleGroup}); |
101 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<d:FileName>=> |
102 |
n3_literal $mod->{FileName}) |
103 |
if defined $mod->{FileName}; |
104 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<d:Namespace>=> |
105 |
$mod->{Namespace}); |
106 |
for (@{$mod->{require_module}||[]}) { |
107 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<d:Require>=> $_); |
108 |
} |
109 |
for (keys %{$mod->{For}}) { |
110 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<d:For>=> $_); |
111 |
} |
112 |
for (@{$mod->{ISA}}) { |
113 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_); |
114 |
} |
115 |
if ($Opt{output_prop_perl}) { |
116 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<dis2pm:packageName>=> |
117 |
n3_literal $mod->{ExpandedURI q<dis2pm:packageName>}) |
118 |
if defined $mod->{ExpandedURI q<dis2pm:packageName>}; |
119 |
if ($Opt{output_resource}) { |
120 |
for (values %{$mod->{ExpandedURI q<dis2pm:package>}}) { |
121 |
my $uri = defined $_->{URI} |
122 |
? $_->{URI} |
123 |
: ($_->{ExpandedURI q<d:anonID>} |
124 |
||= $result->get_new_anon_id (Name => $_->{Name})); |
125 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<dis2pm:package>=> |
126 |
$uri); |
127 |
} |
128 |
} |
129 |
} |
130 |
} else { |
131 |
$result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI}); |
132 |
} |
133 |
}} |
134 |
|
135 |
if ($Opt{output_for}) { |
136 |
for (keys %{$State->{For}}) { |
137 |
my $mod = $State->{For}->{$_}; |
138 |
if ($_ eq $mod->{URI}) { |
139 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<rdf:type>=> |
140 |
ExpandedURI q<d:For>); |
141 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<d:NameURI>=> $mod->{URI}); |
142 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<d:FullName>=> |
143 |
n3_literal $mod->{FullName}) |
144 |
if defined $mod->{FullName}; |
145 |
for (@{$mod->{ISA}}) { |
146 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<rdfs:subClassOf>=> $_); |
147 |
} |
148 |
for (@{$mod->{Implement}}) { |
149 |
$result->add_triple ($mod->{URI} =>ExpandedURI q<d:Implement>=> $_); |
150 |
} |
151 |
} else { |
152 |
$result->add_triple ($_ =>ExpandedURI q<owl:sameAs>=> $mod->{URI}); |
153 |
} |
154 |
}} |
155 |
|
156 |
if ($Opt{output_resource}) { |
157 |
sub class_to_rdf ($;%); |
158 |
sub class_to_rdf ($;%) { |
159 |
my ($mod, %opt) = @_; |
160 |
return unless defined $mod->{Name}; |
161 |
return unless $mod->{parentModule} =~ /$Opt{output_resource_pattern}/; |
162 |
if ((defined $mod->{URI} and $opt{key} eq $mod->{URI}) or |
163 |
not defined $mod->{URI}) { |
164 |
return if defined $mod->{URI} and |
165 |
$mod->{URI} !~ /$Opt{output_resource_uri_pattern}/; |
166 |
return if not defined $mod->{URI} and not $Opt{output_anon_resource}; |
167 |
my $uri = defined $mod->{URI} |
168 |
? $mod->{URI} |
169 |
: ($mod->{ExpandedURI q<d:anonID>} |
170 |
||= $result->get_new_anon_id (Name => $mod->{Name})); |
171 |
$result->add_triple ($uri =>ExpandedURI q<d:Name>=> |
172 |
n3_literal $mod->{Name}) if length $mod->{Name}; |
173 |
$result->add_triple ($uri =>ExpandedURI q<d:NameURI>=> $mod->{NameURI}) |
174 |
if defined $mod->{NameURI}; |
175 |
$result->add_triple ($uri =>ExpandedURI q<d:parentResource>=> |
176 |
$opt{parent_class_uri}) |
177 |
if defined $opt{parent_class_uri}; |
178 |
$result->add_triple ($uri =>ExpandedURI q<d:parentModule>=> |
179 |
$mod->{parentModule}); |
180 |
for (keys %{$mod->{Type}}) { |
181 |
$result->add_triple ($uri =>ExpandedURI q<rdf:type>=> $_); |
182 |
} |
183 |
for (@{$mod->{ISA}}) { |
184 |
$result->add_triple ($uri =>ExpandedURI q<rdfs:subClassOf>=> $_); |
185 |
} |
186 |
for (@{$mod->{Implement}}) { |
187 |
$result->add_triple ($uri =>ExpandedURI q<d:Implement>=> $_); |
188 |
} |
189 |
for (keys %{$mod->{For}}) { |
190 |
$result->add_triple ($uri =>ExpandedURI q<d:For>=> $_); |
191 |
} |
192 |
for (@{$mod->{hasResource}||[]}) { |
193 |
my $ruri = defined $_->{URI} |
194 |
? $_->{URI} |
195 |
: ($_->{ExpandedURI q<d:anonID>} |
196 |
||= $result->get_new_anon_id (Name => $_->{Name})); |
197 |
$result->add_triple ($uri =>ExpandedURI q<d:hasResource>=> $ruri); |
198 |
} |
199 |
if ($Opt{output_prop_perl}) { |
200 |
for my $prop ([ExpandedURI q<dis2pm:packageName>], |
201 |
[ExpandedURI q<dis2pm:ifPackagePrefix>], |
202 |
[ExpandedURI q<dis2pm:methodName>], |
203 |
[ExpandedURI q<ManakaiDOM:isRedefining>, |
204 |
ExpandedURI q<DOMMain:boolean>], |
205 |
[ExpandedURI q<ManakaiDOM:isForInternal>, |
206 |
ExpandedURI q<DOMMain:boolean>]) { |
207 |
$result->add_triple ($uri =>$prop->[0]=> |
208 |
n3_literal $mod->{$prop->[0]}) |
209 |
if defined $mod->{$prop->[0]}; |
210 |
} |
211 |
for (values %{$mod->{ExpandedURI q<dis2pm:method>}||{}}) { |
212 |
my $ruri = defined $_->{URI} |
213 |
? $_->{URI} |
214 |
: ($_->{ExpandedURI q<d:anonID>} |
215 |
||= $result->get_new_anon_id (Name => $_->{Name})); |
216 |
$result->add_triple ($uri =>ExpandedURI q<dis2pm:method>=> $ruri); |
217 |
} |
218 |
} |
219 |
if ($Opt{output_local_resource}) { |
220 |
for (keys %{$mod->{Resource}}) { |
221 |
class_to_rdf ($mod->{Resource}->{$_}, parent_class => $mod, |
222 |
parent_class_uri => $uri, |
223 |
key => $_); |
224 |
} |
225 |
} |
226 |
} else { ## Alias URI |
227 |
return if $opt{key} !~ /$Opt{output_resource_uri_pattern}/; |
228 |
$result->add_triple ($opt{key} =>ExpandedURI q<owl:sameAs>=> $mod->{URI}); |
229 |
} |
230 |
} |
231 |
for (sort keys %{$State->{Type}}) { |
232 |
class_to_rdf ($State->{Type}->{$_}, key => $_); |
233 |
} |
234 |
} |
235 |
|
236 |
if ($Opt{output_as_xml}) { |
237 |
print $result->stringify_as_xml; |
238 |
} else { |
239 |
print $result->stringify; |
240 |
} |
241 |
|
242 |
package manakai::n3; |
243 |
sub new ($) { |
244 |
bless {triple => [], anon => 0}, shift; |
245 |
} |
246 |
|
247 |
sub get_new_anon_id ($;%) { |
248 |
my ($self, %opt) = @_; |
249 |
my $s = $opt{Name} ? $opt{Name} : ''; |
250 |
return sprintf '_:r%d%s', $self->{anon}++, $s; |
251 |
} |
252 |
|
253 |
sub add_triple ($$$$) { |
254 |
my ($self, $s =>$p=> $o) = @_; |
255 |
main::impl_err ("Subject undefined") unless defined $s; |
256 |
main::impl_err ("Property undefined") unless defined $p; |
257 |
main::impl_err ("Object undefined") unless defined $o; |
258 |
push @{$self->{triple}}, [$s =>$p=> $o]; |
259 |
} |
260 |
|
261 |
sub stringify ($) { |
262 |
my ($self) = @_; |
263 |
return join "\n", (map {"$_."} map { |
264 |
sprintf '%s %s %s', map { |
265 |
$_ =~ /^[_"]/ ? $_ : "<$_>" |
266 |
} @{$_}[0, 1, 2]; |
267 |
} @{$self->{triple}}), ''; |
268 |
} |
269 |
|
270 |
sub stringify_as_xml ($) { |
271 |
my ($self) = @_; |
272 |
use RDF::Notation3::XML; |
273 |
my $notation3 = RDF::Notation3::XML->new; |
274 |
$notation3->parse_string ($self->stringify); |
275 |
my $xml = $notation3->get_string; |
276 |
$xml =~ s/\brdf:nodeID="_:/rdf:nodeID="/g; |
277 |
# $xml =~ s/^<\?xml version="1.0" encoding="utf-8"\?>\s*//; |
278 |
$xml; |
279 |
} |
280 |
|
281 |
1; |
282 |
|
283 |
__END__ |
284 |
|
285 |
=head1 NAME |
286 |
|
287 |
dis2rdf.pl - dis to RDF converter |
288 |
|
289 |
=head1 SYNOPSIS |
290 |
|
291 |
$ perl dis2rdf.pl input.dis [options...] > output.rdf |
292 |
|
293 |
=head1 DESCRIPTION |
294 |
|
295 |
This script generates a RDF graph from a "dis" file. |
296 |
|
297 |
=over 4 |
298 |
|
299 |
=item I<input.dis> |
300 |
|
301 |
The "dis" file from which a RDF graph is generated. |
302 |
|
303 |
=item I<output.rdf> |
304 |
|
305 |
An RDF/XML entity is outputed. |
306 |
|
307 |
=item C<--output-module> |
308 |
|
309 |
Show the relationship of modules. |
310 |
|
311 |
=item C<--output-type> |
312 |
|
313 |
Show the relationship of types. |
314 |
|
315 |
=item C<--output-for> |
316 |
|
317 |
Show the relationship of "for"s. |
318 |
|
319 |
=cut |
320 |
|
321 |
=head1 LICENSE |
322 |
|
323 |
Copyright 2004 Wakaba <w@suika.fam.cx>. All rights reserved. |
324 |
|
325 |
This program is free software; you can redistribute it and/or |
326 |
modify it under the same terms as Perl itself. |
327 |
|
328 |
Note that the copyright holder(s) of this script does not claim |
329 |
any rights for materials outputed by this script, although |
330 |
some of its part comes from this script. The copyright |
331 |
holder(s) of source document should define their license terms. |
332 |
|
333 |
=cut |