File Coverage

blib/lib/OpenOffice/OODoc.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             #-----------------------------------------------------------------------------
2             #
3             # $Id : OODoc.pm 2.125 2010-07-08 JMG$
4             #
5             # Created and maintained by Jean-Marie Gouarne
6             # Copyright 2010 by Genicorp, S.A. (www.genicorp.com)
7             #
8             #-----------------------------------------------------------------------------
9              
10 2     2   50593 use OpenOffice::OODoc::File 2.203;
  2         79  
  2         93  
11 2     2   2795 use OpenOffice::OODoc::Meta 2.017;
  0            
  0            
12             use OpenOffice::OODoc::Document 2.023;
13             use OpenOffice::OODoc::Manifest 2.007;
14              
15             #-----------------------------------------------------------------------------
16              
17             package OpenOffice::OODoc;
18             use 5.008_000;
19             use strict;
20             our $VERSION = '2.125';
21              
22             require Exporter;
23             our @ISA = qw(Exporter);
24             our @EXPORT = qw
25             (
26             ooXPath ooText ooMeta ooManifest ooImage ooStyles
27             odfXPath odfText odfMeta odfManifest odfImage odfStyles
28             odfConnector odfDocument ooDocument odfPackage odfContainer ooFile
29             odfLocalEncoding localEncoding ooLocalEncoding
30             odfEncodeText odfDecodeText ooEncodeText ooDecodeText
31             ooLocaltime ooTimelocal odfLocaltime odfTimelocal
32             odfTemplatePath ooTemplatePath
33             odfWorkingDirectory workingDirectory ooWorkingDirectory
34             odfReadConfig readConfig ooReadConfig
35             );
36              
37             our $INSTALLATION_PATH;
38              
39             #-----------------------------------------------------------------------------
40             # config loader
41              
42             sub odfReadConfig
43             {
44             my $filename = shift;
45             unless ($filename)
46             {
47             $filename = $INSTALLATION_PATH . '/config.xml'
48             if $INSTALLATION_PATH;
49             }
50             unless ($filename)
51             {
52             warn "[" . __PACKAGE__ . "::odfReadConfig] " .
53             "Missing configuration file\n";
54             return undef;
55             }
56             my $config = XML::Twig->new->safe_parsefile($filename);
57             unless ($config)
58             {
59             warn "[" . __PACKAGE__ . "::odfReadConfig] " .
60             "Syntax error in configuration file $filename\n";
61             return undef;
62             }
63             my $root = $config->get_xpath('//OpenOffice-OODoc', 0);
64             unless ($root && $root->isElementNode)
65             {
66             return undef;
67             }
68             foreach my $node ($root->getChildNodes)
69             {
70             next unless $node->isElementNode;
71             my $name = $node->getName; $name =~ s/-/::/g;
72             my $varname = 'OpenOffice::OODoc::' . $name;
73             no strict;
74             $$varname = $node->string_value;
75             $$varname = odfDecodeText($$varname);
76             use strict;
77             }
78             OpenOffice::OODoc::Styles::ooLoadColorMap();
79             return 1;
80             }
81              
82             #-----------------------------------------------------------------------------
83             # accessor for local character set control
84              
85             sub odfLocalEncoding
86             {
87             my $newcharset = shift;
88             if ($newcharset)
89             {
90             if (Encode::find_encoding($newcharset))
91             {
92             $OpenOffice::OODoc::XPath::LOCAL_CHARSET = $newcharset;
93             }
94             else
95             {
96             warn "[" . __PACKAGE__ . "::odfLocalEncoding] " .
97             "Unsupported encoding\n";
98             }
99             }
100             return $OpenOffice::OODoc::XPath::LOCAL_CHARSET;
101             }
102              
103             #-----------------------------------------------------------------------------
104             # accessor for default XML templates for document creation
105              
106             sub odfTemplatePath
107             {
108             return OpenOffice::OODoc::File::templatePath(@_);
109             }
110              
111             #-----------------------------------------------------------------------------
112             # accessor for default working directory control
113              
114             sub odfWorkingDirectory
115             {
116             my $path = shift;
117              
118             $OpenOffice::OODoc::File::WORKING_DIRECTORY = $path
119             if defined $path;
120             OpenOffice::OODoc::File::checkWorkingDirectory
121             (
122             $OpenOffice::OODoc::File::WORKING_DIRECTORY
123             );
124              
125             return $OpenOffice::OODoc::File::WORKING_DIRECTORY;
126             }
127            
128             #-----------------------------------------------------------------------------
129             # shortcuts for low-level local/utf8 code conversion
130              
131             sub odfEncodeText
132             {
133             return OpenOffice::OODoc::XPath::encode_text(@_);
134             }
135              
136             sub odfDecodeText
137             {
138             return OpenOffice::OODoc::XPath::decode_text(@_);
139             }
140              
141             #-----------------------------------------------------------------------------
142             # constructors
143              
144             sub odfDocument
145             {
146             return OpenOffice::OODoc::Document->new(@_);
147             }
148              
149             sub odfContainer
150             {
151             return OpenOffice::OODoc::File->new(@_);
152             }
153              
154             sub odfXPath
155             {
156             return OpenOffice::OODoc::XPath->new(@_);
157             }
158            
159             sub odfText
160             {
161             return OpenOffice::OODoc::Text->new(@_);
162             }
163              
164             sub odfStyles
165             {
166             return OpenOffice::OODoc::Styles->new(@_);
167             }
168              
169             sub odfImage
170             {
171             return OpenOffice::OODoc::Image->new(@_);
172             }
173              
174             sub odfMeta
175             {
176             return OpenOffice::OODoc::Meta->new(@_);
177             }
178              
179             sub odfManifest
180             {
181             return OpenOffice::OODoc::Manifest->new(@_);
182             }
183              
184             #-----------------------------------------------------------------------------
185             # initialization
186              
187             BEGIN
188             {
189             *ooDocument = *odfDocument;
190             *odfConnector = *odfDocument;
191             *odfFile = *odfContainer;
192             *odfPackage = *odfContainer;
193             *ooFile = *odfContainer;
194             *ooXPath = *odfXPath;
195             *ooText = *odfText;
196             *ooStyles = *odfStyles;
197             *ooImage = *odfImage;
198             *ooMeta = *odfMeta;
199             *ooManifest = *odfManifest;
200             *localEncoding = *odfLocalEncoding;
201             *workingDirectory = *odfWorkingDirectory;
202             *readConfig = *odfReadConfig;
203             *ooLocalEncoding = *odfLocalEncoding;
204             *ooWorkingDirectory = *odfWorkingDirectory;
205             *ooReadConfig = *odfReadConfig;
206             *ooEncodeText = *odfEncodeText;
207             *ooDecodeText = *odfDecodeText;
208             *ooTemplatePath = *odfTemplatePath;
209             *odfLocaltime = *OpenOffice::OODoc::XPath::odfLocaltime;
210             *odfTimelocal = *OpenOffice::OODoc::XPath::odfTimelocal;
211             *ooLocaltime = *odfLocaltime;
212             *ooTimelocal = *odfTimelocal;
213            
214             my $module_path = $INC{"OpenOffice/OODoc.pm"};
215             $module_path =~ s/\.pm$//;
216             $INSTALLATION_PATH = $module_path;
217             odfReadConfig() if ( -e "$INSTALLATION_PATH/config.xml" );
218             }
219              
220             #-----------------------------------------------------------------------------
221             1;