X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=lib%2FZOOM%2FIRSpy%2FUtils.pm;h=371a7dedeb294f6301dd9be634aa063f75a01ca7;hb=39d725f7d2b64485bae8cd53addb5dfdca87a451;hp=b0faa225986e202234574c2cb0f3ee5318b1fd18;hpb=cbca3efb3361cae1e43b5ebd8fa89bed019b86de;p=irspy-moved-to-github.git
diff --git a/lib/ZOOM/IRSpy/Utils.pm b/lib/ZOOM/IRSpy/Utils.pm
index b0faa22..371a7de 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.20 2006-12-11 13:58:17 sondberg Exp $
package ZOOM::IRSpy::Utils;
@@ -7,10 +7,12 @@ use strict;
use warnings;
use Exporter 'import';
-our @EXPORT_OK = qw(xml_encode
+our @EXPORT_OK = qw(isodate
+ xml_encode
+ cql_quote
+ cql_target
irspy_xpath_context
- modify_xml_document
- inheritance_tree);
+ modify_xml_document);
use XML::LibXML;
use XML::LibXML::XPathContext;
@@ -19,6 +21,14 @@ our $IRSPY_NS = 'http://indexdata.com/irspy/1.0';
# Utility functions follow, exported for use of web UI
+sub isodate {
+ my($time) = @_;
+
+ my($sec, $min, $hour, $mday, $mon, $year) = localtime($time);
+ return sprintf("%04d-%02d-%02dT%02d:%02d:%02d",
+ $year+1900, $mon+1, $mday, $hour, $min, $sec);
+}
+
# 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
@@ -26,16 +36,51 @@ 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, $opts) = @_;
+ if (!defined $opts && ref $fallback) {
+ # The second and third arguments are both optional
+ $opts = $fallback;
+ $fallback = undef;
+ }
+ $opts = {} if !defined $opts;
+
+ $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;
- $text =~ s/['']/'/g;
+ # Internet Explorer can't display ' (!) so don't create it
+ #$text =~ s/['']/'/g;
$text =~ s/[""]/"/g;
+ $text =~ s/ / /g if $opts->{nbsp};
+
return $text;
}
+# 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 +119,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 +133,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 +158,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 +169,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;
}
@@ -176,10 +221,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 +248,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 =~ /((.*?):)?(.*)/;
@@ -240,7 +285,11 @@ sub inheritance_tree {
}
-#print "Loaded ZOOM::IRSpy::Utils.pm";
+# This function is made available in xslt using the register_function call
+sub xslt_strcmp {
+ my ($arg1, $arg2) = @_;
+ return ($arg1->to_literal()) cmp ($arg2->to_literal());
+}
1;