16 |
|
|
17 |
sub new ($) { |
sub new ($) { |
18 |
my $self = bless {fact_level => 'm', grammer_level => 'm'}, shift; |
my $self = bless {fact_level => 'm', grammer_level => 'm'}, shift; |
19 |
|
$self->{next_id} = 0; |
20 |
$self->{onerror} = sub { |
$self->{onerror} = sub { |
21 |
my %opt = @_; |
my %opt = @_; |
22 |
warn $opt{type}, "\n"; |
warn $opt{type}, "\n"; |
160 |
$RDF_URI . 'bagID' => 1, |
$RDF_URI . 'bagID' => 1, |
161 |
); |
); |
162 |
|
|
163 |
|
require Message::DOM::DOMImplementation; |
164 |
|
my $resolve = sub { |
165 |
|
return Message::DOM::DOMImplementation->create_uri_reference ($_[0]) |
166 |
|
->get_absolute_reference ($_[1]->base_uri) |
167 |
|
->uri_reference; |
168 |
|
|
169 |
|
## TODO: Check latest xml:base and IRI spec... |
170 |
|
## (non IRI/URI chars should be percent-encoded before resolve?) |
171 |
|
}; # $resolve |
172 |
|
|
173 |
|
my $generate_bnodeid = sub { |
174 |
|
return 'g'.$_[0]->{next_id}++; |
175 |
|
}; # $generate_bnodeid |
176 |
|
|
177 |
|
my $get_bnodeid = sub { |
178 |
|
return 'b'.$_[0]; |
179 |
|
}; # $get_bnodeid |
180 |
|
|
181 |
sub convert_node_element ($$) { |
sub convert_node_element ($$) { |
182 |
my ($self, $node) = @_; |
my ($self, $node) = @_; |
183 |
|
|
201 |
} |
} |
202 |
|
|
203 |
my $subject; |
my $subject; |
204 |
my $rdf_type_attr; |
my $type_attr; |
205 |
my @prop_attr; |
my @prop_attr; |
206 |
|
|
207 |
for my $attr (@{$node->attributes}) { |
for my $attr (@{$node->attributes}) { |
217 |
my $attr_xuri = $attr->manakai_expanded_uri; |
my $attr_xuri = $attr->manakai_expanded_uri; |
218 |
if ($attr_xuri eq $RDF_URI . 'ID') { |
if ($attr_xuri eq $RDF_URI . 'ID') { |
219 |
unless (defined $subject) { |
unless (defined $subject) { |
220 |
$subject = {uri => '#' . $attr->value}; ## TODO: resolve() |
$subject = {uri => $resolve->('#' . $attr->value, $attr)}; |
221 |
} else { |
} else { |
222 |
## TODO: Ignore triple as W3C RDF Validator does |
## TODO: Ignore triple as W3C RDF Validator does |
223 |
} |
} |
224 |
} elsif ($attr_xuri eq $RDF_URI . 'nodeID') { |
} elsif ($attr_xuri eq $RDF_URI . 'nodeID') { |
225 |
unless (defined $subject) { |
unless (defined $subject) { |
226 |
$subject = {bnodeid => '## TODO: bnode: ' . $attr->value}; |
$subject = {bnodeid => $get_bnodeid->($attr->value)}; |
227 |
} else { |
} else { |
228 |
## TODO: Ignore triple as W3C RDF Validator does |
## TODO: Ignore triple as W3C RDF Validator does |
229 |
} |
} |
230 |
} elsif ($attr_xuri eq $RDF_URI . 'about') { |
} elsif ($attr_xuri eq $RDF_URI . 'about') { |
231 |
unless (defined $subject) { |
unless (defined $subject) { |
232 |
$subject = {uri => $attr->value}; ## TODO: resolve |
$subject = {uri => $resolve->($attr->value, $attr)}; |
233 |
} else { |
} else { |
234 |
## TODO: Ignore triple as W3C RDF Validator does |
## TODO: Ignore triple as W3C RDF Validator does |
235 |
} |
} |
236 |
} elsif ($attr_xuri eq $RDF_URI . 'type') { |
} elsif ($attr_xuri eq $RDF_URI . 'type') { |
237 |
$rdf_type_attr = $attr; |
$type_attr = $attr; |
238 |
} elsif ({ |
} elsif ({ |
239 |
%coreSyntaxTerms, |
%coreSyntaxTerms, |
240 |
$RDF_URI . 'li' => 1, |
$RDF_URI . 'li' => 1, |
252 |
} |
} |
253 |
|
|
254 |
unless (defined $subject) { |
unless (defined $subject) { |
255 |
$subject = {bnodeid => '## TODO: new bnodeid'}; |
$subject = {bnodeid => $generate_bnodeid->($self)}; |
256 |
} |
} |
257 |
|
|
258 |
if ($xuri ne $RDF_URI . 'Description') { |
if ($xuri ne $RDF_URI . 'Description') { |
262 |
node => $node); |
node => $node); |
263 |
} |
} |
264 |
|
|
265 |
if ($rdf_type_attr) { |
if ($type_attr) { |
266 |
$self->{ontriple}->(subject => $subject, |
$self->{ontriple}->(subject => $subject, |
267 |
predicate => {uri => $RDF_URI . 'type'}, |
predicate => {uri => $RDF_URI . 'type'}, |
268 |
object => {uri => $rdf_type_attr->value}, ## TODO: resolve |
object => {uri => $resolve->($type_attr->value, |
269 |
node => $rdf_type_attr); |
$type_attr)}, |
270 |
|
node => $type_attr); |
271 |
} |
} |
272 |
|
|
273 |
for my $attr (@prop_attr) { |
for my $attr (@prop_attr) { |
299 |
return $subject; |
return $subject; |
300 |
} # convert_node_element |
} # convert_node_element |
301 |
|
|
302 |
|
my $get_id_resource = sub { |
303 |
|
return $_[0] ? {uri => $resolve->('#' . $_[0]->value, $_[0])} : undef; |
304 |
|
}; # $get_id_resource |
305 |
|
|
306 |
sub convert_property_element ($$%) { |
sub convert_property_element ($$%) { |
307 |
my ($self, $node, %opt) = @_; |
my ($self, $node, %opt) = @_; |
308 |
|
|
380 |
## TODO: RDF Validator? |
## TODO: RDF Validator? |
381 |
} |
} |
382 |
|
|
383 |
my $object = {bnodeid => '## TODO: generate bnodeid'}; |
my $object = {bnodeid => $generate_bnodeid->($self)}; |
384 |
$self->{ontriple}->(subject => $opt{subject}, |
$self->{ontriple}->(subject => $opt{subject}, |
385 |
predicate => {uri => $xuri}, |
predicate => {uri => $xuri}, |
386 |
object => $object, |
object => $object, |
387 |
node => $node, |
node => $node, |
388 |
id => $id_attr ? {uri => '#' . $id_attr->value} : undef); ## TODO: resolve |
id => $get_id_resource->($id_attr)); |
389 |
|
|
390 |
## As if nodeElement |
## As if nodeElement |
391 |
|
|
422 |
for my $cn (@{$node->child_nodes}) { |
for my $cn (@{$node->child_nodes}) { |
423 |
if ($cn->node_type == $cn->ELEMENT_NODE) { |
if ($cn->node_type == $cn->ELEMENT_NODE) { |
424 |
push @resource, [$self->convert_node_element ($cn), |
push @resource, [$self->convert_node_element ($cn), |
425 |
{bnodeid => '## TODO: bnodeid generated'}, |
{bnodeid => $generate_bnodeid->($self)}, |
426 |
$cn]; |
$cn]; |
427 |
} elsif ($cn->node_type == $cn->TEXT_NODE or |
} elsif ($cn->node_type == $cn->TEXT_NODE or |
428 |
$cn->node_type == $cn->CDATA_SECTION_NODE) { |
$cn->node_type == $cn->CDATA_SECTION_NODE) { |
444 |
predicate => {uri => $xuri}, |
predicate => {uri => $xuri}, |
445 |
object => {uri => $RDF_URI . 'nil'}, |
object => {uri => $RDF_URI . 'nil'}, |
446 |
node => $node, |
node => $node, |
447 |
id => $id_attr ? {uri => '#' . $id_attr->value} : undef); ## TODO: resolve |
id => $get_id_resource->($id_attr)); |
448 |
} |
} |
449 |
|
|
450 |
while (@resource) { |
while (@resource) { |
486 |
object => {nodes => $value, |
object => {nodes => $value, |
487 |
datatype => $RDF_URI . 'XMLLiteral'}, |
datatype => $RDF_URI . 'XMLLiteral'}, |
488 |
node => $node, |
node => $node, |
489 |
id => $id_attr ? {uri => '#' . $id_attr->value} : undef); ## TODO: resolve |
id => $get_id_resource->($id_attr)); |
490 |
} else { |
} else { |
491 |
my $mode = 'unknown'; |
my $mode = 'unknown'; |
492 |
|
|
561 |
predicate => {uri => $xuri}, |
predicate => {uri => $xuri}, |
562 |
object => $object, |
object => $object, |
563 |
node => $node, |
node => $node, |
564 |
id => $id_attr ? {uri => '#' . $id_attr->value} : undef); ## TODO: resolve |
id => $get_id_resource->($id_attr)); |
565 |
} elsif ($mode eq 'literal' or $mode eq 'literal-or-resource') { |
} elsif ($mode eq 'literal' or $mode eq 'literal-or-resource') { |
566 |
# |literalPropertyElt| |
# |literalPropertyElt| |
567 |
|
|
581 |
object => {value => $text, |
object => {value => $text, |
582 |
datatype => $dt_attr->value}, |
datatype => $dt_attr->value}, |
583 |
node => $node, |
node => $node, |
584 |
id => $id_attr ? {uri => '#' . $id_attr->value} : undef); ## TODO: resolve |
id => $get_id_resource->($id_attr)); |
585 |
} else { |
} else { |
586 |
$self->{ontriple}->(subject => $opt{subject}, |
$self->{ontriple}->(subject => $opt{subject}, |
587 |
predicate => {uri => $xuri}, |
predicate => {uri => $xuri}, |
589 |
## TODO: language |
## TODO: language |
590 |
}, |
}, |
591 |
node => $node, |
node => $node, |
592 |
id => $id_attr ? {uri => '#' . $id_attr->value} : undef); ## TODO: resolve |
id => $get_id_resource->($id_attr)); |
593 |
} |
} |
594 |
} else { |
} else { |
595 |
## |emptyPropertyElt| |
## |emptyPropertyElt| |
609 |
## TODO: language |
## TODO: language |
610 |
}, |
}, |
611 |
node => $node, |
node => $node, |
612 |
id => $id_attr ? {uri => '#' . $id_attr->value} : undef); ## TODO: resolve |
id => $get_id_resource->($id_attr)); |
613 |
} else { |
} else { |
614 |
my $object; |
my $object; |
615 |
if ($resource_attr) { |
if ($resource_attr) { |
616 |
$object = {uri => $resource_attr->value}; ## TODO: resolve |
$object = {uri => $resolve->($resource_attr->value, $resource_attr)}; |
617 |
} elsif ($nodeid_attr) { |
} elsif ($nodeid_attr) { |
618 |
$object = {bnodeid => $nodeid_attr->value}; |
$object = {bnodeid => $get_bnodeid->($nodeid_attr->value)}; |
619 |
} else { |
} else { |
620 |
$object = {bnodeid => '## TODO: generated bnodeid'}; |
$object = {bnodeid => $generate_bnodeid->($self)}; |
621 |
} |
} |
622 |
|
|
623 |
for my $attr (@prop_attr) { |
for my $attr (@prop_attr) { |
625 |
if ($attr_xuri eq $RDF_URI . 'type') { |
if ($attr_xuri eq $RDF_URI . 'type') { |
626 |
$self->{ontriple}->(subject => $object, |
$self->{ontriple}->(subject => $object, |
627 |
predicate => {uri => $attr_xuri}, |
predicate => {uri => $attr_xuri}, |
628 |
object => $attr->value, ## TODO: resolve |
object => $resolve->($attr->value, $attr), |
629 |
node => $attr); |
node => $attr); |
630 |
} else { |
} else { |
631 |
## TODO: SHOULD be in NFC |
## TODO: SHOULD be in NFC |
642 |
predicate => {uri => $xuri}, |
predicate => {uri => $xuri}, |
643 |
object => $object, |
object => $object, |
644 |
node => $node, |
node => $node, |
645 |
id => $id_attr ? {uri => '#' . $id_attr->value} : undef); ## TODO: resolve |
id => $get_id_resource->($id_attr)); |
646 |
} |
} |
647 |
} |
} |
648 |
} |
} |