X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=lib%2FZOOM%2FIRSpy%2FUtils.pm;h=8113bcfe969e82ec87fcc7d3633b5b1d3667d995;hb=91a7b13bddd4465e0a3c31994368811f7840a507;hp=adbb75ac0ccc2a5536747ca83256e239b76d3806;hpb=6a89a933956445a8752e4e5229b6ebbbf2f36908;p=irspy-moved-to-github.git diff --git a/lib/ZOOM/IRSpy/Utils.pm b/lib/ZOOM/IRSpy/Utils.pm index adbb75a..8113bcf 100644 --- a/lib/ZOOM/IRSpy/Utils.pm +++ b/lib/ZOOM/IRSpy/Utils.pm @@ -1,4 +1,4 @@ -# $Id: Utils.pm,v 1.13 2006-11-14 16:04:36 mike Exp $ +# $Id: Utils.pm,v 1.16 2006-11-29 17:22:00 mike Exp $ package ZOOM::IRSpy::Utils; @@ -8,9 +8,10 @@ use warnings; use Exporter 'import'; our @EXPORT_OK = qw(xml_encode + cql_quote + cql_target irspy_xpath_context - modify_xml_document - inheritance_tree); + modify_xml_document); use XML::LibXML; use XML::LibXML::XPathContext; @@ -26,7 +27,13 @@ our $IRSPY_NS = 'http://indexdata.com/irspy/1.0'; # Template::Plugin both roll their own. So I will do likewise. D'oh! # sub xml_encode { - my ($text) = @_; + my($text, $fallback) = @_; + + $text = $fallback if !defined $text; + use Carp; + confess "xml_encode(): text and fallback both undefined" + if !defined $text; + $text =~ s/&/&/g; $text =~ s//>/g; @@ -36,6 +43,26 @@ sub xml_encode { } +# Quotes a term for use in a CQL query +sub cql_quote { + my($term) = @_; + + $term =~ s/([""\\])/\\$1/g; + $term = qq["$term"] if $term =~ /\s/; + return $term; +} + + +# Makes a CQL query that finds a specified target +sub cql_target { + my($host, $port, $db) = @_; + + return ("host=" . cql_quote($host) . " and " . + "port=" . cql_quote($port) . " and " . + "path=" . cql_quote($db)); +} + + # PRIVATE to irspy_namespace() and irspy_xpath_context() my %_namespaces = ( e => 'http://explain.z3950.org/dtd/2.0/', @@ -74,7 +101,7 @@ sub irspy_xpath_context { sub modify_xml_document { my($xc, $fieldsByKey, $data) = @_; - my $nchanges = 0; + my @changes = (); foreach my $key (keys %$data) { my $value = $data->{$key}; my $ref = $fieldsByKey->{$key} or die "no field '$key'"; @@ -88,7 +115,7 @@ sub modify_xml_document { if ($node->isa("XML::LibXML::Attr")) { if ($value ne $node->getValue()) { $node->setValue($value); - $nchanges++; + push @changes, $ref; #print "Attr $key: '", $node->getValue(), "' -> '$value' ($xpath)
\n"; } } elsif ($node->isa("XML::LibXML::Element")) { @@ -113,7 +140,7 @@ sub modify_xml_document { $node->removeChildNodes(); my $child = new XML::LibXML::Text($value); $node->appendChild($child); - $nchanges++; + push @changes, $ref; #print "Elem $key: '$old' -> '$value' ($xpath)
\n"; } else { warn "unexpected node type $node"; @@ -124,11 +151,11 @@ sub modify_xml_document { my($ppath, $selector) = $xpath =~ /(.*)\/(.*)/; dom_add_node($xc, $ppath, $selector, $value, @addAfter); #print "New $key ($xpath) = '$value'
\n"; - $nchanges++; + push @changes, $ref; } } - return $nchanges; + return @changes; }