[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
|
---|
[782] | 29 |
|
---|
| 30 | package docprint;
|
---|
| 31 |
|
---|
[32512] | 32 | use constant OUTPUT_META_ONLY => 1;
|
---|
| 33 | use constant OUTPUT_TEXT_ONLY => 2;
|
---|
| 34 | use constant OUTPUT_ALL => 3;
|
---|
| 35 |
|
---|
[13170] | 36 | use strict;
|
---|
[782] | 37 |
|
---|
[13170] | 38 | sub get_section_xml {
|
---|
[32512] | 39 | return &get_section_xml_from_root(@_);
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | sub get_section_xml_from_root {
|
---|
| 43 | my ($doc_obj, $options) = @_;
|
---|
| 44 | return &recursive_get_section_xml($doc_obj,$doc_obj->get_top_section(), $options);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | sub recursive_get_section_xml {
|
---|
| 48 | my ($doc_obj, $section, $options) = @_;
|
---|
[13170] | 49 |
|
---|
[32512] | 50 | # 'output' can be OUTPUT_ALL|OUTPUT_META_ONLY|OUTPUT_TEXT_ONLY
|
---|
| 51 | # If not provided, it defaults to OUTPUT_ALL.
|
---|
| 52 | # If OUTPUT_ALL, the metadata and full text both go into doc.xml
|
---|
| 53 | # If OUTPUT_META_ONLY, the metadata goes into doc.xml and full text goes elsewhere (mysql db).
|
---|
| 54 | # If OUTPUT_TEXT_ONLY, the full text goes into doc.xml and metadata goes elsewhere (mysql db).
|
---|
| 55 | # In the last two cases, an XML comment is left behind to indicate that the "missing" doc
|
---|
| 56 | # information is stored elsewhere.
|
---|
| 57 | if(!defined $options) {
|
---|
| 58 | $options = {'output' => OUTPUT_ALL };
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[13170] | 61 | my $section_ptr = $doc_obj->_lookup_section ($section);
|
---|
| 62 | return "" unless defined $section_ptr;
|
---|
[782] | 63 |
|
---|
[13170] | 64 | my $all_text = "<Section>\n";
|
---|
| 65 | $all_text .= " <Description>\n";
|
---|
[32512] | 66 |
|
---|
| 67 | if($options->{'output'} == OUTPUT_ALL || $options->{'output'} == OUTPUT_META_ONLY) {
|
---|
| 68 | # output metadata
|
---|
| 69 | foreach my $data (@{$section_ptr->{'metadata'}}) {
|
---|
| 70 | my $escaped_value = &escape_text($data->[1]);
|
---|
| 71 | $all_text .= ' <Metadata name="' . $data->[0] . '">' . $escaped_value . "</Metadata>\n";
|
---|
| 72 | }
|
---|
| 73 | } else {
|
---|
| 74 | $all_text .= "<!-- metadata is stored elsewhere (MySQL database) -->\n";
|
---|
[13170] | 75 | }
|
---|
[782] | 76 |
|
---|
[13170] | 77 | $all_text .= " </Description>\n";
|
---|
[782] | 78 |
|
---|
[13170] | 79 | # output the text
|
---|
| 80 | $all_text .= " <Content>";
|
---|
[32512] | 81 | if($options->{'output'} == OUTPUT_ALL || $options->{'output'} == OUTPUT_TEXT_ONLY) {
|
---|
| 82 | $all_text .= &escape_text($section_ptr->{'text'});
|
---|
| 83 | } else {
|
---|
| 84 | $all_text .= "<!-- full text is stored elsewhere (MySQL database) -->\n";
|
---|
| 85 | }
|
---|
[13170] | 86 | $all_text .= "</Content>\n";
|
---|
| 87 |
|
---|
| 88 | # output all the subsections
|
---|
| 89 | foreach my $subsection (@{$section_ptr->{'subsection_order'}}) {
|
---|
[32512] | 90 | $all_text .= &recursive_get_section_xml($doc_obj, "$section.$subsection");
|
---|
[782] | 91 | }
|
---|
| 92 |
|
---|
[13170] | 93 | $all_text .= "</Section>\n";
|
---|
| 94 |
|
---|
| 95 | # make sure no nasty control characters have snuck through
|
---|
| 96 | # (XML::Parser will barf on anything it doesn't consider to be
|
---|
| 97 | # valid UTF-8 text, including things like \c@, \cC etc.)
|
---|
| 98 | $all_text =~ s/[\x00-\x09\x0B\x0C\x0E-\x1F]//g;
|
---|
| 99 |
|
---|
| 100 | return $all_text;
|
---|
[782] | 101 | }
|
---|
| 102 |
|
---|
[13170] | 103 | sub escape_text {
|
---|
| 104 | my ($text) = @_;
|
---|
| 105 | # special characters in the xml encoding
|
---|
[19214] | 106 | $text =~ s/&&/& &/g;
|
---|
[13170] | 107 | $text =~ s/&/&/g; # this has to be first...
|
---|
| 108 | $text =~ s/</</g;
|
---|
| 109 | $text =~ s/>/>/g;
|
---|
| 110 | $text =~ s/\"/"/g;
|
---|
[782] | 111 |
|
---|
[13170] | 112 | return $text;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[782] | 115 | 1;
|
---|