Move xml_encode() from IRSpy to Utils
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Utils.pm
1 # $Id: Utils.pm,v 1.2 2006-10-30 16:13:49 mike Exp $
2
3 package ZOOM::IRSpy::Utils;
4
5 use 5.008;
6 use strict;
7 use warnings;
8
9 use Exporter 'import';
10 our @EXPORT_OK = qw(xml_encode irspy_xpath_context);
11
12
13 # Utility functions follow, exported for use of web UI
14
15 # I can't -- just can't, can't, can't -- believe that this function
16 # isn't provided by one of the core XML modules.  But the evidence all
17 # says that it's not: among other things, XML::Generator and
18 # Template::Plugin both roll their own.  So I will do likewise.  D'oh!
19 #
20 sub xml_encode {
21     my ($text) = @_;
22     $text =~ s/&/&/g;
23     $text =~ s/</&lt;/g;
24     $text =~ s/>/&gt;/g;
25     $text =~ s/['']/&apos;/g;
26     $text =~ s/[""]/&quot;/g;
27     return $text;
28 }
29
30
31 sub irspy_xpath_context {
32     my($zoom_record) = @_;
33
34     my $xml = $zoom_record->render();
35     my $parser = new XML::LibXML();
36     my $doc = $parser->parse_string($xml);
37     my $root = $doc->getDocumentElement();
38     my $xc = XML::LibXML::XPathContext->new($root);
39     $xc->registerNs(e => 'http://explain.z3950.org/dtd/2.0/');
40     $xc->registerNs(i => $ZOOM::IRSpy::irspy_ns);
41     return $xc;
42 }
43
44
45 1;