comparison lib/Benzin/Bugzilla/XmlSerializer.pm @ 9:cc7244ab1b9f

implemented time reports on bugs
author cin
date Sat, 05 Sep 2015 22:01:12 +0300
parents
children
comparison
equal deleted inserted replaced
8:ff9c0c788382 9:cc7244ab1b9f
1 package Benzin::Bugzilla::XmlSerializer;
2
3 use IMPL::declare {
4 require => {
5 XMLWriter => 'XML::Writer'
6 },
7 base => [
8 'IMPL::Object::Fields' => undef
9 ]
10 };
11
12 use fields qw(_writer);
13
14 sub CTOR {
15 my SELF $this = shift;
16
17 $this->{_writer} = XMLWriter->new(@_);
18 }
19
20 sub WriteBugList {
21 my SELF $this = shift;
22 my $bugs = shift || [];
23 my $writer = $this->{_writer};
24
25
26 $writer->xmlDecl("UTF-8");
27 $writer->startTag("bugzilla");
28
29 $writer->startElement("bugs");
30
31 $this->WriteBug($_) foreach @$bugs;
32
33 $writer->endTag();
34 $writer->endTag();
35
36 }
37
38 sub WriteBug {
39 my SELF $this = shift;
40 my $bug = shift;
41 my $writer = $this->{_writer};
42
43 $writer->startTag("bug");
44 foreach my $field ( @{ Bug->BUG_FIELDS } ) {
45 next unless $bug->{$field};
46 $this->WriteElement( $field, $bug->{$field} );
47 }
48 $writer->endTag();
49 }
50
51 sub WriteElement {
52 my SELF $this = shift;
53 my ( $name, $data ) = @_;
54 my $writer = $this->{_writer};
55
56 my @values =
57 ref($data)
58 && ref($data) eq 'ARRAY'
59 ? @{$data}
60 : $data;
61
62 foreach my $v (@values) {
63 $writer->dataElement( $name, $v );
64 }
65 }
66
67 1;