File Coverage

blib/lib/Simple/SAX/Serializer.pm
Criterion Covered Total %
statement 53 55 96.3
branch 10 12 83.3
condition n/a
subroutine 13 14 92.8
pod 5 5 100.0
total 81 86 94.1


line stmt bran cond sub pod time code
1             package Simple::SAX::Serializer;
2              
3 3     3   44006 use warnings;
  3         6  
  3         165  
4 3     3   16 use strict;
  3         5  
  3         107  
5 3     3   21 use vars qw($VERSION);
  3         7  
  3         185  
6              
7             $VERSION = 0.05;
8              
9 3     3   2931 use Abstract::Meta::Class ':all';
  3         37211  
  3         613  
10 3     3   36 use base 'XML::SAX::Base';
  3         6  
  3         5099  
11 3     3   68672 use Carp 'confess';
  3         8  
  3         200  
12 3     3   2115 use Simple::SAX::Serializer::Parser;
  3         10  
  3         137  
13 3     3   2551 use XML::SAX;
  3         14732  
  3         200  
14              
15             BEGIN {
16 3     3   8 eval {
17 3         1300 require XML::LibXML;
18             };
19 3 50       1239 $XML::SAX::ParserPackage = "XML::LibXML::SAX" unless $@;
20             }
21              
22             =head1 NAME
23              
24             Simple::SAX::Serializer - Simple XML serializer
25              
26             =head1 DESCRIPTION
27              
28             Represents xml serializer class,
29              
30             =head1 SYNOPSIS
31              
32             use Simple::SAX::Serializer;
33             my $xml = Simple::SAX::Serializer;
34              
35             $xml->handler('root/child', sub {
36             my ($self, $element, $parent) = @_;
37             my $attributes = $element->attributes;
38             my $result = $parent->children_array_result;
39             push @$result,Child->new(%$attributes);
40             });
41              
42             $xml->handler('root', sub {
43             my ($self, $element) = @_;
44             $element->validate_attributes(['dummy'], {attr2 => 'default_value'});
45             Root->new(%{$element->attributes}, children => $element->children_result);
46             });
47              
48             my $xml_content = "";
49              
50             $xml->parse_string($xml_content);
51             # or $xml->parse_file ...
52              
53             =cut
54              
55             =head2 ATTRIBUTES
56              
57             =over
58              
59             =item handlers
60              
61             =cut
62              
63             has '%.handlers' => (item_accessor => 'handler');
64              
65             =back
66              
67             =head2 METHODS
68              
69             =over
70              
71              
72             =item parse_string
73              
74             Runs the parser and returns result, xml as string
75              
76             =cut
77              
78             sub parse_string {
79 9     9 1 18 my $self = shift;
80 9         30 $self->parse('string', @_);
81             }
82              
83              
84             =item parse_file
85              
86             Runs the parser and returns result, xml as file
87              
88             =cut
89              
90             sub parse_file {
91 0     0 1 0 my $self = shift;
92 0         0 $self->parse('file', @_);
93             }
94              
95              
96             =item parse
97              
98             Runs the parser and returns result
99              
100             =cut
101              
102             sub parse {
103 14     14 1 15339 my ($self, $input_type, $xml, $args) = @_;
104 14         33 my $parse_method = "parse_$input_type";
105 14         125 my $handler = Simple::SAX::Serializer::Parser->new;
106 14         987 $handler->{parser} = $self;
107 14         122 my $parser = XML::SAX::ParserFactory->parser(Handler => $handler);
108 14 100       155513 if($input_type eq 'file') {
109 1 50       25 die "file $xml doesn't exists" unless -e $xml;
110             }
111 14         34 $handler->{root_args} = $args;
112 14         105 $parser->$parse_method($xml);
113 13         2537 $handler->{result};
114             }
115              
116              
117             =item find_handlder
118              
119             Finds handler for current element.
120             It start matching from root/element/searched_element
121             and if not find that it try to resolve by
122             element/searched_element
123             and eventually searched_element
124             If handler is not found then generates an error.
125              
126             =cut
127              
128             sub find_handlder {
129 64     64 1 81 my ($self, $elements) = @_;
130 64         127 my @path = element_path($elements);
131 64         214 my $handlers = $self->handlers;
132 64         538 my $handler;
133 64         174 for (my $i = 0; $i <= $#path; $i++) {
134 118         258 my $path = join '/', @path[$i .. $#path ];
135 118         186 $handler = $handlers->{$path};
136 118 100       304 last if $handler;
137             }
138            
139            
140 64 100       124 $handler = $handlers->{'*'}
141             unless $handler;
142              
143 64 100       300 confess "missing handler for " . join('/', @path)
144             unless $handler;
145 63         242 $handler;
146             }
147              
148              
149             =item element_path
150              
151             Takes array reference of the elements data structures, return list of element name.
152              
153             =cut
154              
155             sub element_path {
156 64     64 1 77 my ($elemets) = @_;
157 64         95 map { $_->[0] } @$elemets;
  127         300  
158             }
159              
160              
161             1;
162              
163             __END__