source: trunk/gsdl/perllib/doc.pm@ 13172

Last change on this file since 13172 was 13172, checked in by kjdon, 17 years ago

Moved all printing stuff out of doc.pm.
docprint now prints a GA representation of a doc obj - use &docprint::get_section_xml instead of $doc_obj->buffer_section_xml or $doc_obj->output_section.
Most of the code has been moved into plugouts, except for the bit thats gone to docprint.pm.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 26.1 KB
Line 
1###########################################################################
2#
3# doc.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistr te it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# base class to hold documents
27
28package doc;
29eval {require bytes};
30
31BEGIN {
32 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
33 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/dynamic/lib/site_perl/5.005/i686-linux");
34}
35
36use unicode;
37use util;
38use ghtml;
39use File::stat;
40##use hashdoc;
41use docprint;
42
43# the document type may be indexed_doc, nonindexed_doc, or
44# classification
45
46my $OIDcount = 0;
47
48sub new {
49 my $class = shift (@_);
50 my ($source_filename, $doc_type) = @_;
51
52 my $self = bless {'associated_files'=>[],
53 'subsection_order'=>[],
54 'next_subsection'=>1,
55 'subsections'=>{},
56 'metadata'=>[],
57 'text'=>"",
58 'OIDtype'=>"hash"}, $class;
59
60 # used to set lastmodified here, but this can screw up the HASH ids, so
61 # the docsave processor now calls set_lastmodified
62
63 if (defined $source_filename) {
64 my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
65
66 if (defined $collect_dir) {
67 my $dirsep = &util::get_dirsep();
68 if ($collect_dir !~ m/$dirsep$/) {
69 $collect_dir .= $dirsep;
70 }
71
72 $collect_dir =~ s/\\/\\\\/g; # escape DOS style file separator
73
74 # if from within GSDLCOLLECTDIR, then remove directory prefix
75 # so source_filename is realative to it. This is done to aid
76 # portability, i.e. the collection can be moved to somewhere
77 # else on the file system and the archives directory will still
78 # work. This is needed, for example in the applet version of
79 # GLI where GSDLHOME/collect on the server will be different to
80 # the collect directory of the remove user. Of course,
81 # GSDLCOLLECTDIR subsequently needs to be put back on to turn
82 # it back into a full pathname.
83
84 if ($source_filename =~ /^$collect_dir(.*)$/) {
85 $source_filename = $1;
86 }
87 }
88
89 $self->set_source_filename ($source_filename);
90 }
91
92 $self->set_doc_type ($doc_type) if defined $doc_type;
93
94 return $self;
95}
96# set lastmodified for OAI purposes, added by GRB, moved by kjdon
97sub set_lastmodified {
98 my $self = shift (@_);
99
100 my $source_filename = $self->get_source_filename();
101 if ((defined $self->get_metadata_element ($self->get_top_section(), "gsdldoctype")) &&
102 (defined $source_filename) && (-e $source_filename)) {
103 my $file_stat = stat($source_filename);
104 my $mtime = $file_stat->mtime;
105 $self->add_utf8_metadata($self->get_top_section(), "lastmodified", $file_stat->mtime);
106 }
107}
108
109# clone the $self object
110sub duplicate {
111 my $self = shift (@_);
112
113 my $newobj = {};
114
115 foreach my $k (keys %$self) {
116 $newobj->{$k} = &clone ($self->{$k});
117 }
118
119 bless $newobj, ref($self);
120 return $newobj;
121}
122
123sub clone {
124 my ($from) = @_;
125 my $type = ref ($from);
126
127 if ($type eq "HASH") {
128 my $to = {};
129 foreach my $key (keys %$from) {
130 $to->{$key} = &clone ($from->{$key});
131 }
132 return $to;
133 } elsif ($type eq "ARRAY") {
134 my $to = [];
135 foreach my $v (@$from) {
136 push (@$to, &clone ($v));
137 }
138 return $to;
139 } else {
140 return $from;
141 }
142}
143
144sub set_OIDtype {
145 my $self = shift (@_);
146 my ($type, $metadata) = @_;
147
148 if (defined $type && $type =~ /^(hash|incremental|dirname|assigned)$/) {
149 $self->{'OIDtype'} = $type;
150 } else {
151 $self->{'OIDtype'} = "hash";
152 }
153 if ($type =~ /^assigned$/) {
154 if (defined $metadata) {
155 $self->{'OIDmetadata'} = $metadata;
156 } else {
157 $self->{'OIDmetadata'} = "dc.Identifier";
158 }
159 }
160}
161
162sub set_source_filename {
163 my $self = shift (@_);
164 my ($source_filename) = @_;
165
166 $self->set_metadata_element ($self->get_top_section(),
167 "gsdlsourcefilename",
168 $source_filename);
169}
170
171sub set_converted_filename {
172 my $self = shift (@_);
173 my ($converted_filename) = @_;
174
175 $self->set_metadata_element ($self->get_top_section(),
176 "gsdlconvertedfilename",
177 $converted_filename);
178}
179
180
181# returns the source_filename as it was provided
182sub get_source_filename {
183 my $self = shift (@_);
184
185 return $self->get_metadata_element ($self->get_top_section(), "gsdlsourcefilename");
186}
187
188# returns converted filename if available else returns source filename
189sub get_filename_for_hashing {
190 my $self = shift (@_);
191
192 my $filename = $self->get_metadata_element ($self->get_top_section(), "gsdlconvertedfilename");
193
194 if (!defined $filename) {
195 my $plugin_name = $self->get_metadata_element ($self->get_top_section(), "Plugin");
196 # if NULPlug processed file, then don't give a filename
197 if (defined $plugin_name && $plugin_name eq "NULPlug") {
198 $filename = undef;
199 } else {
200 $filename = $self->get_metadata_element ($self->get_top_section(), "gsdlsourcefilename");
201 }
202 }
203 return $filename;
204}
205
206sub set_doc_type {
207 my $self = shift (@_);
208 my ($doc_type) = @_;
209
210 $self->set_metadata_element ($self->get_top_section(),
211 "gsdldoctype",
212 $doc_type);
213}
214
215# returns the gsdldoctype as it was provided
216# the default of "indexed_doc" is used if no document
217# type was provided
218sub get_doc_type {
219 my $self = shift (@_);
220
221 my $doc_type = $self->get_metadata_element ($self->get_top_section(), "gsdldoctype");
222 return $doc_type if (defined $doc_type);
223 return "indexed_doc";
224}
225
226
227# look up the reference to the a particular section
228sub _lookup_section {
229 my $self = shift (@_);
230 my ($section) = @_;
231
232 my ($num);
233 my $sectionref = $self;
234
235 while (defined $section && $section ne "") {
236
237 ($num, $section) = $section =~ /^\.?(\d+)(.*)$/;
238
239 $num =~ s/^0+(\d)/$1/ if defined $num ; # remove leading 0s
240
241 $section = "" unless defined $section;
242
243
244 if (defined $num && defined $sectionref->{'subsections'}->{$num}) {
245 $sectionref = $sectionref->{'subsections'}->{$num};
246 } else {
247 return undef;
248 }
249 }
250
251 return $sectionref;
252}
253
254# calculate OID by hashing the contents of the document
255sub _calc_OID {
256 my $self = shift (@_);
257 my ($filename) = @_;
258
259 my $osexe = &util::get_os_exe();
260
261 my $hashfile_exe = &util::filename_cat($ENV{'GSDLHOME'},"bin",
262 $ENV{'GSDLOS'},"hashfile$osexe");
263
264 my $result = "NULL";
265
266 if (-e "$hashfile_exe") {
267# $result = `\"$hashfile_exe\" \"$filename\"`;
268 $result = `hashfile$osexe \"$filename\"`;
269 ($result) = $result =~ /:\s*([0-9a-f]+)/i;
270
271 } else {
272 print STDERR "doc::_calc_OID $hashfile_exe could not be found\n";
273 }
274 return "HASH$result";
275}
276
277# methods dealing with OID, not groups of them.
278
279# if $OID is not provided one is calculated
280sub set_OID {
281 my $self = shift (@_);
282 my ($OID) = @_;
283
284 my $use_hash_oid = 0;
285 # if an OID wasn't provided claculate one
286 if (!defined $OID) {
287 $OID = "NULL";
288 if ($self->{'OIDtype'} eq "hash") {
289 $use_hash_oid = 1;
290 } elsif ($self->{'OIDtype'} eq "incremental") {
291 $OID = "D" . $OIDcount;
292 $OIDcount ++;
293
294 } elsif ($self->{'OIDtype'} eq "dirname") {
295 $OID = 'J';
296 my $filename = $self->get_source_filename();
297 if (defined($filename)) { # && -e $filename) {
298 $OID = &File::Basename::dirname($filename);
299 if (defined $OID) {
300 $OID = 'J'.&File::Basename::basename($OID);
301 $OID =~ s/\.//; #remove any periods
302 } else {
303 print STDERR "Failed to find base for filename ($filename)...generating hash id\n";
304 $use_hash_oid = 1;
305 }
306 } else {
307 print STDERR "Failed to find filename, generating hash id\n";
308 $use_hash_oid = 1;
309 }
310
311 } elsif ($self->{'OIDtype'} eq "assigned") {
312 my $identifier = $self->get_metadata_element ($self->get_top_section(), $self->{'OIDmetadata'});
313 if (defined $identifier && $identifier ne "") {
314 $OID = "D" . $identifier;
315 $OID =~ s/\.//; #remove any periods
316 } else {
317 # need a hash id
318 print STDERR "no $self->{'OIDmetadata'} metadata found, generating hash id\n";
319 $use_hash_oid = 1;
320 }
321
322 } else {
323 $use_hash_oid = 1;
324 }
325
326 if ($use_hash_oid) {
327
328 # "hash" OID - feed file to hashfile.exe
329 #my $filename = $self->get_source_filename();
330 # we want to use the converted file for hashing if available
331 # cos its quicker
332 my $filename = $self->get_filename_for_hashing();
333 # -z: don't want to hash on the file if it is zero size
334 if (defined($filename) && -e $filename && !-z $filename) {
335 $OID = $self->_calc_OID ($filename);
336 } else {
337 $filename = &util::get_tmp_filename();
338 if (!open (OUTFILE, ">$filename")) {
339 print STDERR "doc::set_OID could not write to $filename\n";
340 } else {
341 my $doc_text = &docprint::get_section_xml($self, $self->get_top_section());
342 print OUTFILE $doc_text;
343 close (OUTFILE);
344 }
345 $OID = $self->_calc_OID ($filename);
346 &util::rm ($filename);
347 }
348 }
349 }
350 $self->set_metadata_element ($self->get_top_section(), "Identifier", $OID);
351}
352
353# this uses hashdoc (embedded c thingy) which is faster but still
354# needs a little work to be suffiently stable
355sub ___set_OID {
356 my $self = shift (@_);
357 my ($OID) = @_;
358
359 # if an OID wasn't provided then calculate hash value based on document
360 if (!defined $OID)
361 {
362 my $hash_text = &docprint::get_section_xml($self, $self->get_top_section());
363 my $hash_len = length($hash_text);
364
365 $OID = &hashdoc::buffer($hash_text,$hash_len);
366 }
367
368 $self->set_metadata_element ($self->get_top_section(), "Identifier", $OID);
369}
370
371# returns the OID for this document
372sub get_OID {
373 my $self = shift (@_);
374 my $OID = $self->get_metadata_element ($self->get_top_section(), "Identifier");
375 return $OID if (defined $OID);
376 return "NULL";
377}
378
379sub delete_OID {
380 my $self = shift (@_);
381
382 $self->set_metadata_element ($self->get_top_section(), "Identifier", "NULL");
383}
384
385
386# methods for manipulating section names
387
388# returns the name of the top-most section (the top
389# level of the document
390sub get_top_section {
391 my $self = shift (@_);
392
393 return "";
394}
395
396# returns a section
397sub get_parent_section {
398 my $self = shift (@_);
399 my ($section) = @_;
400
401 $section =~ s/(^|\.)\d+$//;
402
403 return $section;
404}
405
406# returns the first child section (or the end child
407# if there isn't any)
408sub get_begin_child {
409 my $self = shift (@_);
410 my ($section) = @_;
411
412 my $section_ptr = $self->_lookup_section($section);
413 return "" unless defined $section_ptr;
414
415 if (defined $section_ptr->{'subsection_order'}->[0]) {
416 return "$section.$section_ptr->{'subsection_order'}->[0]";
417 }
418
419 return $self->get_end_child ($section);
420}
421
422# returns the next child of a parent section
423sub get_next_child {
424 my $self = shift (@_);
425 my ($section) = @_;
426
427 my $parent_section = $self->get_parent_section($section);
428 my $parent_section_ptr = $self->_lookup_section($parent_section);
429 return undef unless defined $parent_section_ptr;
430
431 my ($section_num) = $section =~ /(\d+)$/;
432 return undef unless defined $section_num;
433
434 my $i = 0;
435 my $section_order = $parent_section_ptr->{'subsection_order'};
436 while ($i < scalar(@$section_order)) {
437 last if $section_order->[$i] eq $section_num;
438 $i++;
439 }
440
441 $i++; # the next child
442 if ($i < scalar(@$section_order)) {
443 return $section_order->[$i] if $parent_section eq "";
444 return "$parent_section.$section_order->[$i]";
445 }
446
447 # no more sections in this level
448 return undef;
449}
450
451# returns a reference to a list of children
452sub get_children {
453 my $self = shift (@_);
454 my ($section) = @_;
455
456 my $section_ptr = $self->_lookup_section($section);
457 return [] unless defined $section_ptr;
458
459 my @children = @{$section_ptr->{'subsection_order'}};
460
461 map {$_ = "$section.$_"; $_ =~ s/^\.+//;} @children;
462 return \@children;
463}
464
465# returns the child section one past the last one (which
466# is coded as "0")
467sub get_end_child {
468 my $self = shift (@_);
469 my ($section) = @_;
470
471 return $section . ".0" unless $section eq "";
472 return "0";
473}
474
475# returns the next section in book order
476sub get_next_section {
477 my $self = shift (@_);
478 my ($section) = @_;
479
480 return undef unless defined $section;
481
482 my $section_ptr = $self->_lookup_section($section);
483 return undef unless defined $section_ptr;
484
485 # first try to find first child
486 if (defined $section_ptr->{'subsection_order'}->[0]) {
487 return $section_ptr->{'subsection_order'}->[0] if ($section eq "");
488 return "$section.$section_ptr->{'subsection_order'}->[0]";
489 }
490
491 do {
492 # try to find sibling
493 my $next_child = $self->get_next_child ($section);
494 return $next_child if (defined $next_child);
495
496 # move up one level
497 $section = $self->get_parent_section ($section);
498 } while $section =~ /\d/;
499
500 return undef;
501}
502
503sub is_leaf_section {
504 my $self = shift (@_);
505 my ($section) = @_;
506
507 my $section_ptr = $self->_lookup_section($section);
508 return 1 unless defined $section_ptr;
509
510 return (scalar (@{$section_ptr->{'subsection_order'}}) == 0);
511}
512
513# methods for dealing with sections
514
515# returns the name of the inserted section
516sub insert_section {
517 my $self = shift (@_);
518 my ($before_section) = @_;
519
520 # get the child to insert before and its parent section
521 my $parent_section = "";
522 my $before_child = "0";
523 my @before_section = split (/\./, $before_section);
524 if (scalar(@before_section) > 0) {
525 $before_child = pop (@before_section);
526 $parent_section = join (".", @before_section);
527 }
528
529 my $parent_section_ptr = $self->_lookup_section($parent_section);
530 if (!defined $parent_section_ptr) {
531 print STDERR "doc::insert_section couldn't find parent section " .
532 "$parent_section\n";
533 return;
534 }
535
536 # get the next section number
537 my $section_num = $parent_section_ptr->{'next_subsection'}++;
538
539 my $i = 0;
540 while ($i < scalar(@{$parent_section_ptr->{'subsection_order'}}) &&
541 $parent_section_ptr->{'subsection_order'}->[$i] ne $before_child) {
542 $i++;
543 }
544
545 # insert the section number into the order list
546 splice (@{$parent_section_ptr->{'subsection_order'}}, $i, 0, $section_num);
547
548 # add this section to the parent section
549 my $section_ptr = {'subsection_order'=>[],
550 'next_subsection'=>1,
551 'subsections'=>{},
552 'metadata'=>[],
553 'text'=>""};
554 $parent_section_ptr->{'subsections'}->{$section_num} = $section_ptr;
555
556 # work out the full section number
557 my $section = $parent_section;
558 $section .= "." unless $section eq "";
559 $section .= $section_num;
560
561 return $section;
562}
563
564# creates a pre-named section
565sub create_named_section {
566 my $self = shift (@_);
567 my ($mastersection) = @_;
568
569 my ($num);
570 my $section = $mastersection;
571 my $sectionref = $self;
572
573 while ($section ne "") {
574 ($num, $section) = $section =~ /^\.?(\d+)(.*)$/;
575 $num =~ s/^0+(\d)/$1/; # remove leading 0s
576 $section = "" unless defined $section;
577
578 if (defined $num) {
579 if (!defined $sectionref->{'subsections'}->{$num}) {
580 push (@{$sectionref->{'subsection_order'}}, $num);
581 $sectionref->{'subsections'}->{$num} = {'subsection_order'=>[],
582 'next_subsection'=>1,
583 'subsections'=>{},
584 'metadata'=>[],
585 'text'=>""};
586 if ($num >= $sectionref->{'next_subsection'}) {
587 $sectionref->{'next_subsection'} = $num + 1;
588 }
589 }
590 $sectionref = $sectionref->{'subsections'}->{$num};
591
592 } else {
593 print STDERR "doc::create_named_section couldn't create section ";
594 print STDERR "$mastersection\n";
595 last;
596 }
597 }
598}
599
600# returns a reference to a list of subsections
601sub list_subsections {
602 my $self = shift (@_);
603 my ($section) = @_;
604
605 my $section_ptr = $self->_lookup_section ($section);
606 if (!defined $section_ptr) {
607 print STDERR "doc::list_subsections couldn't find section $section\n";
608 return [];
609 }
610
611 return [@{$section_ptr->{'subsection_order'}}];
612}
613
614sub delete_section {
615 my $self = shift (@_);
616 my ($section) = @_;
617
618# my $section_ptr = {'subsection_order'=>[],
619# 'next_subsection'=>1,
620# 'subsections'=>{},
621# 'metadata'=>[],
622# 'text'=>""};
623
624 # if this is the top section reset everything
625 if ($section eq "") {
626 $self->{'subsection_order'} = [];
627 $self->{'subsections'} = {};
628 $self->{'metadata'} = [];
629 $self->{'text'} = "";
630 return;
631 }
632
633 # find the parent of the section to delete
634 my $parent_section = "";
635 my $child = "0";
636 my @section = split (/\./, $section);
637 if (scalar(@section) > 0) {
638 $child = pop (@section);
639 $parent_section = join (".", @section);
640 }
641
642 my $parent_section_ptr = $self->_lookup_section($parent_section);
643 if (!defined $parent_section_ptr) {
644 print STDERR "doc::delete_section couldn't find parent section " .
645 "$parent_section\n";
646 return;
647 }
648
649 # remove this section from the subsection_order list
650 my $i = 0;
651 while ($i < scalar (@{$parent_section_ptr->{'subsection_order'}})) {
652 if ($parent_section_ptr->{'subsection_order'}->[$i] eq $child) {
653 splice (@{$parent_section_ptr->{'subsection_order'}}, $i, 1);
654 last;
655 }
656 $i++;
657 }
658
659 # remove this section from the subsection hash
660 if (defined ($parent_section_ptr->{'subsections'}->{$child})) {
661 undef $parent_section_ptr->{'subsections'}->{$child};
662 }
663}
664
665#--
666# methods for dealing with metadata
667
668# set_metadata_element and get_metadata_element are for metadata
669# which should only have one value. add_meta_data and get_metadata
670# are for metadata which can have more than one value.
671
672# returns the first metadata value which matches field
673
674# This version of get metadata element works much like the one above,
675# except it allows for the namespace portion of a metadata element to
676# be ignored, thus if you are searching for dc.Title, the first piece
677# of matching metadata ending with the name Title (once any namespace
678# is removed) would be returned.
679# 28-11-2003 John Thompson
680sub get_metadata_element {
681 my $self = shift (@_);
682 my ($section, $field, $ignore_namespace) = @_;
683 my ($data);
684
685 $ignore_namespace = 0 unless defined $ignore_namespace;
686
687 my $section_ptr = $self->_lookup_section($section);
688 if (!defined $section_ptr) {
689 print STDERR "doc::get_metadata_element couldn't find section ", $section, "\n";
690 return;
691 }
692
693 # Remove the any namespace if we are being told to ignore them
694 if($ignore_namespace) {
695 $field =~ s/^\w*\.//;
696 }
697
698 foreach $data (@{$section_ptr->{'metadata'}}) {
699
700 my $data_name = $data->[0];
701 # Remove the any namespace if we are being told to ignore them
702 if($ignore_namespace) {
703 $data_name =~ s/^\w*\.//;
704 }
705
706 return $data->[1] if (scalar(@$data) >= 2 && $data_name eq $field);
707 }
708
709 return undef; # was not found
710}
711
712# returns a list of the form [value1, value2, ...]
713sub get_metadata {
714 my $self = shift (@_);
715 my ($section, $field, $ignore_namespace) = @_;
716 my ($data);
717
718 $ignore_namespace = 0 unless defined $ignore_namespace;
719
720 my $section_ptr = $self->_lookup_section($section);
721 if (!defined $section_ptr) {
722 print STDERR "doc::get_metadata couldn't find section ",
723 $section, "\n";
724 return;
725 }
726
727 # Remove the any namespace if we are being told to ignore them
728 if($ignore_namespace) {
729 $field =~ s/^\w*\.//;
730 }
731
732 my @metadata = ();
733 foreach $data (@{$section_ptr->{'metadata'}}) {
734
735 my $data_name = $data->[0];
736 # Remove the any namespace if we are being told to ignore them
737 if($ignore_namespace) {
738 $data_name =~ s/^\w*\.//;
739 }
740
741 push (@metadata, $data->[1]) if ($data_name eq $field);
742 }
743
744 return \@metadata;
745}
746
747# returns a list of the form [[field,value],[field,value],...]
748sub get_all_metadata {
749 my $self = shift (@_);
750 my ($section) = @_;
751
752 my $section_ptr = $self->_lookup_section($section);
753 if (!defined $section_ptr) {
754 print STDERR "doc::get_all_metadata couldn't find section ", $section, "\n";
755 return;
756 }
757
758 return $section_ptr->{'metadata'};
759}
760
761# $value is optional
762sub delete_metadata {
763 my $self = shift (@_);
764 my ($section, $field, $value) = @_;
765
766 my $section_ptr = $self->_lookup_section($section);
767 if (!defined $section_ptr) {
768 print STDERR "doc::delete_metadata couldn't find section ", $section, "\n";
769 return;
770 }
771
772 my $i = 0;
773 while ($i < scalar (@{$section_ptr->{'metadata'}})) {
774 if (($section_ptr->{'metadata'}->[$i]->[0] eq $field) &&
775 (!defined $value || $section_ptr->{'metadata'}->[$i]->[1] eq $value)) {
776 splice (@{$section_ptr->{'metadata'}}, $i, 1);
777 } else {
778 $i++;
779 }
780 }
781}
782
783sub delete_all_metadata {
784 my $self = shift (@_);
785 my ($section) = @_;
786
787 my $section_ptr = $self->_lookup_section($section);
788 if (!defined $section_ptr) {
789 print STDERR "doc::delete_all_metadata couldn't find section ", $section, "\n";
790 return;
791 }
792
793 $section_ptr->{'metadata'} = [];
794}
795
796sub set_metadata_element {
797 my $self = shift (@_);
798 my ($section, $field, $value) = @_;
799
800 $self->set_utf8_metadata_element ($section, $field,
801 &unicode::ascii2utf8(\$value));
802}
803
804# set_utf8_metadata_element assumes the text has already been
805# converted to the UTF-8 encoding.
806sub set_utf8_metadata_element {
807 my $self = shift (@_);
808 my ($section, $field, $value) = @_;
809
810 $self->delete_metadata ($section, $field);
811 $self->add_utf8_metadata ($section, $field, $value);
812}
813
814
815# add_metadata assumes the text is in (extended) ascii form. For
816# text which has already been converted to the UTF-8 format use
817# add_utf8_metadata.
818sub add_metadata {
819 my $self = shift (@_);
820 my ($section, $field, $value) = @_;
821
822 $self->add_utf8_metadata ($section, $field,
823 &unicode::ascii2utf8(\$value));
824}
825
826sub add_utf8_metadata {
827 my $self = shift (@_);
828 my ($section, $field, $value) = @_;
829
830 my $section_ptr = $self->_lookup_section($section);
831 if (!defined $section_ptr) {
832 print STDERR "doc::add_utf8_metadata couldn't find section ", $section, "\n";
833 return;
834 }
835 if (!defined $value) {
836 print STDERR "doc::add_utf8_metadata undefined value for $field\n";
837 return;
838 }
839 if (!defined $field) {
840 print STDERR "doc::add_utf8_metadata undefined metadata type \n";
841 return;
842 }
843
844 #print STDERR "###$field=$value\n";
845 # double check that the value is utf-8
846 if (unicode::ensure_utf8(\$value)) {
847 print STDERR "doc::add_utf8_metadata: warning: '$field' wasn't utf8\n";
848 }
849
850 push (@{$section_ptr->{'metadata'}}, [$field, $value]);
851}
852
853
854# methods for dealing with text
855
856# returns the text for a section
857sub get_text {
858 my $self = shift (@_);
859 my ($section) = @_;
860
861 my $section_ptr = $self->_lookup_section($section);
862 if (!defined $section_ptr) {
863 print STDERR "doc::get_text couldn't find section " .
864 "$section\n";
865 return "";
866 }
867
868 return $section_ptr->{'text'};
869}
870
871# returns the (utf-8 encoded) length of the text for a section
872sub get_text_length {
873 my $self = shift (@_);
874 my ($section) = @_;
875
876 my $section_ptr = $self->_lookup_section($section);
877 if (!defined $section_ptr) {
878 print STDERR "doc::get_text_length couldn't find section " .
879 "$section\n";
880 return 0;
881 }
882
883 return length ($section_ptr->{'text'});
884}
885
886sub delete_text {
887 my $self = shift (@_);
888 my ($section) = @_;
889
890 my $section_ptr = $self->_lookup_section($section);
891 if (!defined $section_ptr) {
892 print STDERR "doc::delete_text couldn't find section " .
893 "$section\n";
894 return;
895 }
896
897 $section_ptr->{'text'} = "";
898}
899
900# add_text assumes the text is in (extended) ascii form. For
901# text which has been already converted to the UTF-8 format
902# use add_utf8_text.
903sub add_text {
904 my $self = shift (@_);
905 my ($section, $text) = @_;
906
907 # convert the text to UTF-8 encoded unicode characters
908 # and add the text
909 $self->add_utf8_text($section, &unicode::ascii2utf8(\$text));
910}
911
912
913# add_utf8_text assumes the text to be added has already
914# been converted to the UTF-8 encoding. For ascii text use
915# add_text
916sub add_utf8_text {
917 my $self = shift (@_);
918 my ($section, $text) = @_;
919
920 my $section_ptr = $self->_lookup_section($section);
921 if (!defined $section_ptr) {
922 print STDERR "doc::add_utf8_text couldn't find section " .
923 "$section\n";
924 return;
925 }
926
927 $section_ptr->{'text'} .= $text;
928}
929
930
931# methods for dealing with associated files
932
933# a file is associated with a document, NOT a section.
934# if section is defined it is noted in the data structure
935# only so that files associated from a particular section
936# may be removed later (using delete_section_assoc_files)
937sub associate_file {
938 my $self = shift (@_);
939 my ($real_filename, $assoc_filename, $mime_type, $section) = @_;
940 $mime_type = &ghtml::guess_mime_type ($real_filename) unless defined $mime_type;
941
942 # remove all associated files with the same name
943 $self->delete_assoc_file ($assoc_filename);
944
945 push (@{$self->{'associated_files'}},
946 [$real_filename, $assoc_filename, $mime_type, $section]);
947}
948
949# returns a list of associated files in the form
950# [[real_filename, assoc_filename, mimetype], ...]
951sub get_assoc_files {
952 my $self = shift (@_);
953
954 return $self->{'associated_files'};
955}
956
957sub delete_section_assoc_files {
958 my $self = shift (@_);
959 my ($section) = @_;
960
961 my $i=0;
962 while ($i < scalar (@{$self->{'associated_files'}})) {
963 if (defined $self->{'associated_files'}->[$i]->[3] &&
964 $self->{'associated_files'}->[$i]->[3] eq $section) {
965 splice (@{$self->{'associated_files'}}, $i, 1);
966 } else {
967 $i++;
968 }
969 }
970}
971
972sub delete_assoc_file {
973 my $self = shift (@_);
974 my ($assoc_filename) = @_;
975
976 my $i=0;
977 while ($i < scalar (@{$self->{'associated_files'}})) {
978 if ($self->{'associated_files'}->[$i]->[1] eq $assoc_filename) {
979 splice (@{$self->{'associated_files'}}, $i, 1);
980 } else {
981 $i++;
982 }
983 }
984}
985
986sub reset_nextsection_ptr {
987 my $self = shift (@_);
988 my ($section) = @_;
989
990 my $section_ptr = $self->_lookup_section($section);
991 $section_ptr->{'next_subsection'} = 1;
992}
993
9941;
Note: See TracBrowser for help on using the repository browser.