Remove redundant _string2cdata() method, use xml_encode() instead.
[irspy-moved-to-github.git] / lib / ZOOM / IRSpy / Utils.pm
1 # $Id: Utils.pm,v 1.17 2006-11-29 18:15:10 mike Exp $
2
3 package ZOOM::IRSpy::Utils;
4
5 use 5.008;
6 use strict;
7 use warnings;
8
9 use Exporter 'import';
10 our @EXPORT_OK = qw(isodate
11                     xml_encode 
12                     cql_quote
13                     cql_target
14                     irspy_xpath_context
15                     modify_xml_document);
16
17 use XML::LibXML;
18 use XML::LibXML::XPathContext;
19
20 our $IRSPY_NS = 'http://indexdata.com/irspy/1.0';
21
22
23 # Utility functions follow, exported for use of web UI
24 sub isodate {
25     my($time) = @_;
26
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);
30 }
31
32
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!
37 #
38 sub xml_encode {
39     my($text, $fallback) = @_;
40
41     $text = $fallback if !defined $text;
42     use Carp;
43     confess "xml_encode(): text and fallback both undefined"
44         if !defined $text;
45
46     $text =~ s/&/&/g;
47     $text =~ s/</&lt;/g;
48     $text =~ s/>/&gt;/g;
49     $text =~ s/['']/&apos;/g;
50     $text =~ s/[""]/&quot;/g;
51     return $text;
52 }
53
54
55 # Quotes a term for use in a CQL query
56 sub cql_quote {
57     my($term) = @_;
58
59     $term =~ s/([""\\])/\\$1/g;
60     $term = qq["$term"] if $term =~ /\s/;
61     return $term;
62 }
63
64
65 # Makes a CQL query that finds a specified target
66 sub cql_target {
67     my($host, $port, $db) = @_;
68
69     return ("host=" . cql_quote($host) . " and " .
70             "port=" . cql_quote($port) . " and " .
71             "path=" . cql_quote($db));
72 }
73
74
75 # PRIVATE to irspy_namespace() and irspy_xpath_context()
76 my %_namespaces = (
77                    e => 'http://explain.z3950.org/dtd/2.0/',
78                    i => $IRSPY_NS,
79                    );
80
81
82 sub irspy_namespace {
83     my($prefix) = @_;
84
85     use Carp;
86     confess "irspy_namespace(undef)" if !defined $prefix;
87     my $uri = $_namespaces{$prefix};
88     die "irspy_namespace(): no URI for namespace prefix '$prefix'"
89         if !defined $uri;
90
91     return $uri;
92 }
93
94
95 sub irspy_xpath_context {
96     my($record) = @_;
97
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});
105     }
106     return $xc;
107 }
108
109
110 sub modify_xml_document {
111     my($xc, $fieldsByKey, $data) = @_;
112
113     my @changes = ();
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);
120         if (@nodes) {
121             warn scalar(@nodes), " nodes match '$xpath'" if @nodes > 1;
122             my $node = $nodes[0];
123
124             if ($node->isa("XML::LibXML::Attr")) {
125                 if ($value ne $node->getValue()) {
126                     $node->setValue($value);
127                     push @changes, $ref;
128                     #print "Attr $key: '", $node->getValue(), "' -> '$value' ($xpath)<br/>\n";
129                 }
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
138                 # no-op.
139                 my $old = "???";
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;
146                     }
147                 }
148
149                 $node->removeChildNodes();
150                 my $child = new XML::LibXML::Text($value);
151                 $node->appendChild($child);
152                 push @changes, $ref;
153                 #print "Elem $key: '$old' -> '$value' ($xpath)<br/>\n";
154             } else {
155                 warn "unexpected node type $node";
156             }
157
158         } else {
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";
163             push @changes, $ref;
164         }
165     }
166
167     return @changes;
168 }
169
170
171 sub dom_add_node {
172     my($xc, $ppath, $selector, $value, @addAfter) = @_;
173
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;
177
178     my $is_attr = ($selector =~ s/^@//);
179     my(undef, $prefix, $simpleSel) = $selector =~ /((.*?):)?(.*)/;
180     #warn "selector='$selector', prefix='$prefix', simpleSel='$simpleSel'";
181     if ($is_attr) {
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
185             # to debug it now.
186             $node->setAttributeNS(irspy_namespace($prefix),
187                                   $simpleSel, $value);
188         } else {
189             $node->setAttribute($simpleSel, $value);
190         }
191         return;
192     }
193
194     my $new = new XML::LibXML::Element($simpleSel);
195     $new->setNamespace(irspy_namespace($prefix), $prefix)
196         if defined $prefix;
197
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'";
204             return;
205         }
206     }
207
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();
213     if (@children) {
214         $node->insertBefore($new, $children[0]);
215         #warn "Added new first child";
216     } else {
217         $node->appendChild($new);
218         #warn "Added new only child";
219     }
220
221     if (0) {
222         my $text = xml_encode(inheritance_tree($xc));
223         $text =~ s/\n/<br\/>$&/sg;
224         print "<pre>$text</pre>\n";
225     }
226 }
227
228
229 sub find_or_make_node {
230     my($xc, $path, $recursion_level) = @_;
231
232     die "deep recursion in find_or_make_node($path)"
233         if $recursion_level == 10;
234     $path = "." if $path eq "";
235
236     my @nodes = $xc->findnodes($path);
237     if (@nodes == 0) {
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);
244
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);
251         }
252
253         $parent->appendChild($new);
254         return $new;
255     }
256     warn scalar(@nodes), " nodes match parent '$path'" if @nodes > 1;
257     return $nodes[0];
258 }
259
260
261 sub inheritance_tree {
262     my($type, $level) = @_;
263     $level = 0 if !defined $level;
264     return "Woah!  Too deep, man!\n" if $level > 20;
265
266     $type = ref $type if ref $type;
267     my $text = "";
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);
273     }
274
275     return $text;
276 }
277
278
279 #print "Loaded ZOOM::IRSpy::Utils.pm";
280
281
282 1;