1 # $Id: Utils.pm,v 1.14 2006-11-14 16:21:49 mike Exp $
3 package ZOOM::IRSpy::Utils;
10 our @EXPORT_OK = qw(xml_encode
16 use XML::LibXML::XPathContext;
18 our $IRSPY_NS = 'http://indexdata.com/irspy/1.0';
21 # Utility functions follow, exported for use of web UI
23 # I can't -- just can't, can't, can't -- believe that this function
24 # isn't provided by one of the core XML modules. But the evidence all
25 # says that it's not: among other things, XML::Generator and
26 # Template::Plugin both roll their own. So I will do likewise. D'oh!
29 my ($text, $fallback) = @_;
31 $text = $fallback if !defined $text;
33 confess "xml_encode(): text and fallback both undefined"
39 $text =~ s/['']/'/g;
40 $text =~ s/[""]/"/g;
45 # PRIVATE to irspy_namespace() and irspy_xpath_context()
47 e => 'http://explain.z3950.org/dtd/2.0/',
56 confess "irspy_namespace(undef)" if !defined $prefix;
57 my $uri = $_namespaces{$prefix};
58 die "irspy_namespace(): no URI for namespace prefix '$prefix'"
65 sub irspy_xpath_context {
68 my $xml = ref $record ? $record->render() : $record;
69 my $parser = new XML::LibXML();
70 my $doc = $parser->parse_string($xml);
71 my $root = $doc->getDocumentElement();
72 my $xc = XML::LibXML::XPathContext->new($root);
73 foreach my $prefix (keys %_namespaces) {
74 $xc->registerNs($prefix, $_namespaces{$prefix});
80 sub modify_xml_document {
81 my($xc, $fieldsByKey, $data) = @_;
84 foreach my $key (keys %$data) {
85 my $value = $data->{$key};
86 my $ref = $fieldsByKey->{$key} or die "no field '$key'";
87 my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
88 #print "Considering $key='$value' ($xpath)<br/>\n";
89 my @nodes = $xc->findnodes($xpath);
91 warn scalar(@nodes), " nodes match '$xpath'" if @nodes > 1;
94 if ($node->isa("XML::LibXML::Attr")) {
95 if ($value ne $node->getValue()) {
96 $node->setValue($value);
98 #print "Attr $key: '", $node->getValue(), "' -> '$value' ($xpath)<br/>\n";
100 } elsif ($node->isa("XML::LibXML::Element")) {
101 # The contents could be any mixture of text and
102 # comments and maybe even other crud such as processing
103 # instructions. The simplest thing is just to throw it all
104 # away and start again, making a single Text node the
105 # canonical representation. But before we do that,
106 # we'll check whether the element is already
107 # canonical, to determine whether our change is a
110 my @children = $node->childNodes();
111 if (@children == 1) {
112 my $child = $node->firstChild();
113 if (ref $child && ref $child eq "XML::LibXML::Text") {
114 $old = $child->getData();
115 next if $value eq $old;
119 $node->removeChildNodes();
120 my $child = new XML::LibXML::Text($value);
121 $node->appendChild($child);
123 #print "Elem $key: '$old' -> '$value' ($xpath)<br/>\n";
125 warn "unexpected node type $node";
129 next if !$value; # No need to create a new empty node
130 my($ppath, $selector) = $xpath =~ /(.*)\/(.*)/;
131 dom_add_node($xc, $ppath, $selector, $value, @addAfter);
132 #print "New $key ($xpath) = '$value'<br/>\n";
142 my($xc, $ppath, $selector, $value, @addAfter) = @_;
144 #print "Adding $selector='$value' at '$ppath' after (", join(", ", map { "'$_'" } @addAfter), ")<br/>\n";
145 my $node = find_or_make_node($xc, $ppath, 0);
146 die "couldn't find or make node '$node'" if !defined $node;
148 my $is_attr = ($selector =~ s/^@//);
149 my(undef, $prefix, $simpleSel) = $selector =~ /((.*?):)?(.*)/;
150 #warn "selector='$selector', prefix='$prefix', simpleSel='$simpleSel'";
152 if (defined $prefix) {
153 ### This seems to no-op (thank, DOM!) but I have have no
154 # idea, and it's not needed for IRSpy, so I am not going
156 $node->setAttributeNS(irspy_namespace($prefix),
159 $node->setAttribute($simpleSel, $value);
164 my $new = new XML::LibXML::Element($simpleSel);
165 $new->setNamespace(irspy_namespace($prefix), $prefix)
168 $new->appendText($value);
169 foreach my $predecessor (reverse @addAfter) {
170 my($child) = $xc->findnodes($predecessor, $node);
171 if (defined $child) {
172 $node->insertAfter($new, $child);
173 #warn "Added after '$predecessor'";
178 # Didn't find any of the nodes that are supposed to precede the
179 # new one, so we need to insert the new node as the first of the
180 # parent's children. However *sigh* there is no prependChild()
181 # analogous to appendChild(), so we have to go the long way round.
182 my @children = $node->childNodes();
184 $node->insertBefore($new, $children[0]);
185 #warn "Added new first child";
187 $node->appendChild($new);
188 #warn "Added new only child";
192 my $text = xml_encode(inheritance_tree($xc));
193 $text =~ s/\n/<br\/>$&/sg;
194 print "<pre>$text</pre>\n";
199 sub find_or_make_node {
200 my($xc, $path, $recursion_level) = @_;
202 die "deep recursion in find_or_make_node($path)"
203 if $recursion_level == 10;
204 $path = "." if $path eq "";
206 my @nodes = $xc->findnodes($path);
208 # Oh dear, the parent node doesn't exist. We could make it,
209 my(undef, $ppath, $element) = $path =~ /((.*)\/)?(.*)/;
210 $ppath = "" if !defined $ppath;
211 #warn "path='$path', ppath='$ppath', element='$element'";
212 #warn "no node '$path': making it";
213 my $parent = find_or_make_node($xc, $ppath, $recursion_level-1);
215 my(undef, $prefix, $nsElem) = $element =~ /((.*?):)?(.*)/;
216 #warn "element='$element', prefix='$prefix', nsElem='$nsElem'";
217 my $new = new XML::LibXML::Element($nsElem);
218 if (defined $prefix) {
219 #warn "setNamespace($prefix)";
220 $new->setNamespace(irspy_namespace($prefix), $prefix);
223 $parent->appendChild($new);
226 warn scalar(@nodes), " nodes match parent '$path'" if @nodes > 1;
231 sub inheritance_tree {
232 my($type, $level) = @_;
233 $level = 0 if !defined $level;
234 return "Woah! Too deep, man!\n" if $level > 20;
236 $type = ref $type if ref $type;
238 $text = "--> " if $level == 0;
239 $text .= ("\t" x $level) . "$type\n";
240 my @ISA = eval "\@${type}::ISA";
241 foreach my $superclass (@ISA) {
242 $text .= inheritance_tree($superclass, $level+1);
249 #print "Loaded ZOOM::IRSpy::Utils.pm";