File Coverage

blib/lib/DBIx/Romani/Query/XML/Util.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1              
2             package DBIx::Romani::Query::XML::Util;
3 1     1   7 use base qw( Exporter );
  1         2  
  1         75  
4              
5 1     1   667 use XML::DOM;
  0            
  0            
6             use strict;
7              
8             our @EXPORT_OK = qw(
9             $NS_QUERY
10             $NS_QUERY_FUNCTION
11             $NS_QUERY_OPERATOR
12             get_element_text
13             get_boolean_attribute
14             parse_boolean);
15              
16             our $NS_QUERY = 'http://www.carspot.com/query';
17             our $NS_QUERY_FUNCTION = 'http://www.carspot.com/query-function';
18             our $NS_QUERY_OPERATOR = 'http://www.carspot.com/query-operator';
19              
20             # TEMP: for backward compatibility
21             sub get_text { return get_element_text(@_); }
22             sub get_boolean { return get_boolean_attribute(@_); }
23              
24             sub get_element_text
25             {
26             my $node = shift;
27              
28             my $text = "";
29             my $child = $node->getFirstChild();
30             while ( defined $child )
31             {
32             if ( $child->getNodeType() == XML::DOM::TEXT_NODE )
33             {
34             $text .= $child->getNodeValue();
35             }
36             elsif ( $child->getNodeType() == XML::DOM::ELEMENT_NODE )
37             {
38             die sprintf "Expecting only text inside of \"%s\" tag", $node->getTagName();
39             }
40              
41             $child = $child->getNextSibling();
42             }
43              
44             return $text;
45             }
46              
47             sub parse_boolean
48             {
49             my $text = shift;
50              
51             if ( $text eq '0' or $text eq 'false' )
52             {
53             return 0;
54             }
55             elsif ( $text eq '1' or $text eq 'true' )
56             {
57             return 1;
58             }
59             else
60             {
61             die "Invalid boolean string \"$text\"";
62             }
63             }
64              
65             sub get_boolean_attribute
66             {
67             my ($node, $attr, $default) = @_;
68              
69             if ( $node->getAttributeNode( $attr ) )
70             {
71             return parse_boolean( $node->getAttribute( $attr ) );
72             }
73              
74             return $default;
75             }
76              
77             1;
78