Add and use xml_encode()
authorMike Taylor <mike@indexdata.com>
Mon, 25 Sep 2006 12:39:34 +0000 (12:39 +0000)
committerMike Taylor <mike@indexdata.com>
Mon, 25 Sep 2006 12:39:34 +0000 (12:39 +0000)
Unbelievable.

lib/ZOOM/IRSpy/Record.pm

index 165f62e..081e19d 100644 (file)
@@ -1,4 +1,4 @@
-# $Id: Record.pm,v 1.10 2006-07-25 16:53:28 mike Exp $
+# $Id: Record.pm,v 1.11 2006-09-25 12:39:34 mike Exp $
 
 package ZOOM::IRSpy::Record;
 
 
 package ZOOM::IRSpy::Record;
 
@@ -47,18 +47,37 @@ sub _empty_zeerex_record {
     ### Doesn't recognise SRU/SRW URLs
     my($host, $port, $db) = ZOOM::IRSpy::_parse_target_string($target);
 
     ### Doesn't recognise SRU/SRW URLs
     my($host, $port, $db) = ZOOM::IRSpy::_parse_target_string($target);
 
+    my $xhost = xml_encode($host);
+    my $xport = xml_encode($port);
+    my $xdb = xml_encode($db);
     return <<__EOT__;
 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
  <serverInfo protocol="Z39.50" version="1995">
     return <<__EOT__;
 <explain xmlns="http://explain.z3950.org/dtd/2.0/">
  <serverInfo protocol="Z39.50" version="1995">
-  <host>$host</host>
-  <port>$port</port>
-  <database>$db</database>
+  <host>$xhost</host>
+  <port>$xport</port>
+  <database>$xdb</database>
  </serverInfo>
 </explain>
 __EOT__
 }
 
 
  </serverInfo>
 </explain>
 __EOT__
 }
 
 
+# I can't -- just can't, can't, can't -- believe that this function
+# isn't provided by one of the core XML modules.  But the evidence all
+# says that it's not: among other things, XML::Generator and
+# Template::Plugin both roll their own.  So I will do likewise.  D'oh!
+#
+sub xml_encode {
+    my ($text) = @_;
+    $text =~ s/&/&amp;/g;
+    $text =~ s/</&lt;/g;
+    $text =~ s/>/&gt;/g;
+    $text =~ s/['']/&apos;/g;
+    $text =~ s/[""]/&quot;/g;
+    return $text;
+}
+
+
 sub append_entry {
     my $this = shift();
     my($xpath, $frag) = @_;
 sub append_entry {
     my $this = shift();
     my($xpath, $frag) = @_;