| 1 | # $Id: XML.pm,v 1.26 2009-03-05 17:20:16 mike Exp $ | ||
| 2 | |||
| 3 | package Net::Z3950::DBIServer::XML; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | Net::Z3950::DBIServer::XML - build XML records for DBIServer | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | $rec = Net::Z3950::DBIServer::XML::format( | ||
| 13 | { title=>'Mr', forename=>'Eric', surname=>'1/2 Bee' }, | ||
| 14 | $config); | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | This non-OO module exists only to provide a single function which | ||
| 19 | formats a set of fields as an XML record. | ||
| 20 | |||
| 21 | =head1 METHODS | ||
| 22 | |||
| 23 | =head2 format() | ||
| 24 | |||
| 25 | $rec = Net::Z3950::DBIServer::XML::format($hashref, $config); | ||
| 26 | |||
| 27 | Creates and returns, as an unblessed string, a new XML record | ||
| 28 | containing the specified fields according to the configuration | ||
| 29 | specified in the database-and-record-syntax-specific configuration | ||
| 30 | segment I<$config>, of type C<Net::Z3950::DBIServer::Config::XMLSpec>. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | ### Should we use a proper XML-writing module from CPAN? | ||
| 35 | sub format { | ||
| 36 | my($hashref, $config) = @_; | ||
| 37 | |||
| 38 | my $type = $config->recordName(); | ||
| 39 | my $attrs = $config->recordAttrs(); | ||
| 40 | my $schema = $config->recordSchema(); | ||
| 41 | if (defined $attrs) { | ||
| 42 | $attrs = " $attrs"; | ||
| 43 | } else { | ||
| 44 | $attrs = ""; | ||
| 45 | } | ||
| 46 | my $rec = ''; | ||
| 47 | foreach my $field ($config->fields()) { | ||
| 48 | my $fieldName = $field->tagname(); | ||
| 49 | my $sqlField = $field->columnName(); | ||
| 50 | my $data = make_data($sqlField, $hashref); | ||
| 51 | next if !defined $data || $data eq ""; | ||
| 52 | $rec .= " <$fieldName>" . _quote($data) . "</$fieldName>\n"; | ||
| 53 | } | ||
| 54 | |||
| 55 | return (_maybe_transform($config, "<$type$attrs>\n$rec</$type>\n"), $schema); | ||
| 56 | } | ||
| 57 | |||
| 58 | |||
| 59 | # SHARED with ...::MARC.pm and ...::GRS1.pm | ||
| 60 | sub make_data { | ||
| 61 | my($spec, $hashref) = @_; | ||
| 62 | |||
| 63 | if ($spec =~ s/^\*//) { | ||
| 64 | return $spec; | ||
| 65 | } | ||
| 66 | |||
| 67 | if ($spec !~ /%/) { | ||
| 68 | my $allow_skip = ($spec =~ s/^\?//); | ||
| 69 | my $data = $hashref->{$spec}; | ||
| 70 | if (defined $data) { | ||
| 71 | return $data; | ||
| 72 | } else { | ||
| 73 | if (0 && !$allow_skip) { | ||
| 74 | warn("Looking for field '$spec' but all we have is:\n" . | ||
| 75 | join("\n", map { my $v = $hashref->{$_}; | ||
| 76 | defined $v ? "\t$_ -> '$v'" : "\t$_ undefined" } | ||
| 77 | sort keys %$hashref)); | ||
| 78 | die new Net::Z3950::DBIServer::Exception(100, | ||
| 79 | "misconfiguration: top-level field '$spec' not defined"); | ||
| 80 | } | ||
| 81 | return undef; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | my $res = ""; | ||
| 86 | while ($spec =~ s/(.*?)%\{(.*?)\}//s) { | ||
| 87 | my($prefix, $fieldname) = ($1, $2); | ||
| 88 | my $allow_skip = ($fieldname =~ s/^\?//); | ||
| 89 | my $data = $hashref->{$fieldname}; | ||
| 90 | if (defined $data) { | ||
| 91 | $res .= $prefix . $data; | ||
| 92 | } else { | ||
| 93 | die new Net::Z3950::DBIServer::Exception(100, | ||
| 94 | "misconfiguration: embedded field '$fieldname' not defined") | ||
| 95 | if !$allow_skip; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | return $res . $spec; | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | # PRIVATE to format() | ||
| 104 | sub _quote { | ||
| 105 | my($s) = @_; | ||
| 106 | |||
| 107 | return '' if !defined $s; | ||
| 108 | $s =~ s/&/&/g; | ||
| 109 | $s =~ s/</</g; | ||
| 110 | $s =~ s/>/>/g; | ||
| 111 | |||
| 112 | return $s | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | # PRIVATE to format() | ||
| 117 | sub _maybe_transform { | ||
| 118 | my($config, $text) = @_; | ||
| 119 | |||
| 120 | my $xslFile = $config->recordTransform(); | ||
| 121 | return $text if !defined $xslFile; | ||
| 122 | |||
| 123 | eval { | ||
| 124 | # Don't require these until we need them; that way, | ||
| 125 | # applications that don't need XSL transformations don't | ||
| 126 | # require their developers to install all the supportXML stuff. | ||
| 127 | require XML::LibXML; | ||
| 128 | require XML::LibXSLT; | ||
| 129 | }; if ($@) { | ||
| 130 | # System error in presenting records | ||
| 131 | die new Net::Z3950::DBIServer::Exception(14, <<__EOT__ | ||
| 132 | Oops. It looks like this zSQLgate installation doesn't have XSL | ||
| 133 | support. Contact the server providers and let them know! They'll | ||
| 134 | find what they need at http://sql.z3950.org/xslt | ||
| 135 | |||
| 136 | $@ | ||
| 137 | __EOT__ | ||
| 138 | ); | ||
| 139 | } | ||
| 140 | |||
| 141 | my $parser = new XML::LibXML(); | ||
| 142 | my $xslt = new XML::LibXSLT(); | ||
| 143 | |||
| 144 | my($doc, $style_doc); | ||
| 145 | eval { | ||
| 146 | #warn "about to parse result document"; | ||
| 147 | $doc = $parser->parse_string($text); | ||
| 148 | #warn "about to parse stylesheet document"; | ||
| 149 | $style_doc = $parser->parse_file($xslFile); | ||
| 150 | #warn "parsed both documents"; | ||
| 151 | }; if ($@) { | ||
| 152 | die new Net::Z3950::DBIServer::Exception(14, $@); | ||
| 153 | } | ||
| 154 | |||
| 155 | #warn "about to compile stylesheet"; | ||
| 156 | my $stylesheet; | ||
| 157 | eval { | ||
| 158 | $stylesheet = $xslt->parse_stylesheet($style_doc); | ||
| 159 | }; if ($@) { | ||
| 160 | # By inspection of the XML/LibXSLT.pm code, it looks like this | ||
| 161 | # function can never call die(), despite what the manual says | ||
| 162 | # unless the underlying XS code, _parse_stylesheet() somehow | ||
| 163 | # apes it. Which is a shame for us, but then nearly all XSLT | ||
| 164 | # errors are caught by the earlier call to parse_file() | ||
| 165 | # anyway. However, parse_stylesheet() _does_ emit some | ||
| 166 | # warning messages on stderr, and there doesn't, seem to be a | ||
| 167 | # sensible way to catch them. Things like: | ||
| 168 | # compilation error: file default.xsl element value-of | ||
| 169 | # xsltParseStylesheetTop: ignoring unknown value-of element | ||
| 170 | # compilation error: file default.xsl element text | ||
| 171 | # misplaced text element: 'x' | ||
| 172 | # Best just to keep an eye on zSQLgate's error-log, then. | ||
| 173 | warn "Aha! And I thought parse_stylesheet() could never die! $@"; | ||
| 174 | die new Net::Z3950::DBIServer::Exception(14, $@); | ||
| 175 | } elsif (!defined $stylesheet) { | ||
| 176 | # Again, error messages are produced on standard error, | ||
| 177 | # e.g. "document is not a stylesheet", but there is no | ||
| 178 | # sensible way to catch them. | ||
| 179 | die new Net::Z3950::DBIServer::Exception(14, | ||
| 180 | "Document may be well-formed XML that is not a stylesheet?"); | ||
| 181 | } | ||
| 182 | |||
| 183 | #warn "about to transform"; | ||
| 184 | my $result; | ||
| 185 | eval { | ||
| 186 | $result = $stylesheet->transform($doc); | ||
| 187 | }; if ($@) { | ||
| 188 | # I've not been able to find an error that provokes | ||
| 189 | # $stylesheet->transform() die, and by brief reading of | ||
| 190 | # LibXSLT.xs suggests that only incorrect invocation (e.g. an | ||
| 191 | # odd number of additional parameters) will do so. But this | ||
| 192 | # handler is here out of paranoia. | ||
| 193 | warn "Aha! And I thought transform() could never die! $@"; | ||
| 194 | die new Net::Z3950::DBIServer::Exception(14, $@); | ||
| 195 | } | ||
| 196 | |||
| 197 | #warn "done transform: returning"; | ||
| 198 | return $stylesheet->output_string($result); | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | =head1 AUTHOR | ||
| 203 | |||
| 204 | Mike Taylor E<lt>mike@miketaylor.org.ukE<gt> | ||
| 205 | |||
| 206 | First version Sunday 24th February 2002. | ||
| 207 | |||
| 208 | =head1 SEE ALSO | ||
| 209 | |||
| 210 | C<Net::Z3950::DBIServer> | ||
| 211 | is the module that uses this. | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | |||
| 216 | 1; |