source: gsdl/trunk/perllib/metadatautil.pm@ 15004

Last change on this file since 15004 was 15004, checked in by davidb, 14 years ago

In response to developing a custom plugin that stores metadata similar to MetadataXMLPlug, some functionality was shifted around so the custom plugin could also access it, and a couple of edits made so the two worked properly side by side.

  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1###########################################################################
2#
3# metadatautil.pm -- various useful utilities for dealing with metadata
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) 2006 New Zealand Digital Library Project
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
26package metadatautil;
27
28use strict;
29
30sub combine_metadata_structures {
31 my ($mdref1, $mdref2) = @_;
32 my ($key, $value1, $value2);
33
34 foreach $key (keys %$mdref2) {
35
36 $value1 = $mdref1->{$key};
37 $value2 = $mdref2->{$key};
38
39 # If there is no existing value for this metadata field in
40 # $mdref1, so we simply copy the value from $mdref2 over.
41 if (!defined $value1) {
42 $mdref1->{$key} = &clonedata($value2);
43 }
44 # Otherwise we have to add the new values to the existing ones.
45 # If the second structure is accumulated, then acculate all the
46 # values into the first structure
47 elsif ((ref $value2) eq "ARRAY") {
48 # If the first metadata element is a scalar we have to
49 # convert it into an array before we add anything more.
50 if ((ref $value1) ne 'ARRAY') {
51 $mdref1->{$key} = [$value1];
52 $value1 = $mdref1->{$key};
53 }
54 # Now add the value(s) from the second array to the first
55 $value2 = &clonedata($value2);
56 push @$value1, @$value2;
57 }
58 # Finally, If the second structure is not an array erference, we
59 # know it is in override mode, so override the first structure.
60 else {
61 $mdref1->{$key} = &clonedata($value2);
62 }
63 }
64}
65
66
67# Make a "cloned" copy of a metadata value.
68# This is trivial for a simple scalar value,
69# but not for an array reference.
70
71sub clonedata {
72 my ($value) = @_;
73 my $result;
74
75 if ((ref $value) eq 'ARRAY') {
76 $result = [];
77 foreach my $item (@$value) {
78 push @$result, $item;
79 }
80 } else {
81 $result = $value;
82 }
83 return $result;
84}
85
86sub format_metadata_as_table {
87 my ($metadata, $remove_namespace) = @_;
88
89 my $text = "<table cellpadding=\"4\" cellspacing=\"0\">\n";
90
91 foreach my $field (sort keys(%$metadata)) {
92 # $metadata->{$field} may be an array reference
93 if ($field eq "gsdlassocfile_tobe") {
94 # ignore
95 } else {
96 my $no_ns = $field;
97 if ($remove_namespace) {
98 $no_ns =~ s/^\w+\.//;
99 }
100 if (ref ($metadata->{$field}) eq "ARRAY") {
101 map {
102 $text .= "<tr><td valign=top><nobr><b>$no_ns</b></nobr></td><td>".$_."</td></tr>";
103 } @{$metadata->{$field}};
104 } else {
105 $text .= "<tr><td valign=top><nobr><b>$no_ns</b></nobr></td><td valign=top>$metadata->{$field}</td></tr>\n";
106 }
107 }
108
109 }
110 $text .= "</table>\n";
111 return $text;
112}
113
114
115sub store_saved_metadata
116{
117 my ($plug,$mname,$mvalue,$md_accumulate) = @_;
118
119 if (defined $plug->{'saved_metadata'}->{$mname}) {
120 if ($md_accumulate) {
121 # accumulate mode - add value to existing value(s)
122 if (ref ($plug->{'saved_metadata'}->{$mname}) eq "ARRAY") {
123 push (@{$plug->{'saved_metadata'}->{$mname}}, $mvalue);
124 } else {
125 $plug->{'saved_metadata'}->{$mname} =
126 [$plug->{'saved_metadata'}->{$mname}, $mvalue];
127 }
128 } else {
129 # override mode
130 $plug->{'saved_metadata'}->{$mname} = $mvalue;
131 }
132 } else {
133 if ($md_accumulate) {
134 # accumulate mode - add value into (currently empty) array
135 $plug->{'saved_metadata'}->{$mname} = [$mvalue];
136 } else {
137 # override mode
138 $plug->{'saved_metadata'}->{$mname} = $mvalue;
139 }
140 }
141}
142
143
1441;
Note: See TracBrowser for help on using the repository browser.