Document/TocFile.pm

filter: Statementstmt 14.6% 31 / 212 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 25.0% 9 / 36 Podpod 100% 13 / 13 totaltotal 15.2% 53 / 348 CC 49 SCAR 38.7
CC
49
Cov
10%
_write
182.0
merge
56.0
putFrom
56.0
CRAP 48.1
« File/Spec/PublicationPage.pm ↑ Summary Document/Toc.pm »
1 #================================ TocFile.pm =================================
2 # Filename: TocFile.pm
3 # Description: Manage a Table of contents file.
4 # Original Author: Dale Amon
5 # Revised by: $Author: amon $
6 # Date: $Date: 2008-08-28 23:26:17 $
7 # Version: $Revision: 1.5 $
8 # License: LGPL 2.1, Perl Artistic or BSD
9 #
10 #=============================================================================
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Fault::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Fault::DebugPrinter;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Fault::ErrorHandler;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use DMA::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use DMA::FSM;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Document::Toc;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Document::PageId;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 package Document::TocFile;
21 1 ▶use vars qw{@ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 @ISA = qw( UNIVERSAL );
23  
24 # .toc file format version this module was designed for.
25 #
26 1 my ($MAJOR,$MINOR) = (0,2);
27 1 my $ParseVersion = DMA::Version->new("%d.%02d",'(\d*).(\d\d)',2,
28 $MAJOR,$MINOR);
29  
30 #=============================================================================
31 # Class Methods
32 #=============================================================================
33  
34 0 ▶sub open {
Subroutine open: not called CC=4 SCAR=30.0
Pod documented
35 # Subclasses can override the argument list definition this way
36 0 my $self = bless {}, shift;
37 0 my ($dir,$path,$dates,$document) = $self->_setdirobj ( @_ );
38  
39 # Basic initialization of Ivars
40 0 @$self{'filepath', 'contents',
41 'toc_version','toc_major','toc_minor',
42 'document_title_section','document_dates',
43 'toc_title_section','toc_dates',
44 '_init','_dirty'} =
45 ($path, [],
46 undef, undef,undef,
47 $document,$dates,
48 undef,undef,
49 0,0);
50  
51 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 0 0 0
52 0 my $toc = $self->{'filepath'} = $path . "/.toc";
53 0 ▶ if (-f $toc) {$self->_read || (return undef);
Branch: 2 branches
BranchTrueFalseTotal
if (-f $toc) 0 0 0
$self->_read or return undef 0 0 0
54 0 @$self{'_init','_dirty'} = (1,0);
55 0 return $self;}
56  
57 # Force the creation of an (if necessary) empty .toc file
58 # Use document date and name if it is new.
59 #
60 0 @$self{'document_dates','document_title_section',
61 'toc_dates','toc_title_section',
62 'toc_version',
63 'toc_major','toc_minor',
64 '_init','_dirty'} =
65 ($dates, $document,
66 $dates, $document,
67 $ParseVersion->version,
68 $ParseVersion->list->[0],$ParseVersion->list->[1],
69 1,1);
70 0 return $self;
71 }
72 0 return undef;
73 }
74  
75 #-----------------------------------------------------------------------------
76  
77 0 ▶sub flush {
Subroutine flush: not called CC=2 SCAR=17.9
Pod documented
78 0 my ($self) = @_;
79  
80 0 Fault::DebugPrinter->dbg (1, "TocFile->flush: Flush tocfile to disk");
81  
82 # Update the format version number to the current one just before
83 # writing the output file.
84 0 ▶ if ($ParseVersion->version != $self->version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ParseVersion->version != $self->version) 0 0 0
85 0 Fault::Logger->log ("Updating TocFile from Format Version " .
86 $self->version . " to " .
87 $ParseVersion->version);
88 0 @$self{'toc_version',
89 'toc_major','toc_minor',
90 '_dirty'} =
91 ($ParseVersion->version,
92 $ParseVersion->list->[0],$ParseVersion->list->[1],
93 1);
94 }
95 0 return ($self->_write);
96 }
97  
98 #-----------------------------------------------------------------------------
99 # Flush this object to the .toc file on the file system if this object is
100 # deemed satisfactory and has not already been flushed. Usually called from
101 # the Perl GC.
102  
103 0 ▶sub DESTROY {my ($self) = @_; $self->{'_init'} && $self->flush; return $self;}
Subroutine DESTROY: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_init"} and $self->flush 0 0 0
104  
105 #=============================================================================
106 # Object Methods
107 #=============================================================================
108  
109 0 ▶sub merge {
Subroutine merge: not called CC=7 SCAR=40.3
Pod documented
110 0 my ($self, $toc) = @_;
111  
112 0 ▶ (ref($toc)) || (return undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $toc or return undef 0 0 0
113 0 ▶ ($toc->isa ('Document::Toc')) || (return undef);
Branch: 1 branch
BranchTrueFalseTotal
$toc->isa("Document::Toc") or return undef 0 0 0
114  
115 0 foreach my $record (@{$self->{'contents'}}) {
116 0 my ($pagenum, $title, @categories) = @$record;
117
118 0 ▶ if (!$toc->addPages ($pagenum)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($toc->addPages($pagenum)) 0 0 0
119 0 $self->_err ("Invalid page number in .toc file [$pagenum]");
120 }
121  
122 # Look for the blank title escape "--"
123 0 ▶ if (defined $title) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title) 0 0 0
124 0 ▶ if ($title eq "--") {$title = "";}
Branch: 1 branch
BranchTrueFalseTotal
if ($title eq "--") 0 0 0
125 0 $toc->addPageTitleCategories ($pagenum, $title, @categories);
126 }
127 }
128 0 return $toc;
129 }
130  
131 #-----------------------------------------------------------------------------
132  
133 0 ▶sub putFrom {
Subroutine putFrom: not called CC=7 SCAR=40.3
Pod documented
134 0 my ($self, $toc) = @_;
135  
136 0 Fault::DebugPrinter->dbg
137 (1, "TocFile->putFrom: Overwrite tocfile records from $toc");
138  
139 0 ▶ (ref($toc)) || (return undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $toc or return undef 0 0 0
140 0 ▶ ($toc->isa ('Document::Toc')) || (return undef);
Branch: 1 branch
BranchTrueFalseTotal
$toc->isa("Document::Toc") or return undef 0 0 0
141  
142 # Replace the existing contents with the .toc object data
143 0 $self->{'contents'} = [];
144 0 foreach my $p ($toc->pages) {
145 0 my $cnt = 0;
146 0 foreach my $t ($toc->pageTitles ($p)) {
147 0 my (@c) = $toc->pageTitleCategories($p,$t);
148 0 ▶ if ($t eq "") {$t = "--"};
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq "") 0 0 0
149 0 push @{$self->{'contents'}}, [$p, $t, @c];
150 0 $cnt++;
151 }
152 0 ▶ ($cnt>0) || push @{$self->{'contents'}}, [$p, undef];
Branch: 1 branch
BranchTrueFalseTotal
$cnt > 0 or push @{$$self{"contents"};}, [$p, undef] 0 0 0
153 }
154  
155 0 $self->{'_dirty'} = 1;
156 0 return $toc;
157 }
158  
159 #-----------------------------------------------------------------------------
160  
161 0 ▶sub mergeFrom {
Subroutine mergeFrom: not called CC=2 SCAR=17.9
Pod documented
162 0 my ($self, $toc) = @_;
163  
164 0 Fault::DebugPrinter->dbg (1, "TocFile->mergeFrom: $toc");
165  
166 0 ▶ $self->merge ($toc) || (return undef);
Branch: 1 branch
BranchTrueFalseTotal
$self->merge($toc) or return undef 0 0 0
167 0 return ($self->putFrom ($toc));
168 }
169  
170 #-----------------------------------------------------------------------------
171  
172 0 ▶sub document {return shift->{'document_title_section'};}
Subroutine document: not called CC=1 SCAR=6.9
Pod documented
173 0 ▶sub documentdates {return shift->{'document_dates'};}
Subroutine documentdates: not called CC=1 SCAR=6.9
Pod documented
174 0 ▶sub version {return shift->{'toc_version'};}
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
175 0 ▶sub major {return shift->{'toc_major'};}
Subroutine major: not called CC=1 SCAR=6.9
Pod documented
176 0 ▶sub minor {return shift->{'toc_minor'};}
Subroutine minor: not called CC=1 SCAR=6.9
Pod documented
177 0 ▶sub dates {return shift->{'toc_dates'};}
Subroutine dates: not called CC=1 SCAR=6.9
Pod documented
178 0 ▶sub title {return shift->{'toc_title'};}
Subroutine title: not called CC=1 SCAR=6.9
Pod documented
179 0 ▶sub filepath {return shift->{'filepath'};}
Subroutine filepath: not called CC=1 SCAR=6.9
Pod documented
180  
181 #=============================================================================
182 # Internal Methods
183 #=============================================================================
184 # LexAn State Machine functions
185 #=============================================================================
186 # Confirm we have a valid version number line.
187  
188 0 ▶sub _haveVersion {
Subroutine _haveVersion: not called CC=2 SCAR=17.9
189 0 my ($lexeme, $bb) = @_;
190 0 my $version = $bb->{'vers_obj'};
191  
192 0 ▶ if (!$version->setVersion($lexeme))
Branch: 1 branch
BranchTrueFalseTotal
unless ($version->setVersion($lexeme)) 0 0 0
193 0 { $bb->_err ("Missing or misformatted version: [$lexeme]");
194 0 return (0, $lexeme);
195 }
196 0 return (1, $lexeme);
197 }
198  
199 #-----------------------------------------------------------------------------
200 # Save the version early because it has to be here or we're
201 # totally in never-never land anyway since we'd not know what format
202 # to use or whether this is even *our* .toc file!
203  
204 0 ▶sub _goodVersion {
Subroutine _goodVersion: not called CC=2 SCAR=17.9
205 0 my ($lexeme, $bb) = @_;
206 0 my $version = $bb->{'vers_obj'};
207  
208 0 @$bb{'toc_version','toc_major','toc_minor'} =
209 ($version->version,$version->list->[0],
210 $version->list->[1]);
211  
212 # At a future date we will use this to select different input parsers based
213 # on the major version so that we can retain enough backward compatibility
214 # to read in old formats so we can rewrite the .toc file in the latest
215 # format.
216 #
217 0 ▶ if ($bb->{'toc_major'} gt $ParseVersion->list->[0])
Branch: 1 branch
BranchTrueFalseTotal
if ($bb->{"toc_major"} gt $ParseVersion->list->[0]) 0 0 0
218 0 { $bb->_err
219 ("Cannot interpret a V" .
220 $ParseVersion->version . " .toc file format with a V" .
221 $bb->version . " parser");
222 0 return (0, $lexeme);
223 }
224 0 return (1, $lexeme);
225 }
226  
227 #-----------------------------------------------------------------------------
228 # Confirm we have a valid document name line.
229  
230 0 ▶sub _haveDocName {
Subroutine _haveDocName: not called CC=2 SCAR=17.9
231 0 my ($lexeme, $bb) = @_;
232  
233 # In subclasses docspec is an object; here it is just the text contained in
234 # the current record.
235 #
236 0 my $docspec = $bb->_validate_documentname ($lexeme);
237 0 ▶ if (!defined $docspec)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $docspec) 0 0 0
238 0 { $bb->_err("Document title error: [$lexeme]");
239 0 return (0, $lexeme);
240 }
241 0 @$bb{'toc_title_section','toc_dates'} =
242 ($bb->_split_documentname($docspec));
243  
244 # We have a documpent spec and we now have the critical two first lines that
245 # allow us to declare we have succesfully opened a .toc file.
246 #
247 0 @$bb{'docspec','have_toc'} = ($docspec,1);
248 0 return (1, $lexeme);
249 }
250  
251 #-----------------------------------------------------------------------------
252 # Confirm we have a valid page description line and if so to parse it into
253 # fields and stash it in our contents object.
254 #
255  
256 0 ▶sub _getPageLine {
Subroutine _getPageLine: not called CC=7 SCAR=40.3
257 0 my ($lexeme, $bb) = @_;
258 0 my $tmp;
259 0 my ($pagenum, $title, @categories) = split (' ', $lexeme);
260  
261 0 my ($start,$end) = split ('-', $pagenum);
262 0 ▶ if (!$start) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($start) 0 0 0
263 0 $bb->_err ("Document pagenumber missing: [$lexeme]");
264 0 return (0, $lexeme);}
265  
266 0 ▶ if (!Document::PageId->new ($start)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Document::PageId"->new($start)) 0 0 0
267 0 $bb->_err ("Document pagenumber error: [$lexeme]");
268 0 return (0, $lexeme);}
269
270 0 ▶ if ($end && !Document::PageId->new ($end)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end and not "Document::PageId"->new($end)) 0 0 0
Condition0%0/3
$end and not "Document::PageId"->new($end)
!ll&&!rl&&r
000
MC/DC0%0/2
$end and not "Document::PageId"->new($end)
$end not "Document::PageId"->new($end)
Truth table0%0/3
A and not(B)
A= $endB= not "Document::PageId"->new($end)
ABresult
0X0
100
111
271 0 $bb->_err ("Document end page number error: [$lexeme]");
272 0 return (0, $lexeme);}
273
274 # This is an escape for storing blank titles
275 0 ▶ if ($title eq "--") {$title = "";}
Branch: 1 branch
BranchTrueFalseTotal
if ($title eq '--') { } 0 0 0
276 else {
277 # In subclasses docspec is an object; here it is just the text contained
278 # in the document title record.
279 #
280 0 my $docspec = $bb->{'docspec'};
281 0 ▶ if (! $bb->_validate_pagetitle ($docspec, $start, $end, $title)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bb->_validate_pagetitle($docspec, $start, $end, $title)) 0 0 0
282 0 $bb->_err ("Document title error: [$lexeme]");
283 0 return (0, $lexeme);}
284 }
285
286 0 push @{$bb->{'contents'}}, [$pagenum, $title, @categories];
287 0 return (1, $lexeme);
288 }
289  
290 #-----------------------------------------------------------------------------
291  
292 0 ▶sub _noop {my ($lexeme, $bb) = @_; return (1,$lexeme);}
Subroutine _noop: not called CC=1 SCAR=6.9
293  
294 #=============================================================================
295 # Read using state machine
296  
297 0 ▶sub _read {
Subroutine _read: not called CC=5 SCAR=34.0
298 0 my ($self) = @_;
299 0 my ($fd, @toc,@lexemes);
300 0 my $fst =
301 {
302 'S0' => ["E0","SAME", \&_haveVersion, "S1","SAME","E0","SAME"],
303 'S1' => ["E0","SAME", \&_goodVersion, "S2","NEXT","E0","SAME"],
304 'S2' => ["E0","SAME", \&_haveDocName, "S3","NEXT","E0","SAME"],
305 'S3' => ["D0","SAME", \&_getPageLine, "S3","NEXT","S3","NEXT"],
306 'E0' => ["E0","ERR", \&_noop, "","","",""],
307 'D0' => ["D0","DONE", \&_noop, "","","",""],
308 };
309  
310 0 ▶ if (!CORE::open ($fd, "<" . $self->{'filepath'})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::open $fd, '<' . $self->{"filepath"}) 0 0 0
311 0 Fault::ErrorHandler->warn ("Failed to open .toc file!");
312 0 return 0;
313 }
314  
315 # These files should not be huge so it's faster to get it all in one big
316 # read to avoid I/O blocking.
317 #
318 0 @toc = <$fd>;
319 0 close $fd;
320  
321 0 $self->{'vers_obj'} = DMA::Version->new("%d.%02d",'(\d*).(\d\d)',2);
322 0 $self->{'pub_obj'} = undef;
323  
324 0 ▶ if (!defined $self->{'vers_obj'})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"vers_obj"}) 0 0 0
325 0 { Fault::ErrorHandler->die
326 ("Fatal error while creating empty DMA::Version object");
327 0 return 0;
328 }
329  
330 # This line screws up emacs formatting. Other than that, skip blank lines
331 # and comments, and remove newline from the end if present.
332 0 ▶ for ( @toc ) { !(/^\s*$|^\s*#/) || next; chomp $_; push @lexemes, $_; }
Branch: 1 branch
BranchTrueFalseTotal
/^\s*$|^\s*#/ and next 0 0 0
333  
334 # Parse status is fail until noted otherwise.
335 0 $self->{'have_toc'} = 0;
336 0 my @remaining = DMA::FSM::FSM ( $fst, $self, @lexemes );
337 0 my $status = $self->{'have_toc'};
338 0 delete @$self{'have_toc','pub_obj','vers_obj','state','docspec'};
339  
340 0 return ($status);
341 }
342  
343 #-----------------------------------------------------------------------------
344  
345 0 ▶sub _write {
Subroutine _write: not called CC=13 SCAR=52.0
346 0 my ($self) = @_;
347 0 my $fd;
348 0 my $tocfile = $self->{'filepath'};
349 0 my $tmpfile = "$tocfile.tmp";
350  
351 0 ▶ $self->{'_init'} || (return 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_init"} or return 0 0 0 0
352 0 ▶ $self->{'_dirty'} || (return 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_dirty"} or return 1 0 0 0
353  
354 # First write the new values to a tmp file.
355 0 ▶ if (!CORE::open ($fd, ">$tmpfile")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::open $fd, ">$tmpfile") 0 0 0
356 0 Fault::ErrorHandler->warn("Failed to open temp file $tmpfile! $!");
357 0 return 0;
358 }
359  
360 0 my ($v,$p) = ($ParseVersion->version, $self->_docname);
361
362 0 ▶ if (!printf $fd "$v\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (printf $fd "$v\n") 0 0 0
363 0 Fault::ErrorHandler->warn
364 ("Failed to write version record [$v] to $tmpfile: $!");
365 0 return 0;
366 }
367  
368 0 ▶ if (!printf $fd "$p\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (printf $fd "$p\n") 0 0 0
369 0 Fault::ErrorHandler->warn
370 ("Failed to write document record [$p] to $tmpfile: $!");
371 0 return 0;
372 }
373  
374 # Convert it all to text records
375 0 my @list;
376 0 foreach my $record (@{$self->{'contents'}}) {
377 0 my ($p, $t, @c) = @$record;
378 0 ▶ defined $p || next;
Branch: 1 branch
BranchTrueFalseTotal
defined $p or next 0 0 0
379 0 ▶ push @list, ((defined $t) ? "$p $t " . join " ", @c : $p)
Branch: 1 branch
BranchTrueFalseTotal
defined $t ? : 0 0 0
380 }
381  
382 # Now sort and print them. I'd not do this if I expected lists longer
383 # than a few hundred lines.
384 #
385 0 foreach my $r (sort @list) {
386 0 ▶ if (!printf $fd "$r\n") {
Branch: 1 branch
BranchTrueFalseTotal
unless (printf $fd "$r\n") 0 0 0
387 0 Fault::ErrorHandler->warn
388 ("Failed to write page record [$r] to $tmpfile: $!");
389 0 return 0;
390 }
391 }
392  
393 0 ▶ if (!close $fd) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close $fd) 0 0 0
394 0 Fault::ErrorHandler->warn ("Failed to close $tmpfile: $!");
395 0 return 0;
396 }
397  
398 # Now finalize the changes.
399 0 ▶ if (system ("mv","$tmpfile","$tocfile") != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (system("mv", "$tmpfile", "$tocfile") != 0) 0 0 0
400 0 Fault::ErrorHandler->warn
401 ("Failed to move $tmpfile to $tocfile: $!");
402 0 return 0;
403 }
404  
405 0 $self->{'_dirty'}=0;
406 0 return 1;
407 }
408  
409 #-----------------------------------------------------------------------------
410 # Work around a missing date so we can use this even on undated files.
411 # Returns document_title_section or document_dates-document_title_section if
412 # possible.
413  
414 0 ▶sub _docname {
Subroutine _docname: not called CC=2 SCAR=17.9
415 0 my $self = shift;
416 return $self->{'document_dates'} ?
417 $self->{'document_dates'} . "-" .
418 $self->{'document_title_section'} :
419 0 ▶ $self->{'document_title_section'};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'document_dates'} ? : 0 0 0
420 }
421  
422 #-----------------------------------------------------------------------------
423 # open method input list overrides
424 #-----------------------------------------------------------------------------
425  
426 0 ▶sub _setdirobj {
Subroutine _setdirobj: not called CC=5 SCAR=34.0
427 0 my ($self, $dir, $date, $name) = @_;
428 0 ▶ $dir || (Fault::ErrorHandler->warn
Branch: 1 branch
BranchTrueFalseTotal
$dir or "Fault::ErrorHandler"->warn("Missing document directory path or object"), (return undef) 0 0 0
429 ("Missing document directory path or object"), return undef);
430  
431 0 ▶ $date || (Fault::ErrorHandler->warn
Branch: 1 branch
BranchTrueFalseTotal
$date or "Fault::ErrorHandler"->warn("Missing document date section"), (return undef) 0 0 0
432 ("Missing document date section"), return undef);
433  
434 0 ▶ $name || (Fault::ErrorHandler->warn
Branch: 1 branch
BranchTrueFalseTotal
$name or "Fault::ErrorHandler"->warn("Missing document title section"), (return undef) 0 0 0
435 ("Missing document title section"), return undef);
436  
437 0 ▶ (!ref($dir)) || ($dir = undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $dir and $dir = undef 0 0 0
438 0 return ($dir, "$dir/$date-$name", $date, $name);
439 }
440  
441 #-----------------------------------------------------------------------------
442 # _read method overrides.
443  
444 0 ▶sub _validate_documentname {my ($self, $record) = @_; return $record;}
Subroutine _validate_documentname: not called CC=1 SCAR=6.9
445 0 ▶sub _split_documentname {my ($self, $docspec) = @_; return ($docspec, "");}
Subroutine _split_documentname: not called CC=1 SCAR=6.9
446  
447 #-----------------------------------------------------------------------------
448  
449 sub _validate_pagetitle
450 0 ▶ { my ($self, $docspec, $start, $end, $title) = @_; return 1;}
Subroutine _validate_pagetitle: not called CC=1 SCAR=6.9
451  
452 0 ▶sub _err {my ($self, $str) = @_; Fault::ErrorHandler->warn ($str);}
Subroutine _err: not called CC=1 SCAR=6.9
453
454 #=============================================================================
455 # Pod Documentation
456 #=============================================================================
457 # You may extract and format the documention section with the 'perldoc' cmd.
458  
459 =head1 NAME
460  
461 Document::TocFile - Manage a Table of contents file.
462  
463 =head1 SYNOPSIS
464  
465 use Document::TocFile;
466  
467 $obj = Document:TocFile->open ($path, $date, $title)
468 $obj = $obj->flush
469 $toc = $obj->merge ($toc)
470 $toc = $obj->putFrom ($toc)
471 $toc = $obj->mergeFrom ($toc)
472 $version = $obj->version
473 $major = $obj->major
474 $minor = $obj->minor
475 $title = $obj->title
476 $dates = $obj->dates
477 $filepath = $obj->filepath
478 $title = $obj->document
479 $dates = $obj->documentdates
480  
481 =head1 Inheritance
482  
483 UNIVERSAL
484  
485 =head1 Description
486  
487 This Class manages a table of contents file. The file is always named .toc
488 and contains formatted records of the following syntax (TocFile Format
489 Version 0.02):
490  
491 0.02
492 <dates>-<document_title_section>
493 <pageid> <pagetitle_section> <categorylist>
494  
495 where the first two lines are required. There may be zero or more occurrences
496 of the page lines. A category list is just a list of keywords:
497  
498 015 TheCatCameBack-ANewLook Physics Cosmology Space
499  
500 The Toc format version number will allow us to cleanly upgrade to more
501 sophisticated file structuring designs in the future. It is the first
502 item in the .toc file, on a line by itself so a parser may immediately
503 decide if it will be able to parse the remainder of the file. The minor
504 version number is to be incremented if changes are such that a previous
505 parser will simply not see the additions or changes (changes are upward
506 compatible); if a change affects the ability of the previous parser to keep
507 on keepin' on, the major version number is incremented (changes are not
508 upward compatible). Parsers for a new major version should be able to read
509 and upgrade all previous versions of the .toc file, perhaps by selection off
510 the major version number.
511  
512 =head2 To Do
513  
514 There is absolutely no protection against more than one application working
515 with the same .toc file at the same time. This is an area for future
516 improvement. The same is true of .log and .config files.
517  
518 =head1 Examples
519  
520 use Document::Toc;
521 use Document::TocFile;
522  
523 my ($path,$date,$title) = ("~/test","20040902","LinuxJournal-i126");
524 my $toc = Document::Toc->new ();
525 my $foo = Document:TocFile->open ($path,$date,$title);
526  
527 my $docname_string = $foo->title;
528 my $date_string = $foo->dates;
529 my $version_string = $foo->version;
530 my $major_version_number = $foo->major;
531 my $minor_version_number = $foo->minor;
532 my $filepath = $foo->filepath
533 my $doctitle = $foo->document
534 my $docdates = $foo->documentdates
535 my $merged_toc = $foo->merge ($toc);
536 my $flg = $foo->flush
537 $toc = $foo->putFrom ($merged_toc);
538 $toc = $foo->merge ($toc)
539 $toc = $foo->mergeFrom ($toc)
540  
541 =head1 Class Variables
542  
543 None.
544  
545 =head1 Instance Variables
546  
547 filepath Full pathname of the .toc file suitable for open and
548 close.
549 document_dates Document date string taken from the filepath.
550 document_title_section Document name string taken from the filepath..
551 contents Array of arrays to hold all the lines of the table of
552 contents data read from the .toc.
553 toc_version Version string taken from the .toc file.
554 toc_major First element of version string from .toc
555 toc_minor Second element of version string from .toc.
556 toc_dates Document date string taken from the .toc file.
557 toc_title_section Document name string taken from the .toc file.
558  
559 =head1 Class Methods
560  
561 =over 4
562  
563 =item B<$obj = Document:TocFile-E<gt>open ($path, $date, $title)>
564  
565 Generate an object to manage the interface with a .toc file in a directory
566 named /$path/$date-$title/. The arguments are:
567  
568 path to document directory, eg. /home/mypubs/thisdocument/
569 document dates eg. 20040813
570 document-title-section eg. LinuxManual-PenguinsRule
571  
572 If there is no .toc file, an 'empty' with just:
573  
574 0.02
575 $date-$title
576  
577 in it.. If the file already exists, it is read and if it has a version number
578 this object knows about, the data is parsed into an internal format. When the
579 .toc file is next updated, it will be re-written in the Toc File Format Version
580 used by this object.
581  
582 The open method returns undef if it is unable to build a valid document path
583 name from the input argument list.
584  
585 =back 4
586  
587 =head1 Instance Methods
588  
589 =over 4
590  
591 =item B<$dates = $obj-E<gt>dates>
592  
593 Return the date string from the .toc file document name, eg "19991225"
594  
595 =item B<$title = $obj-E<gt>document>
596  
597 Return the document title from the input args eg:
598  
599 "ReportOnInternationalSecurity-TheThreatModel"
600  
601 =item B<$dates = $obj-E<gt>documentdates>
602  
603 Return the date string from the input args, eg "19991225"
604  
605 =item B<$filepath = $obj-E<gt>filepath>
606  
607 Return the full pathname of the .toc file, suitable for file open, close,
608 etc, eg:
609  
610 "/archive/19991225-ReportOnInternationalSecurity-TheThreatModel/.toc"
611  
612 =item B<$obj = $obj-E<gt>flush>
613  
614 Write a new .toc file from the stored data. Writes to a .toc.tmp first then
615 moves it to .toc. Any error at any point terminates the close before
616 overwriting the .toc. Only if the system 'mv' itself fails in a race could
617 .toc be damaged.
618  
619 flush can be issued any number of times, but will do nothing unless the
620 contents has changed.
621  
622 If the version of this class is different from the version on the .toc, a
623 rewrite is forced.
624  
625 If there is no .toc file, a write is forced so that at the very least there
626 will always be a file with version and document title records in any directory
627 for which a Document::TocFile object is created.
628  
629 Flush is called by the DESTROY method, so if our contents have changed in any
630 of the ways noted above, the .toc file will be rewritten.
631  
632 [If a reason is found that requires prevention of this behavior, a simple
633 code modification is to add a method to clear the internal _init flag ivar.]
634  
635 =item B<$major = $obj-E<gt>major>
636  
637 Return the major version part of the .toc files' format version number.
638  
639 =item B<$toc = $obj-E<gt>merge ($toc)>
640  
641 Merge the internally stored contents of the .toc file into the supplied
642 Document::Toc object. Undef on failure.
643  
644 =item B<$toc = $obj-E<gt>mergeFrom ($toc)>
645  
646 Overwrite page records in the contents array by merging the current contents
647 with those from the supplied Document::Toc object. Does not rewrite the
648 .toc file itself.
649  
650 =item B<$minor = $obj-E<gt>minor>
651  
652 Return the minor version part of the .toc files' format version number.
653  
654 =item B<$toc = $obj-E<gt>putFrom ($toc)>
655  
656 Replace the internally stored contents of the .toc file with the data from
657 the supplied Document::Toc object. Does not rewrite the .toc file itself.
658  
659 Returns undef on failure.
660  
661 =item B<$title = $obj-E<gt>title>
662  
663 Return the document title from the associated .toc file eg:
664  
665 "ReportOnInternationalSecurity-TheThreatModel"
666  
667 =item B<$version = $obj-E<gt>version>
668  
669 Return the .toc files' format version number string.
670  
671 =back 4
672  
673 =head1 Private Class Methods
674  
675 None.
676  
677 =head1 Private Instance Methods
678  
679 =over 4
680  
681 =item B<$obj-E<gt>_err ($msg)>
682  
683 Print a warning message.
684  
685 =item B<$flg = $obj-E<gt>_read>
686  
687 Read the and parse the .toc file from a publication directory. Return true
688 on success; undef if the major version stored in the file is greater than
689 the major version of this Class or other problems are found with the file
690 format.
691  
692 =item B<($path, $pathname, $dates, $title) = $obj-E<gt>_setdirobj ($path, $dates, $title )>
693  
694 Subclass may override. Shuffles the input argument list and returns them in
695 a list along with a $pathname of $dir/$date-$name. Why? This is a tricky way
696 to let subclasses override the open method's input argument list.
697  
698 Open's entire argument list is passed to this method, uninterpreted. So
699 whether open takes one argument or three is entirely dependent what this
700 method looks for.
701  
702 =item B<($toc_title, $toc_dates) = $obj-E<gt>_split_documentname ($toc_title)>
703  
704 This method customizes the behavior of the the .toc file read operation in
705 the open method. Subclass may override. The first output value is just a copy
706 of the input argument; the second, the date, is a null string. This might
707 change if I decided to do some low level parsing here. Yes, it could use a
708 File::Spec, but it is intentionally staying as simple an independent as
709 possible. Child classes will use File::Spec::* when they override this method.
710  
711 =item B<$documentname = $obj-E<gt>_validate_documentname ($documentname)>
712  
713 This method customizes the behavior of the the .toc file read operation in
714 the open method. Subclass may override. Just moves the input arg to the
715 output.
716  
717 =item B<$flg = $obj-E<gt>_validate_pagetitle ($document_title, $stargpage, $endpage, $document_title)>
718  
719 This method customizes the behavior of the the .toc file read operation in
720 the open method. Subclass may override. Always returns true.
721  
722 =item B<$flg = $obj-E<gt>_write>
723  
724 Write an updated .toc file to a publication directory with extreme care and
725 paranoia. First write a temp file, then copy it over top of the old .toc
726 once we're sure all is cool.
727  
728 If object has not been initialized, fail.
729  
730 If it has not changed, succeed immediately. Otherwise flush it to disk and
731 clear _dirty flag.
732  
733 Returns true on success, false if *all* data does not end up in .toc
734  
735 =back 4
736  
737 =head1 KNOWN BUGS
738  
739 See TODO.
740  
741 =head1 SEE ALSO
742  
743 Fault::Logger, Fault::DebugPrinter, Fault::ErrorHandler, DMA::Version,
744 DMA::FSM, Document::Toc, Document::PageId, DMA::ISODate
745  
746 =head1 AUTHOR
747  
748 Dale Amon <amon@vnl.com>
749  
750 =cut
751
752 #=============================================================================
753 # CVS HISTORY
754 #=============================================================================
755 # $Log: TocFile.pm,v $
756 # Revision 1.5 2008-08-28 23:26:17 amon
757 # perldoc section regularization.
758 #
759 # Revision 1.4 2008-08-13 21:04:32 amon
760 # Third phase of reformatting to current standard.
761 #
762 # Revision 1.3 2008-08-13 20:56:53 amon
763 # Second phase of reformatting to current standard.
764 #
765 # Revision 1.2 2008-08-13 14:50:02 amon
766 # First phase of reformatting to current standard.
767 #
768 # Revision 1.1.1.1 2004-09-02 13:08:02 amon
769 # Manages a Document directory
770 #
771 # 20040831 Dale Amon <amon@islandone.org>
772 # Changing this base class to assume document titles rather
773 # than Publication names. I am using it for both cases so
774 # I really need the generality. It now uses FSM, my new
775 # Finite State Machine class.
776 #
777 # 20040813 Dale Amon <amon@islandone.org>
778 # Split it into a base class and subclass to make it easier
779 # to use in simple applications:
780 # Document::TocFile string args
781 # Archivist::TocFile complex object for arg
782 #
783 # 20030107 Dale Amon <amon@vnl.com>
784 # Added "--" token to file format to stand in for blank titles.
785 # Bumped format version to 0.02
786 #
787 # 20021223 Dale Amon <amon@vnl.com>
788 # Major rewrite to make it deal only with the file and to
789 # be able to fill/merge data with an external .toc file
790 # on request rather than managing both the internal and
791 # external representations of the .toc simultaneously.
792 #
793 # 20021216 Dale Amon <amon@vnl.com>
794 # Created. Format version 0.01
795 1 1;
« File/Spec/PublicationPage.pm ↑ Summary Document/Toc.pm »