Constructor : OpenOffice::OODoc::File->new(filename|handle)
Short Forms (equivalent):
odfFile(), odfContainer(), or odfPackage()
Returns an instance of OODoc::File if the argument corresponds to a
valid and accessible zip archive. The argument may be either a
file path/name or an open (in binmode) and seekable IO::File.
The file or file handle used here is used in order to initialize the
ODF Connector for subsequent processing through the other submodules
of the OpenOffice::OODoc API. It becomes the default target file if
the data are updated and the changes committed using save().
If the OODoc::File object is loaded for update, its strongly
recommended to avoid the use of a single IO::File object to read and
write. However, if the source is a file name, the same file name may
be safely used as the source and the target (of course and as usual,
any sensitive document must be backed up before processing).
If the "create" option is set (see below), a file path/name may be
provided, but a file handle must be avoided.
See the explanations about the save() method below.
my $container1 = odfContainer("doc1.odt");
my $fh = new IO::File "< doc2.odt";
my $container2 = odfContainer($fh);
An optional argument can be passed in hash format (key => value),
after the filename. Like this:
work_dir => "path"
which designates the path to the XML working files also generated
during a save for this object (each OpenOffice::OODoc::File object
can have its own working directory); without this option, the
working directory is set according to the content of the class
Note: no content checking is carried out. The archive can be opened
whether or not it is an OpenOffice.org document.
Its possible to create an OpenOffice::OODoc::File object without
providing an existing OpenOffice.org file. To do so, there is a
create => "class"
where "class" is the document class according to the OpenOffice.org
terminology, so it is one of the following values: "text",
spreadsheet", "presentation", "drawing". These values are the same
as the legal parameters of contentClass() in OpenOffice::OODoc::XPath.
If "create" is provided, the first argument must not be a file handle,
knowing that the target (if the data is later saved) cant be an
open file, and that there is by definition no source file. Note that,
for a creation, the value of the file argument is ignored so it may be
undef or an empty string; however a valid path/name may be provided in
order to provide a default target for a subsequent save().
For a very advanced use, its possible to combine "create" with an
template_path => "path"
to generate the new file from special, user-provided ODF templates
instead of those included in the installation. If this option is
not provided, the general template path (possibly changed with
the templatePath() function) is used. The template path must
indicate a directory which contains a set of ODF files whose
names are "template.???", where "???" represents the conventional
ODF and OOo suffixes, knowing that the supported suffixes are
presently odt, ods, odp, odg, sxw, sxc, sxi, sxd. Of course, some
applications dont need all the templates; for example, the
"template.sx?" files should be omitted if the user dont want to
use the legacy OpenOffice.org 1.0 format for new documents.
When the "create" option is used, its possible to provide an
"opendocument" option in order to override the installation-level
default file format for new documents. If this option is set to
"1", "on" or "true", the new document will comply to the OASIS
OpenDocument format; if its set to "0", "off" or "false", the new
document will be created according to the OpenOffice.org 1.0 format.
The "opendocument" option is ignored without the "create" one (this
tool is not a format converter for existing documents). Remember that
the default format is set to OpenDocument in the CPAN distribution
and the OpenOffice.org 1.0 format is deprecated.
The returned object of new(), if successful, is a valid File object,
whose methods listed below become available.
If unsuccessful (generally due to non-existent file or invalid zip
archive or even a corrupt zip archive), the constructor returns a
null value (undef), and an error message is sent to the standard
Returns the decompressed content of the requested member, if
contained in the archive and corresponds to an XML element of the
currently active OpenOffice.org file instance. The <member>
parameter must therefore correspond to one of the members of the
file (see Introduction). If the application uses any of the words
"content", "meta", "styles" or "settings", in upper or lower case,
the .xml extension is automatically added but any other names are
accepted without change if they are indeed existing members of the
The following statements are equivalent:
my $content = $archive->extract(META);
my $content = $archive->extract(meta.xml);
my $content = $archive->extract(meta);
After the above calls, the variable $content contains the XML
document which represents the metadata of an OpenOffice.org file.
This content can be used, for example, to instance a Meta object.
Note: in most "normal" cases, this method does not have to be called
explicitly as it is called silently by each occurrence of XPath
(therefore by Text and Meta which are derivatives of it), but only
if XPath is constructed referencing an OODoc::File object as a
parameter (see OODoc::XPath). An extract call is only useful when
exporting the XML or handling it outside of OODoc::XPath.
On error (e.g. unknown archive member), a null value is returned and
an error message is produced.
Connects a File object to an XPath object given as an argument. This
connection has two output products:
- immediately calls the extract method using the corresponding
"specialist" component of the XPath object (metadata if
OODoc::Meta, content if OODoc::Text, etc).
- stores the link for later updates to all OpenOffice.org file
members which may have been modified by XPath objects (in case a
save is called, see below).
Note: This method is used by OODoc::XPath to connect as "clients" to
OODoc::File objects. It does not have to be called directly by
highest-level programs which only use OODoc::XPath objects.
Orders the deletion of any OpenOffice.org file member.
deletes the physical content of an image loaded in the file.
It is entirely up to the application to ensure that such a deletion
does not compromise the integrity of the file as no dependency
checking is carried out here. In the above example, the delete
operation could be particularly justified if the "image" member
which referenced this content had been (or was going to be)
otherwise removed, or if it had been replaced by an external
This method can be used to remove any XML or non-XML member. It can
be combined with raw_import() in order to effect a raw replacement
of content without interpretation. Caution: this method should not
be used for an XML member (content, style, meta, etc.) which is
currently "active" (i.e. linked to an active OODoc::XPath instance),
unless the member has been loaded as "read only" (search in the
OpenOffice::OODoc::XPath for the "read_only" option).
Note: calls to this method only prepare the deletion, which is
actually carried out by the save() method if it occurs before the
end of the program. If save() is called with a filename which is
different from the source filename, the source file remains
unchanged and the deleted member is simply not transferred to the
raw_export(member [, destination])
Decompresses and exports the physical content of a given member (XML
or non-XML) of an archive. If the second argument is used, it passes
the destination filename (perhaps with access path). If not, the
file is exported using its internal archive name. Examples:
exports the "styles.xml" member into a file of the same name in the
exports the same XML member to a given path.
raw_export executes immediately (and is not deferred like
If successful, the returned value is the filename of the exported
raw_import(member [, source])
Creates or replaces the indicated member by importing an external
source file. If the second argument is omitted, the source file is
taken to have the same access path as the internal member.
or, in more compact form:
The above sequence requests the import of the member "styles.xml"
from an archive called $arch1 into $arch2 (a direct means of using
the styles and page layout of one document as a template for
The imported files can be any type and have any content. This "raw"
method treats an OpenOffice.org file as any other zip archive. It
notably allows the import of non-XML members (images, sounds,
programs, etc) which the application deals with (and which can be
ignored by the office application).
Caution: the import is only completed when a save() method is called
by the importing object. It can only succeed if the source file is
available at that very moment. A raw_import method can be called
before the imported file is available (no check of availability is
made). An error will be caused if the file is absent at the time of
the save() call. If several raw_import statements are run against the
same filename, there will actually be a corresponding number of
copies of the file in its final state which are imported at the
moment of the save() call, even if it had perhaps been modified in the
meantime (probably not a very useful outcome).
Commits all the changes made in the members of the archive and makes
the resulting content persistent.
In addition, the external, non-XML resources (i.e. image files) that
have previously been targeted by import methods, if any, are
physically imported when save() is called (and not before). If these
resources are not available at this time, they are ignored and a
warning is issued for each missing file.
The target may be a file name (with optional path) or an IO::File
object (that must be open, in binmode and writable).
Without argument, save(), attempts to write the on the file or handle
that was specified as the source file at the creation time, if any.
However, if the OODoc::File was initialized with a IO::File, the use of
the same IO::File for writing the output is presently not recommended;
in some situations, bi-directional I/Os against an application-provided
open IO::File may result in unpredictable results with the current
implementation. Of course, if the OODoc::File object has been
initialized with the "create" option and with an undef value or an
empty string as file name, save() requires an explicit target (a valid
file/path or an open and writable IO::File).
Please note that OODoc::File does not check the content, and the save()
method can be used to force through any data which may produce a
file not compliant with the ODF packaging specification.
The filename argument is optional. If it is omitted, the source file
previously supplied by the constructor call (if any) is updated.
Without a filename argument, save() if the source document was got
through a IO::Handle.
Even though the life of an OODoc::File object does not necessarily
end with a save, it is recommended that you avoid repeated
alternation between save and extract (the objects behaviour in this
situation has not been tested). Normally it is preferable to call a
save once and for all at the end of a series of updates.
Only a call to OODoc::Files save() method saves content, metadate and
presentation changes made by other OODoc components to the
OpenOffice.org file, including raw imports of external data
(raw_import). However, the XML members currently associated with
"read only" OODoc::XPath objects are not changed in the file.
No file is created or modified before this method is
called, with the exception of external files created by raw_export.
Nevertheless Files save can be called automatically and silently by
an OODoc::XPath object but only where it has been called as a
parameter explicitly for this purpose (see the chapter on
All XPath objects which are "connected" to a File object by link
must be present at the time of the save call. If one of these
objects has meanwhile been deleted, the consequences are
unpredictable and, in any case, any document updates it could have
made are lost.
Class function (not to be used as a method).
Can be replaced by odfTemplatePath(), see OODoc man page.
Accessor to get/set the path for a user-defined set of ODF templates,
to be used in case of new document creation. This path is empty by
default. Without an explicit template path, the default XML templates
provided with the OpenOffice::OODoc distribution are automatically
The template path must designate a directory containing 4 regular
ODF files, each one corresponding to a supported ODF document class,
i.e. "template.odt", "template.ods", "template.odp", "template.odg".
However, the user dont need to provide templates that will not be
Note that its always possible to override the default template path
when a new document is created, thanks to the template_path option.