Wright/OpenDocumentXML.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 127 CC 9 SCAR 23.1
CC
9
Cov
0%
_add_prepared_row
30.0
_prepare
12.0
addsheet
6.0
CRAP 10.1
1 package Spreadsheet::Wright::OpenDocumentXML;
2  
3 use 5.010;
4 use strict;
5 use warnings;
6 no warnings qw( uninitialized numeric );
7  
8 BEGIN {
9 $Spreadsheet::Wright::OpenDocumentXML::VERSION = '0.107';
10 $Spreadsheet::Wright::OpenDocumentXML::AUTHORITY = 'cpan:TOBYINK';
11 }
12  
13 use Carp;
14 use XML::LibXML;
15  
16 use parent qw(Spreadsheet::Wright);
17 use constant {
18 OFFICE_NS => "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
19 STYLE_NS => "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
20 TEXT_NS => "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
21 TABLE_NS => "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
22 META_NS => "urn:oasis:names:tc:opendocument:xmlns:meta:1.0",
23 NUMBER_NS => "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
24 };
25  
26 sub new
27 {
28 my ($class, %args) = @_;
29
30 my $self = bless { 'options' => \%args }, $class;
31
32 $self->{'_FILENAME'} = $args{'file'} // $args{'filename'}
33 or croak "Need filename.";
34  
35 return $self;
36 }
37  
38 sub _prepare
39 {
40 my $self = shift;
41
42 return $self if $self->{'document'};
43
44 my $namespaces = {
45 office => OFFICE_NS,
46 style => STYLE_NS,
47 text => TEXT_NS,
48 table => TABLE_NS,
49 meta => META_NS,
50 number => NUMBER_NS,
51 };
52
53 $self->{'document'} = XML::LibXML->createDocument;
54 $self->{'document'}->setDocumentElement(
55 $self->{'document'}->createElement('root')
56 );
57 while (my ($prefix, $nsuri) = each %$namespaces)
58 {
59 $self->{'document'}->documentElement->setNamespace($nsuri, $prefix, $prefix eq 'office' ? 1 : 0);
60 }
61 $self->{'document'}->documentElement->setNodeName('office:document-content');
62 $self->{'document'}->documentElement->setAttributeNS(OFFICE_NS, 'version', '1.0');
63 $self->{'body'} = $self->{'document'}->documentElement
64 ->addNewChild(OFFICE_NS, 'body')
65 ->addNewChild(OFFICE_NS, 'spreadsheet');
66 $self->addsheet($self->{'options'}->{'sheet'} // 'Sheet 1');
67
68 return $self;
69 }
70  
71 sub addsheet
72 {
73 my ($self, $caption) = @_;
74  
75 $self->_open() or return;
76  
77 $self->{'tbody'} = $self->{'body'}->addNewChild(TABLE_NS, 'table');
78  
79 if (defined $caption)
80 {
81 $self->{'tbody'}->setAttributeNS(TABLE_NS, 'name', $caption);
82 }
83
84 return $self;
85 }
86  
87 sub _add_prepared_row
88 {
89 my $self = shift;
90  
91 my $tr = $self->{'tbody'}->addNewChild(TABLE_NS, 'table-row');
92
93 foreach my $cell (@_)
94 {
95 my $tcell = $tr->addNewChild(TABLE_NS, 'table-cell');
96 $tcell->setAttributeNS(OFFICE_NS, 'value-type', 'string');
97
98 my $td = $tcell->addNewChild(TEXT_NS, 'p');
99
100 my $content = $cell->{'content'};
101 $content = sprintf($cell->{'sprintf'}, $content)
102 if defined $cell->{'sprintf'};
103
104 $td->appendText($content);
105
106 if ($cell->{'font_weight'} eq 'bold'
107 && $cell->{'font_style'} eq 'italic')
108 {
109 $td->setAttributeNS(TEXT_NS, 'style-name', 'BoldItalic');
110 }
111 elsif ($cell->{'font_weight'} eq 'bold')
112 {
113 $td->setAttributeNS(TEXT_NS, 'style-name', 'Bold');
114 }
115 elsif ($cell->{'font_style'} eq 'italic')
116 {
117 $td->setAttributeNS(TEXT_NS, 'style-name', 'Italic');
118 }
119 }
120 }
121  
122 sub close
123 {
124 my $self=shift;
125 return if $self->{'_CLOSED'};
126 $self->{'_FH'}->print( $self->_make_output );
127 $self->{'_FH'}->close;
128 $self->{'_CLOSED'}=1;
129 return $self;
130 }
131  
132 sub _make_output
133 {
134 my $self = shift;
135 return $self->{'document'}->toString;
136 }
137  
138 1;