1 # $Id: Utils.pm,v 1.13 2006-11-14 16:04:36 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/',
50 confess "irspy_namespace(undef)" if !defined $prefix;
51 my $uri = $_namespaces{$prefix};
52 die "irspy_namespace(): no URI for namespace prefix '$prefix'"
59 sub irspy_xpath_context {
62 my $xml = ref $record ? $record->render() : $record;
63 my $parser = new XML::LibXML();
64 my $doc = $parser->parse_string($xml);
65 my $root = $doc->getDocumentElement();
66 my $xc = XML::LibXML::XPathContext->new($root);
67 foreach my $prefix (keys %_namespaces) {
68 $xc->registerNs($prefix, $_namespaces{$prefix});
74 sub modify_xml_document {
75 my($xc, $fieldsByKey, $data) = @_;
78 foreach my $key (keys %$data) {
79 my $value = $data->{$key};
80 my $ref = $fieldsByKey->{$key} or die "no field '$key'";
81 my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
82 #print "Considering $key='$value' ($xpath)<br/>\n";
83 my @nodes = $xc->findnodes($xpath);
85 warn scalar(@nodes), " nodes match '$xpath'" if @nodes > 1;
88 if ($node->isa("XML::LibXML::Attr")) {
89 if ($value ne $node->getValue()) {
90 $node->setValue($value);
92 #print "Attr $key: '", $node->getValue(), "' -> '$value' ($xpath)<br/>\n";
94 } elsif ($node->isa("XML::LibXML::Element")) {
95 # The contents could be any mixture of text and
96 # comments and maybe even other crud such as processing
97 # instructions. The simplest thing is just to throw it all
98 # away and start again, making a single Text node the
99 # canonical representation. But before we do that,
100 # we'll check whether the element is already
101 # canonical, to determine whether our change is a
104 my @children = $node->childNodes();
105 if (@children == 1) {
106 my $child = $node->firstChild();
107 if (ref $child && ref $child eq "XML::LibXML::Text") {
108 $old = $child->getData();
109 next if $value eq $old;
113 $node->removeChildNodes();
114 my $child = new XML::LibXML::Text($value);
115 $node->appendChild($child);
117 #print "Elem $key: '$old' -> '$value' ($xpath)<br/>\n";
119 warn "unexpected node type $node";
123 next if !$value; # No need to create a new empty node
124 my($ppath, $selector) = $xpath =~ /(.*)\/(.*)/;
125 dom_add_node($xc, $ppath, $selector, $value, @addAfter);
126 #print "New $key ($xpath) = '$value'<br/>\n";
136 my($xc, $ppath, $selector, $value, @addAfter) = @_;
138 #print "Adding $selector='$value' at '$ppath' after (", join(", ", map { "'$_'" } @addAfter), ")<br/>\n";
139 my $node = find_or_make_node($xc, $ppath, 0);
140 die "couldn't find or make node '$node'" if !defined $node;
142 my $is_attr = ($selector =~ s/^@//);
143 my(undef, $prefix, $simpleSel) = $selector =~ /((.*?):)?(.*)/;
144 #warn "selector='$selector', prefix='$prefix', simpleSel='$simpleSel'";
146 if (defined $prefix) {
147 ### This seems to no-op (thank, DOM!) but I have have no
148 # idea, and it's not needed for IRSpy, so I am not going
150 $node->setAttributeNS(irspy_namespace($prefix),
153 $node->setAttribute($simpleSel, $value);
158 my $new = new XML::LibXML::Element($simpleSel);
159 $new->setNamespace(irspy_namespace($prefix), $prefix)
162 $new->appendText($value);
163 foreach my $predecessor (reverse @addAfter) {
164 my($child) = $xc->findnodes($predecessor, $node);
165 if (defined $child) {
166 $node->insertAfter($new, $child);
167 #warn "Added after '$predecessor'";
172 # Didn't find any of the nodes that are supposed to precede the
173 # new one, so we need to insert the new node as the first of the
174 # parent's children. However *sigh* there is no prependChild()
175 # analogous to appendChild(), so we have to go the long way round.
176 my @children = $node->childNodes();
178 $node->insertBefore($new, $children[0]);
179 #warn "Added new first child";
181 $node->appendChild($new);
182 #warn "Added new only child";
186 my $text = xml_encode(inheritance_tree($xc));
187 $text =~ s/\n/<br\/>$&/sg;
188 print "<pre>$text</pre>\n";
193 sub find_or_make_node {
194 my($xc, $path, $recursion_level) = @_;
196 die "deep recursion in find_or_make_node($path)"
197 if $recursion_level == 10;
198 $path = "." if $path eq "";
200 my @nodes = $xc->findnodes($path);
202 # Oh dear, the parent node doesn't exist. We could make it,
203 my(undef, $ppath, $element) = $path =~ /((.*)\/)?(.*)/;
204 $ppath = "" if !defined $ppath;
205 #warn "path='$path', ppath='$ppath', element='$element'";
206 #warn "no node '$path': making it";
207 my $parent = find_or_make_node($xc, $ppath, $recursion_level-1);
209 my(undef, $prefix, $nsElem) = $element =~ /((.*?):)?(.*)/;
210 #warn "element='$element', prefix='$prefix', nsElem='$nsElem'";
211 my $new = new XML::LibXML::Element($nsElem);
212 if (defined $prefix) {
213 #warn "setNamespace($prefix)";
214 $new->setNamespace(irspy_namespace($prefix), $prefix);
217 $parent->appendChild($new);
220 warn scalar(@nodes), " nodes match parent '$path'" if @nodes > 1;
225 sub inheritance_tree {
226 my($type, $level) = @_;
227 $level = 0 if !defined $level;
228 return "Woah! Too deep, man!\n" if $level > 20;
230 $type = ref $type if ref $type;
232 $text = "--> " if $level == 0;
233 $text .= ("\t" x $level) . "$type\n";
234 my @ISA = eval "\@${type}::ISA";
235 foreach my $superclass (@ISA) {
236 $text .= inheritance_tree($superclass, $level+1);
243 #print "Loaded ZOOM::IRSpy::Utils.pm";