root/main/trunk/greenstone2/perllib/docprint.pm @ 32514

Revision 32514, 4.0 KB (checked in by ak19, 12 months ago)

Also need OUTPUT_NONE as an option when docprint.pm writes out doc.xml

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