File: OpenDocumentXML.pm

package info (click to toggle)
libspreadsheet-wright-perl 0.107-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 228 kB
  • sloc: perl: 1,008; makefile: 2
file content (138 lines) | stat: -rw-r--r-- 3,202 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package Spreadsheet::Wright::OpenDocumentXML;

use 5.010;
use strict;
use warnings;
no warnings qw( uninitialized numeric );

BEGIN {
	$Spreadsheet::Wright::OpenDocumentXML::VERSION   = '0.107';
	$Spreadsheet::Wright::OpenDocumentXML::AUTHORITY = 'cpan:TOBYINK';
}

use Carp;
use XML::LibXML;

use parent qw(Spreadsheet::Wright);
use constant {
	OFFICE_NS => "urn:oasis:names:tc:opendocument:xmlns:office:1.0",
	STYLE_NS  => "urn:oasis:names:tc:opendocument:xmlns:style:1.0",
	TEXT_NS   => "urn:oasis:names:tc:opendocument:xmlns:text:1.0",
	TABLE_NS  => "urn:oasis:names:tc:opendocument:xmlns:table:1.0",
	META_NS   => "urn:oasis:names:tc:opendocument:xmlns:meta:1.0",
	NUMBER_NS => "urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0",
	};

sub new
{
	my ($class, %args) = @_;
	
	my $self = bless { 'options' => \%args }, $class;
	
	$self->{'_FILENAME'} = $args{'file'} // $args{'filename'}
		or croak "Need filename.";

	return $self;
}

sub _prepare
{
	my $self = shift;
	
	return $self if $self->{'document'};
	
	my $namespaces = {
		office  => OFFICE_NS,
		style   => STYLE_NS,
		text    => TEXT_NS,
		table   => TABLE_NS,
		meta    => META_NS,
		number  => NUMBER_NS,
		};
	
	$self->{'document'} = XML::LibXML->createDocument;
	$self->{'document'}->setDocumentElement(
		$self->{'document'}->createElement('root')
		);
	while (my ($prefix, $nsuri) = each %$namespaces)
	{
		$self->{'document'}->documentElement->setNamespace($nsuri, $prefix, $prefix eq 'office' ? 1 : 0);
	}
	$self->{'document'}->documentElement->setNodeName('office:document-content');
	$self->{'document'}->documentElement->setAttributeNS(OFFICE_NS, 'version', '1.0');
	$self->{'body'} = $self->{'document'}->documentElement
		->addNewChild(OFFICE_NS, 'body')
		->addNewChild(OFFICE_NS, 'spreadsheet');
	$self->addsheet($self->{'options'}->{'sheet'} // 'Sheet 1');
	
	return $self;
}

sub addsheet
{
	my ($self, $caption) = @_;

	$self->_open() or return;

	$self->{'tbody'} = $self->{'body'}->addNewChild(TABLE_NS, 'table');

	if (defined $caption)
	{
		$self->{'tbody'}->setAttributeNS(TABLE_NS, 'name', $caption);
	}
	
	return $self;
}

sub _add_prepared_row
{
	my $self = shift;

	my $tr = $self->{'tbody'}->addNewChild(TABLE_NS, 'table-row');
	
	foreach my $cell (@_)
	{
		my $tcell = $tr->addNewChild(TABLE_NS, 'table-cell');
		$tcell->setAttributeNS(OFFICE_NS, 'value-type', 'string');
		
		my $td = $tcell->addNewChild(TEXT_NS, 'p');
		
		my $content = $cell->{'content'};
		$content = sprintf($cell->{'sprintf'}, $content)
			if defined $cell->{'sprintf'};
		
		$td->appendText($content);
		
		if ($cell->{'font_weight'} eq 'bold'
		&&  $cell->{'font_style'} eq 'italic')
		{
			$td->setAttributeNS(TEXT_NS, 'style-name', 'BoldItalic');
		}
		elsif ($cell->{'font_weight'} eq 'bold')
		{
			$td->setAttributeNS(TEXT_NS, 'style-name', 'Bold');
		}
		elsif ($cell->{'font_style'} eq 'italic')
		{
			$td->setAttributeNS(TEXT_NS, 'style-name', 'Italic');
		}
	}
}

sub close
{
	my $self=shift;
	return if $self->{'_CLOSED'};
	$self->{'_FH'}->print( $self->_make_output );
	$self->{'_FH'}->close;
	$self->{'_CLOSED'}=1;
	return $self;
}

sub _make_output
{
	my $self = shift;
	return $self->{'document'}->toString;
}

1;