X-Git-Url: http://lists.indexdata.dk/cgi-bin?a=blobdiff_plain;f=lib%2FZOOM%2FIRSpy%2FUtils.pm;h=8113bcfe969e82ec87fcc7d3633b5b1d3667d995;hb=49134f41b7d8b37956a8f4f84b2186a737e766fe;hp=8ddbcab09a462af6f6f48db46565620b306105c7;hpb=fcbc80b8d39f95b1625298daa56caee62dfe980c;p=irspy-moved-to-github.git
diff --git a/lib/ZOOM/IRSpy/Utils.pm b/lib/ZOOM/IRSpy/Utils.pm
index 8ddbcab..8113bcf 100644
--- a/lib/ZOOM/IRSpy/Utils.pm
+++ b/lib/ZOOM/IRSpy/Utils.pm
@@ -1,4 +1,4 @@
-# $Id: Utils.pm,v 1.11 2006-11-13 18:03:34 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;
$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/',
@@ -46,6 +73,8 @@ my %_namespaces = (
sub irspy_namespace {
my($prefix) = @_;
+ use Carp;
+ confess "irspy_namespace(undef)" if !defined $prefix;
my $uri = $_namespaces{$prefix};
die "irspy_namespace(): no URI for namespace prefix '$prefix'"
if !defined $uri;
@@ -72,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'";
@@ -86,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")) {
@@ -111,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";
@@ -119,35 +148,50 @@ sub modify_xml_document {
} else {
next if !$value; # No need to create a new empty node
- my($ppath, $element) = $xpath =~ /(.*)\/(.*)/;
- dom_add_element($xc, $ppath, $element, $value, @addAfter);
+ 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;
}
-sub dom_add_element {
- my($xc, $ppath, $element, $value, @addAfter) = @_;
+sub dom_add_node {
+ my($xc, $ppath, $selector, $value, @addAfter) = @_;
- #print "Adding $element='$value' at '$ppath' after (", join(", ", map { "'$_'" } @addAfter), ")
\n";
+ #print "Adding $selector='$value' at '$ppath' after (", join(", ", map { "'$_'" } @addAfter), ")
\n";
my $node = find_or_make_node($xc, $ppath, 0);
- return if !defined $node; ### should be a "can't happen"
+ die "couldn't find or make node '$node'" if !defined $node;
+
+ my $is_attr = ($selector =~ s/^@//);
+ my(undef, $prefix, $simpleSel) = $selector =~ /((.*?):)?(.*)/;
+ #warn "selector='$selector', prefix='$prefix', simpleSel='$simpleSel'";
+ if ($is_attr) {
+ if (defined $prefix) {
+ ### This seems to no-op (thank, DOM!) but I have have no
+ # idea, and it's not needed for IRSpy, so I am not going
+ # to debug it now.
+ $node->setAttributeNS(irspy_namespace($prefix),
+ $simpleSel, $value);
+ } else {
+ $node->setAttribute($simpleSel, $value);
+ }
+ return;
+ }
- my(undef, $prefix, $nsElem) = $element =~ /((.*?):)?(.*)/;
- my $new = new XML::LibXML::Element($nsElem);
+ my $new = new XML::LibXML::Element($simpleSel);
$new->setNamespace(irspy_namespace($prefix), $prefix)
- if $prefix ne "";
+ if defined $prefix;
$new->appendText($value);
foreach my $predecessor (reverse @addAfter) {
my($child) = $xc->findnodes($predecessor, $node);
if (defined $child) {
$node->insertAfter($new, $child);
- #print "Added after '$predecessor'
\n";
+ #warn "Added after '$predecessor'";
return;
}
}
@@ -159,10 +203,10 @@ sub dom_add_element {
my @children = $node->childNodes();
if (@children) {
$node->insertBefore($new, $children[0]);
- #print "Added new first child
\n";
+ #warn "Added new first child";
} else {
$node->appendChild($new);
- #print "Added new only child
\n";
+ #warn "Added new only child";
}
if (0) {
@@ -178,18 +222,24 @@ sub find_or_make_node {
die "deep recursion in find_or_make_node($path)"
if $recursion_level == 10;
+ $path = "." if $path eq "";
my @nodes = $xc->findnodes($path);
if (@nodes == 0) {
# Oh dear, the parent node doesn't exist. We could make it,
- my($ppath, $element) = $path =~ /(.*)\/(.*)/;
- warn "no node '$path': making it";
+ my(undef, $ppath, $element) = $path =~ /((.*)\/)?(.*)/;
+ $ppath = "" if !defined $ppath;
+ #warn "path='$path', ppath='$ppath', element='$element'";
+ #warn "no node '$path': making it";
my $parent = find_or_make_node($xc, $ppath, $recursion_level-1);
my(undef, $prefix, $nsElem) = $element =~ /((.*?):)?(.*)/;
+ #warn "element='$element', prefix='$prefix', nsElem='$nsElem'";
my $new = new XML::LibXML::Element($nsElem);
- $new->setNamespace(irspy_namespace($prefix), $prefix)
- if $prefix ne "";
+ if (defined $prefix) {
+ #warn "setNamespace($prefix)";
+ $new->setNamespace(irspy_namespace($prefix), $prefix);
+ }
$parent->appendChild($new);
return $new;