1 # $Id: Utils.pm,v 1.17 2006-11-29 18:15:10 mike Exp $
3 package ZOOM::IRSpy::Utils;
10 our @EXPORT_OK = qw(isodate
18 use XML::LibXML::XPathContext;
20 our $IRSPY_NS = 'http://indexdata.com/irspy/1.0';
23 # Utility functions follow, exported for use of web UI
27 my($sec, $min, $hour, $mday, $mon, $year) = localtime($time);
28 return sprintf("%04d-%02d-%02dT%02d:%02d:%02d",
29 $year+1900, $mon+1, $mday, $hour, $min, $sec);
33 # I can't -- just can't, can't, can't -- believe that this function
34 # isn't provided by one of the core XML modules. But the evidence all
35 # says that it's not: among other things, XML::Generator and
36 # Template::Plugin both roll their own. So I will do likewise. D'oh!
39 my($text, $fallback) = @_;
41 $text = $fallback if !defined $text;
43 confess "xml_encode(): text and fallback both undefined"
49 $text =~ s/['']/'/g;
50 $text =~ s/[""]/"/g;
55 # Quotes a term for use in a CQL query
59 $term =~ s/([""\\])/\\$1/g;
60 $term = qq["$term"] if $term =~ /\s/;
65 # Makes a CQL query that finds a specified target
67 my($host, $port, $db) = @_;
69 return ("host=" . cql_quote($host) . " and " .
70 "port=" . cql_quote($port) . " and " .
71 "path=" . cql_quote($db));
75 # PRIVATE to irspy_namespace() and irspy_xpath_context()
77 e => 'http://explain.z3950.org/dtd/2.0/',
86 confess "irspy_namespace(undef)" if !defined $prefix;
87 my $uri = $_namespaces{$prefix};
88 die "irspy_namespace(): no URI for namespace prefix '$prefix'"
95 sub irspy_xpath_context {
98 my $xml = ref $record ? $record->render() : $record;
99 my $parser = new XML::LibXML();
100 my $doc = $parser->parse_string($xml);
101 my $root = $doc->getDocumentElement();
102 my $xc = XML::LibXML::XPathContext->new($root);
103 foreach my $prefix (keys %_namespaces) {
104 $xc->registerNs($prefix, $_namespaces{$prefix});
110 sub modify_xml_document {
111 my($xc, $fieldsByKey, $data) = @_;
114 foreach my $key (keys %$data) {
115 my $value = $data->{$key};
116 my $ref = $fieldsByKey->{$key} or die "no field '$key'";
117 my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
118 #print "Considering $key='$value' ($xpath)<br/>\n";
119 my @nodes = $xc->findnodes($xpath);
121 warn scalar(@nodes), " nodes match '$xpath'" if @nodes > 1;
122 my $node = $nodes[0];
124 if ($node->isa("XML::LibXML::Attr")) {
125 if ($value ne $node->getValue()) {
126 $node->setValue($value);
128 #print "Attr $key: '", $node->getValue(), "' -> '$value' ($xpath)<br/>\n";
130 } elsif ($node->isa("XML::LibXML::Element")) {
131 # The contents could be any mixture of text and
132 # comments and maybe even other crud such as processing
133 # instructions. The simplest thing is just to throw it all
134 # away and start again, making a single Text node the
135 # canonical representation. But before we do that,
136 # we'll check whether the element is already
137 # canonical, to determine whether our change is a
140 my @children = $node->childNodes();
141 if (@children == 1) {
142 my $child = $node->firstChild();
143 if (ref $child && ref $child eq "XML::LibXML::Text") {
144 $old = $child->getData();
145 next if $value eq $old;
149 $node->removeChildNodes();
150 my $child = new XML::LibXML::Text($value);
151 $node->appendChild($child);
153 #print "Elem $key: '$old' -> '$value' ($xpath)<br/>\n";
155 warn "unexpected node type $node";
159 next if !$value; # No need to create a new empty node
160 my($ppath, $selector) = $xpath =~ /(.*)\/(.*)/;
161 dom_add_node($xc, $ppath, $selector, $value, @addAfter);
162 #print "New $key ($xpath) = '$value'<br/>\n";
172 my($xc, $ppath, $selector, $value, @addAfter) = @_;
174 #print "Adding $selector='$value' at '$ppath' after (", join(", ", map { "'$_'" } @addAfter), ")<br/>\n";
175 my $node = find_or_make_node($xc, $ppath, 0);
176 die "couldn't find or make node '$node'" if !defined $node;
178 my $is_attr = ($selector =~ s/^@//);
179 my(undef, $prefix, $simpleSel) = $selector =~ /((.*?):)?(.*)/;
180 #warn "selector='$selector', prefix='$prefix', simpleSel='$simpleSel'";
182 if (defined $prefix) {
183 ### This seems to no-op (thank, DOM!) but I have have no
184 # idea, and it's not needed for IRSpy, so I am not going
186 $node->setAttributeNS(irspy_namespace($prefix),
189 $node->setAttribute($simpleSel, $value);
194 my $new = new XML::LibXML::Element($simpleSel);
195 $new->setNamespace(irspy_namespace($prefix), $prefix)
198 $new->appendText($value);
199 foreach my $predecessor (reverse @addAfter) {
200 my($child) = $xc->findnodes($predecessor, $node);
201 if (defined $child) {
202 $node->insertAfter($new, $child);
203 #warn "Added after '$predecessor'";
208 # Didn't find any of the nodes that are supposed to precede the
209 # new one, so we need to insert the new node as the first of the
210 # parent's children. However *sigh* there is no prependChild()
211 # analogous to appendChild(), so we have to go the long way round.
212 my @children = $node->childNodes();
214 $node->insertBefore($new, $children[0]);
215 #warn "Added new first child";
217 $node->appendChild($new);
218 #warn "Added new only child";
222 my $text = xml_encode(inheritance_tree($xc));
223 $text =~ s/\n/<br\/>$&/sg;
224 print "<pre>$text</pre>\n";
229 sub find_or_make_node {
230 my($xc, $path, $recursion_level) = @_;
232 die "deep recursion in find_or_make_node($path)"
233 if $recursion_level == 10;
234 $path = "." if $path eq "";
236 my @nodes = $xc->findnodes($path);
238 # Oh dear, the parent node doesn't exist. We could make it,
239 my(undef, $ppath, $element) = $path =~ /((.*)\/)?(.*)/;
240 $ppath = "" if !defined $ppath;
241 #warn "path='$path', ppath='$ppath', element='$element'";
242 #warn "no node '$path': making it";
243 my $parent = find_or_make_node($xc, $ppath, $recursion_level-1);
245 my(undef, $prefix, $nsElem) = $element =~ /((.*?):)?(.*)/;
246 #warn "element='$element', prefix='$prefix', nsElem='$nsElem'";
247 my $new = new XML::LibXML::Element($nsElem);
248 if (defined $prefix) {
249 #warn "setNamespace($prefix)";
250 $new->setNamespace(irspy_namespace($prefix), $prefix);
253 $parent->appendChild($new);
256 warn scalar(@nodes), " nodes match parent '$path'" if @nodes > 1;
261 sub inheritance_tree {
262 my($type, $level) = @_;
263 $level = 0 if !defined $level;
264 return "Woah! Too deep, man!\n" if $level > 20;
266 $type = ref $type if ref $type;
268 $text = "--> " if $level == 0;
269 $text .= ("\t" x $level) . "$type\n";
270 my @ISA = eval "\@${type}::ISA";
271 foreach my $superclass (@ISA) {
272 $text .= inheritance_tree($superclass, $level+1);
279 #print "Loaded ZOOM::IRSpy::Utils.pm";