PQF.pm

filter: Statementstmt 87.1% 68 / 78 Branchbran 80.0% 24 / 30 Conditioncond 83.3% 5 / 6 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 83.3% 10 / 12 Podpod 100% 3 / 3 totaltotal 84.8% 112 / 132 CC 19 SCAR 20.7
CC
19
Cov
85%
_parse
14.5
_leaf
4.0
parse
2.0
CRAP 7.9
↑ Summary PQF/Node.pm »
1 # $Id: PQF.pm,v 1.8 2007/10/05 12:12:34 mike Exp $
2  
3 package Net::Z3950::PQF;
4  
5 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Net::Z3950::PQF::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 our $VERSION = '1.0';
12  
13  
14 =head1 NAME
15  
16 Net::Z3950::PQF - Perl extension for parsing PQF (Prefix Query Format)
17  
18 =head1 SYNOPSIS
19  
20 use Net::Z3950::PQF;
21 $parser = new Net::Z3950::PQF();
22 $node = $parser->parse('@and @attr 1=1003 kernighan @attr 1=4 unix');
23 print $node->render(0);
24  
25 =head1 DESCRIPTION
26  
27 This library provides a parser for PQF (Prefix Query Format), an ugly
28 but precise string format for expressing Z39.50 Type-1 queries. This
29 format is widely used behind the scenes of Z39.50 applications, and is
30 also used extensively with test-harness programs such as the YAZ
31 command-line client, C<yaz-client>. A few particularly misguided
32 souls have been known to type it by hand.
33  
34 Unlike PQF itself, this module
35 is simple to use. Create a parser object, then pass PQF strings
36 into its C<parse()> method to yield parse-trees. The trees are made
37 up of nodes whose types are subclasses of
38 C<Net::Z3950::PQF::Node>.
39 and have names of the form
40 C<Net::Z3950::PQF::somethingNode>. You may find it helpful to use
41 C<Data::Dumper> to visualise the structure of the returned
42 parse-trees.
43  
44 What is a PQF parse-tree good for? Not much. You can render a
45 human-readable version by invoking the top node's C<render()> method,
46 which is probably useful only for debugging. Or you can turn it into
47 tree of nodes like those passed into SimpleServer search handlers
48 using C<toSimpleServer()>. If you want to do anything useful, such as
49 implementing an actual query server that understands PQF, you'll have
50 to walk the tree.
51  
52 =head1 METHODS
53  
54 =head2 new()
55  
56 $parser = new Net::Z3950::PQF();
57 $parser = new Net::Z3950::PQF({ 'Net::Z3950::PQF::AndNode' => 'MyApp::And' });
58  
59 Creates a new parser object.
60  
61 One optional argument may be provided. If present, it is interpreted
62 as a map of classnames. The keys are the names of classed generated by
63 this module (C<Net::Z3950::PQF::TermNode>, etc.) and the corresponding
64 values are the names of classes for the various nodes to be blessed
65 into instead of the standard classes.
66  
67 B<WARNING!> Doing this will make the C<render()> and
68 C<toSimpleServer()> unavailable, since they are implemented using
69 private methods of the standard node classes.
70  
71 =cut
72  
73 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
74 3 my $class = shift();
75 3 my($classnameMap) = @_;
76  
77 3 return bless {
78 text => undef,
79 errmsg => undef,
80 classnameMap => $classnameMap,
81 }, $class;
82 }
83  
84  
85 =head2 parse()
86  
87 $query = '@and @attr 1=1003 kernighan @attr 1=4 unix';
88 $node = $parser->parse($query);
89 if (!defined $node) {
90 die "parse($query) failed: " . $parser->errmsg();
91 }
92  
93 Parses the PQF string provided as its argument. If an error occurs,
94 then an undefined value is returned, and the error message can be
95 obtained by calling the C<errmsg()> method. Otherwise, the top node
96 of the parse tree is returned.
97  
98 $node2 = $parser->parse($query, "zthes");
99 $node3 = $parser->parse($query, "1.2.840.10003.3.13");
100  
101 A second argument may be provided after the query itself. If it is
102 provided, then it is taken to be either the name or the OID of a
103 default attribute set, which attributes specified in the query belong
104 to if no alternative attribute set is explicitly specified within the
105 query. When this second argument is absent, the default attribute set
106 is BIB-1.
107  
108 =cut
109  
110 22 ▶sub parse {
Subroutine parse: called CC=2 SCAR=7.0
Pod documented
111 22 my $this = shift();
112 22 my($text, $attrset) = @_;
113 22 ▶ $attrset = "bib-1" if !defined $attrset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrset 22 0 22
114  
115 22 $this->{text} = $text;
116 22 return $this->_parse($attrset, {});
117 }
118  
119  
120 # PRIVATE to parse();
121 #
122 # Underlying parse function. $attrset is the default attribute-set to
123 # use for attributes that are not specified with an explicit set, and
124 # $attrhash is hash of attributes (at most one per type per
125 # attribute-set) to be applied to all nodes below this point. The
126 # keys of this hash are of the form "<attrset>:<type>" and the values
127 # are the corresponding attribute values.
128 #
129 74 ▶sub _parse {
Subroutine _parse: called CC=14 SCAR=26.8
130 74 my $this = shift();
131 74 my($attrset, $attrhash) = @_;
132  
133 74 $this->{text} =~ s/^\s+//;
134  
135 ### This rather nasty hack for quoted terms doesn't recognised
136 # backslash-quoted embedded double quotes.
137 74 ▶ if ($this->{text} =~ s/^"(.*?)"//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"text"} =~ s/^"(.*?)"//) 3 71 74
138 3 return $this->_leaf('term', $1, $attrhash);
139 }
140  
141 # Also recognise multi-word terms enclosed in {curly braces}
142 71 ▶ if ($this->{text} =~ s/^{(.*?)}//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"text"} =~ s/^{(.*?)}//) 1 70 71
143 1 return $this->_leaf('term', $1, $attrhash);
144 }
145  
146 70 my $word = $this->_word();
147 70 ▶ if ($word eq '@attrset') {
Branch: 5 branches
BranchTrueFalseTotal
if ($word eq '@attrset') { } 2 68 70
elsif ($word eq '@attr') { } 22 46 68
elsif ($word eq '@and' or $word eq '@or' or $word eq '@not') { } 14 32 46
elsif ($word eq '@prox') { } 0 32 32
elsif ($word eq '@set') { } 4 28 32
148 2 $attrset = $this->_word();
149 2 return $this->_parse($attrset, $attrhash);
150  
151 } elsif ($word eq '@attr') {
152 22 $word = $this->_word();
153 22 ▶ if ($word !~ /=/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($word =~ /=/) 4 18 22
154 4 $attrset = $word;
155 4 $word = $this->_word();
156 }
157 22 my($type, $val) = ($word =~ /(.*)=(.*)/);
158 22 my %h = %$attrhash;
159 22 $h{"$attrset:$type"} = $val;
160 22 return $this->_parse($attrset, \%h);
161  
162 ▶ } elsif ($word eq '@and' || $word eq '@or' || $word eq '@not') {
Condition83%5/6
$word eq "\@and" or $word eq "\@or" or $word eq "\@not"
l!l&&r!l&&!r
14032
$word eq "\@and" or $word eq "\@or"
l!l&&r!l&&!r
5932
MC/DC66%2/3
$word eq "\@and" or $word eq "\@or" or $word eq "\@not"
$word eq "\@and" $word eq "\@or" $word eq "\@not"
Truth table75%3/4
A or B or C
A= $word eq "\@and"B= $word eq "\@or"C= $word eq "\@not"
ABCresult
1XX1
01X1
0011
0000
163 14 my $sub1 = $this->_parse($attrset, $attrhash);
164 14 my $sub2 = $this->_parse($attrset, $attrhash);
165 14 ▶ if ($word eq '@and') {
Branch: 3 branches
BranchTrueFalseTotal
if ($word eq '@and') { } 5 9 14
elsif ($word eq '@or') { } 9 0 9
elsif ($word eq '@not') { } 0 0 0
166 5 return $this->_maybe_rebless(new Net::Z3950::PQF::AndNode($sub1, $sub2));
167 } elsif ($word eq '@or') {
168 9 return $this->_maybe_rebless(new Net::Z3950::PQF::OrNode($sub1, $sub2));
169 } elsif ($word eq '@not') {
170 0 return $this->_maybe_rebless(new Net::Z3950::PQF::NotNode($sub1, $sub2));
171 } else {
172 0 die "Houston, we have a problem";
173 }
174  
175 } elsif ($word eq '@prox') {
176 0 return $this->_error("proximity not yet implemented");
177  
178 } elsif ($word eq '@set') {
179 4 $word = $this->_word();
180 4 return $this->_leaf('rset', $word, $attrhash);
181 }
182  
183 # It must be a bareword
184 28 return $this->_leaf('term', $word, $attrhash);
185 }
186  
187  
188 # PRIVATE to _parse();
189 102 ▶sub _word {
Subroutine _word: called CC=1 SCAR=0.0
190 102 my $this = shift();
191  
192 102 $this->{text} =~ s/^\s+//;
193 102 $this->{text} =~ s/^(\S+)//;
194 102 return $1;
195 }
196  
197  
198 # PRIVATE to _parse();
199 0 ▶sub _error {
Subroutine _error: not called CC=1 SCAR=6.9
200 0 my $this = shift();
201 0 my (@msg) = @_;
202  
203 0 $this->{errmsg} = join("", @msg);
204 0 return undef;
205 }
206  
207  
208 # PRIVATE to _parse();
209 36 ▶sub _leaf {
Subroutine _leaf: called CC=4 SCAR=14.0
210 36 my $this = shift();
211 36 my($type, $word, $attrhash) = @_;
212  
213 36 my @attrs;
214 36 foreach my $key (sort keys %$attrhash) {
215 26 my($attrset, $type) = split /:/, $key;
216 26 push @attrs, [ $attrset, $type, $attrhash->{$key} ];
217 }
218  
219 36 ▶ if ($type eq 'term') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'term') { } 32 4 36
elsif ($type eq 'rset') { } 4 0 4
220 32 return $this->_maybe_rebless(new Net::Z3950::PQF::TermNode($word, @attrs));
221 } elsif ($type eq 'rset') {
222 4 return $this->_maybe_rebless(new Net::Z3950::PQF::RsetNode($word, @attrs));
223 } else {
224 0 die "_leaf() called with type='$type' (should be 'term' or 'rset')";
225 }
226 }
227  
228  
229 # PRIVATE to _parse() and _leaf();
230 50 ▶sub _maybe_rebless {
Subroutine _maybe_rebless: called CC=2 SCAR=6.9
231 50 my $this = shift();
232 50 my($node) = @_;
233  
234 50 my $oldClassname = ref $node;
235 50 my $newClassname = $this->{classnameMap}->{$oldClassname};
236 50 ▶ if ($newClassname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newClassname) 5 45 50
237 5 bless $node, $newClassname;
238 }
239  
240 50 return $node;
241 }
242  
243  
244 =head2 errmsg()
245  
246 print $parser->errmsg();
247  
248 Returns the last error-message generated by a failed attempt to parse
249 a query.
250  
251 =cut
252  
253 0 ▶sub errmsg {
Subroutine errmsg: not called CC=1 SCAR=6.9
Pod documented
254 0 my $this = shift();
255 0 return $this->{errmsg};
256 }
257  
258  
259 =head1 SEE ALSO
260  
261 The C<Net::Z3950::PQF::Node> module.
262  
263 The definition of the Type-1 query in the Z39.50 standard, the
264 relevant section of which is on-line at
265 http://www.loc.gov/z3950/agency/markup/09.html#3.7
266  
267 The documentation of Prefix Query Format in the YAZ Manual, the
268 relevant section of which is on-line at
269 http://indexdata.com/yaz/doc/tools.tkl#PQF
270  
271 =head1 AUTHOR
272  
273 Mike Taylor, E<lt>mike@indexdata.comE<gt>
274  
275 =head1 COPYRIGHT AND LICENSE
276  
277 Copyright 2004 by Index Data ApS.
278  
279 This library is free software; you can redistribute it and/or modify
280 it under the same terms as Perl itself.
281  
282 =cut
283  
284  
285 4 1;
↑ Summary PQF/Node.pm »