Debian Codegraph @

examples/oodoc_test

raw
1 #!/usr/bin/perl
2 #-----------------------------------------------------------------------------
3 # OpenOffice::OODoc test 2010-02-11
4 #-----------------------------------------------------------------------------
5
6 =head1 NAME
7
8 oodoc_test - OpenOffice::OODoc test document generation
9
10 =head1 DESCRIPTION
11
12 This utility checks the current OpenOffice::OODoc installation and generates
13 a test document from scratch. The generated document can then be found in the
14 current directory, and its name is "odftest.odt".
15
16 =cut
17
18 use strict;
19 use OpenOffice::OODoc;
20
21 #-----------------------------------------------------------------------------
22
23 sub display_date
24 {
25 return localtime(odfTimelocal(shift));
26 }
27
28 #-----------------------------------------------------------------------------
29
30 my $version = $OpenOffice::OODoc::VERSION;
31 my $pkgdate = display_date $OpenOffice::OODoc::BUILD_DATE;
32 my $instpath = $OpenOffice::OODoc::INSTALLATION_PATH;
33
34 print "OpenOffice::OODoc installation test\n Version\t\t$version\n" .
35 " Build date\t\t$pkgdate\n" .
36 " Installation path\t$instpath\n";
37
38 my $generator = "OpenOffice::OODoc " . $OpenOffice::OODoc::VERSION .
39 " installation test";
40 my $testfile = $ARGV[0] ||
41 (
42 $OpenOffice::OODoc::File::DEFAULT_OFFICE_FORMAT == 2 ?
43 "odftest.odt" : "ootest.sxw"
44 );
45 my $class = "text";
46 my $image_file = "$instpath/data/image.png";
47 my $image_size = "60mm, 82mm";
48 my $test_date = odfLocaltime();
49
50 print "Generating $testfile file\n";
51
52 # Creating an empty new ODF file with the default template
53 # unlink $testfile;
54 my $archive = odfContainer($testfile, create => 'text');
55 unless ($archive)
56 {
57 die "# Unable to create the test file\n";
58 }
59
60 #-----------------------------------------------------------------------------
61
62 my $notice =
63 "This document has been generated by the OpenOffice::OODoc " .
64 "installation test. If you can read this paragraph in blue letters with " .
65 "a yellow background, if you can see a centered image at the top of the " .
66 "page, and if the informations below make sense, " .
67 "your installation is probably OK.";
68
69 my $title = "OpenOffice::OODoc test document";
70 my $description = "Generated by $generator";
71
72 # Opening the content using OpenOffice::OODoc::Document
73 my $doc = odfConnector
74 (
75 container => $archive,
76 part => 'content',
77 readable_XML => 'true'
78 )
79 or die "# Unable to find a regular document content\n";
80
81 my $styles = odfConnector
82 (
83 container => $archive,
84 part => 'styles',
85 readable_XML => 'true'
86 )
87 or die "# Unable to get the styles\n";
88
89 # Creating a graphic style
90 $styles->createImageStyle
91 (
92 'Centered Image',
93 properties =>
94 {
95 'horizontal-pos' => 'center',
96 'vertical-pos' => 'from-top',
97 'wrap' => 'none',
98 'fo:margin-bottom' => '1cm'
99 }
100 );
101 # Inserting an image in the document
102 $doc->createImageElement
103 (
104 'Logo',
105 style => 'Centered Image',
106 page => 1,
107 size => $image_size,
108 import => $image_file
109 );
110
111 # Appending a page footer
112 $styles->createStyle
113 (
114 'Centered Paragraph',
115 family => 'paragraph',
116 parent => 'Standard',
117 properties =>
118 {
119 'fo:text-align' => 'center'
120 }
121 );
122 $styles->styleProperties
123 (
124 'Centered Paragraph',
125 area => 'text',
126 'fo:font-size' => '60%'
127 );
128 $styles->masterPageFooter
129 (
130 'Standard',
131 $styles->createParagraph
132 (
133 "Created by OpenOffice::OODoc\n" . localtime(),
134 'Centered Paragraph'
135 )
136 );
137 # Appending a level 1 heading
138 $doc->appendHeading
139 (
140 text => "Congratulations !",
141 level => "1",
142 style => "Heading_20_1"
143 );
144
145 # Creating a coloured paragraph style (blue foreground, yellow background)
146 $styles->createStyle
147 (
148 "Colour",
149 family => 'paragraph',
150 parent => 'Standard',
151 properties =>
152 {
153 -area => 'paragraph',
154 'fo:color' => odfColor(0,0,128),
155 'fo:background-color' => odfColor("yellow"),
156 'fo:text-align' => 'justify'
157 }
158 );
159 if ($doc->isOpenDocument)
160 {
161 $styles->styleProperties
162 ("Colour", -area => 'text', 'fo:color' => '#000080');
163 }
164 # Appending another paragraph using the new style
165 my $pg = $doc->appendParagraph(text => $notice, style => "Colour" );
166
167 # Appending another level 1 heading
168 $doc->appendHeading
169 (
170 text => "Your environment",
171 level => 1,
172 style => "Heading_20_1"
173 );
174 # Appending a table showing some environment details
175 my $table = $doc->appendTable("Environment", 6, 2);
176 $doc->cellValue($table, "A1", "Platform");
177 $doc->cellValue($table, "B1", $^O);
178 $doc->cellValue($table, "A2", "Perl version");
179 $doc->cellValue($table, "B2", $]);
180 $doc->cellValue($table, "A3", "Archive::Zip version");
181 $doc->cellValue($table, "B3", $Archive::Zip::VERSION);
182 $doc->cellValue($table, "A4", "XML::Twig version");
183 $doc->cellValue($table, "B4", $XML::Twig::VERSION);
184 $doc->cellValue($table, "A5", "OpenOffice::OODoc version");
185 $doc->cellValue($table, "B5", $OpenOffice::OODoc::VERSION);
186 $doc->cellValue($table, "A6", "OpenOffice::OODoc build");
187 $doc->cellValueType($table, "B6", 'date');
188 $doc->cellValue($table, "B6", $OpenOffice::OODoc::BUILD_DATE);
189
190 # Appending another level 1 heading
191 $doc->appendHeading
192 (
193 text => "Your installation choices",
194 level => 1,
195 style => "Heading_20_1"
196 );
197
198 # Appending a table with the installation parameters
199 my $office_format = $OpenOffice::OODoc::File::DEFAULT_OFFICE_FORMAT == 2 ?
200 "OASIS Open Document" : "OpenOffice.org 1.0";
201 my $color_map = $OpenOffice::OODoc::Styles::COLORMAP || "<none>";
202 $table = $doc->appendTable("Choices", 4, 2);
203 $doc->cellValue($table, "A1", "Local character set");
204 $doc->cellValue($table, "B1", $OpenOffice::OODoc::XPath::LOCAL_CHARSET);
205 $doc->cellValue($table, "A2", "Working directory");
206 $doc->cellValue($table, "B2", $OpenOffice::OODoc::File::WORKING_DIRECTORY);
207 $doc->cellValue($table, "A3", "RGB color map");
208 $doc->cellValue($table, "B3", $color_map);
209 $doc->cellValue($table, "A4", "Default office document format");
210 $doc->cellValue($table, "B4", $office_format);
211
212 # Opening the metadata of the document
213 my $meta = odfMeta(container => $archive, readable_XML => 'on')
214 or die "# Unable to find regular metadata\n";
215 # Writing some metadata elements
216 $meta->title($title);
217 $meta->creator($ENV{'USER'});
218 $meta->initial_creator($ENV{'USER'});
219 $meta->description($description);
220 $meta->generator($generator);
221 $meta->creation_date($test_date);
222 $meta->date($test_date);
223 $meta->removeUserProperties();
224 $meta->setUserProperty("Application", type => 'string', value => "oodoc_test");
225 $meta->setUserProperty("Test date", type => 'date', value => $test_date);
226
227 # Saving the $testfile file
228 print "Saving $testfile file\n";
229 $archive->save;
230
231 $doc->dispose;
232 $styles->dispose;
233 $meta->dispose;
234
235 exit 0;