File Coverage

blib/lib/WebService/Lucene/XOXOParser.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package WebService::Lucene::XOXOParser;
2              
3 2     2   3244 use strict;
  2         5  
  2         87  
4 2     2   14 use warnings;
  2         5  
  2         62  
5              
6 2     2   1125 use XML::LibXML;
  0            
  0            
7              
8             BEGIN {
9             for my $name ( qw( dl dd dt ) ) {
10             no strict 'refs';
11             *$name = sub { _make_element( $name, @_ ) }
12             }
13             }
14              
15             my %pattern_lut = (
16             '&' => 'amp',
17             '<' => 'lt',
18             '>' => 'gt',
19             '"' => 'quot',
20             "'" => 'apos',
21             );
22             my $pattern = join( '|', keys %pattern_lut );
23              
24             =head1 NAME
25              
26             WebService::Lucene::XOXOParser - Simple XOXO Parser
27              
28             =head1 SYNOPSIS
29              
30             use WebService::Lucene::XOXOParser;
31            
32             my $parser = WebService::Lucene::XOXOParser->new;
33             my @properties = $parser->parse( $xml );
34              
35             =head1 DESCRIPTION
36              
37             This module provides simple XOXO parsing for Lucene documents.
38              
39             =head1 METHODS
40              
41             =head2 new( )
42              
43             Creates a new parser instance.
44              
45             =cut
46              
47             sub new {
48             my ( $class ) = @_;
49             return bless {}, $class;
50             }
51              
52             =head2 parse( $xml )
53              
54             Parses XML and returns an array of hashrefs decribing each
55             property.
56              
57             =cut
58              
59             sub parse {
60             my ( $self, $xml ) = @_;
61              
62             my $parser = XML::LibXML->new;
63             my $root = $parser->parse_string( $xml )->documentElement;
64             my @nodes = $root->findnodes( '//dt | //dd' );
65              
66             my @properties;
67             while ( @nodes ) {
68             my ( $term, $value ) = ( shift( @nodes ), shift( @nodes ) );
69              
70             my $property = {
71             name => $term->textContent,
72             value => $value->textContent,
73             map { $_->name => $_->value } $term->attributes
74             };
75              
76             push @properties, $property;
77             }
78              
79             return @properties;
80             }
81              
82             =head2 construct( @properties )
83              
84             Takes an array of properties and constructs
85             an XOXO XML structure.
86              
87             =cut
88              
89             sub construct {
90             my ( $self, @properties ) = @_;
91              
92             return dl(
93             { class => 'xoxo' },
94             map {
95             my $node = $_;
96             dt( { map { $_ => $node->{ $_ } }
97             grep { $_ !~ /^(name|value)$/ } keys %$_
98             },
99             $self->encode_entities( $_->{ name } )
100             ),
101             dd( $self->encode_entities( $_->{ value } ) )
102             } @properties
103             );
104             }
105              
106             sub _make_element {
107             my $element = shift;
108             my $output = "<$element";
109             if ( ref $_[ 0 ] ) {
110             my $attrs = shift;
111             $output .= ' ';
112             $output .= join( ' ',
113             map { qq($_=") . $attrs->{ $_ } . '"' } keys %$attrs );
114             }
115             $output .= join( '', '>', @_, "" );
116             return $output;
117             }
118              
119             =head2 encode_entities( $value )
120              
121             Escapes some chars to their entities.
122              
123             =cut
124              
125             sub encode_entities {
126             my $self = shift;
127             my $value = shift;
128             $value =~ s/($pattern)/&$pattern_lut{$1};/gso;
129              
130             return $value;
131             }
132              
133             =head2 dl
134              
135             Shortcut to create a definition list
136              
137             =head2 dt
138              
139             Shortcut to create a definition term
140              
141             =head2 dd
142              
143             Shortcut to create a definition description
144              
145             =head1 AUTHORS
146              
147             =over 4
148              
149             =item * Brian Cassidy Ebrian.cassidy@nald.caE
150              
151             =item * Adam Paynter Eadam.paynter@nald.caE
152              
153             =back
154              
155             =head1 COPYRIGHT AND LICENSE
156              
157             Copyright 2006-2009 National Adult Literacy Database
158              
159             This library is free software; you can redistribute it and/or modify
160             it under the same terms as Perl itself.
161              
162             =cut
163              
164             1;