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