X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=lib%2FZOOM%2FIRSpy%2FUtils.pm;h=68e23be370c6c3019361f43155a093c82ab4c3c6;hb=31ab2c939eb9c4bba6c703b733c39f74cc55c541;hp=b0faa225986e202234574c2cb0f3ee5318b1fd18;hpb=f70ec789769b8b066b81d76a00802ba3106aec4c;p=irspy-moved-to-github.git diff --git a/lib/ZOOM/IRSpy/Utils.pm b/lib/ZOOM/IRSpy/Utils.pm index b0faa22..68e23be 100644 --- a/lib/ZOOM/IRSpy/Utils.pm +++ b/lib/ZOOM/IRSpy/Utils.pm @@ -1,4 +1,4 @@ -# $Id: Utils.pm,v 1.12 2006-11-14 14:57:41 mike Exp $ +# $Id: Utils.pm,v 1.15 2006-11-16 17:18:14 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/', @@ -176,10 +203,10 @@ sub dom_add_node { my @children = $node->childNodes(); if (@children) { $node->insertBefore($new, $children[0]); - warn "Added new first child"; + #warn "Added new first child"; } else { $node->appendChild($new); - warn "Added new only child"; + #warn "Added new only child"; } if (0) { @@ -203,7 +230,7 @@ sub find_or_make_node { my(undef, $ppath, $element) = $path =~ /((.*)\/)?(.*)/; $ppath = "" if !defined $ppath; #warn "path='$path', ppath='$ppath', element='$element'"; - warn "no node '$path': making it"; + #warn "no node '$path': making it"; my $parent = find_or_make_node($xc, $ppath, $recursion_level-1); my(undef, $prefix, $nsElem) = $element =~ /((.*?):)?(.*)/;