1 # $Id: Utils.pm,v 1.9 2006-11-09 16:09:35 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 # PRIVATE to irspy_namespace() and irspy_xpath_context()
41 e => 'http://explain.z3950.org/dtd/2.0/',
49 my $uri = $_namespaces{$prefix};
50 die "irspy_namespace(): no URI for namespace prefix '$prefix'"
57 sub irspy_xpath_context {
60 my $xml = ref $record ? $record->render() : $record;
61 my $parser = new XML::LibXML();
62 my $doc = $parser->parse_string($xml);
63 my $root = $doc->getDocumentElement();
64 my $xc = XML::LibXML::XPathContext->new($root);
65 foreach my $prefix (keys %_namespaces) {
66 $xc->registerNs($prefix, $_namespaces{$prefix});
72 sub modify_xml_document {
73 my($xc, $fieldsByKey, $data) = @_;
76 foreach my $key (keys %$data) {
77 my $value = $data->{$key};
78 my $ref = $fieldsByKey->{$key} or die "no field '$key'";
79 my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
80 #print "Considering $key='$value' ($xpath)<br/>\n";
81 my @nodes = $xc->findnodes($xpath);
83 warn scalar(@nodes), " nodes match '$xpath'" if @nodes > 1;
86 if ($node->isa("XML::LibXML::Attr")) {
87 if ($value ne $node->getValue()) {
88 $node->setValue($value);
90 #print "Attr $key: '", $node->getValue(), "' -> '$value' ($xpath)<br/>\n";
92 } elsif ($node->isa("XML::LibXML::Element")) {
93 # The contents could be any mixture of text and
94 # comments and maybe even other crud such as processing
95 # instructions. The simplest thing is just to throw it all
96 # away and start again, making a single Text node the
97 # canonical representation. But before we do that,
98 # we'll check whether the element is already
99 # canonical, to determine whether our change is a
102 my @children = $node->childNodes();
103 if (@children == 1) {
104 my $child = $node->firstChild();
105 if (ref $child && ref $child eq "XML::LibXML::Text") {
106 $old = $child->getData();
107 next if $value eq $old;
111 $node->removeChildNodes();
112 my $child = new XML::LibXML::Text($value);
113 $node->appendChild($child);
115 #print "Elem $key: '$old' -> '$value' ($xpath)<br/>\n";
117 warn "unexpected node type $node";
121 next if !$value; # No need to create a new empty node
122 my($ppath, $element) = $xpath =~ /(.*)\/(.*)/;
123 dom_add_element($xc, $ppath, $element, $value, @addAfter);
124 #print "New $key ($xpath) = '$value'<br/>\n";
133 sub dom_add_element {
134 my($xc, $ppath, $element, $value, @addAfter) = @_;
136 #print "Adding $element='$value' at '$ppath' after (", join(", ", map { "'$_'" } @addAfter), ")<br/>\n";
137 my @nodes = $xc->findnodes($ppath);
139 # Oh dear, the parent node doesn't exist. We could make it,
140 # but for now let's not and say we did.
141 warn "no parent node '$ppath': not adding '$element'='$value'";
144 warn scalar(@nodes), " nodes match parent '$ppath'" if @nodes > 1;
145 my $node = $nodes[0];
147 my(undef, $prefix, $nsElem) = $element =~ /((.*?):)?(.*)/;
148 my $new = new XML::LibXML::Element($nsElem);
149 $new->setNamespace(irspy_namespace($prefix), $prefix)
152 $new->appendText($value);
153 foreach my $predecessor (reverse @addAfter) {
154 my($child) = $xc->findnodes($predecessor, $node);
155 if (defined $child) {
156 $node->insertAfter($new, $child);
157 #print "Added after '$predecessor'<br/>\n";
162 # Didn't find any of the nodes that are supposed to precede the
163 # new one, so we need to insert the new node as the first of the
164 # parent's children. However *sigh* there is no prependChild()
165 # analogous to appendChild(), so we have to go the long way round.
166 my @children = $node->childNodes();
168 $node->insertBefore($new, $children[0]);
169 #print "Added new first child<br/>\n";
171 $node->appendChild($new);
172 #print "Added new only child<br/>\n";
176 my $text = xml_encode(inheritance_tree($xc));
177 $text =~ s/\n/<br\/>$&/sg;
178 print "<pre>$text</pre>\n";
183 sub inheritance_tree {
184 my($type, $level) = @_;
185 $level = 0 if !defined $level;
186 return "Woah! Too deep, man!\n" if $level > 20;
188 $type = ref $type if ref $type;
190 $text = "--> " if $level == 0;
191 $text .= ("\t" x $level) . "$type\n";
192 my @ISA = eval "\@${type}::ISA";
193 foreach my $superclass (@ISA) {
194 $text .= inheritance_tree($superclass, $level+1);
201 #print "Loaded ZOOM::IRSpy::Utils.pm";