Record keeps track of its parent IRSpy object, and uses $irspy->log()
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Record.pm
index 2f67dc1..1ab5033 100644 (file)
@@ -1,4 +1,4 @@
-# $Id: Record.pm,v 1.8 2006-07-24 16:44:00 mike Exp $
+# $Id: Record.pm,v 1.13 2006-09-26 09:08:09 mike Exp $
 
 package ZOOM::IRSpy::Record;
 
@@ -6,6 +6,9 @@ use 5.008;
 use strict;
 use warnings;
 
+use Exporter 'import';
+our @EXPORT_OK = qw(xml_encode);
+
 use XML::LibXML;
 use XML::LibXML::XPathContext;
 
@@ -26,7 +29,7 @@ I<## To follow>
 
 sub new {
     my $class = shift();
-    my($target, $zeerex) = @_;
+    my($irspy, $target, $zeerex) = @_;
 
     if (!defined $zeerex) {
        $zeerex = _empty_zeerex_record($target);
@@ -34,6 +37,7 @@ sub new {
 
     my $parser = new XML::LibXML();
     return bless {
+       irspy => $irspy,
        target => $target,
        parser => $parser,
        zeerex => $parser->parse_string($zeerex)->documentElement(),
@@ -47,23 +51,42 @@ sub _empty_zeerex_record {
     ### 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">
-  <host>$host</host>
-  <port>$port</port>
-  <database>$db</database>
+  <host>$xhost</host>
+  <port>$xport</port>
+  <database>$xdb</database>
  </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) = @_;
 
-    print STDERR "this=$this, xpath='$xpath', frag='$frag'\n";
+    #print STDERR "this=$this, xpath='$xpath', frag='$frag'\n";
     my $root = $this->{zeerex}; # XML::LibXML::Element ISA XML::LibXML::Node
     my $xc = XML::LibXML::XPathContext->new($root);
     $xc->registerNs(zeerex => "http://explain.z3950.org/dtd/2.0/");
@@ -82,14 +105,11 @@ sub append_entry {
            if @nodes == 0;
     }
 
-    ZOOM::Log::log("irspy",
-                  scalar(@nodes), " matches for '$xpath': using first")
+    $this->{irspy}->log("irspy",
+                       scalar(@nodes), " matches for '$xpath': using first")
        if @nodes > 1;
 
-    my $node = $nodes[0];
-    # $node ISA XML::LibXML::ElementXML::LibXML::Element
-    $this->_half_decent_appendWellBalancedChunk($node, $frag);
-    #print STDERR "POST: zeerex='$root' = \n", $root->toString(), "\n";
+    $this->_half_decent_appendWellBalancedChunk($nodes[0], $frag);
 }
 
 
@@ -140,7 +160,7 @@ sub _half_decent_appendWellBalancedChunk {
     die "mismatched XML start/end <$open>...<$close>"
        if $close ne $tag;
     print STDERR "tag='$tag', attrs=[$attrs], content='$content'\n";
-    die "### no code yet to make DOM node";
+    die "## no code yet to make DOM node";
 }