File: AsTMa.pm

package info (click to toggle)
libxtm-perl 0.37-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,536 kB
  • ctags: 410
  • sloc: perl: 23,045; makefile: 37
file content (258 lines) | stat: -rw-r--r-- 5,685 bytes parent folder | download | duplicates (2)
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
package XTM::AsTMa;

use strict;
use vars qw($VERSION @EXPORT_OK);

require Exporter;
require AutoLoader;

use base qw (XTM::IO);

$VERSION = '0.07';

use Carp;
use XTM::Log ('elog');
use XTM::Memory;

=pod

=head1 NAME

XTM::AsTMa - Topic Map Parsing of AsTMa instances.

=head1 SYNOPSIS

  # reading a topic map description from a file/url
  $atm = new XTM::AsTMa (file => 'mymap.atm');
  $tm  = $atm->sync_in();

=head1 DESCRIPTION

This package provides parsing functionality for AsTMA instances as described in the
package documentation (doc directory) or at

=begin html

<BLOCKQUOTE>
<A HREF="http://topicmaps.bond.edu.au/astma/">http://topicmaps.bond.edu.au/astma/</A>
</BLOCKQUOTE>

=end html

=begin man

   http://topicmaps.bond.edu.au/astma/

=end man

Currently, only AsTMa= is supported, with the following constraints/additions:

=over

=item no macro support

This feature was experimental and is now deprecated.

=item following directives are supported:

=over

=item %cancel

Cancels the parse process on this very line and ignores the rest of the AsTMa instance. Useful for
debugging faulty maps. There is an appropriate line written to STDERR.

=item %log [ message ]

Writes a line to STDERR reporting the line number and an optional message. Useful for debugging.

=item %name [ name ]

Adds a name attribute to the topic map.

=item %encoding [ encoding ]

Specifies which encoding to use to interpret the B<following> text. This implies that this
directive may appear several times to change the encoding. Whether this is a good idea
in terms of information management, is a different question.

Note: It is still not allowed to use several I<name> : I<encoding> clauses.

Note: If no encoding is provided, utf8 is assumed.

=item %auto_complete [ on/off ]

Turns on/off auto completion.

Note: topics which have been mentioned in a 'is-reified-by' clause will be B<always>
generated.

=back

A directive can be inserted anywhere in the document but must be at the start of
a line.

=back



=head1 INTERFACE

=head2 Constructor

The constructor expects a hash as parameter containing one of the following fields:

=over

=item I<url>:

If given, then the AsTMa instance will be read from this url.

=item I<file>:

If given, then the AsTMa data will be read from this file (This
is just a convenience function as it will be mapped to I<url>).

=item I<text>:

If given, then the AsTMa instance will be read directly from this text.

=item I<auto_complete>

If set to 0, the AsTMa loader will NOT try to automatically generate topics which
have been mentioned without being declared.

Note: topics which have been mentioned in a 'is-reified-by' clause will be B<always>
generated.

=back

If several fields (C<file>, C<url>, C<text>) are specified, it is undefined which 
one will be taken.

Examples:

   $atm = new XTM::AsTMa (file => 'here.atm');
   $atm = new XTM::AsTMa (url  => 'file:here.atm',  # the same
			  auto_complete => 0);      # but with auto_completion turned off
   $atm = new XTM::AsTMa (text => '# this is AsTMa');

=cut

sub new {
  my $class = shift;
  my %options = @_;
  elog ($class, 4, 'new');

  my $self = bless { }, $class;
  $self->{url} = 'inline:'.$options{text} if $options{text};
  $self->{url} = 'file:'.  $options{file} if $options{file};
  $self->{url} =           $options{url}  if $options{url};

  $self->{auto_complete} = defined $options{auto_complete} ? $options{auto_complete} : 1;
  return $self;
}

=pod

=head2 Methods

=over

=item C<last_mod>

I<$time> = I<$atm>->last_mod

returns the UNIX time when the resource has been modified last. C<undef> is
returned if the result cannot be determined.


=cut

sub last_mod {
  my $self = shift;

  elog ('XTM::AsTMa', 4, "last mod on AsTMa ".$self->{url});
  if ($self->{url} =~ /^file:(.+)/) {
    use File::stat;
    my $stats = stat($1);
    return $stats->mtime;
  } elsif ($self->{url} =~ /^inline:/) {
    return undef;
  } else {
    elog ('XTM::AsTMa', 3, "Warning: unimplemented scheme '".$self->{url}."' in last_mod");
    return undef;
  }
}

=pod

=item C<sync_in>

I<$tmmemory> = I<$atm>->sync_in

loads an AsTMa instance and returns a L<XTM::Memory> object. Note that that all undefined
topics will be defined automatically, unless C<auto_complete> is set to 0.

The only optional parameter is C<consistency>. It will be used when building the map
while reading the AsTMa instance. See L<XTM> for details.

=cut

sub sync_in {
  my $self        = shift;
  my $consistency = shift || $XTM::default_consistency;

  elog ('XTM::AsTMa', 3, 'sync in '.$self->{url});

  my $atm_stream;                             # fetch the stuff in there
  if ($self->{url} =~ /^inline:(.*)/s) {
    $atm_stream = $1;
  } else {                                    # some kind of URL
    use LWP::Simple;
    $atm_stream = get($self->{url}) || die "XTM::AsTMa: Unable to load '$self->{url}'\n";
    elog ('XTM::AsTMa', 5, "synced in stream", \$atm_stream);
  }

  use XTM::AsTMa::MemoryBuilder;
  my $ap = new XTM::AsTMa::MemoryBuilder(tm => new XTM::Memory (consistency => $consistency));
  $ap->handle_astma (text          => $atm_stream,
#		     log_level     => 2,
		     auto_complete => $self->{auto_complete});
  return $ap->{tm};
}

=pod

=item C<sync_out>

is not implemented.

=cut

sub sync_out {
  die "XTM::AsTMa: sync_out not implementable.";
}

=pod

=back

=head1 SEE ALSO

L<XTM>

=head1 AUTHOR INFORMATION

Copyright 200[1-2], Robert Barta <rho@telecoma.net>, 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;

__END__