[782] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[13170] | 3 | # docprint.pm --
|
---|
[782] | 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 | #
|
---|
[13170] | 8 | # Copyright (C) 2006 New Zealand Digital Library Project
|
---|
[782] | 9 | #
|
---|
| 10 | # This program is free software; you can redistribute 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 |
|
---|
[13170] | 26 | # This is used to output an XML representation of a doc_obj - this will be
|
---|
[17747] | 27 | # Greenstone XML format.
|
---|
| 28 | # This is used by GreenstoneXMLPlugout and doc.pm
|
---|
[32536] | 29 | # It's also used by GreenstoneSQLPlugout to output a doc_obj's meta and/or
|
---|
| 30 | # fulltxt to an SQL db, while subroutine unescape_text() is used by
|
---|
| 31 | # GreenstoneSQLPlugin for unescaping txt read back in.
|
---|
[782] | 32 |
|
---|
| 33 | package docprint;
|
---|
| 34 |
|
---|
[32514] | 35 | use constant OUTPUT_NONE => 0;
|
---|
[32512] | 36 | use constant OUTPUT_META_ONLY => 1;
|
---|
| 37 | use constant OUTPUT_TEXT_ONLY => 2;
|
---|
| 38 | use constant OUTPUT_ALL => 3;
|
---|
| 39 |
|
---|
[13170] | 40 | use strict;
|
---|
[782] | 41 |
|
---|
[13170] | 42 | sub get_section_xml {
|
---|
[32512] | 43 | return &get_section_xml_from_root(@_);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | sub get_section_xml_from_root {
|
---|
| 47 | my ($doc_obj, $options) = @_;
|
---|
[32514] | 48 | return &recursive_get_section_xml($doc_obj, $doc_obj->get_top_section(), $options);
|
---|
[32512] | 49 | }
|
---|
| 50 |
|
---|
| 51 | sub recursive_get_section_xml {
|
---|
| 52 | my ($doc_obj, $section, $options) = @_;
|
---|
[13170] | 53 |
|
---|
[32514] | 54 | # 'output' can be OUTPUT_ALL|OUTPUT_META_ONLY|OUTPUT_TEXT_ONLY|OUTPUT_NONE
|
---|
[32512] | 55 | # If not provided, it defaults to OUTPUT_ALL.
|
---|
| 56 | # If OUTPUT_ALL, the metadata and full text both go into doc.xml
|
---|
| 57 | # If OUTPUT_META_ONLY, the metadata goes into doc.xml and full text goes elsewhere (mysql db).
|
---|
| 58 | # If OUTPUT_TEXT_ONLY, the full text goes into doc.xml and metadata goes elsewhere (mysql db).
|
---|
[32514] | 59 | # If OUTPUT_NONE, the full text and metadata goes elsewhere (mysql db)
|
---|
| 60 | # In the last 3 cases, an XML comment is left behind as a 'breadcrumb' to indicate
|
---|
| 61 | # that the "missing" doc information is stored elsewhere.
|
---|
[32512] | 62 | if(!defined $options) {
|
---|
[32514] | 63 | $options = { 'output' => OUTPUT_ALL };
|
---|
[32512] | 64 | }
|
---|
| 65 |
|
---|
[13170] | 66 | my $section_ptr = $doc_obj->_lookup_section ($section);
|
---|
| 67 | return "" unless defined $section_ptr;
|
---|
[782] | 68 |
|
---|
[13170] | 69 | my $all_text = "<Section>\n";
|
---|
| 70 | $all_text .= " <Description>\n";
|
---|
[32512] | 71 |
|
---|
[32518] | 72 | # scalar comparisons on a constant is allowed (but not string evaluation of scalars)
|
---|
| 73 | # https://www.perlmonks.org/?node_id=559456
|
---|
[32512] | 74 | if($options->{'output'} == OUTPUT_ALL || $options->{'output'} == OUTPUT_META_ONLY) {
|
---|
| 75 | # output metadata
|
---|
| 76 | foreach my $data (@{$section_ptr->{'metadata'}}) {
|
---|
| 77 | my $escaped_value = &escape_text($data->[1]);
|
---|
| 78 | $all_text .= ' <Metadata name="' . $data->[0] . '">' . $escaped_value . "</Metadata>\n";
|
---|
| 79 | }
|
---|
| 80 | } else {
|
---|
| 81 | $all_text .= "<!-- metadata is stored elsewhere (MySQL database) -->\n";
|
---|
[13170] | 82 | }
|
---|
[782] | 83 |
|
---|
[13170] | 84 | $all_text .= " </Description>\n";
|
---|
[782] | 85 |
|
---|
[13170] | 86 | # output the text
|
---|
| 87 | $all_text .= " <Content>";
|
---|
[32512] | 88 | if($options->{'output'} == OUTPUT_ALL || $options->{'output'} == OUTPUT_TEXT_ONLY) {
|
---|
| 89 | $all_text .= &escape_text($section_ptr->{'text'});
|
---|
| 90 | } else {
|
---|
| 91 | $all_text .= "<!-- full text is stored elsewhere (MySQL database) -->\n";
|
---|
| 92 | }
|
---|
[13170] | 93 | $all_text .= "</Content>\n";
|
---|
| 94 |
|
---|
| 95 | # output all the subsections
|
---|
| 96 | foreach my $subsection (@{$section_ptr->{'subsection_order'}}) {
|
---|
[32532] | 97 | $all_text .= &recursive_get_section_xml($doc_obj, "$section.$subsection", $options);
|
---|
[782] | 98 | }
|
---|
| 99 |
|
---|
[13170] | 100 | $all_text .= "</Section>\n";
|
---|
| 101 |
|
---|
| 102 | # make sure no nasty control characters have snuck through
|
---|
| 103 | # (XML::Parser will barf on anything it doesn't consider to be
|
---|
| 104 | # valid UTF-8 text, including things like \c@, \cC etc.)
|
---|
| 105 | $all_text =~ s/[\x00-\x09\x0B\x0C\x0E-\x1F]//g;
|
---|
| 106 |
|
---|
| 107 | return $all_text;
|
---|
[782] | 108 | }
|
---|
| 109 |
|
---|
[13170] | 110 | sub escape_text {
|
---|
| 111 | my ($text) = @_;
|
---|
| 112 | # special characters in the xml encoding
|
---|
[19214] | 113 | $text =~ s/&&/& &/g;
|
---|
[13170] | 114 | $text =~ s/&/&/g; # this has to be first...
|
---|
| 115 | $text =~ s/</</g;
|
---|
| 116 | $text =~ s/>/>/g;
|
---|
| 117 | $text =~ s/\"/"/g;
|
---|
[782] | 118 |
|
---|
[13170] | 119 | return $text;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[32536] | 122 | # used by GreenstoneSQLPlugin when reading back from sqldb
|
---|
| 123 | sub unescape_text {
|
---|
| 124 | my ($text) = @_;
|
---|
| 125 | # special characters in the xml encoding
|
---|
| 126 | $text =~ s/& &/&&/g;
|
---|
| 127 | $text =~ s/&/&/g; # this has to be first...
|
---|
| 128 | $text =~ s/</</g;
|
---|
| 129 | $text =~ s/>/>/g;
|
---|
| 130 | $text =~ s/"/"/g;
|
---|
| 131 |
|
---|
| 132 | return $text;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[782] | 135 | 1;
|
---|