1 # $Id: Utils.pm,v 1.7 2006-11-07 17:45:37 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!
33 $text =~ s/['']/'/g;
34 $text =~ s/[""]/"/g;
39 sub irspy_xpath_context {
40 my($zoom_record) = @_;
42 my $xml = $zoom_record->render();
43 my $parser = new XML::LibXML();
44 my $doc = $parser->parse_string($xml);
45 my $root = $doc->getDocumentElement();
46 my $xc = XML::LibXML::XPathContext->new($root);
47 $xc->registerNs(e => 'http://explain.z3950.org/dtd/2.0/');
48 $xc->registerNs(i => $IRSPY_NS);
53 sub modify_xml_document {
54 my($xc, $fieldsByKey, $data) = @_;
57 foreach my $key (keys %$data) {
58 my $value = $data->{$key};
59 my $ref = $fieldsByKey->{$key} or die "no field '$key'";
60 my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
61 #print "Considering $key='$value' ($xpath)<br/>\n";
62 my @nodes = $xc->findnodes($xpath);
64 warn scalar(@nodes), " nodes match '$xpath'" if @nodes > 1;
67 if ($node->isa("XML::LibXML::Attr")) {
68 if ($value ne $node->getValue()) {
69 $node->setValue($value);
71 print "Attr $key: '", $node->getValue(), "' -> '$value' ($xpath)<br/>\n";
73 } elsif ($node->isa("XML::LibXML::Element")) {
74 # The contents could be any mixture of text and
75 # comments and maybe even other crud such as processing
76 # instructions. The simplest thing is just to throw it all
77 # away and start again, making a single Text node the
78 # canonical representation. But before we do that,
79 # we'll check whether the element is already
80 # canonical, to determine whether our change is a
83 my @children = $node->childNodes();
85 my $child = $node->firstChild();
86 if (ref $child && ref $child eq "XML::LibXML::Text") {
87 $old = $child->getData();
88 next if $value eq $old;
92 $node->removeChildNodes();
93 my $child = new XML::LibXML::Text($value);
94 $node->appendChild($child);
96 print "Elem $key: '$old' -> '$value' ($xpath)<br/>\n";
98 warn "unexpected node type $node";
102 next if !$value; # No need to create a new empty node
103 my($ppath, $element) = $xpath =~ /(.*)\/(.*)/;
104 dom_add_element($xc, $ppath, $element, $value, @addAfter);
105 print "Add $key ($xpath) = '$value'<br/>\n";
114 sub dom_add_element {
115 my($xc, $ppath, $element, $value, @addAfter) = @_;
117 print "Adding '$value' at '$ppath' after (", join(", ", map { "'$_'" } @addAfter), ")<br/>\n";
118 my @nodes = $xc->findnodes($ppath);
120 # Oh dear, the parent node doesn't exist. We could make it,
121 # but for now let's not and say we did.
122 warn "no parent node '$ppath': not adding '$element'='$value'";
125 warn scalar(@nodes), " nodes match parent '$ppath'" if @nodes > 1;
126 my $node = $nodes[0];
128 my $new = new XML::LibXML::Text($value);
129 foreach my $predecessor (reverse @addAfter) {
130 my($child) = $xc->findnodes($predecessor, $node);
131 if (defined $child) {
132 $node->insertAfter($new, $child);
133 print "Added after '$predecessor'\n";
138 # Didn't find any of the nodes that are supposed to precede the
139 # new one, so we need to insert the new node as the first of the
140 # parent's children. However *sigh* there is no prependChild()
141 # analogous to appendChild(), so we have to go the long way round.
142 my @children = $node->childNodes();
144 $node->insertBefore($new, $children[0]);
145 print "Added new first child\n";
147 $node->appendChild($new);
148 print "Added new only child\n";
152 my $text = xml_encode(inheritance_tree($xc));
153 $text =~ s/\n/<br\/>$&/sg;
154 print "<pre>$text</pre>\n";
159 sub inheritance_tree {
160 my($type, $level) = @_;
161 $level = 0 if !defined $level;
162 return "Woah! Too deep, man!\n" if $level > 20;
164 $type = ref $type if ref $type;
166 $text = "--> " if $level == 0;
167 $text .= ("\t" x $level) . "$type\n";
168 my @ISA = eval "\@${type}::ISA";
169 foreach my $superclass (@ISA) {
170 $text .= inheritance_tree($superclass, $level+1);
177 #print "Loaded ZOOM::IRSpy::Utils.pm";