comparison lib/Benzin/Bugzilla/XmlWriter.pm @ 10:14a966369278

working version of exporting bugs from bugzilla in tj3 format (without bookings)
author cin
date Mon, 07 Sep 2015 01:37:11 +0300
parents
children 4eb9fdf4efa9
comparison
equal deleted inserted replaced
9:cc7244ab1b9f 10:14a966369278
1 package Benzin::Bugzilla::XmlWriter;
2
3 our %Transform = (
4 'Benzin::Bugzilla::Bug' => 'WriteBug',
5 'Benzin::Bugzilla::BugComment' => 'WriteBugComment',
6 'HASH' => 'WriteHash',
7 -plain => 'WriteValue',
8 -default => 'WriteValue'
9 );
10
11 use IMPL::Const qw(:prop);
12 use IMPL::declare {
13 require => {
14 XMLWriter => 'XML::Writer',
15 Bug => 'Benzin::Bugzilla::Bug',
16 BugComment => 'Benzin::Bugzilla::BugComment'
17 },
18 base => [
19 'IMPL::Transform' => sub { %Transform }
20 ],
21 props => [
22 _writer => PROP_RW
23 ]
24 };
25
26 use fields qw(_writer);
27
28 sub CTOR {
29 my SELF $this = shift;
30
31 $this->_writer(XMLWriter->new(@_));
32 }
33
34 sub WriteBugList {
35 my SELF $this = shift;
36 my $bugs = shift || [];
37 my $writer = $this->_writer;
38
39
40 $writer->xmlDecl("UTF-8");
41 $writer->startTag("bugzilla");
42
43
44 foreach my $bug (@$bugs) {
45 $writer->startTag("bug");
46 $this->WriteBug($bug);
47 $writer->endTag();
48 }
49
50 $writer->endTag();
51
52 }
53
54 sub WriteBug {
55 my SELF $this = shift;
56 my $value = shift;
57 my $writer = $this->_writer;
58
59 foreach my $field ( @{ Bug->BUG_FIELDS } ) {
60 next unless $value->{$field};
61 $this->WriteElement( $field, $value->{$field} );
62 }
63 }
64
65 sub WriteBugComment {
66 my SELF $this = shift;
67 my $value = shift;
68 my $writer = $this->_writer;
69
70 foreach my $field ( @{ BugComment->COMMENT_FIELDS } ) {
71 next unless $value->{$field};
72 $this->WriteElement( $field, $value->{$field} );
73 }
74 }
75
76 sub WriteHash {
77 my SELF $this = shift;
78 my $value = shift;
79
80 while(my ($k,$v) = each %$value) {
81 $this->WriteElement($k,$v);
82 }
83 }
84
85 sub WriteElement {
86 my SELF $this = shift;
87 my ( $name, $data ) = @_;
88 my $writer = $this->_writer;
89
90 my @values =
91 ref($data)
92 && ref($data) eq 'ARRAY'
93 ? @{$data}
94 : $data;
95
96 foreach my $v (@values) {
97 $writer->startTag($name);
98 $this->Transform( $v );
99 $writer->endTag;
100 }
101 }
102
103 sub WriteValue {
104 my SELF $this = shift;
105 my $value = shift;
106
107 $this->_writer->characters($value) if defined $value;
108 }
109
110 1;