1 # $Id: Utils.pm,v 1.11 2006-11-13 18:03:34 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 $node = find_or_make_node($xc, $ppath, 0);
138 return if !defined $node; ### should be a "can't happen"
140 my(undef, $prefix, $nsElem) = $element =~ /((.*?):)?(.*)/;
141 my $new = new XML::LibXML::Element($nsElem);
142 $new->setNamespace(irspy_namespace($prefix), $prefix)
145 $new->appendText($value);
146 foreach my $predecessor (reverse @addAfter) {
147 my($child) = $xc->findnodes($predecessor, $node);
148 if (defined $child) {
149 $node->insertAfter($new, $child);
150 #print "Added after '$predecessor'<br/>\n";
155 # Didn't find any of the nodes that are supposed to precede the
156 # new one, so we need to insert the new node as the first of the
157 # parent's children. However *sigh* there is no prependChild()
158 # analogous to appendChild(), so we have to go the long way round.
159 my @children = $node->childNodes();
161 $node->insertBefore($new, $children[0]);
162 #print "Added new first child<br/>\n";
164 $node->appendChild($new);
165 #print "Added new only child<br/>\n";
169 my $text = xml_encode(inheritance_tree($xc));
170 $text =~ s/\n/<br\/>$&/sg;
171 print "<pre>$text</pre>\n";
176 sub find_or_make_node {
177 my($xc, $path, $recursion_level) = @_;
179 die "deep recursion in find_or_make_node($path)"
180 if $recursion_level == 10;
182 my @nodes = $xc->findnodes($path);
184 # Oh dear, the parent node doesn't exist. We could make it,
185 my($ppath, $element) = $path =~ /(.*)\/(.*)/;
186 warn "no node '$path': making it";
187 my $parent = find_or_make_node($xc, $ppath, $recursion_level-1);
189 my(undef, $prefix, $nsElem) = $element =~ /((.*?):)?(.*)/;
190 my $new = new XML::LibXML::Element($nsElem);
191 $new->setNamespace(irspy_namespace($prefix), $prefix)
194 $parent->appendChild($new);
197 warn scalar(@nodes), " nodes match parent '$path'" if @nodes > 1;
202 sub inheritance_tree {
203 my($type, $level) = @_;
204 $level = 0 if !defined $level;
205 return "Woah! Too deep, man!\n" if $level > 20;
207 $type = ref $type if ref $type;
209 $text = "--> " if $level == 0;
210 $text .= ("\t" x $level) . "$type\n";
211 my @ISA = eval "\@${type}::ISA";
212 foreach my $superclass (@ISA) {
213 $text .= inheritance_tree($superclass, $level+1);
220 #print "Loaded ZOOM::IRSpy::Utils.pm";