/usr/share/perl5/TM/Serializable/JTM.pm is in libtm-perl 1.56-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | package TM::Serializable::JTM;
# $Id: JTM.pm,v 1.1 2010/04/09 09:57:08 az Exp $
use strict;
use Class::Trait 'base';
use Class::Trait 'TM::Serializable';
use JSON::Syck;
use YAML::Syck;
use TM::Literal;
use vars qw($VERSION);
$VERSION = qw(('$Revision: 1.2 $'))[1];
=pod
=head1 NAME
TM::Serializable::JTM - Topic Maps, trait for reading/writing JSON Topic Map instances.
=head1 SYNOPSIS
# NOTE: this is not an end-user package,
# see TM::Materialized::JTM for common application patterns
# reading JSON/YAML:
my $tm=TM->new(...);
Class::Trait->apply($tm,"TM::Serializable::JTM");
$tm->deserialize($jsondata);
# writing JSON/YAML:
# ...a map $tm is instantiated somehow
Class::Trait->apply($tm,"TM::Serializable::JTM");
my $yamltext=$tm->serialize(format=>"yaml");
=head1 DESCRIPTION
This trait provides functionality for reading and writing Topic Maps in
JTM (JSON Topic Map) format, as defined here: L<http://www.cerny-online.com/jtm/1.0/>.
Limitations:
=over
=item * Variants are not supported by TM.
=item * Reification of basenames, occurrences and roles is not supported by TM.
=item * Multiple scopes are not supported by TM.
=back
=head1 INTERFACE
=head2 Methods
=over
=item B<deserialize>
This method take a string and parses JTM content from it. It will
raise an exception on any parsing error. On success, it will return the map object.
The method understands one key/value parameter pair:
=over
=item * B<format> (choices: C<"json">, C<"yaml">)
This option controls whether the JTM is expected to be in JSON format
or in YAML (which is a superset of JSON).
If no format parameter is given but the L<TM::Materialized::JTM> trait is used, then the format
is inherited from there; otherwise the default is C<"json">.
=back
=cut
sub deserialize
{
my ($self,$content,%opts)=@_;
my $base=$self->baseuri;
$opts{format}||=$self->{format}||"json";
my $js;
$js=($opts{format} eq "json"? JSON::Syck::Load($content): YAML::Syck::Load($content));
die "not a JTM topicmap object!\n"
if (!_asserttype($js,"HASH") || lc($js->{item_type}) ne "topicmap"
|| $js->{version} ne "1.0");
die "variants are not supported.\n" if ($js->{variants});
# topic nodes in jtm versus tids in tm.
my %jtm2tid;
# walk through topics, instantiate them
# leave occurrences and basenames for later, as these have scopes and types
# and we want to keep the tids consistent where possible.
for my $t (@{$js->{topics}})
{
# sanitize the data structure
for my $i (qw(item_identifiers subject_identifiers subject_locators names occurrences))
{
$t->{$i}||=[];
die "Malformed data structure (bad $i)\n"
if (!_asserttype($t->{$i},"ARRAY"));
}
# multiple item identifiers: not supported in TM.
die("TM does not support multiple topic identifiers (IDs: "
.join(" ",@{$t->{item_identifiers}}).").\n") if (@{$t->{item_identifiers}}>1);
# multiple subject locators make no sense
die("TM does not support multiple subject locators ("
.join(" ",@{$t->{subject_locators}}).").\n") if (@{$t->{subject_locators}}>1);
# do we have an item id? then suggest that as tid to TM
# ...but check first if this is already present as an infrastructure topic. bah!
my $newtid=$t->{item_identifiers}->[0];
$newtid=$base.$newtid if (!$self->toplet($newtid));
my $sloc;
if ($t->{subject_locators}->[0])
{
$sloc=$t->{subject_locators}->[0];
# base must be added to plain strings (=local topic), but not on uris.
$sloc=$base.$sloc if ($sloc!~/^[a-zA-Z][a-zA-Z0-9+\.-]*:/);
}
# internalize may well return a different tid!
my $actual=$self->internalize($newtid=>$sloc); # $sloc is actual string
$jtm2tid{$t}=$actual;
# add all subject identifiers
for my $sin (@{$t->{subject_identifiers}})
{
my $nochange=$self->internalize($actual=>\$sin); # must be ref
die("confusion: adding subject indicator ($$sin) to $actual created new topic $nochange?!?\n")
if ($nochange ne $actual);
}
}
# now all explicitely named topics are known: tackle basenames and occurrences
for my $t (@{$js->{topics}})
{
for my $what ('names','occurrences')
{
for my $item (@{$t->{$what}})
{
die "variants are not supported.\n" if ($item->{variants});
die "reification of $what is not supported.\n" if ($item->{reifier});
$item->{scope}||=[];
die "multiple scopes are not supported.\n" if (@{$item->{scope}}>1);
# figure out scope
my $scope="us";
my $sr=$item->{scope}->[0];
$scope=$self->_asserttref($sr) if ($sr);
die "couldn't find/create scope topic from topic ref $sr\n" if (!$scope);
# and type
my $type=$what; $type=~s/.$//; my $short=$type;
my $tr=$item->{type};
$type=$self->_asserttref($tr) if ($tr);
die "couldn't find/create type topic from topic ref $tr\n" if (!$type);
my $vo=TM::Literal->new($item->{value},$item->{datatype}||TM::Literal->STRING);
my (@success)=$self->assert(Assertion->new(kind=>($what eq 'names'? TM->NAME: TM->OCC),
type=>$type,
scope=>$scope,
roles=>['thing','value'],
players=>[ $jtm2tid{$t}, $vo]));
die "couldn't create $short assertion for $jtm2tid{$t}\n"
if (@success!=1);
}
}
}
# walk through assocs, and instantiate them too.
for my $a (@{$js->{associations}})
{
die "multiple scopes are not supported.\n" if (@{$a->{scope}}>1);
# figure out scope
my $scope="us";
my $sr=$a->{scope}->[0];
$scope=$self->_asserttref($sr) if ($sr);
die "couldn't find/create scope topic from topic ref $sr\n" if (!$scope);
# and type
die "can't have association without a type!\n" if (!$a->{type});
my $type=$self->_asserttref($a->{type});
die "couldn't find/create type topic from topic ref $a->{type}\n" if (!$type);
my (@roles,@players);
die "can't have association without roles!\n" if (!_asserttype($a->{roles},"ARRAY"));
for my $r (@{$a->{roles}})
{
die "role reification is not supported.\n" if ($r->{reifier});
my $roletype=$self->_asserttref($r->{type});
die "couldn't find/create role topic from topic ref $r->{type}\n" if (!$roletype);
my $player=$self->_asserttref($r->{player});
die "couldn't find/create player topic from topic ref $r->{player}\n" if (!$player);
push @roles,$roletype;
push @players,$player;
}
my (@success)=$self->assert(Assertion->new(kind=>TM->ASSOC,
type=>$type,
scope=>$scope,
roles=>\@roles,
players=>\@players));
die "couldn't create association of type $type!\n" if (@success!=1);
# assoc reifier present? then add that info to the relevant topic
if ($a->{reifier})
{
my $aid=$success[0]->[TM->LID];
my $rtopic=$self->_asserttref($a->{reifier});
die "couldn't find/create reifier topic from topic ref $a->{reifier}\n" if (!$rtopic);
# and now add the subject locator
my $nochange=$self->internalize($rtopic=>$aid);
die "added subject locator $aid to topic $rtopic, which created new topic $nochange?!?\n"
if ($nochange ne $rtopic);
}
}
return $self;
}
sub _asserttype
{
my ($objref,$expected)=@_;
return ($objref && ref($objref) eq $expected);
}
# find topic from jtm topic ref
# this creates a new topic if required - and reuses existing base-less topics
# wherever possible. this could cause a mess (can't have different topics with the
# same name as infrastructure topics) but that's unavoidable - internalize doesn't
# help with finding baseless stuff.
sub _asserttref
{
my ($self,$tr)=@_;
return undef if ($tr !~ /^(ii|si|sl):(.+)$/);
my ($type,$id)=($1,$2);
my $res=$id;
# only find/make stuff if the baseless version doesn't exist.
$res=$self->internalize($type eq 'ii'?
(($self->baseuri.$id)=>undef):
($type eq 'sl'?(undef=>$id):(undef=>\ $id))) if (!$self->toplet($res));
return $res;
}
=pod
=item B<serialize>
This method serializes the map object in JTM notation and returns
the result as a string.
The method understands one key/value parameter pair:
=over
=item * B<format> (choices: C<"json">, C<"yaml">)
This option controls whether the JTM result should be created in the JSON format
or in YAML (which is a superset of JSON).
If no format parameter is given but the L<TM::Materialized::JTM> trait is used, then the format
is inherited from there; otherwise the default is C<"json">.
=back
=cut
sub serialize
{
my ($self, %opts) = @_;
$opts{format}||=$self->{format}||"json";
my $baseuri = $self->baseuri;
# force item-identifier on topic ids (both infrastructure as well as explicit ones)
my $rebase=sub {
my ($x)=@_;
$x =~ s/^$baseuri//;
return "ii:".$x;
};
my (%topics,%js);
$js{version}="1.0";
$js{item_type}="topicmap";
$js{topics}=[];
$js{associations}=[];
# attach bn,oc,in to the relevant topic; prime normal assocs directly
for my $m ($self->asserts (\ '+all'))
{
my $kind = $m->[TM->KIND];
my $type = &$rebase($m->[TM->TYPE]);
my $scope = &$rebase($m->[TM->SCOPE]);
my $lid = $m->[TM->LID];
if ($kind == TM->ASSOC)
{
my %thisa=(type=>$type,scope=>[$scope],roles=>[]);
my ($reifier)=$self->is_reified($m);
$thisa{reifier}=&$rebase($reifier) if $reifier;
# get_role_s returns a role list that is NOT necessarily duplicate-free,
# which stuffs up get_x_players, so we do it by hand. *sigh*.
for my $i (0..$#{$m->[TM->ROLES]})
{
my $role=$m->[TM->ROLES]->[$i];
my $player=$m->[TM->PLAYERS]->[$i];
my $rolename = &$rebase($role);
push @{$thisa{roles}},{player=>&$rebase($player),
type=>$rolename};
}
push @{$js{associations}},\%thisa;
}
elsif ($kind == TM->NAME)
{
my $thing = &$rebase(($self->get_x_players($m,"thing"))[0]);
my $reifier=$self->is_reified ($m);
$reifier=&$rebase($reifier) if $reifier;
for my $p ($self->get_x_players($m,"value"))
{
my %x=(value=>$p->[0], scope=>[$scope], type=>$type);
$x{reifier}=$reifier if $reifier;
push @{$topics{$thing}->{names}},\%x;
}
}
elsif ($kind == TM->OCC)
{
my $thing = &$rebase(($self->get_x_players($m,"thing"))[0]);
my $reifier=$self->is_reified ($m);
$reifier=&$rebase($reifier) if $reifier;
for my $p ($self->get_x_players($m,"value"))
{
my %x=(value=>$p->[0], datatype=>$p->[1], scope=>[$scope], type=>$type);
$x{reifier}=$reifier if $reifier;
push @{$topics{$thing}->{occurrences}},\%x;
}
}
}
# finally add in reification info
foreach my $tt ($self->toplets (\ '+all'))
{
my $t = $tt->[TM->LID];
my $base=$self->baseuri;
my $tn=$t;
$tn=~s/^$base//;
my $unbased=$tn;
$tn='ii:'.$tn;
$topics{$tn}->{subject_identifiers} = $tt->[TM->INDICATORS]
if (@{$tt->[TM->INDICATORS]} > 0);
# only reified topics and external uris are listed here,
# assoc reification is listed with the assoc.
# don't de-base external uri's! damn base-less infrastructure topics make this messy.
my $other=$tt->[TM->ADDRESS];
$other=~s/^$base// if ($other && $self->toplet($other));
$topics{$tn}->{subject_locators}=[$other]
if ($tt->[TM->ADDRESS] && !$self->retrieve($tt->[TM->ADDRESS]));
$topics{$tn}->{item_identifiers}=[$unbased];
push @{$js{topics}},$topics{$tn};
}
return ($opts{format} eq "json"?JSON::Syck::Dump(\%js) : YAML::Syck::Dump(\%js));
}
=pod
=back
=head1 SEE ALSO
L<TM>, L<TM::Serializable>
=head1 AUTHOR INFORMATION
Copyright 2010, Alexander Zangerl, All rights reserved.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl
itself. http://www.perl.com/perl/misc/Artistic.html
=cut
1;
|